Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 2 | /* |
| 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 Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 7 | * destination, processes the data and issues an "irq" (simulated by a delayed |
| 8 | * workqueue). |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 9 | * 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 Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 13 | * Pawel Osciak, <pawel@osciak.com> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 14 | * Marek Szyprowski, <m.szyprowski@samsung.com> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 15 | */ |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/fs.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 19 | #include <linux/sched.h> |
Randy Dunlap | 6b46c39 | 2010-05-07 15:22:26 -0300 | [diff] [blame] | 20 | #include <linux/slab.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 21 | |
| 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 Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 26 | #include <media/v4l2-ctrls.h> |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 27 | #include <media/v4l2-event.h> |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 28 | #include <media/videobuf2-vmalloc.h> |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 29 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 30 | MODULE_DESCRIPTION("Virtual device for mem2mem framework testing"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 31 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 32 | MODULE_LICENSE("GPL"); |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 33 | MODULE_VERSION("0.2"); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 34 | MODULE_ALIAS("mem2mem_testdev"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 35 | |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 36 | static unsigned int debug; |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 37 | module_param(debug, uint, 0644); |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 38 | MODULE_PARM_DESC(debug, "debug level"); |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 39 | |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 40 | /* Default transaction time in msec */ |
| 41 | static unsigned int default_transtime = 40; /* Max 25 fps */ |
| 42 | module_param(default_transtime, uint, 0644); |
| 43 | MODULE_PARM_DESC(default_transtime, "default transaction time in ms"); |
| 44 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 45 | #define MIN_W 32 |
| 46 | #define MIN_H 32 |
| 47 | #define MAX_W 640 |
| 48 | #define MAX_H 480 |
Mauro Carvalho Chehab | c8af44e | 2019-02-28 01:25:50 -0500 | [diff] [blame] | 49 | |
| 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 57 | |
| 58 | /* Flags that indicate a format can be used for capture/output */ |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 59 | #define MEM2MEM_CAPTURE BIT(0) |
| 60 | #define MEM2MEM_OUTPUT BIT(1) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 61 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 62 | #define MEM2MEM_NAME "vim2m" |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 63 | |
| 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ń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 69 | /* Flags that indicate processing mode */ |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 70 | #define MEM2MEM_HFLIP BIT(0) |
| 71 | #define MEM2MEM_VFLIP BIT(1) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 72 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 73 | #define dprintk(dev, lvl, fmt, arg...) \ |
| 74 | v4l2_dbg(lvl, debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 75 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 76 | static void vim2m_dev_release(struct device *dev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 77 | {} |
| 78 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 79 | static struct platform_device vim2m_pdev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 80 | .name = MEM2MEM_NAME, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 81 | .dev.release = vim2m_dev_release, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 82 | }; |
| 83 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 84 | struct vim2m_fmt { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 85 | u32 fourcc; |
| 86 | int depth; |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 87 | /* Types the format can be used for */ |
| 88 | u32 types; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 89 | }; |
| 90 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 91 | static struct vim2m_fmt formats[] = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 92 | { |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 93 | .fourcc = V4L2_PIX_FMT_RGB565, /* rrrrrggg gggbbbbb */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 94 | .depth = 16, |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 95 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 96 | }, { |
| 97 | .fourcc = V4L2_PIX_FMT_RGB565X, /* gggbbbbb rrrrrggg */ |
| 98 | .depth = 16, |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 99 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 100 | }, { |
| 101 | .fourcc = V4L2_PIX_FMT_RGB24, |
| 102 | .depth = 24, |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 103 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 104 | }, { |
| 105 | .fourcc = V4L2_PIX_FMT_BGR24, |
| 106 | .depth = 24, |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 107 | .types = MEM2MEM_CAPTURE | MEM2MEM_OUTPUT, |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 108 | }, { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 109 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 110 | .depth = 16, |
Mauro Carvalho Chehab | 69d68a4 | 2019-03-01 07:41:24 -0500 | [diff] [blame] | 111 | .types = MEM2MEM_CAPTURE, |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 112 | }, { |
| 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 128 | }, |
| 129 | }; |
| 130 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 131 | #define NUM_FORMATS ARRAY_SIZE(formats) |
| 132 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 133 | /* Per-queue, driver-specific private data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 134 | struct vim2m_q_data { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 135 | unsigned int width; |
| 136 | unsigned int height; |
| 137 | unsigned int sizeimage; |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 138 | unsigned int sequence; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 139 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | enum { |
| 143 | V4L2_M2M_SRC = 0, |
| 144 | V4L2_M2M_DST = 1, |
| 145 | }; |
| 146 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 147 | #define V4L2_CID_TRANS_TIME_MSEC (V4L2_CID_USER_BASE + 0x1000) |
| 148 | #define V4L2_CID_TRANS_NUM_BUFS (V4L2_CID_USER_BASE + 0x1001) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 149 | |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 150 | static struct vim2m_fmt *find_format(u32 fourcc) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 151 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 152 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 153 | unsigned int k; |
| 154 | |
| 155 | for (k = 0; k < NUM_FORMATS; k++) { |
| 156 | fmt = &formats[k]; |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 157 | if (fmt->fourcc == fourcc) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 158 | break; |
| 159 | } |
| 160 | |
| 161 | if (k == NUM_FORMATS) |
| 162 | return NULL; |
| 163 | |
| 164 | return &formats[k]; |
| 165 | } |
| 166 | |
Mauro Carvalho Chehab | c8af44e | 2019-02-28 01:25:50 -0500 | [diff] [blame] | 167 | static 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 Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 185 | struct vim2m_dev { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 186 | struct v4l2_device v4l2_dev; |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 187 | struct video_device vfd; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 188 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 189 | struct media_device mdev; |
| 190 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 191 | |
| 192 | atomic_t num_inst; |
| 193 | struct mutex dev_mutex; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 194 | |
| 195 | struct v4l2_m2m_dev *m2m_dev; |
| 196 | }; |
| 197 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 198 | struct vim2m_ctx { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 199 | struct v4l2_fh fh; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 200 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 201 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 202 | struct v4l2_ctrl_handler hdl; |
| 203 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 204 | /* 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 Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 212 | struct mutex vb_mutex; |
| 213 | struct delayed_work work_run; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 214 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 215 | /* Abort requested by m2m */ |
| 216 | int aborting; |
| 217 | |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 218 | /* Processing mode */ |
| 219 | int mode; |
| 220 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 221 | enum v4l2_colorspace colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 222 | enum v4l2_ycbcr_encoding ycbcr_enc; |
| 223 | enum v4l2_xfer_func xfer_func; |
| 224 | enum v4l2_quantization quant; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 225 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 226 | /* Source and destination queue data */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 227 | struct vim2m_q_data q_data[2]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 228 | }; |
| 229 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 230 | static inline struct vim2m_ctx *file2ctx(struct file *file) |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 231 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 232 | return container_of(file->private_data, struct vim2m_ctx, fh); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 233 | } |
| 234 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 235 | static struct vim2m_q_data *get_q_data(struct vim2m_ctx *ctx, |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 236 | enum v4l2_buf_type type) |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 237 | { |
| 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 Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 244 | return NULL; |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 245 | } |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 246 | } |
| 247 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 248 | static 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 Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 260 | #define CLIP(__color) \ |
| 261 | (u8)(((__color) > 0xff) ? 0xff : (((__color) < 0) ? 0 : (__color))) |
| 262 | |
Mauro Carvalho Chehab | 5f78f7e | 2019-03-01 08:10:59 -0500 | [diff] [blame] | 263 | static void copy_line(struct vim2m_q_data *q_data_out, |
| 264 | u8 *src, u8 *dst, bool reverse) |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 265 | { |
Mauro Carvalho Chehab | 5f78f7e | 2019-03-01 08:10:59 -0500 | [diff] [blame] | 266 | int x, depth = q_data_out->fmt->depth >> 3; |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 267 | |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 268 | if (!reverse) { |
Mauro Carvalho Chehab | 5f78f7e | 2019-03-01 08:10:59 -0500 | [diff] [blame] | 269 | 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 Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 277 | return; |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 278 | } |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | static void copy_two_pixels(struct vim2m_q_data *q_data_in, |
| 282 | struct vim2m_q_data *q_data_out, |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 283 | u8 *src[2], u8 **dst, int ypos, bool reverse) |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 284 | { |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 285 | struct vim2m_fmt *out = q_data_out->fmt; |
| 286 | struct vim2m_fmt *in = q_data_in->fmt; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 287 | u8 _r[2], _g[2], _b[2], *r, *g, *b; |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 288 | int i; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 289 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 290 | /* Step 1: read two consecutive pixels from src pointer */ |
| 291 | |
| 292 | r = _r; |
| 293 | g = _g; |
| 294 | b = _b; |
| 295 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 296 | switch (in->fourcc) { |
| 297 | case V4L2_PIX_FMT_RGB565: /* rrrrrggg gggbbbbb */ |
| 298 | for (i = 0; i < 2; i++) { |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 299 | u16 pix = le16_to_cpu(*(__le16 *)(src[i])); |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 300 | |
| 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 Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 304 | } |
| 305 | break; |
| 306 | case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ |
| 307 | for (i = 0; i < 2; i++) { |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 308 | u16 pix = be16_to_cpu(*(__be16 *)(src[i])); |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 309 | |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 310 | *r++ = (u8)(((pix & 0xf800) >> 11) << 3) | 0x07; |
| 311 | *g++ = (u8)((((pix & 0x07e0) >> 5)) << 2) | 0x03; |
| 312 | *b++ = (u8)((pix & 0x1f) << 3) | 0x07; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 313 | } |
| 314 | break; |
Mauro Carvalho Chehab | 69d68a4 | 2019-03-01 07:41:24 -0500 | [diff] [blame] | 315 | default: |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 316 | case V4L2_PIX_FMT_RGB24: |
| 317 | for (i = 0; i < 2; i++) { |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 318 | *r++ = src[i][0]; |
| 319 | *g++ = src[i][1]; |
| 320 | *b++ = src[i][2]; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 321 | } |
| 322 | break; |
| 323 | case V4L2_PIX_FMT_BGR24: |
| 324 | for (i = 0; i < 2; i++) { |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 325 | *b++ = src[i][0]; |
| 326 | *g++ = src[i][1]; |
| 327 | *r++ = src[i][2]; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 328 | } |
| 329 | break; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 330 | } |
| 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 Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 341 | u16 pix; |
| 342 | __le16 *dst_pix = (__le16 *)*dst; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 343 | |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 344 | pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) | |
| 345 | (*b >> 3); |
| 346 | |
| 347 | *dst_pix = cpu_to_le16(pix); |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 348 | |
| 349 | *dst += 2; |
| 350 | } |
| 351 | return; |
| 352 | case V4L2_PIX_FMT_RGB565X: /* gggbbbbb rrrrrggg */ |
| 353 | for (i = 0; i < 2; i++) { |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 354 | u16 pix; |
| 355 | __be16 *dst_pix = (__be16 *)*dst; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 356 | |
Mauro Carvalho Chehab | 060162c | 2019-03-28 15:02:23 -0400 | [diff] [blame] | 357 | pix = ((*r << 8) & 0xf800) | ((*g << 3) & 0x07e0) | |
| 358 | (*b >> 3); |
| 359 | |
| 360 | *dst_pix = cpu_to_be16(pix); |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 361 | |
| 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 Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 379 | case V4L2_PIX_FMT_YUYV: |
| 380 | default: |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 381 | { |
| 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 Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 400 | 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 Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 436 | } |
| 437 | } |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 438 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 439 | static int device_process(struct vim2m_ctx *ctx, |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 440 | struct vb2_v4l2_buffer *in_vb, |
| 441 | struct vb2_v4l2_buffer *out_vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 442 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 443 | struct vim2m_dev *dev = ctx->dev; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 444 | struct vim2m_q_data *q_data_in, *q_data_out; |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 445 | 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 Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 448 | int start, end, step; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 449 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 450 | q_data_in = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT); |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 451 | if (!q_data_in) |
| 452 | return 0; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 453 | bytesperline = (q_data_in->width * q_data_in->fmt->depth) >> 3; |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 454 | bytes_per_pixel = q_data_in->fmt->depth >> 3; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 455 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 456 | q_data_out = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE); |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 457 | if (!q_data_out) |
| 458 | return 0; |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 459 | |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 460 | /* As we're doing scaling, use the output dimensions here */ |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 461 | height = q_data_out->height; |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 462 | width = q_data_out->width; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 463 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 464 | p_in = vb2_plane_vaddr(&in_vb->vb2_buf, 0); |
| 465 | p_out = vb2_plane_vaddr(&out_vb->vb2_buf, 0); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 466 | 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 Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 472 | out_vb->sequence = q_data_out->sequence++; |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 473 | in_vb->sequence = q_data_in->sequence++; |
Ezequiel Garcia | a4d3d61 | 2019-02-05 16:20:33 -0500 | [diff] [blame] | 474 | v4l2_m2m_buf_copy_metadata(in_vb, out_vb, true); |
Hans Verkuil | d95d7c6 | 2013-04-17 03:04:10 -0300 | [diff] [blame] | 475 | |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 476 | 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 Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 485 | y_out = 0; |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 486 | |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 487 | /* |
| 488 | * When format and resolution are identical, |
| 489 | * we can use a faster copy logic |
| 490 | */ |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 491 | 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 Chehab | 5f78f7e | 2019-03-01 08:10:59 -0500 | [diff] [blame] | 499 | copy_line(q_data_out, p, p_out, |
| 500 | ctx->mode & MEM2MEM_HFLIP); |
| 501 | |
| 502 | p_out += bytesperline; |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 503 | } |
| 504 | return 0; |
| 505 | } |
| 506 | |
Mauro Carvalho Chehab | 5f78f7e | 2019-03-01 08:10:59 -0500 | [diff] [blame] | 507 | /* Slower algorithm with format conversion, hflip, vflip and scaler */ |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 508 | |
| 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 Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 513 | for (y = start; y != end; y += step, y_out++) { |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 514 | y_in = (y * q_data_in->height) / q_data_out->height; |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 515 | x_offset = 0; |
| 516 | x_err = 0; |
Mauro Carvalho Chehab | 0b390d0 | 2019-03-01 05:25:43 -0500 | [diff] [blame] | 517 | |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 518 | p_line = p_in + (y_in * bytesperline); |
Mauro Carvalho Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 519 | if (ctx->mode & MEM2MEM_HFLIP) |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 520 | p_line += bytesperline - (q_data_in->fmt->depth >> 3); |
| 521 | p_in_x[0] = p_line; |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 522 | |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 523 | 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 Chehab | 8aa153f | 2019-01-29 14:00:15 -0200 | [diff] [blame] | 538 | ctx->mode & MEM2MEM_HFLIP); |
Mauro Carvalho Chehab | 24cc418 | 2019-02-26 06:36:00 -0500 | [diff] [blame] | 539 | |
Mauro Carvalho Chehab | f972992 | 2019-03-01 06:14:52 -0500 | [diff] [blame] | 540 | /* 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 558 | /* |
| 559 | * mem2mem callbacks |
| 560 | */ |
| 561 | |
Mauro Carvalho Chehab | cba862d | 2017-11-29 08:33:45 -0500 | [diff] [blame] | 562 | /* |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 563 | * job_ready() - check whether an instance is ready to be scheduled to run |
| 564 | */ |
| 565 | static int job_ready(void *priv) |
| 566 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 567 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 568 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 569 | 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 Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 571 | dprintk(ctx->dev, 1, "Not enough buffers available\n"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | return 1; |
| 576 | } |
| 577 | |
| 578 | static void job_abort(void *priv) |
| 579 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 580 | struct vim2m_ctx *ctx = priv; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 581 | |
| 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 | */ |
| 592 | static void device_run(void *priv) |
| 593 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 594 | struct vim2m_ctx *ctx = priv; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 595 | struct vb2_v4l2_buffer *src_buf, *dst_buf; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 596 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 597 | src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); |
| 598 | dst_buf = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 599 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 600 | /* Apply request controls if any */ |
| 601 | v4l2_ctrl_request_setup(src_buf->vb2_buf.req_obj.req, |
| 602 | &ctx->hdl); |
| 603 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 604 | device_process(ctx, src_buf, dst_buf); |
| 605 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 606 | /* Complete request controls if any */ |
| 607 | v4l2_ctrl_request_complete(src_buf->vb2_buf.req_obj.req, |
| 608 | &ctx->hdl); |
| 609 | |
Hans Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 610 | /* Run delayed work, which simulates a hardware irq */ |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 611 | schedule_delayed_work(&ctx->work_run, msecs_to_jiffies(ctx->transtime)); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 612 | } |
| 613 | |
Hans Verkuil | 144bd0e | 2018-05-21 04:54:56 -0400 | [diff] [blame] | 614 | static void device_work(struct work_struct *w) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 615 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 616 | struct vim2m_ctx *curr_ctx; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 617 | struct vim2m_dev *vim2m_dev; |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 618 | struct vb2_v4l2_buffer *src_vb, *dst_vb; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 619 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 620 | curr_ctx = container_of(w, struct vim2m_ctx, work_run.work); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 621 | |
Mauro Carvalho Chehab | 22f05d6 | 2019-02-07 12:28:19 -0500 | [diff] [blame] | 622 | vim2m_dev = curr_ctx->dev; |
| 623 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 624 | 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 Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 626 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 627 | curr_ctx->num_processed++; |
| 628 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 629 | v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_DONE); |
| 630 | v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_DONE); |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 631 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 632 | if (curr_ctx->num_processed == curr_ctx->translen |
| 633 | || curr_ctx->aborting) { |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 634 | dprintk(curr_ctx->dev, 2, "Finishing capture buffer fill\n"); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 635 | curr_ctx->num_processed = 0; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 636 | 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] | 637 | } else { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 638 | device_run(curr_ctx); |
| 639 | } |
| 640 | } |
| 641 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 642 | /* |
| 643 | * video ioctls |
| 644 | */ |
| 645 | static int vidioc_querycap(struct file *file, void *priv, |
| 646 | struct v4l2_capability *cap) |
| 647 | { |
Mauro Carvalho Chehab | 85709cb | 2018-09-10 08:19:16 -0400 | [diff] [blame] | 648 | strscpy(cap->driver, MEM2MEM_NAME, sizeof(cap->driver)); |
| 649 | strscpy(cap->card, MEM2MEM_NAME, sizeof(cap->card)); |
Hans Verkuil | 72c2af6 | 2012-09-14 06:23:12 -0300 | [diff] [blame] | 650 | snprintf(cap->bus_info, sizeof(cap->bus_info), |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 651 | "platform:%s", MEM2MEM_NAME); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int enum_fmt(struct v4l2_fmtdesc *f, u32 type) |
| 656 | { |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 657 | int i, num; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 658 | struct vim2m_fmt *fmt; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 659 | |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 660 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 676 | /* Format found */ |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 677 | fmt = &formats[i]; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 678 | f->pixelformat = fmt->fourcc; |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | /* Format not found */ |
| 683 | return -EINVAL; |
| 684 | } |
| 685 | |
| 686 | static 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 | |
| 692 | static 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 Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 698 | static 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 Chehab | c8af44e | 2019-02-28 01:25:50 -0500 | [diff] [blame] | 712 | |
| 713 | get_alignment(fsize->pixel_format, |
| 714 | &fsize->stepwise.step_width, |
| 715 | &fsize->stepwise.step_height); |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 719 | 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] | 720 | { |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 721 | struct vb2_queue *vq; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 722 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 723 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 724 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 725 | if (!vq) |
| 726 | return -EINVAL; |
| 727 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 728 | q_data = get_q_data(ctx, f->type); |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 729 | if (!q_data) |
| 730 | return -EINVAL; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 731 | |
| 732 | f->fmt.pix.width = q_data->width; |
| 733 | f->fmt.pix.height = q_data->height; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 734 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 735 | 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 Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 738 | f->fmt.pix.colorspace = ctx->colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 739 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
| 746 | static int vidioc_g_fmt_vid_out(struct file *file, void *priv, |
| 747 | struct v4l2_format *f) |
| 748 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 749 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
| 753 | struct v4l2_format *f) |
| 754 | { |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 755 | return vidioc_g_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 756 | } |
| 757 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 758 | 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] | 759 | { |
Mauro Carvalho Chehab | c8af44e | 2019-02-28 01:25:50 -0500 | [diff] [blame] | 760 | int walign, halign; |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 761 | /* |
| 762 | * V4L2 specification specifies the driver corrects the |
| 763 | * format struct if any of the dimensions is unsupported |
| 764 | */ |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 765 | 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 Chehab | c8af44e | 2019-02-28 01:25:50 -0500 | [diff] [blame] | 775 | get_alignment(f->fmt.pix.pixelformat, &walign, &halign); |
| 776 | f->fmt.pix.width &= ~(walign - 1); |
| 777 | f->fmt.pix.height &= ~(halign - 1); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 778 | 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 Verkuil | 5c3112b | 2014-03-10 10:58:29 -0300 | [diff] [blame] | 780 | f->fmt.pix.field = V4L2_FIELD_NONE; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 781 | |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
| 786 | struct v4l2_format *f) |
| 787 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 788 | struct vim2m_fmt *fmt; |
| 789 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 790 | |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 791 | fmt = find_format(f->fmt.pix.pixelformat); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 792 | if (!fmt) { |
| 793 | f->fmt.pix.pixelformat = formats[0].fourcc; |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 794 | fmt = find_format(f->fmt.pix.pixelformat); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 795 | } |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 796 | 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 Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 802 | f->fmt.pix.colorspace = ctx->colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 803 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 806 | |
| 807 | return vidioc_try_fmt(f, fmt); |
| 808 | } |
| 809 | |
| 810 | static int vidioc_try_fmt_vid_out(struct file *file, void *priv, |
| 811 | struct v4l2_format *f) |
| 812 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 813 | struct vim2m_fmt *fmt; |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 814 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 815 | |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 816 | fmt = find_format(f->fmt.pix.pixelformat); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 817 | if (!fmt) { |
| 818 | f->fmt.pix.pixelformat = formats[0].fourcc; |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 819 | fmt = find_format(f->fmt.pix.pixelformat); |
Hans Verkuil | 4e8ec0a | 2014-03-10 10:58:24 -0300 | [diff] [blame] | 820 | } |
Mauro Carvalho Chehab | 1875a7c | 2019-02-01 07:58:46 -0500 | [diff] [blame] | 821 | 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 Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 827 | if (!f->fmt.pix.colorspace) |
| 828 | f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 829 | |
| 830 | return vidioc_try_fmt(f, fmt); |
| 831 | } |
| 832 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 833 | 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] | 834 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 835 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 836 | struct vb2_queue *vq; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 837 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 838 | vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 839 | if (!vq) |
| 840 | return -EINVAL; |
| 841 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 842 | q_data = get_q_data(ctx, f->type); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 843 | if (!q_data) |
| 844 | return -EINVAL; |
| 845 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 846 | if (vb2_is_busy(vq)) { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 847 | v4l2_err(&ctx->dev->v4l2_dev, "%s queue busy\n", __func__); |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 848 | return -EBUSY; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 849 | } |
| 850 | |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 851 | q_data->fmt = find_format(f->fmt.pix.pixelformat); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 852 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 856 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 857 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 865 | |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 866 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | static 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 Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 878 | return vidioc_s_fmt(file2ctx(file), f); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static int vidioc_s_fmt_vid_out(struct file *file, void *priv, |
| 882 | struct v4l2_format *f) |
| 883 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 884 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 885 | int ret; |
| 886 | |
| 887 | ret = vidioc_try_fmt_vid_out(file, priv, f); |
| 888 | if (ret) |
| 889 | return ret; |
| 890 | |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 891 | ret = vidioc_s_fmt(file2ctx(file), f); |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 892 | if (!ret) { |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 893 | ctx->colorspace = f->fmt.pix.colorspace; |
Hans Verkuil | 9f8b333 | 2016-07-18 08:00:20 -0300 | [diff] [blame] | 894 | 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 Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 898 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 899 | } |
| 900 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 901 | static int vim2m_s_ctrl(struct v4l2_ctrl *ctrl) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 902 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 903 | struct vim2m_ctx *ctx = |
| 904 | container_of(ctrl->handler, struct vim2m_ctx, hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 905 | |
| 906 | switch (ctrl->id) { |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 907 | case V4L2_CID_HFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 908 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 909 | ctx->mode |= MEM2MEM_HFLIP; |
| 910 | else |
| 911 | ctx->mode &= ~MEM2MEM_HFLIP; |
| 912 | break; |
| 913 | |
| 914 | case V4L2_CID_VFLIP: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 915 | if (ctrl->val) |
Tomasz Moń | 8007287 | 2012-06-12 06:43:49 -0300 | [diff] [blame] | 916 | ctx->mode |= MEM2MEM_VFLIP; |
| 917 | else |
| 918 | ctx->mode &= ~MEM2MEM_VFLIP; |
| 919 | break; |
| 920 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 921 | case V4L2_CID_TRANS_TIME_MSEC: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 922 | ctx->transtime = ctrl->val; |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 923 | if (ctx->transtime < 1) |
| 924 | ctx->transtime = 1; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 925 | break; |
| 926 | |
| 927 | case V4L2_CID_TRANS_NUM_BUFS: |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 928 | ctx->translen = ctrl->val; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 929 | 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 Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 939 | static const struct v4l2_ctrl_ops vim2m_ctrl_ops = { |
| 940 | .s_ctrl = vim2m_s_ctrl, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 941 | }; |
| 942 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 943 | static const struct v4l2_ioctl_ops vim2m_ioctl_ops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 944 | .vidioc_querycap = vidioc_querycap, |
| 945 | |
| 946 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
Mauro Carvalho Chehab | 9c9c396 | 2019-02-26 16:03:45 -0500 | [diff] [blame] | 947 | .vidioc_enum_framesizes = vidioc_enum_framesizes, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 948 | .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 Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 957 | .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 Verkuil | 7bcff1c | 2015-06-05 11:28:52 -0300 | [diff] [blame] | 961 | .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf, |
| 962 | .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs, |
Hans Verkuil | f5294f4 | 2014-11-18 09:51:07 -0300 | [diff] [blame] | 963 | .vidioc_expbuf = v4l2_m2m_ioctl_expbuf, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 964 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 965 | .vidioc_streamon = v4l2_m2m_ioctl_streamon, |
| 966 | .vidioc_streamoff = v4l2_m2m_ioctl_streamoff, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 967 | |
Hans Verkuil | 97a3c90 | 2012-07-18 10:54:59 -0300 | [diff] [blame] | 968 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 969 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 970 | }; |
| 971 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 972 | /* |
| 973 | * Queue operations |
| 974 | */ |
| 975 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 976 | static int vim2m_queue_setup(struct vb2_queue *vq, |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 977 | unsigned int *nbuffers, |
| 978 | unsigned int *nplanes, |
| 979 | unsigned int sizes[], |
| 980 | struct device *alloc_devs[]) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 981 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 982 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vq); |
| 983 | struct vim2m_q_data *q_data; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 984 | unsigned int size, count = *nbuffers; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 985 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 986 | q_data = get_q_data(ctx, vq->type); |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 987 | if (!q_data) |
| 988 | return -EINVAL; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 989 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 990 | size = q_data->width * q_data->height * q_data->fmt->depth >> 3; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 991 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 992 | while (size * count > MEM2MEM_VID_MEM_LIMIT) |
| 993 | (count)--; |
Hans Verkuil | df9ecb0c | 2015-10-28 00:50:37 -0200 | [diff] [blame] | 994 | *nbuffers = count; |
| 995 | |
| 996 | if (*nplanes) |
| 997 | return sizes[0] < size ? -EINVAL : 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 998 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 999 | *nplanes = 1; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1000 | sizes[0] = size; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1001 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1002 | dprintk(ctx->dev, 1, "%s: get %d buffer(s) of size %d each.\n", |
| 1003 | type_name(vq->type), count, size); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 1008 | static int vim2m_buf_out_validate(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1009 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1010 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1011 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 1012 | |
| 1013 | if (vbuf->field == V4L2_FIELD_ANY) |
| 1014 | vbuf->field = V4L2_FIELD_NONE; |
| 1015 | if (vbuf->field != V4L2_FIELD_NONE) { |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1016 | dprintk(ctx->dev, 1, "%s field isn't supported\n", __func__); |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 1017 | return -EINVAL; |
| 1018 | } |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | static int vim2m_buf_prepare(struct vb2_buffer *vb) |
| 1024 | { |
| 1025 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1026 | struct vim2m_q_data *q_data; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1027 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1028 | dprintk(ctx->dev, 2, "type: %s\n", type_name(vb->vb2_queue->type)); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1029 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 1030 | q_data = get_q_data(ctx, vb->vb2_queue->type); |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 1031 | if (!q_data) |
| 1032 | return -EINVAL; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1033 | if (vb2_plane_size(vb, 0) < q_data->sizeimage) { |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1034 | 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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1038 | return -EINVAL; |
| 1039 | } |
| 1040 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1041 | vb2_set_plane_payload(vb, 0, q_data->sizeimage); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1042 | |
| 1043 | return 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1044 | } |
| 1045 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1046 | static void vim2m_buf_queue(struct vb2_buffer *vb) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1047 | { |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1048 | struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1049 | struct vim2m_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 1050 | |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1051 | v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf); |
Michael Olbrich | 0f910bf | 2011-07-12 09:46:44 -0300 | [diff] [blame] | 1052 | } |
| 1053 | |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 1054 | static int vim2m_start_streaming(struct vb2_queue *q, unsigned int count) |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 1055 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1056 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
| 1057 | struct vim2m_q_data *q_data = get_q_data(ctx, q->type); |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 1058 | |
Mauro Carvalho Chehab | 971d62d | 2019-03-01 11:46:11 -0500 | [diff] [blame] | 1059 | if (!q_data) |
| 1060 | return -EINVAL; |
| 1061 | |
Jernej Skrabec | c362f77 | 2019-09-12 15:55:55 -0300 | [diff] [blame] | 1062 | if (V4L2_TYPE_IS_OUTPUT(q->type)) |
| 1063 | ctx->aborting = 0; |
| 1064 | |
Hans Verkuil | ca5f5fd | 2014-03-10 10:58:28 -0300 | [diff] [blame] | 1065 | q_data->sequence = 0; |
| 1066 | return 0; |
| 1067 | } |
| 1068 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1069 | static void vim2m_stop_streaming(struct vb2_queue *q) |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1070 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1071 | struct vim2m_ctx *ctx = vb2_get_drv_priv(q); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1072 | struct vb2_v4l2_buffer *vbuf; |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1073 | |
Mauro Carvalho Chehab | a8566d7 | 2019-02-18 10:29:31 -0500 | [diff] [blame] | 1074 | cancel_delayed_work_sync(&ctx->work_run); |
Hans Verkuil | 240809e | 2019-01-11 07:07:25 -0500 | [diff] [blame] | 1075 | |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1076 | for (;;) { |
| 1077 | if (V4L2_TYPE_IS_OUTPUT(q->type)) |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1078 | vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1079 | else |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1080 | vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx); |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 1081 | if (!vbuf) |
Hans Verkuil | e37559b | 2014-04-17 02:47:21 -0300 | [diff] [blame] | 1082 | return; |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1083 | v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req, |
| 1084 | &ctx->hdl); |
Junghak Sung | 2d70071 | 2015-09-22 10:30:30 -0300 | [diff] [blame] | 1085 | v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR); |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1086 | } |
Hans Verkuil | a773632 | 2014-03-10 10:58:27 -0300 | [diff] [blame] | 1087 | } |
| 1088 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1089 | static 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 Lawall | b7b361f | 2016-09-08 20:59:10 -0300 | [diff] [blame] | 1096 | static const struct vb2_ops vim2m_qops = { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1097 | .queue_setup = vim2m_queue_setup, |
Hans Verkuil | ab7afaf3 | 2019-01-16 10:01:14 -0200 | [diff] [blame] | 1098 | .buf_out_validate = vim2m_buf_out_validate, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1099 | .buf_prepare = vim2m_buf_prepare, |
| 1100 | .buf_queue = vim2m_buf_queue, |
| 1101 | .start_streaming = vim2m_start_streaming, |
| 1102 | .stop_streaming = vim2m_stop_streaming, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1103 | .wait_prepare = vb2_ops_wait_prepare, |
| 1104 | .wait_finish = vb2_ops_wait_finish, |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1105 | .buf_request_complete = vim2m_buf_request_complete, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1106 | }; |
| 1107 | |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 1108 | static int queue_init(void *priv, struct vb2_queue *src_vq, |
| 1109 | struct vb2_queue *dst_vq) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1110 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1111 | struct vim2m_ctx *ctx = priv; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1112 | int ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1113 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1114 | src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 1115 | src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1116 | src_vq->drv_priv = ctx; |
| 1117 | src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1118 | src_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1119 | src_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 1120 | src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 1121 | src_vq->lock = &ctx->vb_mutex; |
Hans Verkuil | e5079cf | 2018-08-23 10:18:35 -0400 | [diff] [blame] | 1122 | src_vq->supports_requests = true; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1123 | |
| 1124 | ret = vb2_queue_init(src_vq); |
| 1125 | if (ret) |
| 1126 | return ret; |
| 1127 | |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1128 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Hans Verkuil | 782f36c | 2014-03-10 10:58:26 -0300 | [diff] [blame] | 1129 | dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1130 | dst_vq->drv_priv = ctx; |
| 1131 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1132 | dst_vq->ops = &vim2m_qops; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1133 | dst_vq->mem_ops = &vb2_vmalloc_memops; |
Sakari Ailus | ade4868 | 2014-02-25 19:12:19 -0300 | [diff] [blame] | 1134 | dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY; |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 1135 | dst_vq->lock = &ctx->vb_mutex; |
Marek Szyprowski | d80ee38 | 2011-01-12 06:50:55 -0300 | [diff] [blame] | 1136 | |
| 1137 | return vb2_queue_init(dst_vq); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1138 | } |
| 1139 | |
Mauro Carvalho Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 1140 | static struct v4l2_ctrl_config vim2m_ctrl_trans_time_msec = { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1141 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1142 | .id = V4L2_CID_TRANS_TIME_MSEC, |
| 1143 | .name = "Transaction Time (msec)", |
| 1144 | .type = V4L2_CTRL_TYPE_INTEGER, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1145 | .min = 1, |
| 1146 | .max = 10001, |
Hans Verkuil | 4340583 | 2014-03-10 10:58:23 -0300 | [diff] [blame] | 1147 | .step = 1, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1148 | }; |
| 1149 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1150 | static const struct v4l2_ctrl_config vim2m_ctrl_trans_num_bufs = { |
| 1151 | .ops = &vim2m_ctrl_ops, |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1152 | .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 Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1161 | /* |
| 1162 | * File operations |
| 1163 | */ |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1164 | static int vim2m_open(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1165 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1166 | struct vim2m_dev *dev = video_drvdata(file); |
| 1167 | struct vim2m_ctx *ctx = NULL; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1168 | struct v4l2_ctrl_handler *hdl; |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1169 | int rc = 0; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1170 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1171 | if (mutex_lock_interruptible(&dev->dev_mutex)) |
| 1172 | return -ERESTARTSYS; |
Sachin Kamat | 56ed221 | 2012-09-24 02:17:46 -0300 | [diff] [blame] | 1173 | ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1174 | if (!ctx) { |
| 1175 | rc = -ENOMEM; |
| 1176 | goto open_unlock; |
| 1177 | } |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1178 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1179 | v4l2_fh_init(&ctx->fh, video_devdata(file)); |
| 1180 | file->private_data = &ctx->fh; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1181 | ctx->dev = dev; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1182 | hdl = &ctx->hdl; |
| 1183 | v4l2_ctrl_handler_init(hdl, 4); |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1184 | 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 Chehab | f0ef022 | 2019-01-29 14:00:17 -0200 | [diff] [blame] | 1186 | |
| 1187 | vim2m_ctrl_trans_time_msec.def = default_transtime; |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1188 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_time_msec, NULL); |
| 1189 | v4l2_ctrl_new_custom(hdl, &vim2m_ctrl_trans_num_bufs, NULL); |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1190 | if (hdl->error) { |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1191 | rc = hdl->error; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1192 | v4l2_ctrl_handler_free(hdl); |
Santosh Kumar Singh | 7c13a4d | 2016-12-19 14:12:46 -0200 | [diff] [blame] | 1193 | kfree(ctx); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1194 | goto open_unlock; |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1195 | } |
| 1196 | ctx->fh.ctrl_handler = hdl; |
| 1197 | v4l2_ctrl_handler_setup(hdl); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1198 | |
Tomasz Moń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 1199 | ctx->q_data[V4L2_M2M_SRC].fmt = &formats[0]; |
Hans Verkuil | 47556ff | 2012-07-18 11:33:22 -0300 | [diff] [blame] | 1200 | 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ń | 9f4161a | 2012-06-08 04:47:34 -0300 | [diff] [blame] | 1208 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1209 | 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] | 1210 | |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 1211 | mutex_init(&ctx->vb_mutex); |
Mauro Carvalho Chehab | b3e64e5 | 2019-01-29 14:00:16 -0200 | [diff] [blame] | 1212 | INIT_DELAYED_WORK(&ctx->work_run, device_work); |
| 1213 | |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1214 | if (IS_ERR(ctx->fh.m2m_ctx)) { |
| 1215 | rc = PTR_ERR(ctx->fh.m2m_ctx); |
Dan Carpenter | 16ee9bb | 2010-05-05 02:58:57 -0300 | [diff] [blame] | 1216 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1217 | v4l2_ctrl_handler_free(hdl); |
Santosh Kumar Singh | 7c13a4d | 2016-12-19 14:12:46 -0200 | [diff] [blame] | 1218 | v4l2_fh_exit(&ctx->fh); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1219 | kfree(ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1220 | goto open_unlock; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1221 | } |
| 1222 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1223 | v4l2_fh_add(&ctx->fh); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1224 | atomic_inc(&dev->num_inst); |
| 1225 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1226 | dprintk(dev, 1, "Created instance: %p, m2m_ctx: %p\n", |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1227 | ctx, ctx->fh.m2m_ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1228 | |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1229 | open_unlock: |
| 1230 | mutex_unlock(&dev->dev_mutex); |
Dan Carpenter | a7bd775 | 2012-08-14 02:58:56 -0300 | [diff] [blame] | 1231 | return rc; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1232 | } |
| 1233 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1234 | static int vim2m_release(struct file *file) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1235 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1236 | struct vim2m_dev *dev = video_drvdata(file); |
| 1237 | struct vim2m_ctx *ctx = file2ctx(file); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1238 | |
Mauro Carvalho Chehab | de5c46a | 2019-02-26 07:29:57 -0500 | [diff] [blame] | 1239 | dprintk(dev, 1, "Releasing instance %p\n", ctx); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1240 | |
Hans Verkuil | d3dd59c | 2012-07-18 10:35:37 -0300 | [diff] [blame] | 1241 | v4l2_fh_del(&ctx->fh); |
| 1242 | v4l2_fh_exit(&ctx->fh); |
| 1243 | v4l2_ctrl_handler_free(&ctx->hdl); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1244 | mutex_lock(&dev->dev_mutex); |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1245 | v4l2_m2m_ctx_release(ctx->fh.m2m_ctx); |
Hans Verkuil | f469403 | 2012-07-31 03:51:25 -0300 | [diff] [blame] | 1246 | mutex_unlock(&dev->dev_mutex); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1247 | kfree(ctx); |
| 1248 | |
| 1249 | atomic_dec(&dev->num_inst); |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1254 | static 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 Verkuil | 9f22e88 | 2019-11-09 14:03:08 +0100 | [diff] [blame] | 1260 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1261 | media_device_cleanup(&dev->mdev); |
| 1262 | #endif |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1263 | kfree(dev); |
| 1264 | } |
| 1265 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1266 | static const struct v4l2_file_operations vim2m_fops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1267 | .owner = THIS_MODULE, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1268 | .open = vim2m_open, |
| 1269 | .release = vim2m_release, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1270 | .poll = v4l2_m2m_fop_poll, |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 1271 | .unlocked_ioctl = video_ioctl2, |
Sylwester Nawrocki | 20702eb | 2013-08-25 17:27:45 -0300 | [diff] [blame] | 1272 | .mmap = v4l2_m2m_fop_mmap, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1273 | }; |
| 1274 | |
Bhumika Goyal | 5303135 | 2017-08-26 08:57:26 -0400 | [diff] [blame] | 1275 | static const struct video_device vim2m_videodev = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1276 | .name = MEM2MEM_NAME, |
Hans Verkuil | 954f340 | 2012-09-05 06:05:50 -0300 | [diff] [blame] | 1277 | .vfl_dir = VFL_DIR_M2M, |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1278 | .fops = &vim2m_fops, |
| 1279 | .ioctl_ops = &vim2m_ioctl_ops, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1280 | .minor = -1, |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1281 | .release = vim2m_device_release, |
Hans Verkuil | 47fc65fa | 2018-11-15 03:16:22 -0500 | [diff] [blame] | 1282 | .device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1283 | }; |
| 1284 | |
Julia Lawall | 86ab0b1 | 2017-08-06 04:25:19 -0400 | [diff] [blame] | 1285 | static const struct v4l2_m2m_ops m2m_ops = { |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1286 | .device_run = device_run, |
| 1287 | .job_ready = job_ready, |
| 1288 | .job_abort = job_abort, |
| 1289 | }; |
| 1290 | |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1291 | static const struct media_device_ops m2m_media_ops = { |
| 1292 | .req_validate = vb2_request_validate, |
Ezequiel Garcia | ef86eaf | 2018-10-18 14:54:29 -0400 | [diff] [blame] | 1293 | .req_queue = v4l2_m2m_request_queue, |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1294 | }; |
| 1295 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1296 | static int vim2m_probe(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1297 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1298 | struct vim2m_dev *dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1299 | struct video_device *vfd; |
| 1300 | int ret; |
| 1301 | |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1302 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1303 | if (!dev) |
| 1304 | return -ENOMEM; |
| 1305 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1306 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); |
| 1307 | if (ret) |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1308 | goto error_free; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1309 | |
| 1310 | atomic_set(&dev->num_inst, 0); |
| 1311 | mutex_init(&dev->dev_mutex); |
| 1312 | |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1313 | dev->vfd = vim2m_videodev; |
| 1314 | vfd = &dev->vfd; |
Marek Szyprowski | 07e8030 | 2010-12-20 14:39:25 -0300 | [diff] [blame] | 1315 | vfd->lock = &dev->dev_mutex; |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1316 | vfd->v4l2_dev = &dev->v4l2_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1317 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1318 | video_set_drvdata(vfd, dev); |
Hans Verkuil | 8f484d8 | 2013-06-27 02:44:04 -0300 | [diff] [blame] | 1319 | v4l2_info(&dev->v4l2_dev, |
Mauro Carvalho Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 1320 | "Device registered as /dev/video%d\n", vfd->num); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1321 | |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1322 | 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 Wang | 2455d41 | 2019-09-08 01:12:54 -0300 | [diff] [blame] | 1328 | dev->m2m_dev = NULL; |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1329 | goto error_dev; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1330 | } |
| 1331 | |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1332 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1333 | dev->mdev.dev = &pdev->dev; |
Mauro Carvalho Chehab | c0decac | 2018-09-10 08:19:14 -0400 | [diff] [blame] | 1334 | strscpy(dev->mdev.model, "vim2m", sizeof(dev->mdev.model)); |
Hans Verkuil | 281ae39 | 2019-01-30 08:39:17 -0500 | [diff] [blame] | 1335 | strscpy(dev->mdev.bus_info, "platform:vim2m", |
| 1336 | sizeof(dev->mdev.bus_info)); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1337 | media_device_init(&dev->mdev); |
Hans Verkuil | 86b93b2 | 2018-05-21 04:54:57 -0400 | [diff] [blame] | 1338 | dev->mdev.ops = &m2m_media_ops; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1339 | dev->v4l2_dev.mdev = &dev->mdev; |
Hans Verkuil | 1a28dce | 2021-02-02 15:49:23 +0100 | [diff] [blame] | 1340 | #endif |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1341 | |
Hans Verkuil | 1a28dce | 2021-02-02 15:49:23 +0100 | [diff] [blame] | 1342 | 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 Chehab | c310d1f | 2019-03-01 11:45:04 -0500 | [diff] [blame] | 1349 | ret = v4l2_m2m_register_media_controller(dev->m2m_dev, vfd, |
| 1350 | MEDIA_ENT_F_PROC_VIDEO_SCALER); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1351 | if (ret) { |
| 1352 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller\n"); |
Sakari Ailus | cf7f347 | 2020-11-10 00:07:22 +0100 | [diff] [blame] | 1353 | goto error_v4l2; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1354 | } |
| 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 Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1359 | goto error_m2m_mc; |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1360 | } |
| 1361 | #endif |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1362 | return 0; |
| 1363 | |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1364 | #ifdef CONFIG_MEDIA_CONTROLLER |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1365 | error_m2m_mc: |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1366 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1367 | #endif |
Sakari Ailus | cf7f347 | 2020-11-10 00:07:22 +0100 | [diff] [blame] | 1368 | error_v4l2: |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1369 | video_unregister_device(&dev->vfd); |
Kefeng Wang | 20059cb | 2019-05-13 03:18:29 -0400 | [diff] [blame] | 1370 | /* vim2m_device_release called by video_unregister_device to release various objects */ |
| 1371 | return ret; |
Sakari Ailus | cf7f347 | 2020-11-10 00:07:22 +0100 | [diff] [blame] | 1372 | error_m2m: |
| 1373 | v4l2_m2m_release(dev->m2m_dev); |
| 1374 | error_dev: |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1375 | v4l2_device_unregister(&dev->v4l2_dev); |
Hans Verkuil | ea6c7e3 | 2019-02-21 08:35:15 -0500 | [diff] [blame] | 1376 | error_free: |
| 1377 | kfree(dev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1378 | |
| 1379 | return ret; |
| 1380 | } |
| 1381 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1382 | static int vim2m_remove(struct platform_device *pdev) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1383 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1384 | struct vim2m_dev *dev = platform_get_drvdata(pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1385 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1386 | v4l2_info(&dev->v4l2_dev, "Removing " MEM2MEM_NAME); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1387 | |
| 1388 | #ifdef CONFIG_MEDIA_CONTROLLER |
| 1389 | media_device_unregister(&dev->mdev); |
| 1390 | v4l2_m2m_unregister_media_controller(dev->m2m_dev); |
Hans Verkuil | e35f702 | 2018-07-02 11:36:06 -0400 | [diff] [blame] | 1391 | #endif |
Hans Verkuil | 0936542 | 2015-03-09 13:33:56 -0300 | [diff] [blame] | 1392 | video_unregister_device(&dev->vfd); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1397 | static struct platform_driver vim2m_pdrv = { |
| 1398 | .probe = vim2m_probe, |
| 1399 | .remove = vim2m_remove, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1400 | .driver = { |
| 1401 | .name = MEM2MEM_NAME, |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1402 | }, |
| 1403 | }; |
| 1404 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1405 | static void __exit vim2m_exit(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1406 | { |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1407 | platform_driver_unregister(&vim2m_pdrv); |
| 1408 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1409 | } |
| 1410 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1411 | static int __init vim2m_init(void) |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1412 | { |
| 1413 | int ret; |
| 1414 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1415 | ret = platform_device_register(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1416 | if (ret) |
| 1417 | return ret; |
| 1418 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1419 | ret = platform_driver_register(&vim2m_pdrv); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1420 | if (ret) |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1421 | platform_device_unregister(&vim2m_pdev); |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1422 | |
Niklas Söderlund | b20b51f | 2015-12-25 15:25:16 -0200 | [diff] [blame] | 1423 | return ret; |
Pawel Osciak | 96d8eab | 2010-04-23 05:38:38 -0300 | [diff] [blame] | 1424 | } |
| 1425 | |
Hans Verkuil | 1f923a4 | 2014-09-22 09:27:17 -0300 | [diff] [blame] | 1426 | module_init(vim2m_init); |
| 1427 | module_exit(vim2m_exit); |