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