blob: 4d6b4cc57c577e4c1295d646a5e4a57668df2253 [file] [log] [blame]
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001/*
2 * A virtual v4l2-mem2mem example device.
3 *
4 * This is a virtual device driver for testing mem-to-mem videobuf framework.
5 * It simulates a device that uses memory buffers for both source and
6 * destination, processes the data and issues an "irq" (simulated by a timer).
7 * The device is capable of multi-instance, multi-buffer-per-transaction
8 * operation (via the mem2mem framework).
9 *
10 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
Pawel Osciak95072082011-03-13 15:23:32 -030011 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030012 * Marek Szyprowski, <m.szyprowski@samsung.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version
18 */
19#include <linux/module.h>
20#include <linux/delay.h>
21#include <linux/fs.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030022#include <linux/timer.h>
23#include <linux/sched.h>
Randy Dunlap6b46c392010-05-07 15:22:26 -030024#include <linux/slab.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030025
26#include <linux/platform_device.h>
27#include <media/v4l2-mem2mem.h>
28#include <media/v4l2-device.h>
29#include <media/v4l2-ioctl.h>
Hans Verkuild3dd59c2012-07-18 10:35:37 -030030#include <media/v4l2-ctrls.h>
Hans Verkuil97a3c902012-07-18 10:54:59 -030031#include <media/v4l2-event.h>
Marek Szyprowskid80ee382011-01-12 06:50:55 -030032#include <media/videobuf2-vmalloc.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030033
Pawel Osciak96d8eab2010-04-23 05:38:38 -030034MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
Pawel Osciak95072082011-03-13 15:23:32 -030035MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030036MODULE_LICENSE("GPL");
Mauro Carvalho Chehab1990d502011-06-24 14:45:49 -030037MODULE_VERSION("0.1.1");
Hans Verkuil1f923a42014-09-22 09:27:17 -030038MODULE_ALIAS("mem2mem_testdev");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030039
Hans Verkuild95d7c62013-04-17 03:04:10 -030040static unsigned debug;
41module_param(debug, uint, 0644);
42MODULE_PARM_DESC(debug, "activates debug info");
43
Pawel Osciak96d8eab2010-04-23 05:38:38 -030044#define MIN_W 32
45#define MIN_H 32
46#define MAX_W 640
47#define MAX_H 480
Guennadi Liakhovetski9ce3ce42012-04-27 04:16:46 -030048#define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */
Pawel Osciak96d8eab2010-04-23 05:38:38 -030049
50/* Flags that indicate a format can be used for capture/output */
51#define MEM2MEM_CAPTURE (1 << 0)
52#define MEM2MEM_OUTPUT (1 << 1)
53
Hans Verkuil1f923a42014-09-22 09:27:17 -030054#define MEM2MEM_NAME "vim2m"
Pawel Osciak96d8eab2010-04-23 05:38:38 -030055
56/* Per queue */
57#define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
58/* In bytes, per queue */
59#define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
60
61/* Default transaction time in msec */
Hans Verkuil43405832014-03-10 10:58:23 -030062#define MEM2MEM_DEF_TRANSTIME 40
Pawel Osciak96d8eab2010-04-23 05:38:38 -030063#define MEM2MEM_COLOR_STEP (0xff >> 4)
64#define MEM2MEM_NUM_TILES 8
65
Tomasz Moń80072872012-06-12 06:43:49 -030066/* Flags that indicate processing mode */
67#define MEM2MEM_HFLIP (1 << 0)
68#define MEM2MEM_VFLIP (1 << 1)
69
Pawel Osciak96d8eab2010-04-23 05:38:38 -030070#define dprintk(dev, fmt, arg...) \
Hans Verkuild95d7c62013-04-17 03:04:10 -030071 v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030072
73
Hans Verkuil1f923a42014-09-22 09:27:17 -030074static void vim2m_dev_release(struct device *dev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030075{}
76
Hans Verkuil1f923a42014-09-22 09:27:17 -030077static struct platform_device vim2m_pdev = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030078 .name = MEM2MEM_NAME,
Hans Verkuil1f923a42014-09-22 09:27:17 -030079 .dev.release = vim2m_dev_release,
Pawel Osciak96d8eab2010-04-23 05:38:38 -030080};
81
Hans Verkuil1f923a42014-09-22 09:27:17 -030082struct vim2m_fmt {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030083 char *name;
84 u32 fourcc;
85 int depth;
86 /* Types the format can be used for */
87 u32 types;
88};
89
Hans Verkuil1f923a42014-09-22 09:27:17 -030090static struct vim2m_fmt formats[] = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030091 {
92 .name = "RGB565 (BE)",
93 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
94 .depth = 16,
95 /* Both capture and output format */
96 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
97 },
98 {
99 .name = "4:2:2, packed, YUYV",
100 .fourcc = V4L2_PIX_FMT_YUYV,
101 .depth = 16,
102 /* Output-only format */
103 .types = MEM2MEM_OUTPUT,
104 },
105};
106
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300107#define NUM_FORMATS ARRAY_SIZE(formats)
108
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300109/* Per-queue, driver-specific private data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300110struct vim2m_q_data {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300111 unsigned int width;
112 unsigned int height;
113 unsigned int sizeimage;
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300114 unsigned int sequence;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300115 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300116};
117
118enum {
119 V4L2_M2M_SRC = 0,
120 V4L2_M2M_DST = 1,
121};
122
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300123#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
124#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300125
Hans Verkuil1f923a42014-09-22 09:27:17 -0300126static struct vim2m_fmt *find_format(struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300127{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300128 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300129 unsigned int k;
130
131 for (k = 0; k < NUM_FORMATS; k++) {
132 fmt = &formats[k];
133 if (fmt->fourcc == f->fmt.pix.pixelformat)
134 break;
135 }
136
137 if (k == NUM_FORMATS)
138 return NULL;
139
140 return &formats[k];
141}
142
Hans Verkuil1f923a42014-09-22 09:27:17 -0300143struct vim2m_dev {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300144 struct v4l2_device v4l2_dev;
Hans Verkuil09365422015-03-09 13:33:56 -0300145 struct video_device vfd;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300146
147 atomic_t num_inst;
148 struct mutex dev_mutex;
149 spinlock_t irqlock;
150
151 struct timer_list timer;
152
153 struct v4l2_m2m_dev *m2m_dev;
154};
155
Hans Verkuil1f923a42014-09-22 09:27:17 -0300156struct vim2m_ctx {
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300157 struct v4l2_fh fh;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300158 struct vim2m_dev *dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300159
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300160 struct v4l2_ctrl_handler hdl;
161
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300162 /* Processed buffers in this transaction */
163 u8 num_processed;
164
165 /* Transaction length (i.e. how many buffers per transaction) */
166 u32 translen;
167 /* Transaction time (i.e. simulated processing time) in milliseconds */
168 u32 transtime;
169
170 /* Abort requested by m2m */
171 int aborting;
172
Tomasz Moń80072872012-06-12 06:43:49 -0300173 /* Processing mode */
174 int mode;
175
Hans Verkuil47556ff2012-07-18 11:33:22 -0300176 enum v4l2_colorspace colorspace;
177
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300178 /* Source and destination queue data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300179 struct vim2m_q_data q_data[2];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300180};
181
Hans Verkuil1f923a42014-09-22 09:27:17 -0300182static inline struct vim2m_ctx *file2ctx(struct file *file)
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300183{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300184 return container_of(file->private_data, struct vim2m_ctx, fh);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300185}
186
Hans Verkuil1f923a42014-09-22 09:27:17 -0300187static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300188 enum v4l2_buf_type type)
189{
190 switch (type) {
191 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
192 return &ctx->q_data[V4L2_M2M_SRC];
193 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
194 return &ctx->q_data[V4L2_M2M_DST];
195 default:
196 BUG();
197 }
198 return NULL;
199}
200
201
Hans Verkuil1f923a42014-09-22 09:27:17 -0300202static int device_process(struct vim2m_ctx *ctx,
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300203 struct vb2_buffer *in_vb,
204 struct vb2_buffer *out_vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300205{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300206 struct vim2m_dev *dev = ctx->dev;
207 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300208 u8 *p_in, *p_out;
209 int x, y, t, w;
210 int tile_w, bytes_left;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300211 int width, height, bytesperline;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300212
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300213 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300214
215 width = q_data->width;
216 height = q_data->height;
217 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
218
219 p_in = vb2_plane_vaddr(in_vb, 0);
220 p_out = vb2_plane_vaddr(out_vb, 0);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300221 if (!p_in || !p_out) {
222 v4l2_err(&dev->v4l2_dev,
223 "Acquiring kernel pointers to buffers failed\n");
224 return -EFAULT;
225 }
226
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300227 if (vb2_plane_size(in_vb, 0) > vb2_plane_size(out_vb, 0)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300228 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
229 return -EINVAL;
230 }
231
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300232 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300233 / MEM2MEM_NUM_TILES;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300234 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300235 w = 0;
236
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300237 out_vb->v4l2_buf.sequence = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
238 in_vb->v4l2_buf.sequence = q_data->sequence++;
Hans Verkuild95d7c62013-04-17 03:04:10 -0300239 memcpy(&out_vb->v4l2_buf.timestamp,
240 &in_vb->v4l2_buf.timestamp,
241 sizeof(struct timeval));
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300242 if (in_vb->v4l2_buf.flags & V4L2_BUF_FLAG_TIMECODE)
243 memcpy(&out_vb->v4l2_buf.timecode, &in_vb->v4l2_buf.timecode,
244 sizeof(struct v4l2_timecode));
245 out_vb->v4l2_buf.field = in_vb->v4l2_buf.field;
246 out_vb->v4l2_buf.flags = in_vb->v4l2_buf.flags &
247 (V4L2_BUF_FLAG_TIMECODE |
248 V4L2_BUF_FLAG_KEYFRAME |
249 V4L2_BUF_FLAG_PFRAME |
250 V4L2_BUF_FLAG_BFRAME |
251 V4L2_BUF_FLAG_TSTAMP_SRC_MASK);
Hans Verkuild95d7c62013-04-17 03:04:10 -0300252
Tomasz Moń80072872012-06-12 06:43:49 -0300253 switch (ctx->mode) {
254 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
255 p_out += bytesperline * height - bytes_left;
256 for (y = 0; y < height; ++y) {
257 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
258 if (w & 0x1) {
259 for (x = 0; x < tile_w; ++x)
260 *--p_out = *p_in++ +
261 MEM2MEM_COLOR_STEP;
262 } else {
263 for (x = 0; x < tile_w; ++x)
264 *--p_out = *p_in++ -
265 MEM2MEM_COLOR_STEP;
266 }
267 ++w;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300268 }
Tomasz Moń80072872012-06-12 06:43:49 -0300269 p_in += bytes_left;
270 p_out -= bytes_left;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300271 }
Tomasz Moń80072872012-06-12 06:43:49 -0300272 break;
273
274 case MEM2MEM_HFLIP:
275 for (y = 0; y < height; ++y) {
276 p_out += MEM2MEM_NUM_TILES * tile_w;
277 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
278 if (w & 0x01) {
279 for (x = 0; x < tile_w; ++x)
280 *--p_out = *p_in++ +
281 MEM2MEM_COLOR_STEP;
282 } else {
283 for (x = 0; x < tile_w; ++x)
284 *--p_out = *p_in++ -
285 MEM2MEM_COLOR_STEP;
286 }
287 ++w;
288 }
289 p_in += bytes_left;
290 p_out += bytesperline;
291 }
292 break;
293
294 case MEM2MEM_VFLIP:
295 p_out += bytesperline * (height - 1);
296 for (y = 0; y < height; ++y) {
297 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
298 if (w & 0x1) {
299 for (x = 0; x < tile_w; ++x)
300 *p_out++ = *p_in++ +
301 MEM2MEM_COLOR_STEP;
302 } else {
303 for (x = 0; x < tile_w; ++x)
304 *p_out++ = *p_in++ -
305 MEM2MEM_COLOR_STEP;
306 }
307 ++w;
308 }
309 p_in += bytes_left;
310 p_out += bytes_left - 2 * bytesperline;
311 }
312 break;
313
314 default:
315 for (y = 0; y < height; ++y) {
316 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
317 if (w & 0x1) {
318 for (x = 0; x < tile_w; ++x)
319 *p_out++ = *p_in++ +
320 MEM2MEM_COLOR_STEP;
321 } else {
322 for (x = 0; x < tile_w; ++x)
323 *p_out++ = *p_in++ -
324 MEM2MEM_COLOR_STEP;
325 }
326 ++w;
327 }
328 p_in += bytes_left;
329 p_out += bytes_left;
330 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300331 }
332
333 return 0;
334}
335
Hans Verkuil1f923a42014-09-22 09:27:17 -0300336static void schedule_irq(struct vim2m_dev *dev, int msec_timeout)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300337{
338 dprintk(dev, "Scheduling a simulated irq\n");
339 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(msec_timeout));
340}
341
342/*
343 * mem2mem callbacks
344 */
345
346/**
347 * job_ready() - check whether an instance is ready to be scheduled to run
348 */
349static int job_ready(void *priv)
350{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300351 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300352
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300353 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
354 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300355 dprintk(ctx->dev, "Not enough buffers available\n");
356 return 0;
357 }
358
359 return 1;
360}
361
362static void job_abort(void *priv)
363{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300364 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300365
366 /* Will cancel the transaction in the next interrupt handler */
367 ctx->aborting = 1;
368}
369
370/* device_run() - prepares and starts the device
371 *
372 * This simulates all the immediate preparations required before starting
373 * a device. This will be called by the framework when it decides to schedule
374 * a particular instance.
375 */
376static void device_run(void *priv)
377{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300378 struct vim2m_ctx *ctx = priv;
379 struct vim2m_dev *dev = ctx->dev;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300380 struct vb2_buffer *src_buf, *dst_buf;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300381
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300382 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
383 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300384
385 device_process(ctx, src_buf, dst_buf);
386
387 /* Run a timer, which simulates a hardware irq */
388 schedule_irq(dev, ctx->transtime);
389}
390
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300391static void device_isr(unsigned long priv)
392{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300393 struct vim2m_dev *vim2m_dev = (struct vim2m_dev *)priv;
394 struct vim2m_ctx *curr_ctx;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300395 struct vb2_buffer *src_vb, *dst_vb;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300396 unsigned long flags;
397
Hans Verkuil1f923a42014-09-22 09:27:17 -0300398 curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300399
400 if (NULL == curr_ctx) {
Sachin Kamat26fdcf02012-09-24 02:17:47 -0300401 pr_err("Instance released before the end of transaction\n");
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300402 return;
403 }
404
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300405 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
406 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300407
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300408 curr_ctx->num_processed++;
409
Hans Verkuil1f923a42014-09-22 09:27:17 -0300410 spin_lock_irqsave(&vim2m_dev->irqlock, flags);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300411 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
412 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300413 spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300414
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300415 if (curr_ctx->num_processed == curr_ctx->translen
416 || curr_ctx->aborting) {
417 dprintk(curr_ctx->dev, "Finishing transaction\n");
418 curr_ctx->num_processed = 0;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300419 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300420 } else {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300421 device_run(curr_ctx);
422 }
423}
424
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300425/*
426 * video ioctls
427 */
428static int vidioc_querycap(struct file *file, void *priv,
429 struct v4l2_capability *cap)
430{
431 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
432 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
Hans Verkuil72c2af62012-09-14 06:23:12 -0300433 snprintf(cap->bus_info, sizeof(cap->bus_info),
434 "platform:%s", MEM2MEM_NAME);
Mauro Carvalho Chehab79e8c7b2012-08-24 11:25:10 -0300435 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Hans Verkuilb0d14992012-07-18 10:46:01 -0300436 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300437 return 0;
438}
439
440static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
441{
442 int i, num;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300443 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300444
445 num = 0;
446
447 for (i = 0; i < NUM_FORMATS; ++i) {
448 if (formats[i].types & type) {
449 /* index-th format of type type found ? */
450 if (num == f->index)
451 break;
452 /* Correct type but haven't reached our index yet,
453 * just increment per-type index */
454 ++num;
455 }
456 }
457
458 if (i < NUM_FORMATS) {
459 /* Format found */
460 fmt = &formats[i];
461 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
462 f->pixelformat = fmt->fourcc;
463 return 0;
464 }
465
466 /* Format not found */
467 return -EINVAL;
468}
469
470static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
471 struct v4l2_fmtdesc *f)
472{
473 return enum_fmt(f, MEM2MEM_CAPTURE);
474}
475
476static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
477 struct v4l2_fmtdesc *f)
478{
479 return enum_fmt(f, MEM2MEM_OUTPUT);
480}
481
Hans Verkuil1f923a42014-09-22 09:27:17 -0300482static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300483{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300484 struct vb2_queue *vq;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300485 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300486
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300487 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300488 if (!vq)
489 return -EINVAL;
490
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300491 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300492
493 f->fmt.pix.width = q_data->width;
494 f->fmt.pix.height = q_data->height;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300495 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300496 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
497 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
498 f->fmt.pix.sizeimage = q_data->sizeimage;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300499 f->fmt.pix.colorspace = ctx->colorspace;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300500
501 return 0;
502}
503
504static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
505 struct v4l2_format *f)
506{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300507 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300508}
509
510static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
511 struct v4l2_format *f)
512{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300513 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300514}
515
Hans Verkuil1f923a42014-09-22 09:27:17 -0300516static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300517{
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300518 /* V4L2 specification suggests the driver corrects the format struct
519 * if any of the dimensions is unsupported */
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300520 if (f->fmt.pix.height < MIN_H)
521 f->fmt.pix.height = MIN_H;
522 else if (f->fmt.pix.height > MAX_H)
523 f->fmt.pix.height = MAX_H;
524
525 if (f->fmt.pix.width < MIN_W)
526 f->fmt.pix.width = MIN_W;
527 else if (f->fmt.pix.width > MAX_W)
528 f->fmt.pix.width = MAX_W;
529
530 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
531 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
532 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil5c3112b2014-03-10 10:58:29 -0300533 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300534
535 return 0;
536}
537
538static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
539 struct v4l2_format *f)
540{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300541 struct vim2m_fmt *fmt;
542 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300543
544 fmt = find_format(f);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300545 if (!fmt) {
546 f->fmt.pix.pixelformat = formats[0].fourcc;
547 fmt = find_format(f);
548 }
549 if (!(fmt->types & MEM2MEM_CAPTURE)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300550 v4l2_err(&ctx->dev->v4l2_dev,
551 "Fourcc format (0x%08x) invalid.\n",
552 f->fmt.pix.pixelformat);
553 return -EINVAL;
554 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300555 f->fmt.pix.colorspace = ctx->colorspace;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300556
557 return vidioc_try_fmt(f, fmt);
558}
559
560static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
561 struct v4l2_format *f)
562{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300563 struct vim2m_fmt *fmt;
564 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300565
566 fmt = find_format(f);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300567 if (!fmt) {
568 f->fmt.pix.pixelformat = formats[0].fourcc;
569 fmt = find_format(f);
570 }
571 if (!(fmt->types & MEM2MEM_OUTPUT)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300572 v4l2_err(&ctx->dev->v4l2_dev,
573 "Fourcc format (0x%08x) invalid.\n",
574 f->fmt.pix.pixelformat);
575 return -EINVAL;
576 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300577 if (!f->fmt.pix.colorspace)
578 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300579
580 return vidioc_try_fmt(f, fmt);
581}
582
Hans Verkuil1f923a42014-09-22 09:27:17 -0300583static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300584{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300585 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300586 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300587
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300588 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300589 if (!vq)
590 return -EINVAL;
591
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300592 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300593 if (!q_data)
594 return -EINVAL;
595
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300596 if (vb2_is_busy(vq)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300597 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
Marek Szyprowski07e80302010-12-20 14:39:25 -0300598 return -EBUSY;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300599 }
600
601 q_data->fmt = find_format(f);
602 q_data->width = f->fmt.pix.width;
603 q_data->height = f->fmt.pix.height;
604 q_data->sizeimage = q_data->width * q_data->height
605 * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300606
607 dprintk(ctx->dev,
608 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
609 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
610
Marek Szyprowski07e80302010-12-20 14:39:25 -0300611 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300612}
613
614static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
615 struct v4l2_format *f)
616{
617 int ret;
618
619 ret = vidioc_try_fmt_vid_cap(file, priv, f);
620 if (ret)
621 return ret;
622
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300623 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300624}
625
626static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
627 struct v4l2_format *f)
628{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300629 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300630 int ret;
631
632 ret = vidioc_try_fmt_vid_out(file, priv, f);
633 if (ret)
634 return ret;
635
Hans Verkuil47556ff2012-07-18 11:33:22 -0300636 ret = vidioc_s_fmt(file2ctx(file), f);
637 if (!ret)
638 ctx->colorspace = f->fmt.pix.colorspace;
639 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300640}
641
Hans Verkuil1f923a42014-09-22 09:27:17 -0300642static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300643{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300644 struct vim2m_ctx *ctx =
645 container_of(ctrl->handler, struct vim2m_ctx, hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300646
647 switch (ctrl->id) {
Tomasz Moń80072872012-06-12 06:43:49 -0300648 case V4L2_CID_HFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300649 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300650 ctx->mode |= MEM2MEM_HFLIP;
651 else
652 ctx->mode &= ~MEM2MEM_HFLIP;
653 break;
654
655 case V4L2_CID_VFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300656 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300657 ctx->mode |= MEM2MEM_VFLIP;
658 else
659 ctx->mode &= ~MEM2MEM_VFLIP;
660 break;
661
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300662 case V4L2_CID_TRANS_TIME_MSEC:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300663 ctx->transtime = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300664 break;
665
666 case V4L2_CID_TRANS_NUM_BUFS:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300667 ctx->translen = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300668 break;
669
670 default:
671 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
672 return -EINVAL;
673 }
674
675 return 0;
676}
677
Hans Verkuil1f923a42014-09-22 09:27:17 -0300678static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
679 .s_ctrl = vim2m_s_ctrl,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300680};
681
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300682
Hans Verkuil1f923a42014-09-22 09:27:17 -0300683static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300684 .vidioc_querycap = vidioc_querycap,
685
686 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
687 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
688 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
689 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
690
691 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
692 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
693 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
694 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
695
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300696 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
697 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
698 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
699 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Hans Verkuilf5294f42014-11-18 09:51:07 -0300700 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300701
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300702 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
703 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300704
Hans Verkuil97a3c902012-07-18 10:54:59 -0300705 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
706 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300707};
708
709
710/*
711 * Queue operations
712 */
713
Hans Verkuil1f923a42014-09-22 09:27:17 -0300714static int vim2m_queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300715 const struct v4l2_format *fmt,
716 unsigned int *nbuffers, unsigned int *nplanes,
717 unsigned int sizes[], void *alloc_ctxs[])
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300718{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300719 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
720 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300721 unsigned int size, count = *nbuffers;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300722
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300723 q_data = get_q_data(ctx, vq->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300724
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300725 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300726
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300727 while (size * count > MEM2MEM_VID_MEM_LIMIT)
728 (count)--;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300729
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300730 *nplanes = 1;
731 *nbuffers = count;
732 sizes[0] = size;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300733
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300734 /*
735 * videobuf2-vmalloc allocator is context-less so no need to set
736 * alloc_ctxs array.
737 */
738
739 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300740
741 return 0;
742}
743
Hans Verkuil1f923a42014-09-22 09:27:17 -0300744static int vim2m_buf_prepare(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300745{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300746 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
747 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300748
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300749 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300750
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300751 q_data = get_q_data(ctx, vb->vb2_queue->type);
Hans Verkuil5c3112b2014-03-10 10:58:29 -0300752 if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
753 if (vb->v4l2_buf.field == V4L2_FIELD_ANY)
754 vb->v4l2_buf.field = V4L2_FIELD_NONE;
755 if (vb->v4l2_buf.field != V4L2_FIELD_NONE) {
756 dprintk(ctx->dev, "%s field isn't supported\n",
757 __func__);
758 return -EINVAL;
759 }
760 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300761
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300762 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
763 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
764 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300765 return -EINVAL;
766 }
767
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300768 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300769
770 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300771}
772
Hans Verkuil1f923a42014-09-22 09:27:17 -0300773static void vim2m_buf_queue(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300774{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300775 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300776
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300777 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vb);
Michael Olbrich0f910bf2011-07-12 09:46:44 -0300778}
779
Hans Verkuil1f923a42014-09-22 09:27:17 -0300780static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300781{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300782 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
783 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300784
785 q_data->sequence = 0;
786 return 0;
787}
788
Hans Verkuil1f923a42014-09-22 09:27:17 -0300789static void vim2m_stop_streaming(struct vb2_queue *q)
Hans Verkuila7736322014-03-10 10:58:27 -0300790{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300791 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
Hans Verkuila7736322014-03-10 10:58:27 -0300792 struct vb2_buffer *vb;
793 unsigned long flags;
794
795 for (;;) {
796 if (V4L2_TYPE_IS_OUTPUT(q->type))
797 vb = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
798 else
799 vb = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
800 if (vb == NULL)
Hans Verkuile37559b2014-04-17 02:47:21 -0300801 return;
Hans Verkuila7736322014-03-10 10:58:27 -0300802 spin_lock_irqsave(&ctx->dev->irqlock, flags);
803 v4l2_m2m_buf_done(vb, VB2_BUF_STATE_ERROR);
804 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
805 }
Hans Verkuila7736322014-03-10 10:58:27 -0300806}
807
Hans Verkuil1f923a42014-09-22 09:27:17 -0300808static struct vb2_ops vim2m_qops = {
809 .queue_setup = vim2m_queue_setup,
810 .buf_prepare = vim2m_buf_prepare,
811 .buf_queue = vim2m_buf_queue,
812 .start_streaming = vim2m_start_streaming,
813 .stop_streaming = vim2m_stop_streaming,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300814 .wait_prepare = vb2_ops_wait_prepare,
815 .wait_finish = vb2_ops_wait_finish,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300816};
817
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300818static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300819{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300820 struct vim2m_ctx *ctx = priv;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300821 int ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300822
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300823 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Hans Verkuil782f36c2014-03-10 10:58:26 -0300824 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300825 src_vq->drv_priv = ctx;
826 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300827 src_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300828 src_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300829 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300830 src_vq->lock = &ctx->dev->dev_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300831
832 ret = vb2_queue_init(src_vq);
833 if (ret)
834 return ret;
835
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300836 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil782f36c2014-03-10 10:58:26 -0300837 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300838 dst_vq->drv_priv = ctx;
839 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300840 dst_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300841 dst_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300842 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300843 dst_vq->lock = &ctx->dev->dev_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300844
845 return vb2_queue_init(dst_vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300846}
847
Hans Verkuil1f923a42014-09-22 09:27:17 -0300848static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
849 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300850 .id = V4L2_CID_TRANS_TIME_MSEC,
851 .name = "Transaction Time (msec)",
852 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil43405832014-03-10 10:58:23 -0300853 .def = MEM2MEM_DEF_TRANSTIME,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300854 .min = 1,
855 .max = 10001,
Hans Verkuil43405832014-03-10 10:58:23 -0300856 .step = 1,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300857};
858
Hans Verkuil1f923a42014-09-22 09:27:17 -0300859static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
860 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300861 .id = V4L2_CID_TRANS_NUM_BUFS,
862 .name = "Buffers Per Transaction",
863 .type = V4L2_CTRL_TYPE_INTEGER,
864 .def = 1,
865 .min = 1,
866 .max = MEM2MEM_DEF_NUM_BUFS,
867 .step = 1,
868};
869
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300870/*
871 * File operations
872 */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300873static int vim2m_open(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300874{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300875 struct vim2m_dev *dev = video_drvdata(file);
876 struct vim2m_ctx *ctx = NULL;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300877 struct v4l2_ctrl_handler *hdl;
Hans Verkuilf4694032012-07-31 03:51:25 -0300878 int rc = 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300879
Hans Verkuilf4694032012-07-31 03:51:25 -0300880 if (mutex_lock_interruptible(&dev->dev_mutex))
881 return -ERESTARTSYS;
Sachin Kamat56ed2212012-09-24 02:17:46 -0300882 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Hans Verkuilf4694032012-07-31 03:51:25 -0300883 if (!ctx) {
884 rc = -ENOMEM;
885 goto open_unlock;
886 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300887
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300888 v4l2_fh_init(&ctx->fh, video_devdata(file));
889 file->private_data = &ctx->fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300890 ctx->dev = dev;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300891 hdl = &ctx->hdl;
892 v4l2_ctrl_handler_init(hdl, 4);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300893 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
894 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
895 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
896 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300897 if (hdl->error) {
Dan Carpentera7bd7752012-08-14 02:58:56 -0300898 rc = hdl->error;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300899 v4l2_ctrl_handler_free(hdl);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300900 goto open_unlock;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300901 }
902 ctx->fh.ctrl_handler = hdl;
903 v4l2_ctrl_handler_setup(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300904
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300905 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
Hans Verkuil47556ff2012-07-18 11:33:22 -0300906 ctx->q_data[V4L2_M2M_SRC].width = 640;
907 ctx->q_data[V4L2_M2M_SRC].height = 480;
908 ctx->q_data[V4L2_M2M_SRC].sizeimage =
909 ctx->q_data[V4L2_M2M_SRC].width *
910 ctx->q_data[V4L2_M2M_SRC].height *
911 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
912 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
913 ctx->colorspace = V4L2_COLORSPACE_REC709;
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300914
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300915 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300916
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300917 if (IS_ERR(ctx->fh.m2m_ctx)) {
918 rc = PTR_ERR(ctx->fh.m2m_ctx);
Dan Carpenter16ee9bb2010-05-05 02:58:57 -0300919
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300920 v4l2_ctrl_handler_free(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300921 kfree(ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300922 goto open_unlock;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300923 }
924
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300925 v4l2_fh_add(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300926 atomic_inc(&dev->num_inst);
927
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300928 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
929 ctx, ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300930
Hans Verkuilf4694032012-07-31 03:51:25 -0300931open_unlock:
932 mutex_unlock(&dev->dev_mutex);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300933 return rc;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300934}
935
Hans Verkuil1f923a42014-09-22 09:27:17 -0300936static int vim2m_release(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300937{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300938 struct vim2m_dev *dev = video_drvdata(file);
939 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300940
941 dprintk(dev, "Releasing instance %p\n", ctx);
942
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300943 v4l2_fh_del(&ctx->fh);
944 v4l2_fh_exit(&ctx->fh);
945 v4l2_ctrl_handler_free(&ctx->hdl);
Hans Verkuilf4694032012-07-31 03:51:25 -0300946 mutex_lock(&dev->dev_mutex);
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300947 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300948 mutex_unlock(&dev->dev_mutex);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300949 kfree(ctx);
950
951 atomic_dec(&dev->num_inst);
952
953 return 0;
954}
955
Hans Verkuil1f923a42014-09-22 09:27:17 -0300956static const struct v4l2_file_operations vim2m_fops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300957 .owner = THIS_MODULE,
Hans Verkuil1f923a42014-09-22 09:27:17 -0300958 .open = vim2m_open,
959 .release = vim2m_release,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300960 .poll = v4l2_m2m_fop_poll,
Marek Szyprowski07e80302010-12-20 14:39:25 -0300961 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300962 .mmap = v4l2_m2m_fop_mmap,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300963};
964
Hans Verkuil1f923a42014-09-22 09:27:17 -0300965static struct video_device vim2m_videodev = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300966 .name = MEM2MEM_NAME,
Hans Verkuil954f3402012-09-05 06:05:50 -0300967 .vfl_dir = VFL_DIR_M2M,
Hans Verkuil1f923a42014-09-22 09:27:17 -0300968 .fops = &vim2m_fops,
969 .ioctl_ops = &vim2m_ioctl_ops,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300970 .minor = -1,
Hans Verkuil09365422015-03-09 13:33:56 -0300971 .release = video_device_release_empty,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300972};
973
974static struct v4l2_m2m_ops m2m_ops = {
975 .device_run = device_run,
976 .job_ready = job_ready,
977 .job_abort = job_abort,
978};
979
Hans Verkuil1f923a42014-09-22 09:27:17 -0300980static int vim2m_probe(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300981{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300982 struct vim2m_dev *dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300983 struct video_device *vfd;
984 int ret;
985
Sachin Kamat38a79962012-09-24 02:17:48 -0300986 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300987 if (!dev)
988 return -ENOMEM;
989
990 spin_lock_init(&dev->irqlock);
991
992 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
993 if (ret)
Sachin Kamat38a79962012-09-24 02:17:48 -0300994 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300995
996 atomic_set(&dev->num_inst, 0);
997 mutex_init(&dev->dev_mutex);
998
Hans Verkuil09365422015-03-09 13:33:56 -0300999 dev->vfd = vim2m_videodev;
1000 vfd = &dev->vfd;
Marek Szyprowski07e80302010-12-20 14:39:25 -03001001 vfd->lock = &dev->dev_mutex;
Hans Verkuil8f484d82013-06-27 02:44:04 -03001002 vfd->v4l2_dev = &dev->v4l2_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001003
1004 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1005 if (ret) {
1006 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
Hans Verkuil09365422015-03-09 13:33:56 -03001007 goto unreg_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001008 }
1009
1010 video_set_drvdata(vfd, dev);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001011 snprintf(vfd->name, sizeof(vfd->name), "%s", vim2m_videodev.name);
Hans Verkuil8f484d82013-06-27 02:44:04 -03001012 v4l2_info(&dev->v4l2_dev,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001013 "Device registered as /dev/video%d\n", vfd->num);
1014
1015 setup_timer(&dev->timer, device_isr, (long)dev);
1016 platform_set_drvdata(pdev, dev);
1017
1018 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1019 if (IS_ERR(dev->m2m_dev)) {
1020 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1021 ret = PTR_ERR(dev->m2m_dev);
1022 goto err_m2m;
1023 }
1024
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001025 return 0;
1026
1027err_m2m:
Sachin Kamatb7e13ef2012-09-24 02:17:45 -03001028 v4l2_m2m_release(dev->m2m_dev);
Hans Verkuil09365422015-03-09 13:33:56 -03001029 video_unregister_device(&dev->vfd);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001030unreg_dev:
1031 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001032
1033 return ret;
1034}
1035
Hans Verkuil1f923a42014-09-22 09:27:17 -03001036static int vim2m_remove(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001037{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001038 struct vim2m_dev *dev = platform_get_drvdata(pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001039
Hans Verkuil1f923a42014-09-22 09:27:17 -03001040 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001041 v4l2_m2m_release(dev->m2m_dev);
1042 del_timer_sync(&dev->timer);
Hans Verkuil09365422015-03-09 13:33:56 -03001043 video_unregister_device(&dev->vfd);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001044 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001045
1046 return 0;
1047}
1048
Hans Verkuil1f923a42014-09-22 09:27:17 -03001049static struct platform_driver vim2m_pdrv = {
1050 .probe = vim2m_probe,
1051 .remove = vim2m_remove,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001052 .driver = {
1053 .name = MEM2MEM_NAME,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001054 },
1055};
1056
Hans Verkuil1f923a42014-09-22 09:27:17 -03001057static void __exit vim2m_exit(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001058{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001059 platform_driver_unregister(&vim2m_pdrv);
1060 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001061}
1062
Hans Verkuil1f923a42014-09-22 09:27:17 -03001063static int __init vim2m_init(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001064{
1065 int ret;
1066
Hans Verkuil1f923a42014-09-22 09:27:17 -03001067 ret = platform_device_register(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001068 if (ret)
1069 return ret;
1070
Hans Verkuil1f923a42014-09-22 09:27:17 -03001071 ret = platform_driver_register(&vim2m_pdrv);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001072 if (ret)
Hans Verkuil1f923a42014-09-22 09:27:17 -03001073 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001074
1075 return 0;
1076}
1077
Hans Verkuil1f923a42014-09-22 09:27:17 -03001078module_init(vim2m_init);
1079module_exit(vim2m_exit);