blob: 924120d69da2d5e251c82e678b63442820c450ab [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
Hans Verkuil144bd0e2018-05-21 04:54:56 -04006 * destination, processes the data and issues an "irq" (simulated by a delayed
7 * workqueue).
Pawel Osciak96d8eab2010-04-23 05:38:38 -03008 * The device is capable of multi-instance, multi-buffer-per-transaction
9 * operation (via the mem2mem framework).
10 *
11 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
Pawel Osciak95072082011-03-13 15:23:32 -030012 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030013 * Marek Szyprowski, <m.szyprowski@samsung.com>
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the
18 * License, or (at your option) any later version
19 */
20#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/fs.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030023#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 Liakhovetski9ce3ce4d42012-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 u32 fourcc;
84 int depth;
85 /* Types the format can be used for */
86 u32 types;
87};
88
Hans Verkuil1f923a42014-09-22 09:27:17 -030089static struct vim2m_fmt formats[] = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030090 {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030091 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
92 .depth = 16,
93 /* Both capture and output format */
94 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
95 },
96 {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030097 .fourcc = V4L2_PIX_FMT_YUYV,
98 .depth = 16,
99 /* Output-only format */
100 .types = MEM2MEM_OUTPUT,
101 },
102};
103
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300104#define NUM_FORMATS ARRAY_SIZE(formats)
105
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300106/* Per-queue, driver-specific private data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300107struct vim2m_q_data {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300108 unsigned int width;
109 unsigned int height;
110 unsigned int sizeimage;
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300111 unsigned int sequence;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300112 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300113};
114
115enum {
116 V4L2_M2M_SRC = 0,
117 V4L2_M2M_DST = 1,
118};
119
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300120#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
121#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300122
Hans Verkuil1f923a42014-09-22 09:27:17 -0300123static struct vim2m_fmt *find_format(struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300124{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300125 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300126 unsigned int k;
127
128 for (k = 0; k < NUM_FORMATS; k++) {
129 fmt = &formats[k];
130 if (fmt->fourcc == f->fmt.pix.pixelformat)
131 break;
132 }
133
134 if (k == NUM_FORMATS)
135 return NULL;
136
137 return &formats[k];
138}
139
Hans Verkuil1f923a42014-09-22 09:27:17 -0300140struct vim2m_dev {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300141 struct v4l2_device v4l2_dev;
Hans Verkuil09365422015-03-09 13:33:56 -0300142 struct video_device vfd;
Hans Verkuile35f7022018-07-02 11:36:06 -0400143#ifdef CONFIG_MEDIA_CONTROLLER
144 struct media_device mdev;
145#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300146
147 atomic_t num_inst;
148 struct mutex dev_mutex;
149 spinlock_t irqlock;
150
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400151 struct delayed_work work_run;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300152
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;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300177 enum v4l2_ycbcr_encoding ycbcr_enc;
178 enum v4l2_xfer_func xfer_func;
179 enum v4l2_quantization quant;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300180
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300181 /* Source and destination queue data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300182 struct vim2m_q_data q_data[2];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300183};
184
Hans Verkuil1f923a42014-09-22 09:27:17 -0300185static inline struct vim2m_ctx *file2ctx(struct file *file)
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300186{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300187 return container_of(file->private_data, struct vim2m_ctx, fh);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300188}
189
Hans Verkuil1f923a42014-09-22 09:27:17 -0300190static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300191 enum v4l2_buf_type type)
192{
193 switch (type) {
194 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
195 return &ctx->q_data[V4L2_M2M_SRC];
196 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
197 return &ctx->q_data[V4L2_M2M_DST];
198 default:
199 BUG();
200 }
201 return NULL;
202}
203
204
Hans Verkuil1f923a42014-09-22 09:27:17 -0300205static int device_process(struct vim2m_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300206 struct vb2_v4l2_buffer *in_vb,
207 struct vb2_v4l2_buffer *out_vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300208{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300209 struct vim2m_dev *dev = ctx->dev;
210 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300211 u8 *p_in, *p_out;
212 int x, y, t, w;
213 int tile_w, bytes_left;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300214 int width, height, bytesperline;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300215
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300216 q_data = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300217
218 width = q_data->width;
219 height = q_data->height;
220 bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
221
Junghak Sung2d700712015-09-22 10:30:30 -0300222 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
223 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300224 if (!p_in || !p_out) {
225 v4l2_err(&dev->v4l2_dev,
226 "Acquiring kernel pointers to buffers failed\n");
227 return -EFAULT;
228 }
229
Junghak Sung2d700712015-09-22 10:30:30 -0300230 if (vb2_plane_size(&in_vb->vb2_buf, 0) >
231 vb2_plane_size(&out_vb->vb2_buf, 0)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300232 v4l2_err(&dev->v4l2_dev, "Output buffer is too small\n");
233 return -EINVAL;
234 }
235
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300236 tile_w = (width * (q_data[V4L2_M2M_DST].fmt->depth >> 3))
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300237 / MEM2MEM_NUM_TILES;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300238 bytes_left = bytesperline - tile_w * MEM2MEM_NUM_TILES;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300239 w = 0;
240
Junghak Sung2d700712015-09-22 10:30:30 -0300241 out_vb->sequence =
242 get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++;
243 in_vb->sequence = q_data->sequence++;
Hans Verkuil86824692019-01-11 06:43:12 -0500244 v4l2_m2m_buf_copy_data(in_vb, out_vb, true);
Hans Verkuild95d7c62013-04-17 03:04:10 -0300245
Tomasz Moń80072872012-06-12 06:43:49 -0300246 switch (ctx->mode) {
247 case MEM2MEM_HFLIP | MEM2MEM_VFLIP:
248 p_out += bytesperline * height - bytes_left;
249 for (y = 0; y < height; ++y) {
250 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
251 if (w & 0x1) {
252 for (x = 0; x < tile_w; ++x)
253 *--p_out = *p_in++ +
254 MEM2MEM_COLOR_STEP;
255 } else {
256 for (x = 0; x < tile_w; ++x)
257 *--p_out = *p_in++ -
258 MEM2MEM_COLOR_STEP;
259 }
260 ++w;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300261 }
Tomasz Moń80072872012-06-12 06:43:49 -0300262 p_in += bytes_left;
263 p_out -= bytes_left;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300264 }
Tomasz Moń80072872012-06-12 06:43:49 -0300265 break;
266
267 case MEM2MEM_HFLIP:
268 for (y = 0; y < height; ++y) {
269 p_out += MEM2MEM_NUM_TILES * tile_w;
270 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
271 if (w & 0x01) {
272 for (x = 0; x < tile_w; ++x)
273 *--p_out = *p_in++ +
274 MEM2MEM_COLOR_STEP;
275 } else {
276 for (x = 0; x < tile_w; ++x)
277 *--p_out = *p_in++ -
278 MEM2MEM_COLOR_STEP;
279 }
280 ++w;
281 }
282 p_in += bytes_left;
283 p_out += bytesperline;
284 }
285 break;
286
287 case MEM2MEM_VFLIP:
288 p_out += bytesperline * (height - 1);
289 for (y = 0; y < height; ++y) {
290 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
291 if (w & 0x1) {
292 for (x = 0; x < tile_w; ++x)
293 *p_out++ = *p_in++ +
294 MEM2MEM_COLOR_STEP;
295 } else {
296 for (x = 0; x < tile_w; ++x)
297 *p_out++ = *p_in++ -
298 MEM2MEM_COLOR_STEP;
299 }
300 ++w;
301 }
302 p_in += bytes_left;
303 p_out += bytes_left - 2 * bytesperline;
304 }
305 break;
306
307 default:
308 for (y = 0; y < height; ++y) {
309 for (t = 0; t < MEM2MEM_NUM_TILES; ++t) {
310 if (w & 0x1) {
311 for (x = 0; x < tile_w; ++x)
312 *p_out++ = *p_in++ +
313 MEM2MEM_COLOR_STEP;
314 } else {
315 for (x = 0; x < tile_w; ++x)
316 *p_out++ = *p_in++ -
317 MEM2MEM_COLOR_STEP;
318 }
319 ++w;
320 }
321 p_in += bytes_left;
322 p_out += bytes_left;
323 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300324 }
325
326 return 0;
327}
328
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300329/*
330 * mem2mem callbacks
331 */
332
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -0500333/*
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300334 * job_ready() - check whether an instance is ready to be scheduled to run
335 */
336static int job_ready(void *priv)
337{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300338 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300339
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300340 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
341 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300342 dprintk(ctx->dev, "Not enough buffers available\n");
343 return 0;
344 }
345
346 return 1;
347}
348
349static void job_abort(void *priv)
350{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300351 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300352
353 /* Will cancel the transaction in the next interrupt handler */
354 ctx->aborting = 1;
355}
356
357/* device_run() - prepares and starts the device
358 *
359 * This simulates all the immediate preparations required before starting
360 * a device. This will be called by the framework when it decides to schedule
361 * a particular instance.
362 */
363static void device_run(void *priv)
364{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300365 struct vim2m_ctx *ctx = priv;
366 struct vim2m_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300367 struct vb2_v4l2_buffer *src_buf, *dst_buf;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300368
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300369 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
370 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300371
Hans Verkuil86b93b22018-05-21 04:54:57 -0400372 /* Apply request controls if any */
373 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
374 &ctx->hdl);
375
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300376 device_process(ctx, src_buf, dst_buf);
377
Hans Verkuil86b93b22018-05-21 04:54:57 -0400378 /* Complete request controls if any */
379 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
380 &ctx->hdl);
381
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400382 /* Run delayed work, which simulates a hardware irq */
383 schedule_delayed_work(&dev->work_run, msecs_to_jiffies(ctx->transtime));
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300384}
385
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400386static void device_work(struct work_struct *w)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300387{
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400388 struct vim2m_dev *vim2m_dev =
389 container_of(w, struct vim2m_dev, work_run.work);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300390 struct vim2m_ctx *curr_ctx;
Junghak Sung2d700712015-09-22 10:30:30 -0300391 struct vb2_v4l2_buffer *src_vb, *dst_vb;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300392 unsigned long flags;
393
Hans Verkuil1f923a42014-09-22 09:27:17 -0300394 curr_ctx = v4l2_m2m_get_curr_priv(vim2m_dev->m2m_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300395
396 if (NULL == curr_ctx) {
Sachin Kamat26fdcf02012-09-24 02:17:47 -0300397 pr_err("Instance released before the end of transaction\n");
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300398 return;
399 }
400
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300401 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
402 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300403
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300404 curr_ctx->num_processed++;
405
Hans Verkuil1f923a42014-09-22 09:27:17 -0300406 spin_lock_irqsave(&vim2m_dev->irqlock, flags);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300407 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
408 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300409 spin_unlock_irqrestore(&vim2m_dev->irqlock, flags);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300410
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300411 if (curr_ctx->num_processed == curr_ctx->translen
412 || curr_ctx->aborting) {
413 dprintk(curr_ctx->dev, "Finishing transaction\n");
414 curr_ctx->num_processed = 0;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300415 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300416 } else {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300417 device_run(curr_ctx);
418 }
419}
420
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300421/*
422 * video ioctls
423 */
424static int vidioc_querycap(struct file *file, void *priv,
425 struct v4l2_capability *cap)
426{
427 strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1);
428 strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1);
Hans Verkuil72c2af62012-09-14 06:23:12 -0300429 snprintf(cap->bus_info, sizeof(cap->bus_info),
430 "platform:%s", MEM2MEM_NAME);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300431 return 0;
432}
433
434static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
435{
436 int i, num;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300437 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300438
439 num = 0;
440
441 for (i = 0; i < NUM_FORMATS; ++i) {
442 if (formats[i].types & type) {
443 /* index-th format of type type found ? */
444 if (num == f->index)
445 break;
446 /* Correct type but haven't reached our index yet,
447 * just increment per-type index */
448 ++num;
449 }
450 }
451
452 if (i < NUM_FORMATS) {
453 /* Format found */
454 fmt = &formats[i];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300455 f->pixelformat = fmt->fourcc;
456 return 0;
457 }
458
459 /* Format not found */
460 return -EINVAL;
461}
462
463static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
464 struct v4l2_fmtdesc *f)
465{
466 return enum_fmt(f, MEM2MEM_CAPTURE);
467}
468
469static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
470 struct v4l2_fmtdesc *f)
471{
472 return enum_fmt(f, MEM2MEM_OUTPUT);
473}
474
Hans Verkuil1f923a42014-09-22 09:27:17 -0300475static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300476{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300477 struct vb2_queue *vq;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300478 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300479
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300480 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300481 if (!vq)
482 return -EINVAL;
483
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300484 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300485
486 f->fmt.pix.width = q_data->width;
487 f->fmt.pix.height = q_data->height;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300488 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300489 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
490 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
491 f->fmt.pix.sizeimage = q_data->sizeimage;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300492 f->fmt.pix.colorspace = ctx->colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300493 f->fmt.pix.xfer_func = ctx->xfer_func;
494 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
495 f->fmt.pix.quantization = ctx->quant;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300496
497 return 0;
498}
499
500static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
501 struct v4l2_format *f)
502{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300503 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300504}
505
506static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
507 struct v4l2_format *f)
508{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300509 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300510}
511
Hans Verkuil1f923a42014-09-22 09:27:17 -0300512static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300513{
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300514 /* V4L2 specification suggests the driver corrects the format struct
515 * if any of the dimensions is unsupported */
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300516 if (f->fmt.pix.height < MIN_H)
517 f->fmt.pix.height = MIN_H;
518 else if (f->fmt.pix.height > MAX_H)
519 f->fmt.pix.height = MAX_H;
520
521 if (f->fmt.pix.width < MIN_W)
522 f->fmt.pix.width = MIN_W;
523 else if (f->fmt.pix.width > MAX_W)
524 f->fmt.pix.width = MAX_W;
525
526 f->fmt.pix.width &= ~DIM_ALIGN_MASK;
527 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
528 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil5c3112b2014-03-10 10:58:29 -0300529 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300530
531 return 0;
532}
533
534static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
535 struct v4l2_format *f)
536{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300537 struct vim2m_fmt *fmt;
538 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300539
540 fmt = find_format(f);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300541 if (!fmt) {
542 f->fmt.pix.pixelformat = formats[0].fourcc;
543 fmt = find_format(f);
544 }
545 if (!(fmt->types & MEM2MEM_CAPTURE)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300546 v4l2_err(&ctx->dev->v4l2_dev,
547 "Fourcc format (0x%08x) invalid.\n",
548 f->fmt.pix.pixelformat);
549 return -EINVAL;
550 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300551 f->fmt.pix.colorspace = ctx->colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300552 f->fmt.pix.xfer_func = ctx->xfer_func;
553 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
554 f->fmt.pix.quantization = ctx->quant;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300555
556 return vidioc_try_fmt(f, fmt);
557}
558
559static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
560 struct v4l2_format *f)
561{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300562 struct vim2m_fmt *fmt;
563 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300564
565 fmt = find_format(f);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300566 if (!fmt) {
567 f->fmt.pix.pixelformat = formats[0].fourcc;
568 fmt = find_format(f);
569 }
570 if (!(fmt->types & MEM2MEM_OUTPUT)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300571 v4l2_err(&ctx->dev->v4l2_dev,
572 "Fourcc format (0x%08x) invalid.\n",
573 f->fmt.pix.pixelformat);
574 return -EINVAL;
575 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300576 if (!f->fmt.pix.colorspace)
577 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300578
579 return vidioc_try_fmt(f, fmt);
580}
581
Hans Verkuil1f923a42014-09-22 09:27:17 -0300582static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300583{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300584 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300585 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300586
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300587 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300588 if (!vq)
589 return -EINVAL;
590
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300591 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300592 if (!q_data)
593 return -EINVAL;
594
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300595 if (vb2_is_busy(vq)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300596 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
Marek Szyprowski07e80302010-12-20 14:39:25 -0300597 return -EBUSY;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300598 }
599
600 q_data->fmt = find_format(f);
601 q_data->width = f->fmt.pix.width;
602 q_data->height = f->fmt.pix.height;
603 q_data->sizeimage = q_data->width * q_data->height
604 * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300605
606 dprintk(ctx->dev,
607 "Setting format for type %d, wxh: %dx%d, fmt: %d\n",
608 f->type, q_data->width, q_data->height, q_data->fmt->fourcc);
609
Marek Szyprowski07e80302010-12-20 14:39:25 -0300610 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300611}
612
613static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
614 struct v4l2_format *f)
615{
616 int ret;
617
618 ret = vidioc_try_fmt_vid_cap(file, priv, f);
619 if (ret)
620 return ret;
621
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300622 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300623}
624
625static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
626 struct v4l2_format *f)
627{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300628 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300629 int ret;
630
631 ret = vidioc_try_fmt_vid_out(file, priv, f);
632 if (ret)
633 return ret;
634
Hans Verkuil47556ff2012-07-18 11:33:22 -0300635 ret = vidioc_s_fmt(file2ctx(file), f);
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300636 if (!ret) {
Hans Verkuil47556ff2012-07-18 11:33:22 -0300637 ctx->colorspace = f->fmt.pix.colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300638 ctx->xfer_func = f->fmt.pix.xfer_func;
639 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
640 ctx->quant = f->fmt.pix.quantization;
641 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300642 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300643}
644
Hans Verkuil1f923a42014-09-22 09:27:17 -0300645static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300646{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300647 struct vim2m_ctx *ctx =
648 container_of(ctrl->handler, struct vim2m_ctx, hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300649
650 switch (ctrl->id) {
Tomasz Moń80072872012-06-12 06:43:49 -0300651 case V4L2_CID_HFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300652 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300653 ctx->mode |= MEM2MEM_HFLIP;
654 else
655 ctx->mode &= ~MEM2MEM_HFLIP;
656 break;
657
658 case V4L2_CID_VFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300659 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300660 ctx->mode |= MEM2MEM_VFLIP;
661 else
662 ctx->mode &= ~MEM2MEM_VFLIP;
663 break;
664
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300665 case V4L2_CID_TRANS_TIME_MSEC:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300666 ctx->transtime = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300667 break;
668
669 case V4L2_CID_TRANS_NUM_BUFS:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300670 ctx->translen = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300671 break;
672
673 default:
674 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
675 return -EINVAL;
676 }
677
678 return 0;
679}
680
Hans Verkuil1f923a42014-09-22 09:27:17 -0300681static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
682 .s_ctrl = vim2m_s_ctrl,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300683};
684
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300685
Hans Verkuil1f923a42014-09-22 09:27:17 -0300686static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300687 .vidioc_querycap = vidioc_querycap,
688
689 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
690 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
691 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
692 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
693
694 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
695 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
696 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
697 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
698
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300699 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
700 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
701 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
702 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Hans Verkuil7bcff1c2015-06-05 11:28:52 -0300703 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
704 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Hans Verkuilf5294f42014-11-18 09:51:07 -0300705 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300706
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300707 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
708 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300709
Hans Verkuil97a3c902012-07-18 10:54:59 -0300710 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
711 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300712};
713
714
715/*
716 * Queue operations
717 */
718
Hans Verkuil1f923a42014-09-22 09:27:17 -0300719static int vim2m_queue_setup(struct vb2_queue *vq,
Guennadi Liakhovetskifc714e702011-08-24 10:30:21 -0300720 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300721 unsigned int sizes[], struct device *alloc_devs[])
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300722{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300723 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
724 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300725 unsigned int size, count = *nbuffers;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300726
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300727 q_data = get_q_data(ctx, vq->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300728
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300729 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300730
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300731 while (size * count > MEM2MEM_VID_MEM_LIMIT)
732 (count)--;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200733 *nbuffers = count;
734
735 if (*nplanes)
736 return sizes[0] < size ? -EINVAL : 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300737
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300738 *nplanes = 1;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300739 sizes[0] = size;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300740
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300741 dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300742
743 return 0;
744}
745
Hans Verkuilab7afaf32019-01-16 10:01:14 -0200746static int vim2m_buf_out_validate(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300747{
Junghak Sung2d700712015-09-22 10:30:30 -0300748 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300749 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuilab7afaf32019-01-16 10:01:14 -0200750
751 if (vbuf->field == V4L2_FIELD_ANY)
752 vbuf->field = V4L2_FIELD_NONE;
753 if (vbuf->field != V4L2_FIELD_NONE) {
754 dprintk(ctx->dev, "%s field isn't supported\n", __func__);
755 return -EINVAL;
756 }
757
758 return 0;
759}
760
761static int vim2m_buf_prepare(struct vb2_buffer *vb)
762{
763 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300764 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300765
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300766 dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300767
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300768 q_data = get_q_data(ctx, vb->vb2_queue->type);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300769 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
770 dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n",
771 __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300772 return -EINVAL;
773 }
774
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300775 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300776
777 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300778}
779
Hans Verkuil1f923a42014-09-22 09:27:17 -0300780static void vim2m_buf_queue(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300781{
Junghak Sung2d700712015-09-22 10:30:30 -0300782 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300783 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300784
Junghak Sung2d700712015-09-22 10:30:30 -0300785 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbrich0f910bf2011-07-12 09:46:44 -0300786}
787
Hans Verkuil1f923a42014-09-22 09:27:17 -0300788static int vim2m_start_streaming(struct vb2_queue *q, unsigned count)
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300789{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300790 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
791 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300792
793 q_data->sequence = 0;
794 return 0;
795}
796
Hans Verkuil1f923a42014-09-22 09:27:17 -0300797static void vim2m_stop_streaming(struct vb2_queue *q)
Hans Verkuila7736322014-03-10 10:58:27 -0300798{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300799 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
Hans Verkuil52117be2018-11-07 09:04:54 -0500800 struct vim2m_dev *dev = ctx->dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300801 struct vb2_v4l2_buffer *vbuf;
Hans Verkuila7736322014-03-10 10:58:27 -0300802 unsigned long flags;
803
Hans Verkuil52117be2018-11-07 09:04:54 -0500804 cancel_delayed_work_sync(&dev->work_run);
Hans Verkuila7736322014-03-10 10:58:27 -0300805 for (;;) {
806 if (V4L2_TYPE_IS_OUTPUT(q->type))
Junghak Sung2d700712015-09-22 10:30:30 -0300807 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Hans Verkuila7736322014-03-10 10:58:27 -0300808 else
Junghak Sung2d700712015-09-22 10:30:30 -0300809 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
810 if (vbuf == NULL)
Hans Verkuile37559b2014-04-17 02:47:21 -0300811 return;
Hans Verkuil86b93b22018-05-21 04:54:57 -0400812 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
813 &ctx->hdl);
Hans Verkuila7736322014-03-10 10:58:27 -0300814 spin_lock_irqsave(&ctx->dev->irqlock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300815 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
Hans Verkuila7736322014-03-10 10:58:27 -0300816 spin_unlock_irqrestore(&ctx->dev->irqlock, flags);
817 }
Hans Verkuila7736322014-03-10 10:58:27 -0300818}
819
Hans Verkuil86b93b22018-05-21 04:54:57 -0400820static void vim2m_buf_request_complete(struct vb2_buffer *vb)
821{
822 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
823
824 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
825}
826
Julia Lawallb7b361f2016-09-08 20:59:10 -0300827static const struct vb2_ops vim2m_qops = {
Hans Verkuil1f923a42014-09-22 09:27:17 -0300828 .queue_setup = vim2m_queue_setup,
Hans Verkuilab7afaf32019-01-16 10:01:14 -0200829 .buf_out_validate = vim2m_buf_out_validate,
Hans Verkuil1f923a42014-09-22 09:27:17 -0300830 .buf_prepare = vim2m_buf_prepare,
831 .buf_queue = vim2m_buf_queue,
832 .start_streaming = vim2m_start_streaming,
833 .stop_streaming = vim2m_stop_streaming,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300834 .wait_prepare = vb2_ops_wait_prepare,
835 .wait_finish = vb2_ops_wait_finish,
Hans Verkuil86b93b22018-05-21 04:54:57 -0400836 .buf_request_complete = vim2m_buf_request_complete,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300837};
838
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300839static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300840{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300841 struct vim2m_ctx *ctx = priv;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300842 int ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300843
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300844 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Hans Verkuil782f36c2014-03-10 10:58:26 -0300845 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300846 src_vq->drv_priv = ctx;
847 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300848 src_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300849 src_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300850 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300851 src_vq->lock = &ctx->dev->dev_mutex;
Hans Verkuile5079cf2018-08-23 10:18:35 -0400852 src_vq->supports_requests = true;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300853
854 ret = vb2_queue_init(src_vq);
855 if (ret)
856 return ret;
857
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300858 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil782f36c2014-03-10 10:58:26 -0300859 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300860 dst_vq->drv_priv = ctx;
861 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300862 dst_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300863 dst_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -0300864 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300865 dst_vq->lock = &ctx->dev->dev_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300866
867 return vb2_queue_init(dst_vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300868}
869
Hans Verkuil1f923a42014-09-22 09:27:17 -0300870static const struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
871 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300872 .id = V4L2_CID_TRANS_TIME_MSEC,
873 .name = "Transaction Time (msec)",
874 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuil43405832014-03-10 10:58:23 -0300875 .def = MEM2MEM_DEF_TRANSTIME,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300876 .min = 1,
877 .max = 10001,
Hans Verkuil43405832014-03-10 10:58:23 -0300878 .step = 1,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300879};
880
Hans Verkuil1f923a42014-09-22 09:27:17 -0300881static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
882 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300883 .id = V4L2_CID_TRANS_NUM_BUFS,
884 .name = "Buffers Per Transaction",
885 .type = V4L2_CTRL_TYPE_INTEGER,
886 .def = 1,
887 .min = 1,
888 .max = MEM2MEM_DEF_NUM_BUFS,
889 .step = 1,
890};
891
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300892/*
893 * File operations
894 */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300895static int vim2m_open(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300896{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300897 struct vim2m_dev *dev = video_drvdata(file);
898 struct vim2m_ctx *ctx = NULL;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300899 struct v4l2_ctrl_handler *hdl;
Hans Verkuilf4694032012-07-31 03:51:25 -0300900 int rc = 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300901
Hans Verkuilf4694032012-07-31 03:51:25 -0300902 if (mutex_lock_interruptible(&dev->dev_mutex))
903 return -ERESTARTSYS;
Sachin Kamat56ed2212012-09-24 02:17:46 -0300904 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Hans Verkuilf4694032012-07-31 03:51:25 -0300905 if (!ctx) {
906 rc = -ENOMEM;
907 goto open_unlock;
908 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300909
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300910 v4l2_fh_init(&ctx->fh, video_devdata(file));
911 file->private_data = &ctx->fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300912 ctx->dev = dev;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300913 hdl = &ctx->hdl;
914 v4l2_ctrl_handler_init(hdl, 4);
Hans Verkuil1f923a42014-09-22 09:27:17 -0300915 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
916 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
917 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
918 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300919 if (hdl->error) {
Dan Carpentera7bd7752012-08-14 02:58:56 -0300920 rc = hdl->error;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300921 v4l2_ctrl_handler_free(hdl);
Santosh Kumar Singh7c13a4d2016-12-19 14:12:46 -0200922 kfree(ctx);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300923 goto open_unlock;
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300924 }
925 ctx->fh.ctrl_handler = hdl;
926 v4l2_ctrl_handler_setup(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300927
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300928 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
Hans Verkuil47556ff2012-07-18 11:33:22 -0300929 ctx->q_data[V4L2_M2M_SRC].width = 640;
930 ctx->q_data[V4L2_M2M_SRC].height = 480;
931 ctx->q_data[V4L2_M2M_SRC].sizeimage =
932 ctx->q_data[V4L2_M2M_SRC].width *
933 ctx->q_data[V4L2_M2M_SRC].height *
934 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
935 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
936 ctx->colorspace = V4L2_COLORSPACE_REC709;
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300937
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300938 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300939
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300940 if (IS_ERR(ctx->fh.m2m_ctx)) {
941 rc = PTR_ERR(ctx->fh.m2m_ctx);
Dan Carpenter16ee9bb12010-05-05 02:58:57 -0300942
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300943 v4l2_ctrl_handler_free(hdl);
Santosh Kumar Singh7c13a4d2016-12-19 14:12:46 -0200944 v4l2_fh_exit(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300945 kfree(ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300946 goto open_unlock;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300947 }
948
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300949 v4l2_fh_add(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300950 atomic_inc(&dev->num_inst);
951
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300952 dprintk(dev, "Created instance: %p, m2m_ctx: %p\n",
953 ctx, ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300954
Hans Verkuilf4694032012-07-31 03:51:25 -0300955open_unlock:
956 mutex_unlock(&dev->dev_mutex);
Dan Carpentera7bd7752012-08-14 02:58:56 -0300957 return rc;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300958}
959
Hans Verkuil1f923a42014-09-22 09:27:17 -0300960static int vim2m_release(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300961{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300962 struct vim2m_dev *dev = video_drvdata(file);
963 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300964
965 dprintk(dev, "Releasing instance %p\n", ctx);
966
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300967 v4l2_fh_del(&ctx->fh);
968 v4l2_fh_exit(&ctx->fh);
969 v4l2_ctrl_handler_free(&ctx->hdl);
Hans Verkuilf4694032012-07-31 03:51:25 -0300970 mutex_lock(&dev->dev_mutex);
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300971 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -0300972 mutex_unlock(&dev->dev_mutex);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300973 kfree(ctx);
974
975 atomic_dec(&dev->num_inst);
976
977 return 0;
978}
979
Hans Verkuil1f923a42014-09-22 09:27:17 -0300980static const struct v4l2_file_operations vim2m_fops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300981 .owner = THIS_MODULE,
Hans Verkuil1f923a42014-09-22 09:27:17 -0300982 .open = vim2m_open,
983 .release = vim2m_release,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300984 .poll = v4l2_m2m_fop_poll,
Marek Szyprowski07e80302010-12-20 14:39:25 -0300985 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300986 .mmap = v4l2_m2m_fop_mmap,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300987};
988
Bhumika Goyal53031352017-08-26 08:57:26 -0400989static const struct video_device vim2m_videodev = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300990 .name = MEM2MEM_NAME,
Hans Verkuil954f3402012-09-05 06:05:50 -0300991 .vfl_dir = VFL_DIR_M2M,
Hans Verkuil1f923a42014-09-22 09:27:17 -0300992 .fops = &vim2m_fops,
993 .ioctl_ops = &vim2m_ioctl_ops,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300994 .minor = -1,
Hans Verkuil09365422015-03-09 13:33:56 -0300995 .release = video_device_release_empty,
Hans Verkuil47fc65fa2018-11-15 03:16:22 -0500996 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300997};
998
Julia Lawall86ab0b12017-08-06 04:25:19 -0400999static const struct v4l2_m2m_ops m2m_ops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001000 .device_run = device_run,
1001 .job_ready = job_ready,
1002 .job_abort = job_abort,
1003};
1004
Hans Verkuil86b93b22018-05-21 04:54:57 -04001005static const struct media_device_ops m2m_media_ops = {
1006 .req_validate = vb2_request_validate,
Ezequiel Garciaef86eaf2018-10-18 14:54:29 -04001007 .req_queue = v4l2_m2m_request_queue,
Hans Verkuil86b93b22018-05-21 04:54:57 -04001008};
1009
Hans Verkuil1f923a42014-09-22 09:27:17 -03001010static int vim2m_probe(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001011{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001012 struct vim2m_dev *dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001013 struct video_device *vfd;
1014 int ret;
1015
Sachin Kamat38a79962012-09-24 02:17:48 -03001016 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001017 if (!dev)
1018 return -ENOMEM;
1019
1020 spin_lock_init(&dev->irqlock);
1021
1022 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1023 if (ret)
Sachin Kamat38a79962012-09-24 02:17:48 -03001024 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001025
1026 atomic_set(&dev->num_inst, 0);
1027 mutex_init(&dev->dev_mutex);
1028
Hans Verkuil09365422015-03-09 13:33:56 -03001029 dev->vfd = vim2m_videodev;
1030 vfd = &dev->vfd;
Marek Szyprowski07e80302010-12-20 14:39:25 -03001031 vfd->lock = &dev->dev_mutex;
Hans Verkuil8f484d82013-06-27 02:44:04 -03001032 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil144bd0e2018-05-21 04:54:56 -04001033 INIT_DELAYED_WORK(&dev->work_run, device_work);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001034
1035 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
1036 if (ret) {
1037 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
Hans Verkuile35f7022018-07-02 11:36:06 -04001038 goto unreg_v4l2;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001039 }
1040
1041 video_set_drvdata(vfd, dev);
Hans Verkuil8f484d82013-06-27 02:44:04 -03001042 v4l2_info(&dev->v4l2_dev,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001043 "Device registered as /dev/video%d\n", vfd->num);
1044
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001045 platform_set_drvdata(pdev, dev);
1046
1047 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1048 if (IS_ERR(dev->m2m_dev)) {
1049 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1050 ret = PTR_ERR(dev->m2m_dev);
Hans Verkuile35f7022018-07-02 11:36:06 -04001051 goto unreg_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001052 }
1053
Hans Verkuile35f7022018-07-02 11:36:06 -04001054#ifdef CONFIG_MEDIA_CONTROLLER
1055 dev->mdev.dev = &pdev->dev;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001056 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
Hans Verkuile35f7022018-07-02 11:36:06 -04001057 media_device_init(&dev->mdev);
Hans Verkuil86b93b22018-05-21 04:54:57 -04001058 dev->mdev.ops = &m2m_media_ops;
Hans Verkuile35f7022018-07-02 11:36:06 -04001059 dev->v4l2_dev.mdev = &dev->mdev;
1060
1061 ret = v4l2_m2m_register_media_controller(dev->m2m_dev,
1062 vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER);
1063 if (ret) {
1064 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
1065 goto unreg_m2m;
1066 }
1067
1068 ret = media_device_register(&dev->mdev);
1069 if (ret) {
1070 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
1071 goto unreg_m2m_mc;
1072 }
1073#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001074 return 0;
1075
Hans Verkuile35f7022018-07-02 11:36:06 -04001076#ifdef CONFIG_MEDIA_CONTROLLER
1077unreg_m2m_mc:
1078 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1079unreg_m2m:
Sachin Kamatb7e13ef2012-09-24 02:17:45 -03001080 v4l2_m2m_release(dev->m2m_dev);
Hans Verkuile35f7022018-07-02 11:36:06 -04001081#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001082unreg_dev:
Hans Verkuile35f7022018-07-02 11:36:06 -04001083 video_unregister_device(&dev->vfd);
1084unreg_v4l2:
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001085 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001086
1087 return ret;
1088}
1089
Hans Verkuil1f923a42014-09-22 09:27:17 -03001090static int vim2m_remove(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001091{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001092 struct vim2m_dev *dev = platform_get_drvdata(pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001093
Hans Verkuil1f923a42014-09-22 09:27:17 -03001094 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
Hans Verkuile35f7022018-07-02 11:36:06 -04001095
1096#ifdef CONFIG_MEDIA_CONTROLLER
1097 media_device_unregister(&dev->mdev);
1098 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
1099 media_device_cleanup(&dev->mdev);
1100#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001101 v4l2_m2m_release(dev->m2m_dev);
Hans Verkuil09365422015-03-09 13:33:56 -03001102 video_unregister_device(&dev->vfd);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001103 v4l2_device_unregister(&dev->v4l2_dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001104
1105 return 0;
1106}
1107
Hans Verkuil1f923a42014-09-22 09:27:17 -03001108static struct platform_driver vim2m_pdrv = {
1109 .probe = vim2m_probe,
1110 .remove = vim2m_remove,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001111 .driver = {
1112 .name = MEM2MEM_NAME,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001113 },
1114};
1115
Hans Verkuil1f923a42014-09-22 09:27:17 -03001116static void __exit vim2m_exit(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001117{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001118 platform_driver_unregister(&vim2m_pdrv);
1119 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001120}
1121
Hans Verkuil1f923a42014-09-22 09:27:17 -03001122static int __init vim2m_init(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001123{
1124 int ret;
1125
Hans Verkuil1f923a42014-09-22 09:27:17 -03001126 ret = platform_device_register(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001127 if (ret)
1128 return ret;
1129
Hans Verkuil1f923a42014-09-22 09:27:17 -03001130 ret = platform_driver_register(&vim2m_pdrv);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001131 if (ret)
Hans Verkuil1f923a42014-09-22 09:27:17 -03001132 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001133
Niklas Söderlundb20b51f2015-12-25 15:25:16 -02001134 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001135}
1136
Hans Verkuil1f923a42014-09-22 09:27:17 -03001137module_init(vim2m_init);
1138module_exit(vim2m_exit);