Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1 | /* |
| 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 Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 6 | * destination, processes the data and issues an "irq" (simulated by a delayed |
| 7 | * workqueue). |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 8 | * 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 Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 12 | * Pawel Osciak, <pawel@osciak.com> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 13 | * 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 23 | #include <linux/sched.h> |
Randy Dunlap | 6b46c39 | 2010-05-07 15:22:26 -0300 | [diff] [blame] | 24 | #include <linux/slab.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 25 | |
| 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 Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 30 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 31 | #include <media/v4l2-event.h> |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 32 | #include <media/videobuf2-vmalloc.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 33 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 34 | MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 35 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 36 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | 1990d50 | 2011-06-24 14:45:49 -0300 | [diff] [blame] | 37 | MODULE_VERSION("0.1.1"); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 38 | MODULE_ALIAS("mem2mem_testdev"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 39 | |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 40 | static unsigned debug; |
| 41 | module_param(debug, uint, 0644); |
| 42 | MODULE_PARM_DESC(debug, "activates debug info"); |
| 43 | |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 44 | /* Default transaction time in msec */ |
| 45 | static unsigned int default_transtime = 40; /* Max 25 fps */ |
| 46 | module_param(default_transtime, uint, 0644); |
| 47 | MODULE_PARM_DESC(default_transtime, "default transaction time in ms"); |
| 48 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 49 | #define MIN_W 32 |
| 50 | #define MIN_H 32 |
| 51 | #define MAX_W 640 |
| 52 | #define MAX_H 480 |
Guennadi Liakhovetski | 9ce3ce4d4 | 2012-04-27 04:16:46 -0300 | [diff] [blame] | 53 | #define DIM_ALIGN_MASK 7 /* 8-byte alignment for line length */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 54 | |
| 55 | /* Flags that indicate a format can be used for capture/output */ |
| 56 | #define MEM2MEM_CAPTURE (1 << 0) |
| 57 | #define MEM2MEM_OUTPUT (1 << 1) |
| 58 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 59 | #define MEM2MEM_NAME "vim2m" |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 60 | |
| 61 | /* Per queue */ |
| 62 | #define MEM2MEM_DEF_NUM_BUFS VIDEO_MAX_FRAME |
| 63 | /* In bytes, per queue */ |
| 64 | #define MEM2MEM_VID_MEM_LIMIT (16 * 1024 * 1024) |
| 65 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 66 | /* Flags that indicate processing mode */ |
| 67 | #define MEM2MEM_HFLIP (1 << 0) |
| 68 | #define MEM2MEM_VFLIP (1 << 1) |
| 69 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 70 | #define dprintk(dev, fmt, arg...) \ |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 71 | v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 72 | |
| 73 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 74 | static void vim2m_dev_release(struct device *dev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 75 | {} |
| 76 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 77 | static struct platform_device vim2m_pdev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 78 | .name = MEM2MEM_NAME, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 79 | .dev.release = vim2m_dev_release, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 80 | }; |
| 81 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 82 | struct vim2m_fmt { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 83 | u32 fourcc; |
| 84 | int depth; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 85 | }; |
| 86 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 87 | static struct vim2m_fmt formats[] = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 88 | { |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 89 | .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 90 | .depth = 16, |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 91 | }, { |
| 92 | .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */ |
| 93 | .depth = 16, |
| 94 | }, { |
| 95 | .fourcc = V4L2_PIX_FMT_RGB24, |
| 96 | .depth = 24, |
| 97 | }, { |
| 98 | .fourcc = V4L2_PIX_FMT_BGR24, |
| 99 | .depth = 24, |
| 100 | }, { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 101 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 102 | .depth = 16, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 103 | }, |
| 104 | }; |
| 105 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 106 | #define NUM_FORMATS ARRAY_SIZE(formats) |
| 107 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 108 | /* Per-queue, driver-specific private data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 109 | struct vim2m_q_data { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 110 | unsigned int width; |
| 111 | unsigned int height; |
| 112 | unsigned int sizeimage; |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 113 | unsigned int sequence; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 114 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | enum { |
| 118 | V4L2_M2M_SRC = 0, |
| 119 | V4L2_M2M_DST = 1, |
| 120 | }; |
| 121 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 122 | #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) |
| 123 | #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 124 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 125 | static struct vim2m_fmt *find_format(struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 126 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 127 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 128 | unsigned int k; |
| 129 | |
| 130 | for (k = 0; k < NUM_FORMATS; k++) { |
| 131 | fmt = &formats[k]; |
| 132 | if (fmt->fourcc == f->fmt.pix.pixelformat) |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | if (k == NUM_FORMATS) |
| 137 | return NULL; |
| 138 | |
| 139 | return &formats[k]; |
| 140 | } |
| 141 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 142 | struct vim2m_dev { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 143 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 144 | struct video_device vfd; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 145 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 146 | struct media_device mdev; |
| 147 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 148 | |
| 149 | atomic_t num_inst; |
| 150 | struct mutex dev_mutex; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 151 | |
| 152 | struct v4l2_m2m_dev *m2m_dev; |
| 153 | }; |
| 154 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 155 | struct vim2m_ctx { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 156 | struct v4l2_fh fh; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 157 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 158 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 159 | struct v4l2_ctrl_handler hdl; |
| 160 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 161 | /* Processed buffers in this transaction */ |
| 162 | u8 num_processed; |
| 163 | |
| 164 | /* Transaction length (i.e. how many buffers per transaction) */ |
| 165 | u32 translen; |
| 166 | /* Transaction time (i.e. simulated processing time) in milliseconds */ |
| 167 | u32 transtime; |
| 168 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 169 | struct mutex vb_mutex; |
| 170 | struct delayed_work work_run; |
| 171 | spinlock_t irqlock; |
| 172 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 173 | /* Abort requested by m2m */ |
| 174 | int aborting; |
| 175 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 176 | /* Processing mode */ |
| 177 | int mode; |
| 178 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 179 | enum v4l2_colorspace colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 180 | enum v4l2_ycbcr_encoding ycbcr_enc; |
| 181 | enum v4l2_xfer_func xfer_func; |
| 182 | enum v4l2_quantization quant; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 183 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 184 | /* Source and destination queue data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 185 | struct vim2m_q_data q_data[2]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 186 | }; |
| 187 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 188 | static inline struct vim2m_ctx *file2ctx(struct file *file) |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 189 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 190 | return container_of(file->private_data, struct vim2m_ctx, fh); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 191 | } |
| 192 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 193 | static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 194 | enum v4l2_buf_type type) |
| 195 | { |
| 196 | switch (type) { |
| 197 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 198 | return &ctx->q_data[V4L2_M2M_SRC]; |
| 199 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 200 | return &ctx->q_data[V4L2_M2M_DST]; |
| 201 | default: |
| 202 | BUG(); |
| 203 | } |
| 204 | return NULL; |
| 205 | } |
| 206 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 207 | #define CLIP(__color) \ |
| 208 | (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color))) |
| 209 | |
| 210 | static void copy_two_pixels(struct vim2m_fmt *in, struct vim2m_fmt *out, |
| 211 | u8 **src, u8 **dst, bool reverse) |
| 212 | { |
| 213 | u8 _r[2], _g[2], _b[2], *r, *g, *b; |
| 214 | int i, step; |
| 215 | |
| 216 | // If format is the same just copy the data, respecting the width |
| 217 | if (in->fourcc == out->fourcc) { |
| 218 | int depth = out->depth >> 3; |
| 219 | |
| 220 | if (reverse) { |
| 221 | if (in->fourcc == V4L2_PIX_FMT_YUYV) { |
| 222 | int u, v, y, y1; |
| 223 | |
| 224 | *src -= 2; |
| 225 | |
| 226 | y1 = (*src)[0]; /* copy as second point */ |
| 227 | u = (*src)[1]; |
| 228 | y = (*src)[2]; /* copy as first point */ |
| 229 | v = (*src)[3]; |
| 230 | |
| 231 | *src -= 2; |
| 232 | |
| 233 | *(*dst)++ = y; |
| 234 | *(*dst)++ = u; |
| 235 | *(*dst)++ = y1; |
| 236 | *(*dst)++ = v; |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | memcpy(*dst, *src, depth); |
| 241 | memcpy(*dst + depth, *src - depth, depth); |
| 242 | *src -= depth << 1; |
| 243 | } else { |
| 244 | memcpy(*dst, *src, depth << 1); |
| 245 | *src += depth << 1; |
| 246 | } |
| 247 | *dst += depth << 1; |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | /* Step 1: read two consecutive pixels from src pointer */ |
| 252 | |
| 253 | r = _r; |
| 254 | g = _g; |
| 255 | b = _b; |
| 256 | |
| 257 | if (reverse) |
| 258 | step = -1; |
| 259 | else |
| 260 | step = 1; |
| 261 | |
| 262 | switch (in->fourcc) { |
| 263 | case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */ |
| 264 | for (i = 0; i < 2; i++) { |
| 265 | u16 pix = *(u16 *)*src; |
| 266 | |
| 267 | *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07; |
| 268 | *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03; |
| 269 | *b++ = (u8)((pix & 0x1f) << 3) | 0x07; |
| 270 | |
| 271 | *src += step << 1; |
| 272 | } |
| 273 | break; |
| 274 | case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ |
| 275 | for (i = 0; i < 2; i++) { |
| 276 | u16 pix = *(u16 *)*src; |
| 277 | |
| 278 | *r++ = (u8)(((0x00f8 & pix) >> 3) << 3) | 0x07; |
| 279 | *g++ = (u8)(((pix & 0x7) << 2) | |
| 280 | ((pix & 0xe000) >> 5)) | 0x03; |
| 281 | *b++ = (u8)(((pix & 0x1f00) >> 8) << 3) | 0x07; |
| 282 | |
| 283 | *src += step << 1; |
| 284 | } |
| 285 | break; |
| 286 | case V4L2_PIX_FMT_RGB24: |
| 287 | for (i = 0; i < 2; i++) { |
| 288 | *r++ = (*src)[0]; |
| 289 | *g++ = (*src)[1]; |
| 290 | *b++ = (*src)[2]; |
| 291 | |
| 292 | *src += step * 3; |
| 293 | } |
| 294 | break; |
| 295 | case V4L2_PIX_FMT_BGR24: |
| 296 | for (i = 0; i < 2; i++) { |
| 297 | *b++ = (*src)[0]; |
| 298 | *g++ = (*src)[1]; |
| 299 | *r++ = (*src)[2]; |
| 300 | |
| 301 | *src += step * 3; |
| 302 | } |
| 303 | break; |
| 304 | default: /* V4L2_PIX_FMT_YUYV */ |
| 305 | { |
| 306 | int u, v, y, y1, u1, v1, tmp; |
| 307 | |
| 308 | if (reverse) { |
| 309 | *src -= 2; |
| 310 | |
| 311 | y1 = (*src)[0]; /* copy as second point */ |
| 312 | u = (*src)[1]; |
| 313 | y = (*src)[2]; /* copy as first point */ |
| 314 | v = (*src)[3]; |
| 315 | |
| 316 | *src -= 2; |
| 317 | } else { |
| 318 | y = *(*src)++; |
| 319 | u = *(*src)++; |
| 320 | y1 = *(*src)++; |
| 321 | v = *(*src)++; |
| 322 | } |
| 323 | |
| 324 | u1 = (((u - 128) << 7) + (u - 128)) >> 6; |
| 325 | tmp = (((u - 128) << 1) + (u - 128) + |
| 326 | ((v - 128) << 2) + ((v - 128) << 1)) >> 3; |
| 327 | v1 = (((v - 128) << 1) + (v - 128)) >> 1; |
| 328 | |
| 329 | *r++ = CLIP(y + v1); |
| 330 | *g++ = CLIP(y - tmp); |
| 331 | *b++ = CLIP(y + u1); |
| 332 | |
| 333 | *r = CLIP(y1 + v1); |
| 334 | *g = CLIP(y1 - tmp); |
| 335 | *b = CLIP(y1 + u1); |
| 336 | break; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /* Step 2: store two consecutive points, reversing them if needed */ |
| 341 | |
| 342 | r = _r; |
| 343 | g = _g; |
| 344 | b = _b; |
| 345 | |
| 346 | switch (out->fourcc) { |
| 347 | case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */ |
| 348 | for (i = 0; i < 2; i++) { |
| 349 | u16 *pix = (u16 *)*dst; |
| 350 | |
| 351 | *pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) | |
| 352 | (*b >> 3); |
| 353 | |
| 354 | *dst += 2; |
| 355 | } |
| 356 | return; |
| 357 | case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ |
| 358 | for (i = 0; i < 2; i++) { |
| 359 | u16 *pix = (u16 *)*dst; |
| 360 | u8 green = *g++ >> 2; |
| 361 | |
| 362 | *pix = ((green << 8) & 0xe000) | (green & 0x07) | |
| 363 | ((*b++ << 5) & 0x1f00) | ((*r++ & 0xf8)); |
| 364 | |
| 365 | *dst += 2; |
| 366 | } |
| 367 | return; |
| 368 | case V4L2_PIX_FMT_RGB24: |
| 369 | for (i = 0; i < 2; i++) { |
| 370 | *(*dst)++ = *r++; |
| 371 | *(*dst)++ = *g++; |
| 372 | *(*dst)++ = *b++; |
| 373 | } |
| 374 | return; |
| 375 | case V4L2_PIX_FMT_BGR24: |
| 376 | for (i = 0; i < 2; i++) { |
| 377 | *(*dst)++ = *b++; |
| 378 | *(*dst)++ = *g++; |
| 379 | *(*dst)++ = *r++; |
| 380 | } |
| 381 | return; |
| 382 | default: /* V4L2_PIX_FMT_YUYV */ |
| 383 | { |
| 384 | u8 y, y1, u, v; |
| 385 | |
| 386 | y = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b) |
| 387 | + 524288) >> 15); |
| 388 | u = ((-4878 * (*r) - 9578 * (*g) + 14456 * (*b) |
| 389 | + 4210688) >> 15); |
| 390 | v = ((14456 * (*r++) - 12105 * (*g++) - 2351 * (*b++) |
| 391 | + 4210688) >> 15); |
| 392 | y1 = ((8453 * (*r) + 16594 * (*g) + 3223 * (*b) |
| 393 | + 524288) >> 15); |
| 394 | |
| 395 | *(*dst)++ = y; |
| 396 | *(*dst)++ = u; |
| 397 | |
| 398 | *(*dst)++ = y1; |
| 399 | *(*dst)++ = v; |
| 400 | return; |
| 401 | } |
| 402 | } |
| 403 | } |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 404 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 405 | static int device_process(struct vim2m_ctx *ctx, |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 406 | struct vb2_v4l2_buffer *in_vb, |
| 407 | struct vb2_v4l2_buffer *out_vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 408 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 409 | struct vim2m_dev *dev = ctx->dev; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 410 | struct vim2m_q_data *q_data_in, *q_data_out; |
| 411 | u8 *p_in, *p, *p_out; |
| 412 | int width, height, bytesperline, x, y, start, end, step; |
| 413 | struct vim2m_fmt *in, *out; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 414 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 415 | q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
| 416 | in = q_data_in->fmt; |
| 417 | width = q_data_in->width; |
| 418 | height = q_data_in->height; |
| 419 | bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 420 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 421 | q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 422 | out = q_data_out->fmt; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 423 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 424 | p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); |
| 425 | p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 426 | if (!p_in || !p_out) { |
| 427 | v4l2_err(&dev->v4l2_dev, |
| 428 | "Acquiring kernel pointers to buffers failed\n"); |
| 429 | return -EFAULT; |
| 430 | } |
| 431 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 432 | out_vb->sequence = get_q_data(ctx, |
| 433 | V4L2_BUF_TYPE_VIDEO_CAPTURE)->sequence++; |
| 434 | in_vb->sequence = q_data_in->sequence++; |
Ezequiel Garcia | a4d3d61 | 2019-02-05 16:20:33 -0500 | [diff] [blame] | 435 | v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true); |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 436 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 437 | if (ctx->mode & MEM2MEM_VFLIP) { |
| 438 | start = height - 1; |
| 439 | end = -1; |
| 440 | step = -1; |
| 441 | } else { |
| 442 | start = 0; |
| 443 | end = height; |
| 444 | step = 1; |
| 445 | } |
| 446 | for (y = start; y != end; y += step) { |
| 447 | p = p_in + (y * bytesperline); |
| 448 | if (ctx->mode & MEM2MEM_HFLIP) |
| 449 | p += bytesperline - (q_data_in->fmt->depth >> 3); |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 450 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 451 | for (x = 0; x < width >> 1; x++) |
| 452 | copy_two_pixels(in, out, &p, &p_out, |
| 453 | ctx->mode & MEM2MEM_HFLIP); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | return 0; |
| 457 | } |
| 458 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 459 | /* |
| 460 | * mem2mem callbacks |
| 461 | */ |
| 462 | |
Mauro Carvalho Chehab | cba862d | 2017-11-29 08:33:45 -0500 | [diff] [blame] | 463 | /* |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 464 | * job_ready() - check whether an instance is ready to be scheduled to run |
| 465 | */ |
| 466 | static int job_ready(void *priv) |
| 467 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 468 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 469 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 470 | if (v4l2_m2m_num_src_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen |
| 471 | || v4l2_m2m_num_dst_bufs_ready(ctx->fh.m2m_ctx) < ctx->translen) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 472 | dprintk(ctx->dev, "Not enough buffers available\n"); |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | static void job_abort(void *priv) |
| 480 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 481 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 482 | |
| 483 | /* Will cancel the transaction in the next interrupt handler */ |
| 484 | ctx->aborting = 1; |
| 485 | } |
| 486 | |
| 487 | /* device_run() - prepares and starts the device |
| 488 | * |
| 489 | * This simulates all the immediate preparations required before starting |
| 490 | * a device. This will be called by the framework when it decides to schedule |
| 491 | * a particular instance. |
| 492 | */ |
| 493 | static void device_run(void *priv) |
| 494 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 495 | struct vim2m_ctx *ctx = priv; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 496 | struct vb2_v4l2_buffer *src_buf, *dst_buf; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 497 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 498 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
| 499 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 500 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 501 | /* Apply request controls if any */ |
| 502 | v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, |
| 503 | &ctx->hdl); |
| 504 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 505 | device_process(ctx, src_buf, dst_buf); |
| 506 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 507 | /* Complete request controls if any */ |
| 508 | v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req, |
| 509 | &ctx->hdl); |
| 510 | |
Hans Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 511 | /* Run delayed work, which simulates a hardware irq */ |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 512 | schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime)); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 513 | } |
| 514 | |
Hans Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 515 | static void device_work(struct work_struct *w) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 516 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 517 | struct vim2m_ctx *curr_ctx; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 518 | struct vim2m_dev *vim2m_dev; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 519 | struct vb2_v4l2_buffer *src_vb, *dst_vb; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 520 | unsigned long flags; |
| 521 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 522 | curr_ctx = container_of(w, struct vim2m_ctx, work_run.work); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 523 | |
| 524 | if (NULL == curr_ctx) { |
Sachin Kamat | 26fdcf0 | 2012-09-24 02:17:47 -0300 | [diff] [blame] | 525 | pr_err("Instance released before the end of transaction\n"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 526 | return; |
| 527 | } |
| 528 | |
Mauro Carvalho Chehab | 22f05d6 | 2019-02-07 12:28:19 -0500 | [diff] [blame] | 529 | vim2m_dev = curr_ctx->dev; |
| 530 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 531 | src_vb = v4l2_m2m_src_buf_remove(curr_ctx->fh.m2m_ctx); |
| 532 | dst_vb = v4l2_m2m_dst_buf_remove(curr_ctx->fh.m2m_ctx); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 533 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 534 | curr_ctx->num_processed++; |
| 535 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 536 | spin_lock_irqsave(&curr_ctx->irqlock, flags); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 537 | v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); |
| 538 | v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 539 | spin_unlock_irqrestore(&curr_ctx->irqlock, flags); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 540 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 541 | if (curr_ctx->num_processed == curr_ctx->translen |
| 542 | || curr_ctx->aborting) { |
| 543 | dprintk(curr_ctx->dev, "Finishing transaction\n"); |
| 544 | curr_ctx->num_processed = 0; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 545 | v4l2_m2m_job_finish(vim2m_dev->m2m_dev, curr_ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 546 | } else { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 547 | device_run(curr_ctx); |
| 548 | } |
| 549 | } |
| 550 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 551 | /* |
| 552 | * video ioctls |
| 553 | */ |
| 554 | static int vidioc_querycap(struct file *file, void *priv, |
| 555 | struct v4l2_capability *cap) |
| 556 | { |
| 557 | strncpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver) - 1); |
| 558 | strncpy(cap->card, MEM2MEM_NAME, sizeof(cap->card) - 1); |
Hans Verkuil | 72c2af6 | 2012-09-14 06:23:12 -0300 | [diff] [blame] | 559 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
| 560 | "platform:%s", MEM2MEM_NAME); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) |
| 565 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 566 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 567 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 568 | if (f->index < NUM_FORMATS) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 569 | /* Format found */ |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 570 | fmt = &formats[f->index]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 571 | f->pixelformat = fmt->fourcc; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /* Format not found */ |
| 576 | return -EINVAL; |
| 577 | } |
| 578 | |
| 579 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
| 580 | struct v4l2_fmtdesc *f) |
| 581 | { |
| 582 | return enum_fmt(f, MEM2MEM_CAPTURE); |
| 583 | } |
| 584 | |
| 585 | static int vidioc_enum_fmt_vid_out(struct file *file, void *priv, |
| 586 | struct v4l2_fmtdesc *f) |
| 587 | { |
| 588 | return enum_fmt(f, MEM2MEM_OUTPUT); |
| 589 | } |
| 590 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 591 | static int vidioc_g_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 592 | { |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 593 | struct vb2_queue *vq; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 594 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 595 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 596 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 597 | if (!vq) |
| 598 | return -EINVAL; |
| 599 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 600 | q_data = get_q_data(ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 601 | |
| 602 | f->fmt.pix.width = q_data->width; |
| 603 | f->fmt.pix.height = q_data->height; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 604 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 605 | f->fmt.pix.pixelformat = q_data->fmt->fourcc; |
| 606 | f->fmt.pix.bytesperline = (q_data->width * q_data->fmt->depth) >> 3; |
| 607 | f->fmt.pix.sizeimage = q_data->sizeimage; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 608 | f->fmt.pix.colorspace = ctx->colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 609 | f->fmt.pix.xfer_func = ctx->xfer_func; |
| 610 | f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; |
| 611 | f->fmt.pix.quantization = ctx->quant; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
| 616 | static int vidioc_g_fmt_vid_out(struct file *file, void *priv, |
| 617 | struct v4l2_format *f) |
| 618 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 619 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 623 | struct v4l2_format *f) |
| 624 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 625 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 626 | } |
| 627 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 628 | static int vidioc_try_fmt(struct v4l2_format *f, struct vim2m_fmt *fmt) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 629 | { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 630 | /* V4L2 specification suggests the driver corrects the format struct |
| 631 | * if any of the dimensions is unsupported */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 632 | if (f->fmt.pix.height < MIN_H) |
| 633 | f->fmt.pix.height = MIN_H; |
| 634 | else if (f->fmt.pix.height > MAX_H) |
| 635 | f->fmt.pix.height = MAX_H; |
| 636 | |
| 637 | if (f->fmt.pix.width < MIN_W) |
| 638 | f->fmt.pix.width = MIN_W; |
| 639 | else if (f->fmt.pix.width > MAX_W) |
| 640 | f->fmt.pix.width = MAX_W; |
| 641 | |
| 642 | f->fmt.pix.width &= ~DIM_ALIGN_MASK; |
| 643 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; |
| 644 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; |
Hans Verkuil | 5c3112b | 2014-03-10 10:58:29 -0300 | [diff] [blame] | 645 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 651 | struct v4l2_format *f) |
| 652 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 653 | struct vim2m_fmt *fmt; |
| 654 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 655 | |
| 656 | fmt = find_format(f); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 657 | if (!fmt) { |
| 658 | f->fmt.pix.pixelformat = formats[0].fourcc; |
| 659 | fmt = find_format(f); |
| 660 | } |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 661 | f->fmt.pix.colorspace = ctx->colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 662 | f->fmt.pix.xfer_func = ctx->xfer_func; |
| 663 | f->fmt.pix.ycbcr_enc = ctx->ycbcr_enc; |
| 664 | f->fmt.pix.quantization = ctx->quant; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 665 | |
| 666 | return vidioc_try_fmt(f, fmt); |
| 667 | } |
| 668 | |
| 669 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, |
| 670 | struct v4l2_format *f) |
| 671 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 672 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 673 | |
| 674 | fmt = find_format(f); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 675 | if (!fmt) { |
| 676 | f->fmt.pix.pixelformat = formats[0].fourcc; |
| 677 | fmt = find_format(f); |
| 678 | } |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 679 | if (!f->fmt.pix.colorspace) |
| 680 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 681 | |
| 682 | return vidioc_try_fmt(f, fmt); |
| 683 | } |
| 684 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 685 | static int vidioc_s_fmt(struct vim2m_ctx *ctx, struct v4l2_format *f) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 686 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 687 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 688 | struct vb2_queue *vq; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 689 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 690 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 691 | if (!vq) |
| 692 | return -EINVAL; |
| 693 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 694 | q_data = get_q_data(ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 695 | if (!q_data) |
| 696 | return -EINVAL; |
| 697 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 698 | if (vb2_is_busy(vq)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 699 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 700 | return -EBUSY; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | q_data->fmt = find_format(f); |
| 704 | q_data->width = f->fmt.pix.width; |
| 705 | q_data->height = f->fmt.pix.height; |
| 706 | q_data->sizeimage = q_data->width * q_data->height |
| 707 | * q_data->fmt->depth >> 3; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 708 | |
| 709 | dprintk(ctx->dev, |
| 710 | "Setting format for type %d, wxh: %dx%d, fmt: %d\n", |
| 711 | f->type, q_data->width, q_data->height, q_data->fmt->fourcc); |
| 712 | |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 713 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
| 717 | struct v4l2_format *f) |
| 718 | { |
| 719 | int ret; |
| 720 | |
| 721 | ret = vidioc_try_fmt_vid_cap(file, priv, f); |
| 722 | if (ret) |
| 723 | return ret; |
| 724 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 725 | return vidioc_s_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, |
| 729 | struct v4l2_format *f) |
| 730 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 731 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 732 | int ret; |
| 733 | |
| 734 | ret = vidioc_try_fmt_vid_out(file, priv, f); |
| 735 | if (ret) |
| 736 | return ret; |
| 737 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 738 | ret = vidioc_s_fmt(file2ctx(file), f); |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 739 | if (!ret) { |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 740 | ctx->colorspace = f->fmt.pix.colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 741 | ctx->xfer_func = f->fmt.pix.xfer_func; |
| 742 | ctx->ycbcr_enc = f->fmt.pix.ycbcr_enc; |
| 743 | ctx->quant = f->fmt.pix.quantization; |
| 744 | } |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 745 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 746 | } |
| 747 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 748 | static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 749 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 750 | struct vim2m_ctx *ctx = |
| 751 | container_of(ctrl->handler, struct vim2m_ctx, hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 752 | |
| 753 | switch (ctrl->id) { |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 754 | case V4L2_CID_HFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 755 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 756 | ctx->mode |= MEM2MEM_HFLIP; |
| 757 | else |
| 758 | ctx->mode &= ~MEM2MEM_HFLIP; |
| 759 | break; |
| 760 | |
| 761 | case V4L2_CID_VFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 762 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 763 | ctx->mode |= MEM2MEM_VFLIP; |
| 764 | else |
| 765 | ctx->mode &= ~MEM2MEM_VFLIP; |
| 766 | break; |
| 767 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 768 | case V4L2_CID_TRANS_TIME_MSEC: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 769 | ctx->transtime = ctrl->val; |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 770 | if (ctx->transtime < 1) |
| 771 | ctx->transtime = 1; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 772 | break; |
| 773 | |
| 774 | case V4L2_CID_TRANS_NUM_BUFS: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 775 | ctx->translen = ctrl->val; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | default: |
| 779 | v4l2_err(&ctx->dev->v4l2_dev, "Invalid control\n"); |
| 780 | return -EINVAL; |
| 781 | } |
| 782 | |
| 783 | return 0; |
| 784 | } |
| 785 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 786 | static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { |
| 787 | .s_ctrl = vim2m_s_ctrl, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 788 | }; |
| 789 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 790 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 791 | static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 792 | .vidioc_querycap = vidioc_querycap, |
| 793 | |
| 794 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 795 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 796 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 797 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 798 | |
| 799 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out, |
| 800 | .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out, |
| 801 | .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out, |
| 802 | .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out, |
| 803 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 804 | .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs, |
| 805 | .vidioc_querybuf = v4l2_m2m_ioctl_querybuf, |
| 806 | .vidioc_qbuf = v4l2_m2m_ioctl_qbuf, |
| 807 | .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf, |
Hans Verkuil | 7bcff1c | 2015-06-05 11:28:52 -0300 | [diff] [blame] | 808 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
| 809 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
Hans Verkuil | f5294f4 | 2014-11-18 09:51:07 -0300 | [diff] [blame] | 810 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 811 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 812 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
| 813 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 814 | |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 815 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 816 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 817 | }; |
| 818 | |
| 819 | |
| 820 | /* |
| 821 | * Queue operations |
| 822 | */ |
| 823 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 824 | static int vim2m_queue_setup(struct vb2_queue *vq, |
Guennadi Liakhovetski | fc714e70 | 2011-08-24 10:30:21 -0300 | [diff] [blame] | 825 | unsigned int *nbuffers, unsigned int *nplanes, |
Hans Verkuil | 36c0f8b | 2016-04-15 09:15:05 -0300 | [diff] [blame] | 826 | unsigned int sizes[], struct device *alloc_devs[]) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 827 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 828 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); |
| 829 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 830 | unsigned int size, count = *nbuffers; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 831 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 832 | q_data = get_q_data(ctx, vq->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 833 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 834 | size = q_data->width * q_data->height * q_data->fmt->depth >> 3; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 835 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 836 | while (size * count > MEM2MEM_VID_MEM_LIMIT) |
| 837 | (count)--; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 838 | *nbuffers = count; |
| 839 | |
| 840 | if (*nplanes) |
| 841 | return sizes[0] < size ? -EINVAL : 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 842 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 843 | *nplanes = 1; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 844 | sizes[0] = size; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 845 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 846 | dprintk(ctx->dev, "get %d buffer(s) of size %d each.\n", count, size); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 847 | |
| 848 | return 0; |
| 849 | } |
| 850 | |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 851 | static int vim2m_buf_out_validate(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 852 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 853 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 854 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 855 | |
| 856 | if (vbuf->field == V4L2_FIELD_ANY) |
| 857 | vbuf->field = V4L2_FIELD_NONE; |
| 858 | if (vbuf->field != V4L2_FIELD_NONE) { |
| 859 | dprintk(ctx->dev, "%s field isn't supported\n", __func__); |
| 860 | return -EINVAL; |
| 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | } |
| 865 | |
| 866 | static int vim2m_buf_prepare(struct vb2_buffer *vb) |
| 867 | { |
| 868 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 869 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 870 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 871 | dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 872 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 873 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 874 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
| 875 | dprintk(ctx->dev, "%s data will not fit into plane (%lu < %lu)\n", |
| 876 | __func__, vb2_plane_size(vb, 0), (long)q_data->sizeimage); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 877 | return -EINVAL; |
| 878 | } |
| 879 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 880 | vb2_set_plane_payload(vb, 0, q_data->sizeimage); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 881 | |
| 882 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 883 | } |
| 884 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 885 | static void vim2m_buf_queue(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 886 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 887 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 888 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 889 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 890 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
Michael Olbrich | 0f910bf | 2011-07-12 09:46:44 -0300 | [diff] [blame] | 891 | } |
| 892 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 893 | static int vim2m_start_streaming(struct vb2_queue *q, unsigned count) |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 894 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 895 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
| 896 | struct vim2m_q_data *q_data = get_q_data(ctx, q->type); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 897 | |
| 898 | q_data->sequence = 0; |
| 899 | return 0; |
| 900 | } |
| 901 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 902 | static void vim2m_stop_streaming(struct vb2_queue *q) |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 903 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 904 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 905 | struct vb2_v4l2_buffer *vbuf; |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 906 | unsigned long flags; |
| 907 | |
Mauro Carvalho Chehab | a8566d7 | 2019-02-18 10:29:31 -0500 | [diff] [blame^] | 908 | cancel_delayed_work_sync(&ctx->work_run); |
Hans Verkuil | 240809e | 2019-01-11 07:07:25 -0500 | [diff] [blame] | 909 | |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 910 | for (;;) { |
| 911 | if (V4L2_TYPE_IS_OUTPUT(q->type)) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 912 | vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 913 | else |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 914 | vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
| 915 | if (vbuf == NULL) |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 916 | return; |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 917 | v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, |
| 918 | &ctx->hdl); |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 919 | spin_lock_irqsave(&ctx->irqlock, flags); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 920 | v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 921 | spin_unlock_irqrestore(&ctx->irqlock, flags); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 922 | } |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 923 | } |
| 924 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 925 | static void vim2m_buf_request_complete(struct vb2_buffer *vb) |
| 926 | { |
| 927 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
| 928 | |
| 929 | v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl); |
| 930 | } |
| 931 | |
Julia Lawall | b7b361f | 2016-09-08 20:59:10 -0300 | [diff] [blame] | 932 | static const struct vb2_ops vim2m_qops = { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 933 | .queue_setup = vim2m_queue_setup, |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 934 | .buf_out_validate = vim2m_buf_out_validate, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 935 | .buf_prepare = vim2m_buf_prepare, |
| 936 | .buf_queue = vim2m_buf_queue, |
| 937 | .start_streaming = vim2m_start_streaming, |
| 938 | .stop_streaming = vim2m_stop_streaming, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 939 | .wait_prepare = vb2_ops_wait_prepare, |
| 940 | .wait_finish = vb2_ops_wait_finish, |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 941 | .buf_request_complete = vim2m_buf_request_complete, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 942 | }; |
| 943 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 944 | static int queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 945 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 946 | struct vim2m_ctx *ctx = priv; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 947 | int ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 948 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 949 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 950 | src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 951 | src_vq->drv_priv = ctx; |
| 952 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 953 | src_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 954 | src_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 955 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 956 | src_vq->lock = &ctx->vb_mutex; |
Hans Verkuil | e5079cf | 2018-08-23 10:18:35 -0400 | [diff] [blame] | 957 | src_vq->supports_requests = true; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 958 | |
| 959 | ret = vb2_queue_init(src_vq); |
| 960 | if (ret) |
| 961 | return ret; |
| 962 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 963 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 964 | dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 965 | dst_vq->drv_priv = ctx; |
| 966 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 967 | dst_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 968 | dst_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 969 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 970 | dst_vq->lock = &ctx->vb_mutex; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 971 | |
| 972 | return vb2_queue_init(dst_vq); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 973 | } |
| 974 | |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 975 | static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 976 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 977 | .id = V4L2_CID_TRANS_TIME_MSEC, |
| 978 | .name = "Transaction Time (msec)", |
| 979 | .type = V4L2_CTRL_TYPE_INTEGER, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 980 | .min = 1, |
| 981 | .max = 10001, |
Hans Verkuil | 4340583 | 2014-03-10 10:58:23 -0300 | [diff] [blame] | 982 | .step = 1, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 983 | }; |
| 984 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 985 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { |
| 986 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 987 | .id = V4L2_CID_TRANS_NUM_BUFS, |
| 988 | .name = "Buffers Per Transaction", |
| 989 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 990 | .def = 1, |
| 991 | .min = 1, |
| 992 | .max = MEM2MEM_DEF_NUM_BUFS, |
| 993 | .step = 1, |
| 994 | }; |
| 995 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 996 | /* |
| 997 | * File operations |
| 998 | */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 999 | static int vim2m_open(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1000 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1001 | struct vim2m_dev *dev = video_drvdata(file); |
| 1002 | struct vim2m_ctx *ctx = NULL; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1003 | struct v4l2_ctrl_handler *hdl; |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1004 | int rc = 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1005 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1006 | if (mutex_lock_interruptible(&dev->dev_mutex)) |
| 1007 | return -ERESTARTSYS; |
Sachin Kamat | 56ed221 | 2012-09-24 02:17:46 -0300 | [diff] [blame] | 1008 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1009 | if (!ctx) { |
| 1010 | rc = -ENOMEM; |
| 1011 | goto open_unlock; |
| 1012 | } |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1013 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1014 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 1015 | file->private_data = &ctx->fh; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1016 | ctx->dev = dev; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1017 | hdl = &ctx->hdl; |
| 1018 | v4l2_ctrl_handler_init(hdl, 4); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1019 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_HFLIP, 0, 1, 1, 0); |
| 1020 | v4l2_ctrl_new_std(hdl, &vim2m_ctrl_ops, V4L2_CID_VFLIP, 0, 1, 1, 0); |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 1021 | |
| 1022 | vim2m_ctrl_trans_time_msec.def = default_transtime; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1023 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); |
| 1024 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1025 | if (hdl->error) { |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1026 | rc = hdl->error; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1027 | v4l2_ctrl_handler_free(hdl); |
Santosh Kumar Singh | 7c13a4d | 2016-12-19 14:12:46 -0200 | [diff] [blame] | 1028 | kfree(ctx); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1029 | goto open_unlock; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1030 | } |
| 1031 | ctx->fh.ctrl_handler = hdl; |
| 1032 | v4l2_ctrl_handler_setup(hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1033 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 1034 | ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 1035 | ctx->q_data[V4L2_M2M_SRC].width = 640; |
| 1036 | ctx->q_data[V4L2_M2M_SRC].height = 480; |
| 1037 | ctx->q_data[V4L2_M2M_SRC].sizeimage = |
| 1038 | ctx->q_data[V4L2_M2M_SRC].width * |
| 1039 | ctx->q_data[V4L2_M2M_SRC].height * |
| 1040 | (ctx->q_data[V4L2_M2M_SRC].fmt->depth >> 3); |
| 1041 | ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC]; |
| 1042 | ctx->colorspace = V4L2_COLORSPACE_REC709; |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 1043 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1044 | ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1045 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 1046 | mutex_init(&ctx->vb_mutex); |
| 1047 | spin_lock_init(&ctx->irqlock); |
| 1048 | INIT_DELAYED_WORK(&ctx->work_run, device_work); |
| 1049 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1050 | if (IS_ERR(ctx->fh.m2m_ctx)) { |
| 1051 | rc = PTR_ERR(ctx->fh.m2m_ctx); |
Dan Carpenter | 16ee9bb1 | 2010-05-05 02:58:57 -0300 | [diff] [blame] | 1052 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1053 | v4l2_ctrl_handler_free(hdl); |
Santosh Kumar Singh | 7c13a4d | 2016-12-19 14:12:46 -0200 | [diff] [blame] | 1054 | v4l2_fh_exit(&ctx->fh); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1055 | kfree(ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1056 | goto open_unlock; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1057 | } |
| 1058 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1059 | v4l2_fh_add(&ctx->fh); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1060 | atomic_inc(&dev->num_inst); |
| 1061 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1062 | dprintk(dev, "Created instance: %p, m2m_ctx: %p\n", |
| 1063 | ctx, ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1064 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1065 | open_unlock: |
| 1066 | mutex_unlock(&dev->dev_mutex); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1067 | return rc; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1068 | } |
| 1069 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1070 | static int vim2m_release(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1071 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1072 | struct vim2m_dev *dev = video_drvdata(file); |
| 1073 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1074 | |
| 1075 | dprintk(dev, "Releasing instance %p\n", ctx); |
| 1076 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1077 | v4l2_fh_del(&ctx->fh); |
| 1078 | v4l2_fh_exit(&ctx->fh); |
| 1079 | v4l2_ctrl_handler_free(&ctx->hdl); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1080 | mutex_lock(&dev->dev_mutex); |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1081 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1082 | mutex_unlock(&dev->dev_mutex); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1083 | kfree(ctx); |
| 1084 | |
| 1085 | atomic_dec(&dev->num_inst); |
| 1086 | |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1090 | static const struct v4l2_file_operations vim2m_fops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1091 | .owner = THIS_MODULE, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1092 | .open = vim2m_open, |
| 1093 | .release = vim2m_release, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1094 | .poll = v4l2_m2m_fop_poll, |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 1095 | .unlocked_ioctl = video_ioctl2, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1096 | .mmap = v4l2_m2m_fop_mmap, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1097 | }; |
| 1098 | |
Bhumika Goyal | 5303135 | 2017-08-26 08:57:26 -0400 | [diff] [blame] | 1099 | static const struct video_device vim2m_videodev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1100 | .name = MEM2MEM_NAME, |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 1101 | .vfl_dir = VFL_DIR_M2M, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1102 | .fops = &vim2m_fops, |
| 1103 | .ioctl_ops = &vim2m_ioctl_ops, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1104 | .minor = -1, |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1105 | .release = video_device_release_empty, |
Hans Verkuil | 47fc65fa | 2018-11-15 03:16:22 -0500 | [diff] [blame] | 1106 | .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1107 | }; |
| 1108 | |
Julia Lawall | 86ab0b1 | 2017-08-06 04:25:19 -0400 | [diff] [blame] | 1109 | static const struct v4l2_m2m_ops m2m_ops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1110 | .device_run = device_run, |
| 1111 | .job_ready = job_ready, |
| 1112 | .job_abort = job_abort, |
| 1113 | }; |
| 1114 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1115 | static const struct media_device_ops m2m_media_ops = { |
| 1116 | .req_validate = vb2_request_validate, |
Ezequiel Garcia | ef86eaf | 2018-10-18 14:54:29 -0400 | [diff] [blame] | 1117 | .req_queue = v4l2_m2m_request_queue, |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1118 | }; |
| 1119 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1120 | static int vim2m_probe(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1121 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1122 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1123 | struct video_device *vfd; |
| 1124 | int ret; |
| 1125 | |
Sachin Kamat | 38a7996 | 2012-09-24 02:17:48 -0300 | [diff] [blame] | 1126 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1127 | if (!dev) |
| 1128 | return -ENOMEM; |
| 1129 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1130 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 1131 | if (ret) |
Sachin Kamat | 38a7996 | 2012-09-24 02:17:48 -0300 | [diff] [blame] | 1132 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1133 | |
| 1134 | atomic_set(&dev->num_inst, 0); |
| 1135 | mutex_init(&dev->dev_mutex); |
| 1136 | |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1137 | dev->vfd = vim2m_videodev; |
| 1138 | vfd = &dev->vfd; |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 1139 | vfd->lock = &dev->dev_mutex; |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1140 | vfd->v4l2_dev = &dev->v4l2_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1141 | |
| 1142 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); |
| 1143 | if (ret) { |
| 1144 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1145 | goto unreg_v4l2; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | video_set_drvdata(vfd, dev); |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1149 | v4l2_info(&dev->v4l2_dev, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1150 | "Device registered as /dev/video%d\n", vfd->num); |
| 1151 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1152 | platform_set_drvdata(pdev, dev); |
| 1153 | |
| 1154 | dev->m2m_dev = v4l2_m2m_init(&m2m_ops); |
| 1155 | if (IS_ERR(dev->m2m_dev)) { |
| 1156 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); |
| 1157 | ret = PTR_ERR(dev->m2m_dev); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1158 | goto unreg_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1159 | } |
| 1160 | |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1161 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1162 | dev->mdev.dev = &pdev->dev; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 1163 | strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model)); |
Hans Verkuil | 281ae39 | 2019-01-30 08:39:17 -0500 | [diff] [blame] | 1164 | strscpy(dev->mdev.bus_info, "platform:vim2m", |
| 1165 | sizeof(dev->mdev.bus_info)); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1166 | media_device_init(&dev->mdev); |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1167 | dev->mdev.ops = &m2m_media_ops; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1168 | dev->v4l2_dev.mdev = &dev->mdev; |
| 1169 | |
| 1170 | ret = v4l2_m2m_register_media_controller(dev->m2m_dev, |
| 1171 | vfd, MEDIA_ENT_F_PROC_VIDEO_SCALER); |
| 1172 | if (ret) { |
| 1173 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); |
| 1174 | goto unreg_m2m; |
| 1175 | } |
| 1176 | |
| 1177 | ret = media_device_register(&dev->mdev); |
| 1178 | if (ret) { |
| 1179 | v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n"); |
| 1180 | goto unreg_m2m_mc; |
| 1181 | } |
| 1182 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1183 | return 0; |
| 1184 | |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1185 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1186 | unreg_m2m_mc: |
| 1187 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); |
| 1188 | unreg_m2m: |
Sachin Kamat | b7e13ef | 2012-09-24 02:17:45 -0300 | [diff] [blame] | 1189 | v4l2_m2m_release(dev->m2m_dev); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1190 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1191 | unreg_dev: |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1192 | video_unregister_device(&dev->vfd); |
| 1193 | unreg_v4l2: |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1194 | v4l2_device_unregister(&dev->v4l2_dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1195 | |
| 1196 | return ret; |
| 1197 | } |
| 1198 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1199 | static int vim2m_remove(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1200 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1201 | struct vim2m_dev *dev = platform_get_drvdata(pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1202 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1203 | v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1204 | |
| 1205 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1206 | media_device_unregister(&dev->mdev); |
| 1207 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); |
| 1208 | media_device_cleanup(&dev->mdev); |
| 1209 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1210 | v4l2_m2m_release(dev->m2m_dev); |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1211 | video_unregister_device(&dev->vfd); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1212 | v4l2_device_unregister(&dev->v4l2_dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1217 | static struct platform_driver vim2m_pdrv = { |
| 1218 | .probe = vim2m_probe, |
| 1219 | .remove = vim2m_remove, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1220 | .driver = { |
| 1221 | .name = MEM2MEM_NAME, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1222 | }, |
| 1223 | }; |
| 1224 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1225 | static void __exit vim2m_exit(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1226 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1227 | platform_driver_unregister(&vim2m_pdrv); |
| 1228 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1229 | } |
| 1230 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1231 | static int __init vim2m_init(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1232 | { |
| 1233 | int ret; |
| 1234 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1235 | ret = platform_device_register(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1236 | if (ret) |
| 1237 | return ret; |
| 1238 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1239 | ret = platform_driver_register(&vim2m_pdrv); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1240 | if (ret) |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1241 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1242 | |
Niklas Söderlund | b20b51f | 2015-12-25 15:25:16 -0200 | [diff] [blame] | 1243 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1244 | } |
| 1245 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1246 | module_init(vim2m_init); |
| 1247 | module_exit(vim2m_exit); |