blob: 47575490e74af8c692326fedc4a8d548b5b26a83 [file] [log] [blame]
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -05001// SPDX-License-Identifier: GPL-2.0+
Pawel Osciak96d8eab2010-04-23 05:38:38 -03002/*
3 * A virtual v4l2-mem2mem example device.
4 *
5 * This is a virtual device driver for testing mem-to-mem videobuf framework.
6 * It simulates a device that uses memory buffers for both source and
Hans Verkuil144bd0e2018-05-21 04:54:56 -04007 * destination, processes the data and issues an "irq" (simulated by a delayed
8 * workqueue).
Pawel Osciak96d8eab2010-04-23 05:38:38 -03009 * The device is capable of multi-instance, multi-buffer-per-transaction
10 * operation (via the mem2mem framework).
11 *
12 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
Pawel Osciak95072082011-03-13 15:23:32 -030013 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030014 * Marek Szyprowski, <m.szyprowski@samsung.com>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030015 */
16#include <linux/module.h>
17#include <linux/delay.h>
18#include <linux/fs.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030019#include <linux/sched.h>
Randy Dunlap6b46c392010-05-07 15:22:26 -030020#include <linux/slab.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030021
22#include <linux/platform_device.h>
23#include <media/v4l2-mem2mem.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
Hans Verkuild3dd59c2012-07-18 10:35:37 -030026#include <media/v4l2-ctrls.h>
Hans Verkuil97a3c902012-07-18 10:54:59 -030027#include <media/v4l2-event.h>
Marek Szyprowskid80ee382011-01-12 06:50:55 -030028#include <media/videobuf2-vmalloc.h>
Pawel Osciak96d8eab2010-04-23 05:38:38 -030029
Pawel Osciak96d8eab2010-04-23 05:38:38 -030030MODULE_DESCRIPTION("Virtual device for mem2mem framework testing");
Pawel Osciak95072082011-03-13 15:23:32 -030031MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030032MODULE_LICENSE("GPL");
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -050033MODULE_VERSION("0.2");
Hans Verkuil1f923a42014-09-22 09:27:17 -030034MODULE_ALIAS("mem2mem_testdev");
Pawel Osciak96d8eab2010-04-23 05:38:38 -030035
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -050036static unsigned int debug;
Hans Verkuild95d7c62013-04-17 03:04:10 -030037module_param(debug, uint, 0644);
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -050038MODULE_PARM_DESC(debug, "debug level");
Hans Verkuild95d7c62013-04-17 03:04:10 -030039
Mauro Carvalho Chehabf0ef0222019-01-29 14:00:17 -020040/* Default transaction time in msec */
41static unsigned int default_transtime = 40; /* Max 25 fps */
42module_param(default_transtime, uint, 0644);
43MODULE_PARM_DESC(default_transtime, "default transaction time in ms");
44
Pawel Osciak96d8eab2010-04-23 05:38:38 -030045#define MIN_W 32
46#define MIN_H 32
47#define MAX_W 640
48#define MAX_H 480
Mauro Carvalho Chehabc8af44e2019-02-28 01:25:50 -050049
50/* Pixel alignment for non-bayer formats */
51#define WIDTH_ALIGN 2
52#define HEIGHT_ALIGN 1
53
54/* Pixel alignment for bayer formats */
55#define BAYER_WIDTH_ALIGN 2
56#define BAYER_HEIGHT_ALIGN 2
Pawel Osciak96d8eab2010-04-23 05:38:38 -030057
58/* Flags that indicate a format can be used for capture/output */
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -050059#define MEM2MEM_CAPTURE BIT(0)
60#define MEM2MEM_OUTPUT BIT(1)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030061
Hans Verkuil1f923a42014-09-22 09:27:17 -030062#define MEM2MEM_NAME "vim2m"
Pawel Osciak96d8eab2010-04-23 05:38:38 -030063
64/* Per queue */
65#define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME
66/* In bytes, per queue */
67#define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024)
68
Tomasz Moń80072872012-06-12 06:43:49 -030069/* Flags that indicate processing mode */
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -050070#define MEM2MEM_HFLIP BIT(0)
71#define MEM2MEM_VFLIP BIT(1)
Tomasz Moń80072872012-06-12 06:43:49 -030072
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -050073#define dprintk(dev, lvl, fmt, arg...) \
74 v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030075
Hans Verkuil1f923a42014-09-22 09:27:17 -030076static void vim2m_dev_release(struct device *dev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -030077{}
78
Hans Verkuil1f923a42014-09-22 09:27:17 -030079static struct platform_device vim2m_pdev = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030080 .name = MEM2MEM_NAME,
Hans Verkuil1f923a42014-09-22 09:27:17 -030081 .dev.release = vim2m_dev_release,
Pawel Osciak96d8eab2010-04-23 05:38:38 -030082};
83
Hans Verkuil1f923a42014-09-22 09:27:17 -030084struct vim2m_fmt {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030085 u32 fourcc;
86 int depth;
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -050087 /* Types the format can be used for */
88 u32 types;
Pawel Osciak96d8eab2010-04-23 05:38:38 -030089};
90
Hans Verkuil1f923a42014-09-22 09:27:17 -030091static struct vim2m_fmt formats[] = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -030092 {
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -020093 .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */
Pawel Osciak96d8eab2010-04-23 05:38:38 -030094 .depth = 16,
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -050095 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -020096 }, {
97 .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */
98 .depth = 16,
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -050099 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200100 }, {
101 .fourcc = V4L2_PIX_FMT_RGB24,
102 .depth = 24,
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500103 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200104 }, {
105 .fourcc = V4L2_PIX_FMT_BGR24,
106 .depth = 24,
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500107 .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT,
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200108 }, {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300109 .fourcc = V4L2_PIX_FMT_YUYV,
110 .depth = 16,
Mauro Carvalho Chehab69d68a42019-03-01 07:41:24 -0500111 .types = MEM2MEM_CAPTURE,
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500112 }, {
113 .fourcc = V4L2_PIX_FMT_SBGGR8,
114 .depth = 8,
115 .types = MEM2MEM_CAPTURE,
116 }, {
117 .fourcc = V4L2_PIX_FMT_SGBRG8,
118 .depth = 8,
119 .types = MEM2MEM_CAPTURE,
120 }, {
121 .fourcc = V4L2_PIX_FMT_SGRBG8,
122 .depth = 8,
123 .types = MEM2MEM_CAPTURE,
124 }, {
125 .fourcc = V4L2_PIX_FMT_SRGGB8,
126 .depth = 8,
127 .types = MEM2MEM_CAPTURE,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300128 },
129};
130
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300131#define NUM_FORMATS ARRAY_SIZE(formats)
132
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300133/* Per-queue, driver-specific private data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300134struct vim2m_q_data {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300135 unsigned int width;
136 unsigned int height;
137 unsigned int sizeimage;
Hans Verkuilca5f5fd2014-03-10 10:58:28 -0300138 unsigned int sequence;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300139 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300140};
141
142enum {
143 V4L2_M2M_SRC = 0,
144 V4L2_M2M_DST = 1,
145};
146
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300147#define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000)
148#define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300149
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500150static struct vim2m_fmt *find_format(u32 fourcc)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300151{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300152 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300153 unsigned int k;
154
155 for (k = 0; k < NUM_FORMATS; k++) {
156 fmt = &formats[k];
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500157 if (fmt->fourcc == fourcc)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300158 break;
159 }
160
161 if (k == NUM_FORMATS)
162 return NULL;
163
164 return &formats[k];
165}
166
Mauro Carvalho Chehabc8af44e2019-02-28 01:25:50 -0500167static void get_alignment(u32 fourcc,
168 unsigned int *walign, unsigned int *halign)
169{
170 switch (fourcc) {
171 case V4L2_PIX_FMT_SBGGR8:
172 case V4L2_PIX_FMT_SGBRG8:
173 case V4L2_PIX_FMT_SGRBG8:
174 case V4L2_PIX_FMT_SRGGB8:
175 *walign = BAYER_WIDTH_ALIGN;
176 *halign = BAYER_HEIGHT_ALIGN;
177 return;
178 default:
179 *walign = WIDTH_ALIGN;
180 *halign = HEIGHT_ALIGN;
181 return;
182 }
183}
184
Hans Verkuil1f923a42014-09-22 09:27:17 -0300185struct vim2m_dev {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300186 struct v4l2_device v4l2_dev;
Hans Verkuil09365422015-03-09 13:33:56 -0300187 struct video_device vfd;
Hans Verkuile35f7022018-07-02 11:36:06 -0400188#ifdef CONFIG_MEDIA_CONTROLLER
189 struct media_device mdev;
190#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300191
192 atomic_t num_inst;
193 struct mutex dev_mutex;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300194
195 struct v4l2_m2m_dev *m2m_dev;
196};
197
Hans Verkuil1f923a42014-09-22 09:27:17 -0300198struct vim2m_ctx {
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300199 struct v4l2_fh fh;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300200 struct vim2m_dev *dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300201
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300202 struct v4l2_ctrl_handler hdl;
203
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300204 /* Processed buffers in this transaction */
205 u8 num_processed;
206
207 /* Transaction length (i.e. how many buffers per transaction) */
208 u32 translen;
209 /* Transaction time (i.e. simulated processing time) in milliseconds */
210 u32 transtime;
211
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -0200212 struct mutex vb_mutex;
213 struct delayed_work work_run;
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -0200214
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300215 /* Abort requested by m2m */
216 int aborting;
217
Tomasz Moń80072872012-06-12 06:43:49 -0300218 /* Processing mode */
219 int mode;
220
Hans Verkuil47556ff2012-07-18 11:33:22 -0300221 enum v4l2_colorspace colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300222 enum v4l2_ycbcr_encoding ycbcr_enc;
223 enum v4l2_xfer_func xfer_func;
224 enum v4l2_quantization quant;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300225
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300226 /* Source and destination queue data */
Hans Verkuil1f923a42014-09-22 09:27:17 -0300227 struct vim2m_q_data q_data[2];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300228};
229
Hans Verkuil1f923a42014-09-22 09:27:17 -0300230static inline struct vim2m_ctx *file2ctx(struct file *file)
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300231{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300232 return container_of(file->private_data, struct vim2m_ctx, fh);
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300233}
234
Hans Verkuil1f923a42014-09-22 09:27:17 -0300235static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx,
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -0500236 enum v4l2_buf_type type)
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300237{
238 switch (type) {
239 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
240 return &ctx->q_data[V4L2_M2M_SRC];
241 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
242 return &ctx->q_data[V4L2_M2M_DST];
243 default:
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500244 return NULL;
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300245 }
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300246}
247
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -0500248static const char *type_name(enum v4l2_buf_type type)
249{
250 switch (type) {
251 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
252 return "Output";
253 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
254 return "Capture";
255 default:
256 return "Invalid";
257 }
258}
259
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200260#define CLIP(__color) \
261 (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color)))
262
Mauro Carvalho Chehab5f78f7e2019-03-01 08:10:59 -0500263static void copy_line(struct vim2m_q_data *q_data_out,
264 u8 *src, u8 *dst, bool reverse)
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500265{
Mauro Carvalho Chehab5f78f7e2019-03-01 08:10:59 -0500266 int x, depth = q_data_out->fmt->depth >> 3;
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500267
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500268 if (!reverse) {
Mauro Carvalho Chehab5f78f7e2019-03-01 08:10:59 -0500269 memcpy(dst, src, q_data_out->width * depth);
270 } else {
271 for (x = 0; x < q_data_out->width >> 1; x++) {
272 memcpy(dst, src, depth);
273 memcpy(dst + depth, src - depth, depth);
274 src -= depth << 1;
275 dst += depth << 1;
276 }
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500277 return;
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500278 }
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500279}
280
281static void copy_two_pixels(struct vim2m_q_data *q_data_in,
282 struct vim2m_q_data *q_data_out,
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500283 u8 *src[2], u8 **dst, int ypos, bool reverse)
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200284{
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500285 struct vim2m_fmt *out = q_data_out->fmt;
286 struct vim2m_fmt *in = q_data_in->fmt;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200287 u8 _r[2], _g[2], _b[2], *r, *g, *b;
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500288 int i;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200289
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200290 /* Step 1: read two consecutive pixels from src pointer */
291
292 r = _r;
293 g = _g;
294 b = _b;
295
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200296 switch (in->fourcc) {
297 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
298 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400299 u16 pix = le16_to_cpu(*(__le16 *)(src[i]));
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200300
301 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
302 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
303 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200304 }
305 break;
306 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
307 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400308 u16 pix = be16_to_cpu(*(__be16 *)(src[i]));
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200309
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400310 *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07;
311 *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03;
312 *b++ = (u8)((pix & 0x1f) << 3) | 0x07;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200313 }
314 break;
Mauro Carvalho Chehab69d68a42019-03-01 07:41:24 -0500315 default:
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200316 case V4L2_PIX_FMT_RGB24:
317 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500318 *r++ = src[i][0];
319 *g++ = src[i][1];
320 *b++ = src[i][2];
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200321 }
322 break;
323 case V4L2_PIX_FMT_BGR24:
324 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500325 *b++ = src[i][0];
326 *g++ = src[i][1];
327 *r++ = src[i][2];
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200328 }
329 break;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200330 }
331
332 /* Step 2: store two consecutive points, reversing them if needed */
333
334 r = _r;
335 g = _g;
336 b = _b;
337
338 switch (out->fourcc) {
339 case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */
340 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400341 u16 pix;
342 __le16 *dst_pix = (__le16 *)*dst;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200343
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400344 pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
345 (*b >> 3);
346
347 *dst_pix = cpu_to_le16(pix);
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200348
349 *dst += 2;
350 }
351 return;
352 case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */
353 for (i = 0; i < 2; i++) {
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400354 u16 pix;
355 __be16 *dst_pix = (__be16 *)*dst;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200356
Mauro Carvalho Chehab060162c2019-03-28 15:02:23 -0400357 pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) |
358 (*b >> 3);
359
360 *dst_pix = cpu_to_be16(pix);
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200361
362 *dst += 2;
363 }
364 return;
365 case V4L2_PIX_FMT_RGB24:
366 for (i = 0; i < 2; i++) {
367 *(*dst)++ = *r++;
368 *(*dst)++ = *g++;
369 *(*dst)++ = *b++;
370 }
371 return;
372 case V4L2_PIX_FMT_BGR24:
373 for (i = 0; i < 2; i++) {
374 *(*dst)++ = *b++;
375 *(*dst)++ = *g++;
376 *(*dst)++ = *r++;
377 }
378 return;
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500379 case V4L2_PIX_FMT_YUYV:
380 default:
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200381 {
382 u8 y, y1, u, v;
383
384 y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
385 + 524288) >> 15);
386 u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b)
387 + 4210688) >> 15);
388 v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++)
389 + 4210688) >> 15);
390 y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b)
391 + 524288) >> 15);
392
393 *(*dst)++ = y;
394 *(*dst)++ = u;
395
396 *(*dst)++ = y1;
397 *(*dst)++ = v;
398 return;
399 }
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500400 case V4L2_PIX_FMT_SBGGR8:
401 if (!(ypos & 1)) {
402 *(*dst)++ = *b;
403 *(*dst)++ = *++g;
404 } else {
405 *(*dst)++ = *g;
406 *(*dst)++ = *++r;
407 }
408 return;
409 case V4L2_PIX_FMT_SGBRG8:
410 if (!(ypos & 1)) {
411 *(*dst)++ = *g;
412 *(*dst)++ = *++b;
413 } else {
414 *(*dst)++ = *r;
415 *(*dst)++ = *++g;
416 }
417 return;
418 case V4L2_PIX_FMT_SGRBG8:
419 if (!(ypos & 1)) {
420 *(*dst)++ = *g;
421 *(*dst)++ = *++r;
422 } else {
423 *(*dst)++ = *b;
424 *(*dst)++ = *++g;
425 }
426 return;
427 case V4L2_PIX_FMT_SRGGB8:
428 if (!(ypos & 1)) {
429 *(*dst)++ = *r;
430 *(*dst)++ = *++g;
431 } else {
432 *(*dst)++ = *g;
433 *(*dst)++ = *++b;
434 }
435 return;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200436 }
437}
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300438
Hans Verkuil1f923a42014-09-22 09:27:17 -0300439static int device_process(struct vim2m_ctx *ctx,
Junghak Sung2d700712015-09-22 10:30:30 -0300440 struct vb2_v4l2_buffer *in_vb,
441 struct vb2_v4l2_buffer *out_vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300442{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300443 struct vim2m_dev *dev = ctx->dev;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200444 struct vim2m_q_data *q_data_in, *q_data_out;
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500445 u8 *p_in, *p_line, *p_in_x[2], *p, *p_out;
446 unsigned int width, height, bytesperline, bytes_per_pixel;
447 unsigned int x, y, y_in, y_out, x_int, x_fract, x_err, x_offset;
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500448 int start, end, step;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300449
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200450 q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500451 if (!q_data_in)
452 return 0;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200453 bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3;
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500454 bytes_per_pixel = q_data_in->fmt->depth >> 3;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300455
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200456 q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500457 if (!q_data_out)
458 return 0;
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500459
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500460 /* As we're doing scaling, use the output dimensions here */
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500461 height = q_data_out->height;
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500462 width = q_data_out->width;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300463
Junghak Sung2d700712015-09-22 10:30:30 -0300464 p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0);
465 p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300466 if (!p_in || !p_out) {
467 v4l2_err(&dev->v4l2_dev,
468 "Acquiring kernel pointers to buffers failed\n");
469 return -EFAULT;
470 }
471
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500472 out_vb->sequence = q_data_out->sequence++;
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200473 in_vb->sequence = q_data_in->sequence++;
Ezequiel Garciaa4d3d612019-02-05 16:20:33 -0500474 v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true);
Hans Verkuild95d7c62013-04-17 03:04:10 -0300475
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200476 if (ctx->mode & MEM2MEM_VFLIP) {
477 start = height - 1;
478 end = -1;
479 step = -1;
480 } else {
481 start = 0;
482 end = height;
483 step = 1;
484 }
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500485 y_out = 0;
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500486
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -0500487 /*
488 * When format and resolution are identical,
489 * we can use a faster copy logic
490 */
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500491 if (q_data_in->fmt->fourcc == q_data_out->fmt->fourcc &&
492 q_data_in->width == q_data_out->width &&
493 q_data_in->height == q_data_out->height) {
494 for (y = start; y != end; y += step, y_out++) {
495 p = p_in + (y * bytesperline);
496 if (ctx->mode & MEM2MEM_HFLIP)
497 p += bytesperline - (q_data_in->fmt->depth >> 3);
498
Mauro Carvalho Chehab5f78f7e2019-03-01 08:10:59 -0500499 copy_line(q_data_out, p, p_out,
500 ctx->mode & MEM2MEM_HFLIP);
501
502 p_out += bytesperline;
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500503 }
504 return 0;
505 }
506
Mauro Carvalho Chehab5f78f7e2019-03-01 08:10:59 -0500507 /* Slower algorithm with format conversion, hflip, vflip and scaler */
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500508
509 /* To speed scaler up, use Bresenham for X dimension */
510 x_int = q_data_in->width / q_data_out->width;
511 x_fract = q_data_in->width % q_data_out->width;
512
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500513 for (y = start; y != end; y += step, y_out++) {
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500514 y_in = (y * q_data_in->height) / q_data_out->height;
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500515 x_offset = 0;
516 x_err = 0;
Mauro Carvalho Chehab0b390d02019-03-01 05:25:43 -0500517
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500518 p_line = p_in + (y_in * bytesperline);
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200519 if (ctx->mode & MEM2MEM_HFLIP)
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500520 p_line += bytesperline - (q_data_in->fmt->depth >> 3);
521 p_in_x[0] = p_line;
Tomasz Moń80072872012-06-12 06:43:49 -0300522
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500523 for (x = 0; x < width >> 1; x++) {
524 x_offset += x_int;
525 x_err += x_fract;
526 if (x_err > width) {
527 x_offset++;
528 x_err -= width;
529 }
530
531 if (ctx->mode & MEM2MEM_HFLIP)
532 p_in_x[1] = p_line - x_offset * bytes_per_pixel;
533 else
534 p_in_x[1] = p_line + x_offset * bytes_per_pixel;
535
536 copy_two_pixels(q_data_in, q_data_out,
537 p_in_x, &p_out, y_out,
Mauro Carvalho Chehab8aa153f2019-01-29 14:00:15 -0200538 ctx->mode & MEM2MEM_HFLIP);
Mauro Carvalho Chehab24cc4182019-02-26 06:36:00 -0500539
Mauro Carvalho Chehabf9729922019-03-01 06:14:52 -0500540 /* Calculate the next p_in_x0 */
541 x_offset += x_int;
542 x_err += x_fract;
543 if (x_err > width) {
544 x_offset++;
545 x_err -= width;
546 }
547
548 if (ctx->mode & MEM2MEM_HFLIP)
549 p_in_x[0] = p_line - x_offset * bytes_per_pixel;
550 else
551 p_in_x[0] = p_line + x_offset * bytes_per_pixel;
552 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300553 }
554
555 return 0;
556}
557
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300558/*
559 * mem2mem callbacks
560 */
561
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -0500562/*
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300563 * job_ready() - check whether an instance is ready to be scheduled to run
564 */
565static int job_ready(void *priv)
566{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300567 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300568
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300569 if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen
570 || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) {
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -0500571 dprintk(ctx->dev, 1, "Not enough buffers available\n");
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300572 return 0;
573 }
574
575 return 1;
576}
577
578static void job_abort(void *priv)
579{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300580 struct vim2m_ctx *ctx = priv;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300581
582 /* Will cancel the transaction in the next interrupt handler */
583 ctx->aborting = 1;
584}
585
586/* device_run() - prepares and starts the device
587 *
588 * This simulates all the immediate preparations required before starting
589 * a device. This will be called by the framework when it decides to schedule
590 * a particular instance.
591 */
592static void device_run(void *priv)
593{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300594 struct vim2m_ctx *ctx = priv;
Junghak Sung2d700712015-09-22 10:30:30 -0300595 struct vb2_v4l2_buffer *src_buf, *dst_buf;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300596
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300597 src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
598 dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300599
Hans Verkuil86b93b22018-05-21 04:54:57 -0400600 /* Apply request controls if any */
601 v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req,
602 &ctx->hdl);
603
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300604 device_process(ctx, src_buf, dst_buf);
605
Hans Verkuil86b93b22018-05-21 04:54:57 -0400606 /* Complete request controls if any */
607 v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req,
608 &ctx->hdl);
609
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400610 /* Run delayed work, which simulates a hardware irq */
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -0200611 schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime));
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300612}
613
Hans Verkuil144bd0e2018-05-21 04:54:56 -0400614static void device_work(struct work_struct *w)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300615{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300616 struct vim2m_ctx *curr_ctx;
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -0200617 struct vim2m_dev *vim2m_dev;
Junghak Sung2d700712015-09-22 10:30:30 -0300618 struct vb2_v4l2_buffer *src_vb, *dst_vb;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300619
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -0200620 curr_ctx = container_of(w, struct vim2m_ctx, work_run.work);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300621
Mauro Carvalho Chehab22f05d62019-02-07 12:28:19 -0500622 vim2m_dev = curr_ctx->dev;
623
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300624 src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx);
625 dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300626
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300627 curr_ctx->num_processed++;
628
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300629 v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE);
630 v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE);
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300631
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300632 if (curr_ctx->num_processed == curr_ctx->translen
633 || curr_ctx->aborting) {
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -0500634 dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n");
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300635 curr_ctx->num_processed = 0;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300636 v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300637 } else {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300638 device_run(curr_ctx);
639 }
640}
641
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300642/*
643 * video ioctls
644 */
645static int vidioc_querycap(struct file *file, void *priv,
646 struct v4l2_capability *cap)
647{
Mauro Carvalho Chehab85709cb2018-09-10 08:19:16 -0400648 strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver));
649 strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card));
Hans Verkuil72c2af62012-09-14 06:23:12 -0300650 snprintf(cap->bus_info, sizeof(cap->bus_info),
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -0500651 "platform:%s", MEM2MEM_NAME);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300652 return 0;
653}
654
655static int enum_fmt(struct v4l2_fmtdesc *f, u32 type)
656{
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500657 int i, num;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300658 struct vim2m_fmt *fmt;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300659
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500660 num = 0;
661
662 for (i = 0; i < NUM_FORMATS; ++i) {
663 if (formats[i].types & type) {
664 /* index-th format of type type found ? */
665 if (num == f->index)
666 break;
667 /*
668 * Correct type but haven't reached our index yet,
669 * just increment per-type index
670 */
671 ++num;
672 }
673 }
674
675 if (i < NUM_FORMATS) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300676 /* Format found */
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500677 fmt = &formats[i];
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300678 f->pixelformat = fmt->fourcc;
679 return 0;
680 }
681
682 /* Format not found */
683 return -EINVAL;
684}
685
686static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
687 struct v4l2_fmtdesc *f)
688{
689 return enum_fmt(f, MEM2MEM_CAPTURE);
690}
691
692static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
693 struct v4l2_fmtdesc *f)
694{
695 return enum_fmt(f, MEM2MEM_OUTPUT);
696}
697
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500698static int vidioc_enum_framesizes(struct file *file, void *priv,
699 struct v4l2_frmsizeenum *fsize)
700{
701 if (fsize->index != 0)
702 return -EINVAL;
703
704 if (!find_format(fsize->pixel_format))
705 return -EINVAL;
706
707 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
708 fsize->stepwise.min_width = MIN_W;
709 fsize->stepwise.min_height = MIN_H;
710 fsize->stepwise.max_width = MAX_W;
711 fsize->stepwise.max_height = MAX_H;
Mauro Carvalho Chehabc8af44e2019-02-28 01:25:50 -0500712
713 get_alignment(fsize->pixel_format,
714 &fsize->stepwise.step_width,
715 &fsize->stepwise.step_height);
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500716 return 0;
717}
718
Hans Verkuil1f923a42014-09-22 09:27:17 -0300719static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300720{
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300721 struct vb2_queue *vq;
Hans Verkuil1f923a42014-09-22 09:27:17 -0300722 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300723
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300724 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300725 if (!vq)
726 return -EINVAL;
727
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300728 q_data = get_q_data(ctx, f->type);
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500729 if (!q_data)
730 return -EINVAL;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300731
732 f->fmt.pix.width = q_data->width;
733 f->fmt.pix.height = q_data->height;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300734 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300735 f->fmt.pix.pixelformat = q_data->fmt->fourcc;
736 f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3;
737 f->fmt.pix.sizeimage = q_data->sizeimage;
Hans Verkuil47556ff2012-07-18 11:33:22 -0300738 f->fmt.pix.colorspace = ctx->colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300739 f->fmt.pix.xfer_func = ctx->xfer_func;
740 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
741 f->fmt.pix.quantization = ctx->quant;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300742
743 return 0;
744}
745
746static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
747 struct v4l2_format *f)
748{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300749 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300750}
751
752static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
753 struct v4l2_format *f)
754{
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300755 return vidioc_g_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300756}
757
Hans Verkuil1f923a42014-09-22 09:27:17 -0300758static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300759{
Mauro Carvalho Chehabc8af44e2019-02-28 01:25:50 -0500760 int walign, halign;
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -0500761 /*
762 * V4L2 specification specifies the driver corrects the
763 * format struct if any of the dimensions is unsupported
764 */
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300765 if (f->fmt.pix.height < MIN_H)
766 f->fmt.pix.height = MIN_H;
767 else if (f->fmt.pix.height > MAX_H)
768 f->fmt.pix.height = MAX_H;
769
770 if (f->fmt.pix.width < MIN_W)
771 f->fmt.pix.width = MIN_W;
772 else if (f->fmt.pix.width > MAX_W)
773 f->fmt.pix.width = MAX_W;
774
Mauro Carvalho Chehabc8af44e2019-02-28 01:25:50 -0500775 get_alignment(f->fmt.pix.pixelformat, &walign, &halign);
776 f->fmt.pix.width &= ~(walign - 1);
777 f->fmt.pix.height &= ~(halign - 1);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300778 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
779 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
Hans Verkuil5c3112b2014-03-10 10:58:29 -0300780 f->fmt.pix.field = V4L2_FIELD_NONE;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300781
782 return 0;
783}
784
785static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
786 struct v4l2_format *f)
787{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300788 struct vim2m_fmt *fmt;
789 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300790
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500791 fmt = find_format(f->fmt.pix.pixelformat);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300792 if (!fmt) {
793 f->fmt.pix.pixelformat = formats[0].fourcc;
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500794 fmt = find_format(f->fmt.pix.pixelformat);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300795 }
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500796 if (!(fmt->types & MEM2MEM_CAPTURE)) {
797 v4l2_err(&ctx->dev->v4l2_dev,
798 "Fourcc format (0x%08x) invalid.\n",
799 f->fmt.pix.pixelformat);
800 return -EINVAL;
801 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300802 f->fmt.pix.colorspace = ctx->colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300803 f->fmt.pix.xfer_func = ctx->xfer_func;
804 f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc;
805 f->fmt.pix.quantization = ctx->quant;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300806
807 return vidioc_try_fmt(f, fmt);
808}
809
810static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
811 struct v4l2_format *f)
812{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300813 struct vim2m_fmt *fmt;
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500814 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300815
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500816 fmt = find_format(f->fmt.pix.pixelformat);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300817 if (!fmt) {
818 f->fmt.pix.pixelformat = formats[0].fourcc;
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500819 fmt = find_format(f->fmt.pix.pixelformat);
Hans Verkuil4e8ec0a2014-03-10 10:58:24 -0300820 }
Mauro Carvalho Chehab1875a7c2019-02-01 07:58:46 -0500821 if (!(fmt->types & MEM2MEM_OUTPUT)) {
822 v4l2_err(&ctx->dev->v4l2_dev,
823 "Fourcc format (0x%08x) invalid.\n",
824 f->fmt.pix.pixelformat);
825 return -EINVAL;
826 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300827 if (!f->fmt.pix.colorspace)
828 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300829
830 return vidioc_try_fmt(f, fmt);
831}
832
Hans Verkuil1f923a42014-09-22 09:27:17 -0300833static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300834{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300835 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300836 struct vb2_queue *vq;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300837
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300838 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300839 if (!vq)
840 return -EINVAL;
841
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300842 q_data = get_q_data(ctx, f->type);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300843 if (!q_data)
844 return -EINVAL;
845
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300846 if (vb2_is_busy(vq)) {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300847 v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__);
Marek Szyprowski07e80302010-12-20 14:39:25 -0300848 return -EBUSY;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300849 }
850
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500851 q_data->fmt = find_format(f->fmt.pix.pixelformat);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300852 q_data->width = f->fmt.pix.width;
853 q_data->height = f->fmt.pix.height;
854 q_data->sizeimage = q_data->width * q_data->height
855 * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300856
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -0500857 dprintk(ctx->dev, 1,
858 "Format for type %s: %dx%d (%d bpp), fmt: %c%c%c%c\n",
859 type_name(f->type), q_data->width, q_data->height,
860 q_data->fmt->depth,
861 (q_data->fmt->fourcc & 0xff),
862 (q_data->fmt->fourcc >> 8) & 0xff,
863 (q_data->fmt->fourcc >> 16) & 0xff,
864 (q_data->fmt->fourcc >> 24) & 0xff);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300865
Marek Szyprowski07e80302010-12-20 14:39:25 -0300866 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300867}
868
869static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
870 struct v4l2_format *f)
871{
872 int ret;
873
874 ret = vidioc_try_fmt_vid_cap(file, priv, f);
875 if (ret)
876 return ret;
877
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300878 return vidioc_s_fmt(file2ctx(file), f);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300879}
880
881static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
882 struct v4l2_format *f)
883{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300884 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300885 int ret;
886
887 ret = vidioc_try_fmt_vid_out(file, priv, f);
888 if (ret)
889 return ret;
890
Hans Verkuil47556ff2012-07-18 11:33:22 -0300891 ret = vidioc_s_fmt(file2ctx(file), f);
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300892 if (!ret) {
Hans Verkuil47556ff2012-07-18 11:33:22 -0300893 ctx->colorspace = f->fmt.pix.colorspace;
Hans Verkuil9f8b3332016-07-18 08:00:20 -0300894 ctx->xfer_func = f->fmt.pix.xfer_func;
895 ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc;
896 ctx->quant = f->fmt.pix.quantization;
897 }
Hans Verkuil47556ff2012-07-18 11:33:22 -0300898 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300899}
900
Hans Verkuil1f923a42014-09-22 09:27:17 -0300901static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl)
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300902{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300903 struct vim2m_ctx *ctx =
904 container_of(ctrl->handler, struct vim2m_ctx, hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300905
906 switch (ctrl->id) {
Tomasz Moń80072872012-06-12 06:43:49 -0300907 case V4L2_CID_HFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300908 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300909 ctx->mode |= MEM2MEM_HFLIP;
910 else
911 ctx->mode &= ~MEM2MEM_HFLIP;
912 break;
913
914 case V4L2_CID_VFLIP:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300915 if (ctrl->val)
Tomasz Moń80072872012-06-12 06:43:49 -0300916 ctx->mode |= MEM2MEM_VFLIP;
917 else
918 ctx->mode &= ~MEM2MEM_VFLIP;
919 break;
920
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300921 case V4L2_CID_TRANS_TIME_MSEC:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300922 ctx->transtime = ctrl->val;
Mauro Carvalho Chehabf0ef0222019-01-29 14:00:17 -0200923 if (ctx->transtime < 1)
924 ctx->transtime = 1;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300925 break;
926
927 case V4L2_CID_TRANS_NUM_BUFS:
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300928 ctx->translen = ctrl->val;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300929 break;
930
931 default:
932 v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n");
933 return -EINVAL;
934 }
935
936 return 0;
937}
938
Hans Verkuil1f923a42014-09-22 09:27:17 -0300939static const struct v4l2_ctrl_ops vim2m_ctrl_ops = {
940 .s_ctrl = vim2m_s_ctrl,
Hans Verkuild3dd59c2012-07-18 10:35:37 -0300941};
942
Hans Verkuil1f923a42014-09-22 09:27:17 -0300943static const struct v4l2_ioctl_ops vim2m_ioctl_ops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300944 .vidioc_querycap = vidioc_querycap,
945
946 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
Mauro Carvalho Chehab9c9c3962019-02-26 16:03:45 -0500947 .vidioc_enum_framesizes = vidioc_enum_framesizes,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300948 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
949 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
950 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
951
952 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
953 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
954 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
955 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
956
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300957 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
958 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
959 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
960 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Hans Verkuil7bcff1c2015-06-05 11:28:52 -0300961 .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
962 .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
Hans Verkuilf5294f42014-11-18 09:51:07 -0300963 .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300964
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -0300965 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
966 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300967
Hans Verkuil97a3c902012-07-18 10:54:59 -0300968 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
969 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300970};
971
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300972/*
973 * Queue operations
974 */
975
Hans Verkuil1f923a42014-09-22 09:27:17 -0300976static int vim2m_queue_setup(struct vb2_queue *vq,
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -0500977 unsigned int *nbuffers,
978 unsigned int *nplanes,
979 unsigned int sizes[],
980 struct device *alloc_devs[])
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300981{
Hans Verkuil1f923a42014-09-22 09:27:17 -0300982 struct vim2m_ctx *ctx = vb2_get_drv_priv(vq);
983 struct vim2m_q_data *q_data;
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300984 unsigned int size, count = *nbuffers;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300985
Tomasz Moń9f4161a2012-06-08 04:47:34 -0300986 q_data = get_q_data(ctx, vq->type);
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -0500987 if (!q_data)
988 return -EINVAL;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300989
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300990 size = q_data->width * q_data->height * q_data->fmt->depth >> 3;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300991
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300992 while (size * count > MEM2MEM_VID_MEM_LIMIT)
993 (count)--;
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200994 *nbuffers = count;
995
996 if (*nplanes)
997 return sizes[0] < size ? -EINVAL : 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -0300998
Marek Szyprowskid80ee382011-01-12 06:50:55 -0300999 *nplanes = 1;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001000 sizes[0] = size;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001001
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001002 dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n",
1003 type_name(vq->type), count, size);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001004
1005 return 0;
1006}
1007
Hans Verkuilab7afaf32019-01-16 10:01:14 -02001008static int vim2m_buf_out_validate(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001009{
Junghak Sung2d700712015-09-22 10:30:30 -03001010 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001011 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuilab7afaf32019-01-16 10:01:14 -02001012
1013 if (vbuf->field == V4L2_FIELD_ANY)
1014 vbuf->field = V4L2_FIELD_NONE;
1015 if (vbuf->field != V4L2_FIELD_NONE) {
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001016 dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__);
Hans Verkuilab7afaf32019-01-16 10:01:14 -02001017 return -EINVAL;
1018 }
1019
1020 return 0;
1021}
1022
1023static int vim2m_buf_prepare(struct vb2_buffer *vb)
1024{
1025 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001026 struct vim2m_q_data *q_data;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001027
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001028 dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type));
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001029
Tomasz Moń9f4161a2012-06-08 04:47:34 -03001030 q_data = get_q_data(ctx, vb->vb2_queue->type);
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -05001031 if (!q_data)
1032 return -EINVAL;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001033 if (vb2_plane_size(vb, 0) < q_data->sizeimage) {
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001034 dprintk(ctx->dev, 1,
1035 "%s data will not fit into plane (%lu < %lu)\n",
1036 __func__, vb2_plane_size(vb, 0),
1037 (long)q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001038 return -EINVAL;
1039 }
1040
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001041 vb2_set_plane_payload(vb, 0, q_data->sizeimage);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001042
1043 return 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001044}
1045
Hans Verkuil1f923a42014-09-22 09:27:17 -03001046static void vim2m_buf_queue(struct vb2_buffer *vb)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001047{
Junghak Sung2d700712015-09-22 10:30:30 -03001048 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001049 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -03001050
Junghak Sung2d700712015-09-22 10:30:30 -03001051 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Michael Olbrich0f910bf2011-07-12 09:46:44 -03001052}
1053
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -05001054static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count)
Hans Verkuilca5f5fd2014-03-10 10:58:28 -03001055{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001056 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
1057 struct vim2m_q_data *q_data = get_q_data(ctx, q->type);
Hans Verkuilca5f5fd2014-03-10 10:58:28 -03001058
Mauro Carvalho Chehab971d62d2019-03-01 11:46:11 -05001059 if (!q_data)
1060 return -EINVAL;
1061
Jernej Skrabecc362f772019-09-12 15:55:55 -03001062 if (V4L2_TYPE_IS_OUTPUT(q->type))
1063 ctx->aborting = 0;
1064
Hans Verkuilca5f5fd2014-03-10 10:58:28 -03001065 q_data->sequence = 0;
1066 return 0;
1067}
1068
Hans Verkuil1f923a42014-09-22 09:27:17 -03001069static void vim2m_stop_streaming(struct vb2_queue *q)
Hans Verkuila7736322014-03-10 10:58:27 -03001070{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001071 struct vim2m_ctx *ctx = vb2_get_drv_priv(q);
Junghak Sung2d700712015-09-22 10:30:30 -03001072 struct vb2_v4l2_buffer *vbuf;
Hans Verkuila7736322014-03-10 10:58:27 -03001073
Mauro Carvalho Chehaba8566d72019-02-18 10:29:31 -05001074 cancel_delayed_work_sync(&ctx->work_run);
Hans Verkuil240809e2019-01-11 07:07:25 -05001075
Hans Verkuila7736322014-03-10 10:58:27 -03001076 for (;;) {
1077 if (V4L2_TYPE_IS_OUTPUT(q->type))
Junghak Sung2d700712015-09-22 10:30:30 -03001078 vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
Hans Verkuila7736322014-03-10 10:58:27 -03001079 else
Junghak Sung2d700712015-09-22 10:30:30 -03001080 vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -05001081 if (!vbuf)
Hans Verkuile37559b2014-04-17 02:47:21 -03001082 return;
Hans Verkuil86b93b22018-05-21 04:54:57 -04001083 v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1084 &ctx->hdl);
Junghak Sung2d700712015-09-22 10:30:30 -03001085 v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
Hans Verkuila7736322014-03-10 10:58:27 -03001086 }
Hans Verkuila7736322014-03-10 10:58:27 -03001087}
1088
Hans Verkuil86b93b22018-05-21 04:54:57 -04001089static void vim2m_buf_request_complete(struct vb2_buffer *vb)
1090{
1091 struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1092
1093 v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1094}
1095
Julia Lawallb7b361f2016-09-08 20:59:10 -03001096static const struct vb2_ops vim2m_qops = {
Hans Verkuil1f923a42014-09-22 09:27:17 -03001097 .queue_setup = vim2m_queue_setup,
Hans Verkuilab7afaf32019-01-16 10:01:14 -02001098 .buf_out_validate = vim2m_buf_out_validate,
Hans Verkuil1f923a42014-09-22 09:27:17 -03001099 .buf_prepare = vim2m_buf_prepare,
1100 .buf_queue = vim2m_buf_queue,
1101 .start_streaming = vim2m_start_streaming,
1102 .stop_streaming = vim2m_stop_streaming,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001103 .wait_prepare = vb2_ops_wait_prepare,
1104 .wait_finish = vb2_ops_wait_finish,
Hans Verkuil86b93b22018-05-21 04:54:57 -04001105 .buf_request_complete = vim2m_buf_request_complete,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001106};
1107
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -05001108static int queue_init(void *priv, struct vb2_queue *src_vq,
1109 struct vb2_queue *dst_vq)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001110{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001111 struct vim2m_ctx *ctx = priv;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001112 int ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001113
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001114 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
Hans Verkuil782f36c2014-03-10 10:58:26 -03001115 src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001116 src_vq->drv_priv = ctx;
1117 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001118 src_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001119 src_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001120 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -02001121 src_vq->lock = &ctx->vb_mutex;
Hans Verkuile5079cf2018-08-23 10:18:35 -04001122 src_vq->supports_requests = true;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001123
1124 ret = vb2_queue_init(src_vq);
1125 if (ret)
1126 return ret;
1127
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001128 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Hans Verkuil782f36c2014-03-10 10:58:26 -03001129 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001130 dst_vq->drv_priv = ctx;
1131 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001132 dst_vq->ops = &vim2m_qops;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001133 dst_vq->mem_ops = &vb2_vmalloc_memops;
Sakari Ailusade48682014-02-25 19:12:19 -03001134 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -02001135 dst_vq->lock = &ctx->vb_mutex;
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001136
1137 return vb2_queue_init(dst_vq);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001138}
1139
Mauro Carvalho Chehabf0ef0222019-01-29 14:00:17 -02001140static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = {
Hans Verkuil1f923a42014-09-22 09:27:17 -03001141 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001142 .id = V4L2_CID_TRANS_TIME_MSEC,
1143 .name = "Transaction Time (msec)",
1144 .type = V4L2_CTRL_TYPE_INTEGER,
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001145 .min = 1,
1146 .max = 10001,
Hans Verkuil43405832014-03-10 10:58:23 -03001147 .step = 1,
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001148};
1149
Hans Verkuil1f923a42014-09-22 09:27:17 -03001150static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = {
1151 .ops = &vim2m_ctrl_ops,
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001152 .id = V4L2_CID_TRANS_NUM_BUFS,
1153 .name = "Buffers Per Transaction",
1154 .type = V4L2_CTRL_TYPE_INTEGER,
1155 .def = 1,
1156 .min = 1,
1157 .max = MEM2MEM_DEF_NUM_BUFS,
1158 .step = 1,
1159};
1160
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001161/*
1162 * File operations
1163 */
Hans Verkuil1f923a42014-09-22 09:27:17 -03001164static int vim2m_open(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001165{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001166 struct vim2m_dev *dev = video_drvdata(file);
1167 struct vim2m_ctx *ctx = NULL;
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001168 struct v4l2_ctrl_handler *hdl;
Hans Verkuilf4694032012-07-31 03:51:25 -03001169 int rc = 0;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001170
Hans Verkuilf4694032012-07-31 03:51:25 -03001171 if (mutex_lock_interruptible(&dev->dev_mutex))
1172 return -ERESTARTSYS;
Sachin Kamat56ed2212012-09-24 02:17:46 -03001173 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
Hans Verkuilf4694032012-07-31 03:51:25 -03001174 if (!ctx) {
1175 rc = -ENOMEM;
1176 goto open_unlock;
1177 }
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001178
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001179 v4l2_fh_init(&ctx->fh, video_devdata(file));
1180 file->private_data = &ctx->fh;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001181 ctx->dev = dev;
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001182 hdl = &ctx->hdl;
1183 v4l2_ctrl_handler_init(hdl, 4);
Hans Verkuil1f923a42014-09-22 09:27:17 -03001184 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0);
1185 v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0);
Mauro Carvalho Chehabf0ef0222019-01-29 14:00:17 -02001186
1187 vim2m_ctrl_trans_time_msec.def = default_transtime;
Hans Verkuil1f923a42014-09-22 09:27:17 -03001188 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL);
1189 v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL);
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001190 if (hdl->error) {
Dan Carpentera7bd7752012-08-14 02:58:56 -03001191 rc = hdl->error;
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001192 v4l2_ctrl_handler_free(hdl);
Santosh Kumar Singh7c13a4d2016-12-19 14:12:46 -02001193 kfree(ctx);
Dan Carpentera7bd7752012-08-14 02:58:56 -03001194 goto open_unlock;
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001195 }
1196 ctx->fh.ctrl_handler = hdl;
1197 v4l2_ctrl_handler_setup(hdl);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001198
Tomasz Moń9f4161a2012-06-08 04:47:34 -03001199 ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0];
Hans Verkuil47556ff2012-07-18 11:33:22 -03001200 ctx->q_data[V4L2_M2M_SRC].width = 640;
1201 ctx->q_data[V4L2_M2M_SRC].height = 480;
1202 ctx->q_data[V4L2_M2M_SRC].sizeimage =
1203 ctx->q_data[V4L2_M2M_SRC].width *
1204 ctx->q_data[V4L2_M2M_SRC].height *
1205 (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3);
1206 ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1207 ctx->colorspace = V4L2_COLORSPACE_REC709;
Tomasz Moń9f4161a2012-06-08 04:47:34 -03001208
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001209 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
Marek Szyprowskid80ee382011-01-12 06:50:55 -03001210
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -02001211 mutex_init(&ctx->vb_mutex);
Mauro Carvalho Chehabb3e64e52019-01-29 14:00:16 -02001212 INIT_DELAYED_WORK(&ctx->work_run, device_work);
1213
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001214 if (IS_ERR(ctx->fh.m2m_ctx)) {
1215 rc = PTR_ERR(ctx->fh.m2m_ctx);
Dan Carpenter16ee9bb2010-05-05 02:58:57 -03001216
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001217 v4l2_ctrl_handler_free(hdl);
Santosh Kumar Singh7c13a4d2016-12-19 14:12:46 -02001218 v4l2_fh_exit(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001219 kfree(ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -03001220 goto open_unlock;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001221 }
1222
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001223 v4l2_fh_add(&ctx->fh);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001224 atomic_inc(&dev->num_inst);
1225
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001226 dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n",
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001227 ctx, ctx->fh.m2m_ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001228
Hans Verkuilf4694032012-07-31 03:51:25 -03001229open_unlock:
1230 mutex_unlock(&dev->dev_mutex);
Dan Carpentera7bd7752012-08-14 02:58:56 -03001231 return rc;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001232}
1233
Hans Verkuil1f923a42014-09-22 09:27:17 -03001234static int vim2m_release(struct file *file)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001235{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001236 struct vim2m_dev *dev = video_drvdata(file);
1237 struct vim2m_ctx *ctx = file2ctx(file);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001238
Mauro Carvalho Chehabde5c46a2019-02-26 07:29:57 -05001239 dprintk(dev, 1, "Releasing instance %p\n", ctx);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001240
Hans Verkuild3dd59c2012-07-18 10:35:37 -03001241 v4l2_fh_del(&ctx->fh);
1242 v4l2_fh_exit(&ctx->fh);
1243 v4l2_ctrl_handler_free(&ctx->hdl);
Hans Verkuilf4694032012-07-31 03:51:25 -03001244 mutex_lock(&dev->dev_mutex);
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001245 v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
Hans Verkuilf4694032012-07-31 03:51:25 -03001246 mutex_unlock(&dev->dev_mutex);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001247 kfree(ctx);
1248
1249 atomic_dec(&dev->num_inst);
1250
1251 return 0;
1252}
1253
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001254static void vim2m_device_release(struct video_device *vdev)
1255{
1256 struct vim2m_dev *dev = container_of(vdev, struct vim2m_dev, vfd);
1257
1258 v4l2_device_unregister(&dev->v4l2_dev);
1259 v4l2_m2m_release(dev->m2m_dev);
Hans Verkuil9f22e882019-11-09 14:03:08 +01001260#ifdef CONFIG_MEDIA_CONTROLLER
1261 media_device_cleanup(&dev->mdev);
1262#endif
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001263 kfree(dev);
1264}
1265
Hans Verkuil1f923a42014-09-22 09:27:17 -03001266static const struct v4l2_file_operations vim2m_fops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001267 .owner = THIS_MODULE,
Hans Verkuil1f923a42014-09-22 09:27:17 -03001268 .open = vim2m_open,
1269 .release = vim2m_release,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001270 .poll = v4l2_m2m_fop_poll,
Marek Szyprowski07e80302010-12-20 14:39:25 -03001271 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrocki20702eb2013-08-25 17:27:45 -03001272 .mmap = v4l2_m2m_fop_mmap,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001273};
1274
Bhumika Goyal53031352017-08-26 08:57:26 -04001275static const struct video_device vim2m_videodev = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001276 .name = MEM2MEM_NAME,
Hans Verkuil954f3402012-09-05 06:05:50 -03001277 .vfl_dir = VFL_DIR_M2M,
Hans Verkuil1f923a42014-09-22 09:27:17 -03001278 .fops = &vim2m_fops,
1279 .ioctl_ops = &vim2m_ioctl_ops,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001280 .minor = -1,
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001281 .release = vim2m_device_release,
Hans Verkuil47fc65fa2018-11-15 03:16:22 -05001282 .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001283};
1284
Julia Lawall86ab0b12017-08-06 04:25:19 -04001285static const struct v4l2_m2m_ops m2m_ops = {
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001286 .device_run = device_run,
1287 .job_ready = job_ready,
1288 .job_abort = job_abort,
1289};
1290
Hans Verkuil86b93b22018-05-21 04:54:57 -04001291static const struct media_device_ops m2m_media_ops = {
1292 .req_validate = vb2_request_validate,
Ezequiel Garciaef86eaf2018-10-18 14:54:29 -04001293 .req_queue = v4l2_m2m_request_queue,
Hans Verkuil86b93b22018-05-21 04:54:57 -04001294};
1295
Hans Verkuil1f923a42014-09-22 09:27:17 -03001296static int vim2m_probe(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001297{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001298 struct vim2m_dev *dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001299 struct video_device *vfd;
1300 int ret;
1301
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001302 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001303 if (!dev)
1304 return -ENOMEM;
1305
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001306 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1307 if (ret)
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001308 goto error_free;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001309
1310 atomic_set(&dev->num_inst, 0);
1311 mutex_init(&dev->dev_mutex);
1312
Hans Verkuil09365422015-03-09 13:33:56 -03001313 dev->vfd = vim2m_videodev;
1314 vfd = &dev->vfd;
Marek Szyprowski07e80302010-12-20 14:39:25 -03001315 vfd->lock = &dev->dev_mutex;
Hans Verkuil8f484d82013-06-27 02:44:04 -03001316 vfd->v4l2_dev = &dev->v4l2_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001317
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001318 video_set_drvdata(vfd, dev);
Hans Verkuil8f484d82013-06-27 02:44:04 -03001319 v4l2_info(&dev->v4l2_dev,
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -05001320 "Device registered as /dev/video%d\n", vfd->num);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001321
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001322 platform_set_drvdata(pdev, dev);
1323
1324 dev->m2m_dev = v4l2_m2m_init(&m2m_ops);
1325 if (IS_ERR(dev->m2m_dev)) {
1326 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
1327 ret = PTR_ERR(dev->m2m_dev);
Kefeng Wang2455d412019-09-08 01:12:54 -03001328 dev->m2m_dev = NULL;
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001329 goto error_dev;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001330 }
1331
Hans Verkuile35f7022018-07-02 11:36:06 -04001332#ifdef CONFIG_MEDIA_CONTROLLER
1333 dev->mdev.dev = &pdev->dev;
Mauro Carvalho Chehabc0decac2018-09-10 08:19:14 -04001334 strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model));
Hans Verkuil281ae392019-01-30 08:39:17 -05001335 strscpy(dev->mdev.bus_info, "platform:vim2m",
1336 sizeof(dev->mdev.bus_info));
Hans Verkuile35f7022018-07-02 11:36:06 -04001337 media_device_init(&dev->mdev);
Hans Verkuil86b93b22018-05-21 04:54:57 -04001338 dev->mdev.ops = &m2m_media_ops;
Hans Verkuile35f7022018-07-02 11:36:06 -04001339 dev->v4l2_dev.mdev = &dev->mdev;
Hans Verkuil1a28dce2021-02-02 15:49:23 +01001340#endif
Hans Verkuile35f7022018-07-02 11:36:06 -04001341
Hans Verkuil1a28dce2021-02-02 15:49:23 +01001342 ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
1343 if (ret) {
1344 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
1345 goto error_m2m;
1346 }
1347
1348#ifdef CONFIG_MEDIA_CONTROLLER
Mauro Carvalho Chehabc310d1f2019-03-01 11:45:04 -05001349 ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd,
1350 MEDIA_ENT_F_PROC_VIDEO_SCALER);
Hans Verkuile35f7022018-07-02 11:36:06 -04001351 if (ret) {
1352 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n");
Sakari Ailuscf7f3472020-11-10 00:07:22 +01001353 goto error_v4l2;
Hans Verkuile35f7022018-07-02 11:36:06 -04001354 }
1355
1356 ret = media_device_register(&dev->mdev);
1357 if (ret) {
1358 v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001359 goto error_m2m_mc;
Hans Verkuile35f7022018-07-02 11:36:06 -04001360 }
1361#endif
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001362 return 0;
1363
Hans Verkuile35f7022018-07-02 11:36:06 -04001364#ifdef CONFIG_MEDIA_CONTROLLER
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001365error_m2m_mc:
Hans Verkuile35f7022018-07-02 11:36:06 -04001366 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
Hans Verkuile35f7022018-07-02 11:36:06 -04001367#endif
Sakari Ailuscf7f3472020-11-10 00:07:22 +01001368error_v4l2:
Hans Verkuile35f7022018-07-02 11:36:06 -04001369 video_unregister_device(&dev->vfd);
Kefeng Wang20059cb2019-05-13 03:18:29 -04001370 /* vim2m_device_release called by video_unregister_device to release various objects */
1371 return ret;
Sakari Ailuscf7f3472020-11-10 00:07:22 +01001372error_m2m:
1373 v4l2_m2m_release(dev->m2m_dev);
1374error_dev:
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001375 v4l2_device_unregister(&dev->v4l2_dev);
Hans Verkuilea6c7e32019-02-21 08:35:15 -05001376error_free:
1377 kfree(dev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001378
1379 return ret;
1380}
1381
Hans Verkuil1f923a42014-09-22 09:27:17 -03001382static int vim2m_remove(struct platform_device *pdev)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001383{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001384 struct vim2m_dev *dev = platform_get_drvdata(pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001385
Hans Verkuil1f923a42014-09-22 09:27:17 -03001386 v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME);
Hans Verkuile35f7022018-07-02 11:36:06 -04001387
1388#ifdef CONFIG_MEDIA_CONTROLLER
1389 media_device_unregister(&dev->mdev);
1390 v4l2_m2m_unregister_media_controller(dev->m2m_dev);
Hans Verkuile35f7022018-07-02 11:36:06 -04001391#endif
Hans Verkuil09365422015-03-09 13:33:56 -03001392 video_unregister_device(&dev->vfd);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001393
1394 return 0;
1395}
1396
Hans Verkuil1f923a42014-09-22 09:27:17 -03001397static struct platform_driver vim2m_pdrv = {
1398 .probe = vim2m_probe,
1399 .remove = vim2m_remove,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001400 .driver = {
1401 .name = MEM2MEM_NAME,
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001402 },
1403};
1404
Hans Verkuil1f923a42014-09-22 09:27:17 -03001405static void __exit vim2m_exit(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001406{
Hans Verkuil1f923a42014-09-22 09:27:17 -03001407 platform_driver_unregister(&vim2m_pdrv);
1408 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001409}
1410
Hans Verkuil1f923a42014-09-22 09:27:17 -03001411static int __init vim2m_init(void)
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001412{
1413 int ret;
1414
Hans Verkuil1f923a42014-09-22 09:27:17 -03001415 ret = platform_device_register(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001416 if (ret)
1417 return ret;
1418
Hans Verkuil1f923a42014-09-22 09:27:17 -03001419 ret = platform_driver_register(&vim2m_pdrv);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001420 if (ret)
Hans Verkuil1f923a42014-09-22 09:27:17 -03001421 platform_device_unregister(&vim2m_pdev);
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001422
Niklas Söderlundb20b51f2015-12-25 15:25:16 -02001423 return ret;
Pawel Osciak96d8eab2010-04-23 05:38:38 -03001424}
1425
Hans Verkuil1f923a42014-09-22 09:27:17 -03001426module_init(vim2m_init);
1427module_exit(vim2m_exit);