xref: /linux/drivers/media/test-drivers/vivid/vivid-core.c (revision 8e1bb4a41aa78d6105e59186af3dcd545fc66e70)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 /*
4  * vivid-core.c - A Virtual Video Test Driver, core initialization
5  *
6  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
7  */
8 
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/vmalloc.h>
16 #include <linux/font.h>
17 #include <linux/mutex.h>
18 #include <linux/platform_device.h>
19 #include <linux/videodev2.h>
20 #include <linux/v4l2-dv-timings.h>
21 #include <media/videobuf2-vmalloc.h>
22 #include <media/videobuf2-dma-contig.h>
23 #include <media/v4l2-dv-timings.h>
24 #include <media/v4l2-ioctl.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
27 
28 #include "vivid-core.h"
29 #include "vivid-vid-common.h"
30 #include "vivid-vid-cap.h"
31 #include "vivid-vid-out.h"
32 #include "vivid-radio-common.h"
33 #include "vivid-radio-rx.h"
34 #include "vivid-radio-tx.h"
35 #include "vivid-sdr-cap.h"
36 #include "vivid-vbi-cap.h"
37 #include "vivid-vbi-out.h"
38 #include "vivid-osd.h"
39 #include "vivid-cec.h"
40 #include "vivid-ctrls.h"
41 #include "vivid-meta-cap.h"
42 #include "vivid-meta-out.h"
43 #include "vivid-touch-cap.h"
44 
45 #define VIVID_MODULE_NAME "vivid"
46 #define MAX_STRING_LENGTH 23
47 
48 MODULE_DESCRIPTION("Virtual Video Test Driver");
49 MODULE_AUTHOR("Hans Verkuil");
50 MODULE_LICENSE("GPL");
51 
52 unsigned int n_devs = 1;
53 module_param(n_devs, uint, 0444);
54 MODULE_PARM_DESC(n_devs, " number of driver instances to create");
55 
56 static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
57 module_param_array(vid_cap_nr, int, NULL, 0444);
58 MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
59 
60 static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
61 module_param_array(vid_out_nr, int, NULL, 0444);
62 MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
63 
64 static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
65 module_param_array(vbi_cap_nr, int, NULL, 0444);
66 MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
67 
68 static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
69 module_param_array(vbi_out_nr, int, NULL, 0444);
70 MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
71 
72 static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
73 module_param_array(sdr_cap_nr, int, NULL, 0444);
74 MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
75 
76 static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
77 module_param_array(radio_rx_nr, int, NULL, 0444);
78 MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
79 
80 static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
81 module_param_array(radio_tx_nr, int, NULL, 0444);
82 MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
83 
84 static int meta_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
85 module_param_array(meta_cap_nr, int, NULL, 0444);
86 MODULE_PARM_DESC(meta_cap_nr, " videoX start number, -1 is autodetect");
87 
88 static int meta_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
89 module_param_array(meta_out_nr, int, NULL, 0444);
90 MODULE_PARM_DESC(meta_out_nr, " videoX start number, -1 is autodetect");
91 
92 static int touch_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
93 module_param_array(touch_cap_nr, int, NULL, 0444);
94 MODULE_PARM_DESC(touch_cap_nr, " v4l-touchX start number, -1 is autodetect");
95 
96 static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
97 module_param_array(ccs_cap_mode, int, NULL, 0444);
98 MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
99 			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
100 			   "\t\t    -1=user-controlled (default)");
101 
102 static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
103 module_param_array(ccs_out_mode, int, NULL, 0444);
104 MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
105 			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
106 			   "\t\t    -1=user-controlled (default)");
107 
108 static unsigned multiplanar[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 1 };
109 module_param_array(multiplanar, uint, NULL, 0444);
110 MODULE_PARM_DESC(multiplanar, " 1 (default) creates a single planar device, 2 creates a multiplanar device.");
111 
112 /*
113  * Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr +
114  * vbi-out + vid-out + meta-cap
115  */
116 static unsigned int node_types[VIVID_MAX_DEVS] = {
117 	[0 ... (VIVID_MAX_DEVS - 1)] = 0xe1d3d
118 };
119 module_param_array(node_types, uint, NULL, 0444);
120 MODULE_PARM_DESC(node_types, " node types, default is 0xe1d3d. Bitmask with the following meaning:\n"
121 			     "\t\t    bit 0: Video Capture node\n"
122 			     "\t\t    bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
123 			     "\t\t    bit 4: Radio Receiver node\n"
124 			     "\t\t    bit 5: Software Defined Radio Receiver node\n"
125 			     "\t\t    bit 8: Video Output node\n"
126 			     "\t\t    bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
127 			     "\t\t    bit 12: Radio Transmitter node\n"
128 			     "\t\t    bit 16: Framebuffer for testing output overlays\n"
129 			     "\t\t    bit 17: Metadata Capture node\n"
130 			     "\t\t    bit 18: Metadata Output node\n"
131 			     "\t\t    bit 19: Touch Capture node\n");
132 
133 /* Default: 4 inputs */
134 static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
135 module_param_array(num_inputs, uint, NULL, 0444);
136 MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
137 
138 /* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
139 static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
140 module_param_array(input_types, uint, NULL, 0444);
141 MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
142 			      "\t\t    bits 0-1 == input 0, bits 31-30 == input 15.\n"
143 			      "\t\t    Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
144 
145 /* Default: 2 outputs */
146 static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
147 module_param_array(num_outputs, uint, NULL, 0444);
148 MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
149 
150 /* Default: output 0 = SVID, 1 = HDMI */
151 static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
152 module_param_array(output_types, uint, NULL, 0444);
153 MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
154 			      "\t\t    bit 0 == output 0, bit 15 == output 15.\n"
155 			      "\t\t    Type 0 == S-Video, 1 == HDMI");
156 
157 unsigned vivid_debug;
158 module_param(vivid_debug, uint, 0644);
159 MODULE_PARM_DESC(vivid_debug, " activates debug info");
160 
161 static bool no_error_inj;
162 module_param(no_error_inj, bool, 0444);
163 MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
164 
165 static unsigned int allocators[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0 };
166 module_param_array(allocators, uint, NULL, 0444);
167 MODULE_PARM_DESC(allocators, " memory allocator selection, default is 0.\n"
168 			     "\t\t    0 == vmalloc\n"
169 			     "\t\t    1 == dma-contig");
170 
171 static unsigned int cache_hints[VIVID_MAX_DEVS] = {
172 	[0 ... (VIVID_MAX_DEVS - 1)] = 0
173 };
174 module_param_array(cache_hints, uint, NULL, 0444);
175 MODULE_PARM_DESC(cache_hints, " user-space cache hints, default is 0.\n"
176 			     "\t\t    0 == forbid\n"
177 			     "\t\t    1 == allow");
178 
179 static unsigned int supports_requests[VIVID_MAX_DEVS] = {
180 	[0 ... (VIVID_MAX_DEVS - 1)] = 1
181 };
182 module_param_array(supports_requests, uint, NULL, 0444);
183 MODULE_PARM_DESC(supports_requests, " support for requests, default is 1.\n"
184 			     "\t\t    0 == no support\n"
185 			     "\t\t    1 == supports requests\n"
186 			     "\t\t    2 == requires requests");
187 
188 struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
189 
190 DEFINE_SPINLOCK(hdmi_output_skip_mask_lock);
191 struct workqueue_struct *update_hdmi_ctrls_workqueue;
192 u64 hdmi_to_output_menu_skip_mask;
193 u64 hdmi_input_update_outputs_mask;
194 
195 struct vivid_dev *vivid_ctrl_hdmi_to_output_instance[MAX_MENU_ITEMS];
196 unsigned int vivid_ctrl_hdmi_to_output_index[MAX_MENU_ITEMS];
197 
198 char *vivid_ctrl_hdmi_to_output_strings[MAX_MENU_ITEMS + 1] = {
199 	"Test Pattern Generator",
200 	"None"
201 };
202 
203 DEFINE_SPINLOCK(svid_output_skip_mask_lock);
204 struct workqueue_struct *update_svid_ctrls_workqueue;
205 u64 svid_to_output_menu_skip_mask;
206 
207 struct vivid_dev *vivid_ctrl_svid_to_output_instance[MAX_MENU_ITEMS];
208 unsigned int vivid_ctrl_svid_to_output_index[MAX_MENU_ITEMS];
209 
210 char *vivid_ctrl_svid_to_output_strings[MAX_MENU_ITEMS + 1] = {
211 	"Test Pattern Generator",
212 	"None"
213 };
214 
215 const struct v4l2_rect vivid_min_rect = {
216 	0, 0, MIN_WIDTH, MIN_HEIGHT
217 };
218 
219 const struct v4l2_rect vivid_max_rect = {
220 	0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
221 };
222 
223 static const u8 vivid_hdmi_edid[256] = {
224 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
225 	0x31, 0xd8, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
226 	0x22, 0x1a, 0x01, 0x03, 0x80, 0x60, 0x36, 0x78,
227 	0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
228 	0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
229 	0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
230 	0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x08, 0xe8,
231 	0x00, 0x30, 0xf2, 0x70, 0x5a, 0x80, 0xb0, 0x58,
232 	0x8a, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00, 0x1e,
233 	0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
234 	0x87, 0x3c, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
235 	0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x76,
236 	0x69, 0x76, 0x69, 0x64, 0x0a, 0x20, 0x20, 0x20,
237 	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
238 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7b,
240 
241 	0x02, 0x03, 0x3f, 0xf1, 0x51, 0x61, 0x60, 0x5f,
242 	0x5e, 0x5d, 0x10, 0x1f, 0x04, 0x13, 0x22, 0x21,
243 	0x20, 0x05, 0x14, 0x02, 0x11, 0x01, 0x23, 0x09,
244 	0x07, 0x07, 0x83, 0x01, 0x00, 0x00, 0x6d, 0x03,
245 	0x0c, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x21, 0x00,
246 	0x60, 0x01, 0x02, 0x03, 0x67, 0xd8, 0x5d, 0xc4,
247 	0x01, 0x78, 0x00, 0x00, 0xe2, 0x00, 0xca, 0xe3,
248 	0x05, 0x00, 0x00, 0xe3, 0x06, 0x01, 0x00, 0x4d,
249 	0xd0, 0x00, 0xa0, 0xf0, 0x70, 0x3e, 0x80, 0x30,
250 	0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00,
251 	0x1e, 0x1a, 0x36, 0x80, 0xa0, 0x70, 0x38, 0x1f,
252 	0x40, 0x30, 0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32,
253 	0x00, 0x00, 0x1a, 0x1a, 0x1d, 0x00, 0x80, 0x51,
254 	0xd0, 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0xc0,
255 	0x1c, 0x32, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
256 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x92,
257 };
258 
259 static int vidioc_querycap(struct file *file, void  *priv,
260 					struct v4l2_capability *cap)
261 {
262 	struct vivid_dev *dev = video_drvdata(file);
263 
264 	strscpy(cap->driver, "vivid", sizeof(cap->driver));
265 	strscpy(cap->card, "vivid", sizeof(cap->card));
266 	snprintf(cap->bus_info, sizeof(cap->bus_info),
267 		 "platform:%s-%03d", VIVID_MODULE_NAME, dev->inst);
268 
269 	cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
270 		dev->vbi_cap_caps | dev->vbi_out_caps |
271 		dev->radio_rx_caps | dev->radio_tx_caps |
272 		dev->sdr_cap_caps | dev->meta_cap_caps |
273 		dev->meta_out_caps | dev->touch_cap_caps |
274 		V4L2_CAP_DEVICE_CAPS;
275 	return 0;
276 }
277 
278 static int vidioc_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
279 {
280 	struct video_device *vdev = video_devdata(file);
281 
282 	if (vdev->vfl_type == VFL_TYPE_RADIO)
283 		return vivid_radio_rx_s_hw_freq_seek(file, fh, a);
284 	return -ENOTTY;
285 }
286 
287 static int vidioc_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
288 {
289 	struct video_device *vdev = video_devdata(file);
290 
291 	if (vdev->vfl_type == VFL_TYPE_RADIO)
292 		return vivid_radio_rx_enum_freq_bands(file, fh, band);
293 	if (vdev->vfl_type == VFL_TYPE_SDR)
294 		return vivid_sdr_enum_freq_bands(file, fh, band);
295 	return -ENOTTY;
296 }
297 
298 static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
299 {
300 	struct video_device *vdev = video_devdata(file);
301 
302 	if (vdev->vfl_type == VFL_TYPE_RADIO)
303 		return vivid_radio_rx_g_tuner(file, fh, vt);
304 	if (vdev->vfl_type == VFL_TYPE_SDR)
305 		return vivid_sdr_g_tuner(file, fh, vt);
306 	return vivid_video_g_tuner(file, fh, vt);
307 }
308 
309 static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
310 {
311 	struct video_device *vdev = video_devdata(file);
312 
313 	if (vdev->vfl_type == VFL_TYPE_RADIO)
314 		return vivid_radio_rx_s_tuner(file, fh, vt);
315 	if (vdev->vfl_type == VFL_TYPE_SDR)
316 		return vivid_sdr_s_tuner(file, fh, vt);
317 	return vivid_video_s_tuner(file, fh, vt);
318 }
319 
320 static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
321 {
322 	struct vivid_dev *dev = video_drvdata(file);
323 	struct video_device *vdev = video_devdata(file);
324 
325 	if (vdev->vfl_type == VFL_TYPE_RADIO)
326 		return vivid_radio_g_frequency(file,
327 			vdev->vfl_dir == VFL_DIR_RX ?
328 			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
329 	if (vdev->vfl_type == VFL_TYPE_SDR)
330 		return vivid_sdr_g_frequency(file, fh, vf);
331 	return vivid_video_g_frequency(file, fh, vf);
332 }
333 
334 static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
335 {
336 	struct vivid_dev *dev = video_drvdata(file);
337 	struct video_device *vdev = video_devdata(file);
338 
339 	if (vdev->vfl_type == VFL_TYPE_RADIO)
340 		return vivid_radio_s_frequency(file,
341 			vdev->vfl_dir == VFL_DIR_RX ?
342 			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
343 	if (vdev->vfl_type == VFL_TYPE_SDR)
344 		return vivid_sdr_s_frequency(file, fh, vf);
345 	return vivid_video_s_frequency(file, fh, vf);
346 }
347 
348 static int vidioc_overlay(struct file *file, void *fh, unsigned i)
349 {
350 	struct video_device *vdev = video_devdata(file);
351 
352 	if (vdev->vfl_dir == VFL_DIR_RX)
353 		return -ENOTTY;
354 	return vivid_vid_out_overlay(file, fh, i);
355 }
356 
357 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *a)
358 {
359 	struct video_device *vdev = video_devdata(file);
360 
361 	if (vdev->vfl_dir == VFL_DIR_RX)
362 		return -ENOTTY;
363 	return vivid_vid_out_g_fbuf(file, fh, a);
364 }
365 
366 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
367 {
368 	struct video_device *vdev = video_devdata(file);
369 
370 	if (vdev->vfl_dir == VFL_DIR_RX)
371 		return -ENOTTY;
372 	return vivid_vid_out_s_fbuf(file, fh, a);
373 }
374 
375 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
376 {
377 	struct video_device *vdev = video_devdata(file);
378 
379 	if (vdev->vfl_dir == VFL_DIR_RX)
380 		return vivid_vid_cap_s_std(file, fh, id);
381 	return vivid_vid_out_s_std(file, fh, id);
382 }
383 
384 static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
385 {
386 	struct video_device *vdev = video_devdata(file);
387 
388 	if (vdev->vfl_dir == VFL_DIR_RX)
389 		return vivid_vid_cap_s_dv_timings(file, fh, timings);
390 	return vivid_vid_out_s_dv_timings(file, fh, timings);
391 }
392 
393 static int vidioc_g_pixelaspect(struct file *file, void *fh,
394 				int type, struct v4l2_fract *f)
395 {
396 	struct video_device *vdev = video_devdata(file);
397 
398 	if (vdev->vfl_dir == VFL_DIR_RX)
399 		return vivid_vid_cap_g_pixelaspect(file, fh, type, f);
400 	return vivid_vid_out_g_pixelaspect(file, fh, type, f);
401 }
402 
403 static int vidioc_g_selection(struct file *file, void *fh,
404 			      struct v4l2_selection *sel)
405 {
406 	struct video_device *vdev = video_devdata(file);
407 
408 	if (vdev->vfl_dir == VFL_DIR_RX)
409 		return vivid_vid_cap_g_selection(file, fh, sel);
410 	return vivid_vid_out_g_selection(file, fh, sel);
411 }
412 
413 static int vidioc_s_selection(struct file *file, void *fh,
414 			      struct v4l2_selection *sel)
415 {
416 	struct video_device *vdev = video_devdata(file);
417 
418 	if (vdev->vfl_dir == VFL_DIR_RX)
419 		return vivid_vid_cap_s_selection(file, fh, sel);
420 	return vivid_vid_out_s_selection(file, fh, sel);
421 }
422 
423 static int vidioc_g_parm(struct file *file, void *fh,
424 			  struct v4l2_streamparm *parm)
425 {
426 	struct video_device *vdev = video_devdata(file);
427 
428 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
429 		return vivid_g_parm_tch(file, fh, parm);
430 	if (vdev->vfl_dir == VFL_DIR_RX)
431 		return vivid_vid_cap_g_parm(file, fh, parm);
432 	return vivid_vid_out_g_parm(file, fh, parm);
433 }
434 
435 static int vidioc_s_parm(struct file *file, void *fh,
436 			  struct v4l2_streamparm *parm)
437 {
438 	struct video_device *vdev = video_devdata(file);
439 
440 	if (vdev->vfl_dir == VFL_DIR_RX)
441 		return vivid_vid_cap_s_parm(file, fh, parm);
442 	return -ENOTTY;
443 }
444 
445 static int vidioc_log_status(struct file *file, void *fh)
446 {
447 	struct vivid_dev *dev = video_drvdata(file);
448 	struct video_device *vdev = video_devdata(file);
449 
450 	v4l2_ctrl_log_status(file, fh);
451 	if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_VIDEO)
452 		tpg_log_status(&dev->tpg);
453 	return 0;
454 }
455 
456 static ssize_t vivid_radio_read(struct file *file, char __user *buf,
457 			 size_t size, loff_t *offset)
458 {
459 	struct video_device *vdev = video_devdata(file);
460 
461 	if (vdev->vfl_dir == VFL_DIR_TX)
462 		return -EINVAL;
463 	return vivid_radio_rx_read(file, buf, size, offset);
464 }
465 
466 static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
467 			  size_t size, loff_t *offset)
468 {
469 	struct video_device *vdev = video_devdata(file);
470 
471 	if (vdev->vfl_dir == VFL_DIR_RX)
472 		return -EINVAL;
473 	return vivid_radio_tx_write(file, buf, size, offset);
474 }
475 
476 static __poll_t vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
477 {
478 	struct video_device *vdev = video_devdata(file);
479 
480 	if (vdev->vfl_dir == VFL_DIR_RX)
481 		return vivid_radio_rx_poll(file, wait);
482 	return vivid_radio_tx_poll(file, wait);
483 }
484 
485 static int vivid_enum_input(struct file *file, void *priv,
486 			    struct v4l2_input *inp)
487 {
488 	struct video_device *vdev = video_devdata(file);
489 
490 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
491 		return vivid_enum_input_tch(file, priv, inp);
492 	return vidioc_enum_input(file, priv, inp);
493 }
494 
495 static int vivid_g_input(struct file *file, void *priv, unsigned int *i)
496 {
497 	struct video_device *vdev = video_devdata(file);
498 
499 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
500 		return vivid_g_input_tch(file, priv, i);
501 	return vidioc_g_input(file, priv, i);
502 }
503 
504 static int vivid_s_input(struct file *file, void *priv, unsigned int i)
505 {
506 	struct video_device *vdev = video_devdata(file);
507 
508 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
509 		return vivid_s_input_tch(file, priv, i);
510 	return vidioc_s_input(file, priv, i);
511 }
512 
513 static int vivid_enum_fmt_cap(struct file *file, void  *priv,
514 			      struct v4l2_fmtdesc *f)
515 {
516 	struct video_device *vdev = video_devdata(file);
517 
518 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
519 		return vivid_enum_fmt_tch(file, priv, f);
520 	return vivid_enum_fmt_vid(file, priv, f);
521 }
522 
523 static int vivid_g_fmt_cap(struct file *file, void *priv,
524 			   struct v4l2_format *f)
525 {
526 	struct video_device *vdev = video_devdata(file);
527 
528 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
529 		return vivid_g_fmt_tch(file, priv, f);
530 	return vidioc_g_fmt_vid_cap(file, priv, f);
531 }
532 
533 static int vivid_try_fmt_cap(struct file *file, void *priv,
534 			     struct v4l2_format *f)
535 {
536 	struct video_device *vdev = video_devdata(file);
537 
538 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
539 		return vivid_g_fmt_tch(file, priv, f);
540 	return vidioc_try_fmt_vid_cap(file, priv, f);
541 }
542 
543 static int vivid_s_fmt_cap(struct file *file, void *priv,
544 			   struct v4l2_format *f)
545 {
546 	struct video_device *vdev = video_devdata(file);
547 
548 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
549 		return vivid_g_fmt_tch(file, priv, f);
550 	return vidioc_s_fmt_vid_cap(file, priv, f);
551 }
552 
553 static int vivid_g_fmt_cap_mplane(struct file *file, void *priv,
554 				  struct v4l2_format *f)
555 {
556 	struct video_device *vdev = video_devdata(file);
557 
558 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
559 		return vivid_g_fmt_tch_mplane(file, priv, f);
560 	return vidioc_g_fmt_vid_cap_mplane(file, priv, f);
561 }
562 
563 static int vivid_try_fmt_cap_mplane(struct file *file, void *priv,
564 				    struct v4l2_format *f)
565 {
566 	struct video_device *vdev = video_devdata(file);
567 
568 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
569 		return vivid_g_fmt_tch_mplane(file, priv, f);
570 	return vidioc_try_fmt_vid_cap_mplane(file, priv, f);
571 }
572 
573 static int vivid_s_fmt_cap_mplane(struct file *file, void *priv,
574 				  struct v4l2_format *f)
575 {
576 	struct video_device *vdev = video_devdata(file);
577 
578 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
579 		return vivid_g_fmt_tch_mplane(file, priv, f);
580 	return vidioc_s_fmt_vid_cap_mplane(file, priv, f);
581 }
582 
583 static bool vivid_is_in_use(bool valid, struct video_device *vdev)
584 {
585 	unsigned long flags;
586 	bool res;
587 
588 	if (!valid)
589 		return false;
590 	spin_lock_irqsave(&vdev->fh_lock, flags);
591 	res = !list_empty(&vdev->fh_list);
592 	spin_unlock_irqrestore(&vdev->fh_lock, flags);
593 	return res;
594 }
595 
596 static bool vivid_is_last_user(struct vivid_dev *dev)
597 {
598 	unsigned int uses =
599 		vivid_is_in_use(dev->has_vid_cap, &dev->vid_cap_dev) +
600 		vivid_is_in_use(dev->has_vid_out, &dev->vid_out_dev) +
601 		vivid_is_in_use(dev->has_vbi_cap, &dev->vbi_cap_dev) +
602 		vivid_is_in_use(dev->has_vbi_out, &dev->vbi_out_dev) +
603 		vivid_is_in_use(dev->has_radio_rx, &dev->radio_rx_dev) +
604 		vivid_is_in_use(dev->has_radio_tx, &dev->radio_tx_dev) +
605 		vivid_is_in_use(dev->has_sdr_cap, &dev->sdr_cap_dev) +
606 		vivid_is_in_use(dev->has_meta_cap, &dev->meta_cap_dev) +
607 		vivid_is_in_use(dev->has_meta_out, &dev->meta_out_dev) +
608 		vivid_is_in_use(dev->has_touch_cap, &dev->touch_cap_dev);
609 
610 	return uses == 1;
611 }
612 
613 static void vivid_reconnect(struct vivid_dev *dev)
614 {
615 	if (dev->has_vid_cap)
616 		set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
617 	if (dev->has_vid_out)
618 		set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
619 	if (dev->has_vbi_cap)
620 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
621 	if (dev->has_vbi_out)
622 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
623 	if (dev->has_radio_rx)
624 		set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
625 	if (dev->has_radio_tx)
626 		set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
627 	if (dev->has_sdr_cap)
628 		set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
629 	if (dev->has_meta_cap)
630 		set_bit(V4L2_FL_REGISTERED, &dev->meta_cap_dev.flags);
631 	if (dev->has_meta_out)
632 		set_bit(V4L2_FL_REGISTERED, &dev->meta_out_dev.flags);
633 	if (dev->has_touch_cap)
634 		set_bit(V4L2_FL_REGISTERED, &dev->touch_cap_dev.flags);
635 	dev->disconnect_error = false;
636 }
637 
638 static int vivid_fop_release(struct file *file)
639 {
640 	struct vivid_dev *dev = video_drvdata(file);
641 	struct video_device *vdev = video_devdata(file);
642 
643 	mutex_lock(&dev->mutex);
644 	if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
645 	    dev->disconnect_error && !video_is_registered(vdev) &&
646 	    vivid_is_last_user(dev)) {
647 		/*
648 		 * I am the last user of this driver, and a disconnect
649 		 * was forced (since this video_device is unregistered),
650 		 * so re-register all video_device's again.
651 		 */
652 		v4l2_info(&dev->v4l2_dev, "reconnect\n");
653 		vivid_reconnect(dev);
654 	}
655 	if (file->private_data == dev->radio_rx_rds_owner) {
656 		dev->radio_rx_rds_last_block = 0;
657 		dev->radio_rx_rds_owner = NULL;
658 	}
659 	if (file->private_data == dev->radio_tx_rds_owner) {
660 		dev->radio_tx_rds_last_block = 0;
661 		dev->radio_tx_rds_owner = NULL;
662 	}
663 	mutex_unlock(&dev->mutex);
664 	if (vdev->queue)
665 		return vb2_fop_release(file);
666 	return v4l2_fh_release(file);
667 }
668 
669 static const struct v4l2_file_operations vivid_fops = {
670 	.owner		= THIS_MODULE,
671 	.open           = v4l2_fh_open,
672 	.release        = vivid_fop_release,
673 	.read           = vb2_fop_read,
674 	.write          = vb2_fop_write,
675 	.poll		= vb2_fop_poll,
676 	.unlocked_ioctl = video_ioctl2,
677 	.mmap           = vb2_fop_mmap,
678 };
679 
680 static const struct v4l2_file_operations vivid_radio_fops = {
681 	.owner		= THIS_MODULE,
682 	.open           = v4l2_fh_open,
683 	.release        = vivid_fop_release,
684 	.read           = vivid_radio_read,
685 	.write          = vivid_radio_write,
686 	.poll		= vivid_radio_poll,
687 	.unlocked_ioctl = video_ioctl2,
688 };
689 
690 static int vidioc_reqbufs(struct file *file, void *priv,
691 			  struct v4l2_requestbuffers *p)
692 {
693 	struct video_device *vdev = video_devdata(file);
694 	int r;
695 
696 	/*
697 	 * Sliced and raw VBI capture share the same queue so we must
698 	 * change the type.
699 	 */
700 	if (p->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
701 	    p->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
702 		r = vb2_queue_change_type(vdev->queue, p->type);
703 		if (r)
704 			return r;
705 	}
706 
707 	return vb2_ioctl_reqbufs(file, priv, p);
708 }
709 
710 static int vidioc_create_bufs(struct file *file, void *priv,
711 			      struct v4l2_create_buffers *p)
712 {
713 	struct video_device *vdev = video_devdata(file);
714 	int r;
715 
716 	/*
717 	 * Sliced and raw VBI capture share the same queue so we must
718 	 * change the type.
719 	 */
720 	if (p->format.type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
721 	    p->format.type == V4L2_BUF_TYPE_VBI_CAPTURE) {
722 		r = vb2_queue_change_type(vdev->queue, p->format.type);
723 		if (r)
724 			return r;
725 	}
726 
727 	return vb2_ioctl_create_bufs(file, priv, p);
728 }
729 
730 static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
731 	.vidioc_querycap		= vidioc_querycap,
732 
733 	.vidioc_enum_fmt_vid_cap	= vivid_enum_fmt_cap,
734 	.vidioc_g_fmt_vid_cap		= vivid_g_fmt_cap,
735 	.vidioc_try_fmt_vid_cap		= vivid_try_fmt_cap,
736 	.vidioc_s_fmt_vid_cap		= vivid_s_fmt_cap,
737 	.vidioc_g_fmt_vid_cap_mplane	= vivid_g_fmt_cap_mplane,
738 	.vidioc_try_fmt_vid_cap_mplane	= vivid_try_fmt_cap_mplane,
739 	.vidioc_s_fmt_vid_cap_mplane	= vivid_s_fmt_cap_mplane,
740 
741 	.vidioc_enum_fmt_vid_out	= vivid_enum_fmt_vid,
742 	.vidioc_g_fmt_vid_out		= vidioc_g_fmt_vid_out,
743 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt_vid_out,
744 	.vidioc_s_fmt_vid_out		= vidioc_s_fmt_vid_out,
745 	.vidioc_g_fmt_vid_out_mplane	= vidioc_g_fmt_vid_out_mplane,
746 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
747 	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out_mplane,
748 
749 	.vidioc_g_selection		= vidioc_g_selection,
750 	.vidioc_s_selection		= vidioc_s_selection,
751 	.vidioc_g_pixelaspect		= vidioc_g_pixelaspect,
752 
753 	.vidioc_g_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
754 	.vidioc_try_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
755 	.vidioc_s_fmt_vbi_cap		= vidioc_s_fmt_vbi_cap,
756 
757 	.vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
758 	.vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
759 	.vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
760 	.vidioc_g_sliced_vbi_cap	= vidioc_g_sliced_vbi_cap,
761 
762 	.vidioc_g_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
763 	.vidioc_try_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
764 	.vidioc_s_fmt_vbi_out		= vidioc_s_fmt_vbi_out,
765 
766 	.vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
767 	.vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
768 	.vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
769 
770 	.vidioc_enum_fmt_sdr_cap	= vidioc_enum_fmt_sdr_cap,
771 	.vidioc_g_fmt_sdr_cap		= vidioc_g_fmt_sdr_cap,
772 	.vidioc_try_fmt_sdr_cap		= vidioc_try_fmt_sdr_cap,
773 	.vidioc_s_fmt_sdr_cap		= vidioc_s_fmt_sdr_cap,
774 
775 	.vidioc_overlay			= vidioc_overlay,
776 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
777 	.vidioc_enum_frameintervals	= vidioc_enum_frameintervals,
778 	.vidioc_g_parm			= vidioc_g_parm,
779 	.vidioc_s_parm			= vidioc_s_parm,
780 
781 	.vidioc_g_fmt_vid_out_overlay	= vidioc_g_fmt_vid_out_overlay,
782 	.vidioc_try_fmt_vid_out_overlay	= vidioc_try_fmt_vid_out_overlay,
783 	.vidioc_s_fmt_vid_out_overlay	= vidioc_s_fmt_vid_out_overlay,
784 	.vidioc_g_fbuf			= vidioc_g_fbuf,
785 	.vidioc_s_fbuf			= vidioc_s_fbuf,
786 
787 	.vidioc_reqbufs			= vidioc_reqbufs,
788 	.vidioc_create_bufs		= vidioc_create_bufs,
789 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
790 	.vidioc_querybuf		= vb2_ioctl_querybuf,
791 	.vidioc_qbuf			= vb2_ioctl_qbuf,
792 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
793 	.vidioc_expbuf			= vb2_ioctl_expbuf,
794 	.vidioc_streamon		= vb2_ioctl_streamon,
795 	.vidioc_streamoff		= vb2_ioctl_streamoff,
796 	.vidioc_remove_bufs		= vb2_ioctl_remove_bufs,
797 
798 	.vidioc_enum_input		= vivid_enum_input,
799 	.vidioc_g_input			= vivid_g_input,
800 	.vidioc_s_input			= vivid_s_input,
801 	.vidioc_s_audio			= vidioc_s_audio,
802 	.vidioc_g_audio			= vidioc_g_audio,
803 	.vidioc_enumaudio		= vidioc_enumaudio,
804 	.vidioc_s_frequency		= vidioc_s_frequency,
805 	.vidioc_g_frequency		= vidioc_g_frequency,
806 	.vidioc_s_tuner			= vidioc_s_tuner,
807 	.vidioc_g_tuner			= vidioc_g_tuner,
808 	.vidioc_s_modulator		= vidioc_s_modulator,
809 	.vidioc_g_modulator		= vidioc_g_modulator,
810 	.vidioc_s_hw_freq_seek		= vidioc_s_hw_freq_seek,
811 	.vidioc_enum_freq_bands		= vidioc_enum_freq_bands,
812 
813 	.vidioc_enum_output		= vidioc_enum_output,
814 	.vidioc_g_output		= vidioc_g_output,
815 	.vidioc_s_output		= vidioc_s_output,
816 	.vidioc_s_audout		= vidioc_s_audout,
817 	.vidioc_g_audout		= vidioc_g_audout,
818 	.vidioc_enumaudout		= vidioc_enumaudout,
819 
820 	.vidioc_querystd		= vidioc_querystd,
821 	.vidioc_g_std			= vidioc_g_std,
822 	.vidioc_s_std			= vidioc_s_std,
823 	.vidioc_s_dv_timings		= vidioc_s_dv_timings,
824 	.vidioc_g_dv_timings		= vidioc_g_dv_timings,
825 	.vidioc_query_dv_timings	= vidioc_query_dv_timings,
826 	.vidioc_enum_dv_timings		= vidioc_enum_dv_timings,
827 	.vidioc_dv_timings_cap		= vidioc_dv_timings_cap,
828 	.vidioc_g_edid			= vidioc_g_edid,
829 	.vidioc_s_edid			= vidioc_s_edid,
830 
831 	.vidioc_log_status		= vidioc_log_status,
832 	.vidioc_subscribe_event		= vidioc_subscribe_event,
833 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
834 
835 	.vidioc_enum_fmt_meta_cap	= vidioc_enum_fmt_meta_cap,
836 	.vidioc_g_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
837 	.vidioc_s_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
838 	.vidioc_try_fmt_meta_cap	= vidioc_g_fmt_meta_cap,
839 
840 	.vidioc_enum_fmt_meta_out       = vidioc_enum_fmt_meta_out,
841 	.vidioc_g_fmt_meta_out          = vidioc_g_fmt_meta_out,
842 	.vidioc_s_fmt_meta_out          = vidioc_g_fmt_meta_out,
843 	.vidioc_try_fmt_meta_out        = vidioc_g_fmt_meta_out,
844 };
845 
846 /* -----------------------------------------------------------------
847 	Initialization and module stuff
848    ------------------------------------------------------------------*/
849 
850 static void vivid_dev_release(struct v4l2_device *v4l2_dev)
851 {
852 	struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
853 
854 	cancel_work_sync(&dev->update_hdmi_ctrl_work);
855 	vivid_free_controls(dev);
856 	v4l2_device_unregister(&dev->v4l2_dev);
857 #ifdef CONFIG_MEDIA_CONTROLLER
858 	media_device_cleanup(&dev->mdev);
859 #endif
860 	vfree(dev->scaled_line);
861 	vfree(dev->blended_line);
862 	vfree(dev->edid);
863 	tpg_free(&dev->tpg);
864 	kfree(dev->query_dv_timings_qmenu);
865 	kfree(dev->query_dv_timings_qmenu_strings);
866 	kfree(dev);
867 }
868 
869 #ifdef CONFIG_MEDIA_CONTROLLER
870 static int vivid_req_validate(struct media_request *req)
871 {
872 	struct vivid_dev *dev = container_of(req->mdev, struct vivid_dev, mdev);
873 
874 	if (dev->req_validate_error) {
875 		dev->req_validate_error = false;
876 		return -EINVAL;
877 	}
878 	return vb2_request_validate(req);
879 }
880 
881 static const struct media_device_ops vivid_media_ops = {
882 	.req_validate = vivid_req_validate,
883 	.req_queue = vb2_request_queue,
884 };
885 #endif
886 
887 static int vivid_create_queue(struct vivid_dev *dev,
888 			      struct vb2_queue *q,
889 			      u32 buf_type,
890 			      unsigned int min_reqbufs_allocation,
891 			      const struct vb2_ops *ops)
892 {
893 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->multiplanar)
894 		buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
895 	else if (buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT && dev->multiplanar)
896 		buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
897 	else if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE && !dev->has_raw_vbi_cap)
898 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
899 	else if (buf_type == V4L2_BUF_TYPE_VBI_OUTPUT && !dev->has_raw_vbi_out)
900 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
901 
902 	q->type = buf_type;
903 	q->io_modes = VB2_MMAP | VB2_DMABUF;
904 	q->io_modes |= V4L2_TYPE_IS_OUTPUT(buf_type) ?  VB2_WRITE : VB2_READ;
905 
906 	/*
907 	 * The maximum number of buffers is 32768 if PAGE_SHIFT == 12,
908 	 * see also MAX_BUFFER_INDEX in videobuf2-core.c. It will be less if
909 	 * PAGE_SHIFT > 12, but then max_num_buffers will be clamped by
910 	 * videobuf2-core.c to MAX_BUFFER_INDEX.
911 	 */
912 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
913 		q->max_num_buffers = 64;
914 	if (buf_type == V4L2_BUF_TYPE_SDR_CAPTURE)
915 		q->max_num_buffers = 1024;
916 	if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE)
917 		q->max_num_buffers = 32768;
918 
919 	if (allocators[dev->inst] != 1)
920 		q->io_modes |= VB2_USERPTR;
921 	q->drv_priv = dev;
922 	q->buf_struct_size = sizeof(struct vivid_buffer);
923 	q->ops = ops;
924 	q->mem_ops = allocators[dev->inst] == 1 ? &vb2_dma_contig_memops :
925 						  &vb2_vmalloc_memops;
926 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
927 	q->min_reqbufs_allocation = min_reqbufs_allocation;
928 	q->lock = &dev->mutex;
929 	q->dev = dev->v4l2_dev.dev;
930 	q->supports_requests = supports_requests[dev->inst];
931 	q->requires_requests = supports_requests[dev->inst] >= 2;
932 	q->allow_cache_hints = (cache_hints[dev->inst] == 1);
933 
934 	return vb2_queue_init(q);
935 }
936 
937 static int vivid_detect_feature_set(struct vivid_dev *dev, int inst,
938 				    unsigned node_type,
939 				    bool *has_tuner,
940 				    bool *has_modulator,
941 				    int *ccs_cap,
942 				    int *ccs_out,
943 				    unsigned in_type_counter[4],
944 				    unsigned out_type_counter[4])
945 {
946 	int i;
947 
948 	/* do we use single- or multi-planar? */
949 	dev->multiplanar = multiplanar[inst] > 1;
950 	v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
951 			dev->multiplanar ? "multi" : "single ");
952 
953 	/* how many inputs do we have and of what type? */
954 	dev->num_inputs = num_inputs[inst];
955 	if (node_type & 0x20007) {
956 		if (dev->num_inputs < 1)
957 			dev->num_inputs = 1;
958 	} else {
959 		dev->num_inputs = 0;
960 	}
961 	if (dev->num_inputs >= MAX_INPUTS)
962 		dev->num_inputs = MAX_INPUTS;
963 	for (i = 0; i < dev->num_inputs; i++) {
964 		dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
965 		dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
966 	}
967 	dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
968 	if (in_type_counter[HDMI] == 16) {
969 		/* The CEC physical address only allows for max 15 inputs */
970 		in_type_counter[HDMI]--;
971 		dev->num_inputs--;
972 	}
973 	dev->num_hdmi_inputs = in_type_counter[HDMI];
974 	dev->num_svid_inputs = in_type_counter[SVID];
975 
976 	/* how many outputs do we have and of what type? */
977 	dev->num_outputs = num_outputs[inst];
978 	if (node_type & 0x40300) {
979 		if (dev->num_outputs < 1)
980 			dev->num_outputs = 1;
981 	} else {
982 		dev->num_outputs = 0;
983 	}
984 	if (dev->num_outputs >= MAX_OUTPUTS)
985 		dev->num_outputs = MAX_OUTPUTS;
986 	for (i = 0; i < dev->num_outputs; i++) {
987 		dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
988 		dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
989 	}
990 	dev->has_audio_outputs = out_type_counter[SVID];
991 	if (out_type_counter[HDMI] == 16) {
992 		/*
993 		 * The CEC physical address only allows for max 15 inputs,
994 		 * so outputs are also limited to 15 to allow for easy
995 		 * CEC output to input mapping.
996 		 */
997 		out_type_counter[HDMI]--;
998 		dev->num_outputs--;
999 	}
1000 	dev->num_hdmi_outputs = out_type_counter[HDMI];
1001 
1002 	/* do we create a video capture device? */
1003 	dev->has_vid_cap = node_type & 0x0001;
1004 
1005 	/* do we create a vbi capture device? */
1006 	if (in_type_counter[TV] || in_type_counter[SVID]) {
1007 		dev->has_raw_vbi_cap = node_type & 0x0004;
1008 		dev->has_sliced_vbi_cap = node_type & 0x0008;
1009 		dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
1010 	}
1011 
1012 	/* do we create a meta capture device */
1013 	dev->has_meta_cap = node_type & 0x20000;
1014 
1015 	/* sanity checks */
1016 	if ((in_type_counter[WEBCAM] || in_type_counter[HDMI]) &&
1017 	    !dev->has_vid_cap && !dev->has_meta_cap) {
1018 		v4l2_warn(&dev->v4l2_dev,
1019 			  "Webcam or HDMI input without video or metadata nodes\n");
1020 		return -EINVAL;
1021 	}
1022 	if ((in_type_counter[TV] || in_type_counter[SVID]) &&
1023 	    !dev->has_vid_cap && !dev->has_vbi_cap && !dev->has_meta_cap) {
1024 		v4l2_warn(&dev->v4l2_dev,
1025 			  "TV or S-Video input without video, VBI or metadata nodes\n");
1026 		return -EINVAL;
1027 	}
1028 
1029 	/* do we create a video output device? */
1030 	dev->has_vid_out = node_type & 0x0100;
1031 
1032 	/* do we create a vbi output device? */
1033 	if (out_type_counter[SVID]) {
1034 		dev->has_raw_vbi_out = node_type & 0x0400;
1035 		dev->has_sliced_vbi_out = node_type & 0x0800;
1036 		dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
1037 	}
1038 
1039 	/* do we create a metadata output device */
1040 	dev->has_meta_out = node_type & 0x40000;
1041 
1042 	/* sanity checks */
1043 	if (out_type_counter[SVID] &&
1044 	    !dev->has_vid_out && !dev->has_vbi_out && !dev->has_meta_out) {
1045 		v4l2_warn(&dev->v4l2_dev,
1046 			  "S-Video output without video, VBI or metadata nodes\n");
1047 		return -EINVAL;
1048 	}
1049 	if (out_type_counter[HDMI] && !dev->has_vid_out && !dev->has_meta_out) {
1050 		v4l2_warn(&dev->v4l2_dev,
1051 			  "HDMI output without video or metadata nodes\n");
1052 		return -EINVAL;
1053 	}
1054 
1055 	/* do we create a radio receiver device? */
1056 	dev->has_radio_rx = node_type & 0x0010;
1057 
1058 	/* do we create a radio transmitter device? */
1059 	dev->has_radio_tx = node_type & 0x1000;
1060 
1061 	/* do we create a software defined radio capture device? */
1062 	dev->has_sdr_cap = node_type & 0x0020;
1063 
1064 	/* do we have a TV tuner? */
1065 	dev->has_tv_tuner = in_type_counter[TV];
1066 
1067 	/* do we have a tuner? */
1068 	*has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
1069 		      dev->has_radio_rx || dev->has_sdr_cap;
1070 
1071 	/* do we have a modulator? */
1072 	*has_modulator = dev->has_radio_tx;
1073 
1074 	if (dev->has_vid_cap)
1075 		/* do we have a framebuffer for overlay testing? */
1076 		dev->has_fb = node_type & 0x10000;
1077 
1078 	/* can we do crop/compose/scaling while capturing? */
1079 	if (no_error_inj && *ccs_cap == -1)
1080 		*ccs_cap = 7;
1081 
1082 	/* if ccs_cap == -1, then the user can select it using controls */
1083 	if (*ccs_cap != -1) {
1084 		dev->has_crop_cap = *ccs_cap & 1;
1085 		dev->has_compose_cap = *ccs_cap & 2;
1086 		dev->has_scaler_cap = *ccs_cap & 4;
1087 		v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
1088 			dev->has_crop_cap ? 'Y' : 'N',
1089 			dev->has_compose_cap ? 'Y' : 'N',
1090 			dev->has_scaler_cap ? 'Y' : 'N');
1091 	}
1092 
1093 	/* can we do crop/compose/scaling with video output? */
1094 	if (no_error_inj && *ccs_out == -1)
1095 		*ccs_out = 7;
1096 
1097 	/* if ccs_out == -1, then the user can select it using controls */
1098 	if (*ccs_out != -1) {
1099 		dev->has_crop_out = *ccs_out & 1;
1100 		dev->has_compose_out = *ccs_out & 2;
1101 		dev->has_scaler_out = *ccs_out & 4;
1102 		v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
1103 			dev->has_crop_out ? 'Y' : 'N',
1104 			dev->has_compose_out ? 'Y' : 'N',
1105 			dev->has_scaler_out ? 'Y' : 'N');
1106 	}
1107 
1108 	/* do we create a touch capture device */
1109 	dev->has_touch_cap = node_type & 0x80000;
1110 
1111 	return 0;
1112 }
1113 
1114 static void vivid_set_capabilities(struct vivid_dev *dev)
1115 {
1116 	if (dev->has_vid_cap) {
1117 		/* set up the capabilities of the video capture device */
1118 		dev->vid_cap_caps = dev->multiplanar ?
1119 			V4L2_CAP_VIDEO_CAPTURE_MPLANE :
1120 			V4L2_CAP_VIDEO_CAPTURE;
1121 		dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1122 		if (dev->has_audio_inputs)
1123 			dev->vid_cap_caps |= V4L2_CAP_AUDIO;
1124 		if (dev->has_tv_tuner)
1125 			dev->vid_cap_caps |= V4L2_CAP_TUNER;
1126 	}
1127 	if (dev->has_vid_out) {
1128 		/* set up the capabilities of the video output device */
1129 		dev->vid_out_caps = dev->multiplanar ?
1130 			V4L2_CAP_VIDEO_OUTPUT_MPLANE :
1131 			V4L2_CAP_VIDEO_OUTPUT;
1132 		if (dev->has_fb)
1133 			dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1134 		dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1135 		if (dev->has_audio_outputs)
1136 			dev->vid_out_caps |= V4L2_CAP_AUDIO;
1137 	}
1138 	if (dev->has_vbi_cap) {
1139 		/* set up the capabilities of the vbi capture device */
1140 		dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
1141 				    (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
1142 		dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1143 		if (dev->has_audio_inputs)
1144 			dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
1145 		if (dev->has_tv_tuner)
1146 			dev->vbi_cap_caps |= V4L2_CAP_TUNER;
1147 	}
1148 	if (dev->has_vbi_out) {
1149 		/* set up the capabilities of the vbi output device */
1150 		dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
1151 				    (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
1152 		dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1153 		if (dev->has_audio_outputs)
1154 			dev->vbi_out_caps |= V4L2_CAP_AUDIO;
1155 	}
1156 	if (dev->has_sdr_cap) {
1157 		/* set up the capabilities of the sdr capture device */
1158 		dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
1159 		dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1160 	}
1161 	/* set up the capabilities of the radio receiver device */
1162 	if (dev->has_radio_rx)
1163 		dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
1164 				     V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
1165 				     V4L2_CAP_READWRITE;
1166 	/* set up the capabilities of the radio transmitter device */
1167 	if (dev->has_radio_tx)
1168 		dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
1169 				     V4L2_CAP_READWRITE;
1170 
1171 	/* set up the capabilities of meta capture device */
1172 	if (dev->has_meta_cap) {
1173 		dev->meta_cap_caps = V4L2_CAP_META_CAPTURE |
1174 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1175 		if (dev->has_audio_inputs)
1176 			dev->meta_cap_caps |= V4L2_CAP_AUDIO;
1177 		if (dev->has_tv_tuner)
1178 			dev->meta_cap_caps |= V4L2_CAP_TUNER;
1179 	}
1180 	/* set up the capabilities of meta output device */
1181 	if (dev->has_meta_out) {
1182 		dev->meta_out_caps = V4L2_CAP_META_OUTPUT |
1183 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1184 		if (dev->has_audio_outputs)
1185 			dev->meta_out_caps |= V4L2_CAP_AUDIO;
1186 	}
1187 	/* set up the capabilities of the touch capture device */
1188 	if (dev->has_touch_cap) {
1189 		dev->touch_cap_caps = V4L2_CAP_TOUCH | V4L2_CAP_STREAMING |
1190 				      V4L2_CAP_READWRITE;
1191 		dev->touch_cap_caps |= dev->multiplanar ?
1192 			V4L2_CAP_VIDEO_CAPTURE_MPLANE : V4L2_CAP_VIDEO_CAPTURE;
1193 	}
1194 }
1195 
1196 static void vivid_disable_unused_ioctls(struct vivid_dev *dev,
1197 					bool has_tuner,
1198 					bool has_modulator,
1199 					unsigned in_type_counter[4],
1200 					unsigned out_type_counter[4])
1201 {
1202 	/* disable invalid ioctls based on the feature set */
1203 	if (!dev->has_audio_inputs) {
1204 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
1205 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
1206 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
1207 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
1208 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
1209 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
1210 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_AUDIO);
1211 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_AUDIO);
1212 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_ENUMAUDIO);
1213 	}
1214 	if (!dev->has_audio_outputs) {
1215 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
1216 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
1217 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
1218 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
1219 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
1220 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
1221 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_AUDOUT);
1222 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_AUDOUT);
1223 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_ENUMAUDOUT);
1224 	}
1225 	if (!in_type_counter[TV] && !in_type_counter[SVID]) {
1226 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
1227 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
1228 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
1229 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
1230 	}
1231 	if (!out_type_counter[SVID]) {
1232 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
1233 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
1234 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
1235 	}
1236 	if (!has_tuner && !has_modulator) {
1237 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
1238 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
1239 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
1240 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
1241 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_FREQUENCY);
1242 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_FREQUENCY);
1243 	}
1244 	if (!has_tuner) {
1245 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
1246 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
1247 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
1248 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
1249 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_TUNER);
1250 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_TUNER);
1251 	}
1252 	if (in_type_counter[HDMI] == 0) {
1253 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
1254 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
1255 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
1256 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
1257 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
1258 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
1259 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
1260 	}
1261 	if (out_type_counter[HDMI] == 0) {
1262 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
1263 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
1264 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
1265 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
1266 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
1267 	}
1268 	if (!dev->has_fb) {
1269 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
1270 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
1271 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
1272 	}
1273 	v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1274 	v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1275 	v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1276 	v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1277 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
1278 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
1279 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
1280 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1281 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
1282 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
1283 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_FREQUENCY);
1284 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_FREQUENCY);
1285 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_S_PARM);
1286 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMESIZES);
1287 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1288 }
1289 
1290 static int vivid_init_dv_timings(struct vivid_dev *dev)
1291 {
1292 	int i;
1293 
1294 	while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
1295 		dev->query_dv_timings_size++;
1296 
1297 	/*
1298 	 * Create a char pointer array that points to the names of all the
1299 	 * preset timings
1300 	 */
1301 	dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
1302 						    sizeof(char *), GFP_KERNEL);
1303 	/*
1304 	 * Create a string array containing the names of all the preset
1305 	 * timings. Each name is max 31 chars long (+ terminating 0).
1306 	 */
1307 	dev->query_dv_timings_qmenu_strings =
1308 		kmalloc_array(dev->query_dv_timings_size, 32, GFP_KERNEL);
1309 
1310 	if (!dev->query_dv_timings_qmenu ||
1311 	    !dev->query_dv_timings_qmenu_strings)
1312 		return -ENOMEM;
1313 
1314 	for (i = 0; i < dev->query_dv_timings_size; i++) {
1315 		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1316 		char *p = dev->query_dv_timings_qmenu_strings + i * 32;
1317 		u32 htot, vtot;
1318 
1319 		dev->query_dv_timings_qmenu[i] = p;
1320 
1321 		htot = V4L2_DV_BT_FRAME_WIDTH(bt);
1322 		vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
1323 		snprintf(p, 32, "%ux%u%s%u",
1324 			bt->width, bt->height, bt->interlaced ? "i" : "p",
1325 			(u32)bt->pixelclock / (htot * vtot));
1326 	}
1327 
1328 	return 0;
1329 }
1330 
1331 static int vivid_create_queues(struct vivid_dev *dev)
1332 {
1333 	int ret;
1334 
1335 	/* start creating the vb2 queues */
1336 	if (dev->has_vid_cap) {
1337 		/* initialize vid_cap queue */
1338 		ret = vivid_create_queue(dev, &dev->vb_vid_cap_q,
1339 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1340 					 &vivid_vid_cap_qops);
1341 		if (ret)
1342 			return ret;
1343 	}
1344 
1345 	if (dev->has_vid_out) {
1346 		/* initialize vid_out queue */
1347 		ret = vivid_create_queue(dev, &dev->vb_vid_out_q,
1348 					 V4L2_BUF_TYPE_VIDEO_OUTPUT, 2,
1349 					 &vivid_vid_out_qops);
1350 		if (ret)
1351 			return ret;
1352 	}
1353 
1354 	if (dev->has_vbi_cap) {
1355 		/* initialize vbi_cap queue */
1356 		ret = vivid_create_queue(dev, &dev->vb_vbi_cap_q,
1357 					 V4L2_BUF_TYPE_VBI_CAPTURE, 2,
1358 					 &vivid_vbi_cap_qops);
1359 		if (ret)
1360 			return ret;
1361 	}
1362 
1363 	if (dev->has_vbi_out) {
1364 		/* initialize vbi_out queue */
1365 		ret = vivid_create_queue(dev, &dev->vb_vbi_out_q,
1366 					 V4L2_BUF_TYPE_VBI_OUTPUT, 2,
1367 					 &vivid_vbi_out_qops);
1368 		if (ret)
1369 			return ret;
1370 	}
1371 
1372 	if (dev->has_sdr_cap) {
1373 		/* initialize sdr_cap queue */
1374 		ret = vivid_create_queue(dev, &dev->vb_sdr_cap_q,
1375 					 V4L2_BUF_TYPE_SDR_CAPTURE, 8,
1376 					 &vivid_sdr_cap_qops);
1377 		if (ret)
1378 			return ret;
1379 	}
1380 
1381 	if (dev->has_meta_cap) {
1382 		/* initialize meta_cap queue */
1383 		ret = vivid_create_queue(dev, &dev->vb_meta_cap_q,
1384 					 V4L2_BUF_TYPE_META_CAPTURE, 2,
1385 					 &vivid_meta_cap_qops);
1386 		if (ret)
1387 			return ret;
1388 	}
1389 
1390 	if (dev->has_meta_out) {
1391 		/* initialize meta_out queue */
1392 		ret = vivid_create_queue(dev, &dev->vb_meta_out_q,
1393 					 V4L2_BUF_TYPE_META_OUTPUT, 2,
1394 					 &vivid_meta_out_qops);
1395 		if (ret)
1396 			return ret;
1397 	}
1398 
1399 	if (dev->has_touch_cap) {
1400 		/* initialize touch_cap queue */
1401 		ret = vivid_create_queue(dev, &dev->vb_touch_cap_q,
1402 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1403 					 &vivid_touch_cap_qops);
1404 		if (ret)
1405 			return ret;
1406 	}
1407 
1408 	if (dev->has_fb) {
1409 		/* Create framebuffer for testing output overlay */
1410 		ret = vivid_fb_init(dev);
1411 		if (ret)
1412 			return ret;
1413 		v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1414 			  dev->fb_info.node);
1415 	}
1416 	return 0;
1417 }
1418 
1419 static int vivid_create_devnodes(struct platform_device *pdev,
1420 				 struct vivid_dev *dev, int inst,
1421 				 v4l2_std_id tvnorms_cap,
1422 				 v4l2_std_id tvnorms_out,
1423 				 unsigned in_type_counter[4],
1424 				 unsigned out_type_counter[4])
1425 {
1426 	struct video_device *vfd;
1427 	int ret;
1428 
1429 	if (dev->has_vid_cap) {
1430 		vfd = &dev->vid_cap_dev;
1431 		snprintf(vfd->name, sizeof(vfd->name),
1432 			 "vivid-%03d-vid-cap", inst);
1433 		vfd->fops = &vivid_fops;
1434 		vfd->ioctl_ops = &vivid_ioctl_ops;
1435 		vfd->device_caps = dev->vid_cap_caps;
1436 		vfd->release = video_device_release_empty;
1437 		vfd->v4l2_dev = &dev->v4l2_dev;
1438 		vfd->queue = &dev->vb_vid_cap_q;
1439 		vfd->tvnorms = tvnorms_cap;
1440 
1441 		/*
1442 		 * Provide a mutex to v4l2 core. It will be used to protect
1443 		 * all fops and v4l2 ioctls.
1444 		 */
1445 		vfd->lock = &dev->mutex;
1446 		video_set_drvdata(vfd, dev);
1447 
1448 #ifdef CONFIG_MEDIA_CONTROLLER
1449 		dev->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
1450 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_cap_pad);
1451 		if (ret)
1452 			return ret;
1453 #endif
1454 
1455 #ifdef CONFIG_VIDEO_VIVID_CEC
1456 		if (in_type_counter[HDMI]) {
1457 			ret = cec_register_adapter(dev->cec_rx_adap, &pdev->dev);
1458 			if (ret < 0) {
1459 				cec_delete_adapter(dev->cec_rx_adap);
1460 				dev->cec_rx_adap = NULL;
1461 				return ret;
1462 			}
1463 			cec_s_phys_addr(dev->cec_rx_adap, 0, false);
1464 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input\n",
1465 				  dev_name(&dev->cec_rx_adap->devnode.dev));
1466 		}
1467 #endif
1468 
1469 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_cap_nr[inst]);
1470 		if (ret < 0)
1471 			return ret;
1472 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1473 					  video_device_node_name(vfd));
1474 	}
1475 
1476 	if (dev->has_vid_out) {
1477 #ifdef CONFIG_VIDEO_VIVID_CEC
1478 		int i;
1479 #endif
1480 		vfd = &dev->vid_out_dev;
1481 		snprintf(vfd->name, sizeof(vfd->name),
1482 			 "vivid-%03d-vid-out", inst);
1483 		vfd->vfl_dir = VFL_DIR_TX;
1484 		vfd->fops = &vivid_fops;
1485 		vfd->ioctl_ops = &vivid_ioctl_ops;
1486 		vfd->device_caps = dev->vid_out_caps;
1487 		vfd->release = video_device_release_empty;
1488 		vfd->v4l2_dev = &dev->v4l2_dev;
1489 		vfd->queue = &dev->vb_vid_out_q;
1490 		vfd->tvnorms = tvnorms_out;
1491 
1492 		/*
1493 		 * Provide a mutex to v4l2 core. It will be used to protect
1494 		 * all fops and v4l2 ioctls.
1495 		 */
1496 		vfd->lock = &dev->mutex;
1497 		video_set_drvdata(vfd, dev);
1498 
1499 #ifdef CONFIG_MEDIA_CONTROLLER
1500 		dev->vid_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1501 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_out_pad);
1502 		if (ret)
1503 			return ret;
1504 #endif
1505 
1506 #ifdef CONFIG_VIDEO_VIVID_CEC
1507 		for (i = 0; i < dev->num_hdmi_outputs; i++) {
1508 			ret = cec_register_adapter(dev->cec_tx_adap[i], &pdev->dev);
1509 			if (ret < 0) {
1510 				for (; i >= 0; i--) {
1511 					cec_delete_adapter(dev->cec_tx_adap[i]);
1512 					dev->cec_tx_adap[i] = NULL;
1513 				}
1514 				return ret;
1515 			}
1516 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1517 				  dev_name(&dev->cec_tx_adap[i]->devnode.dev), i);
1518 		}
1519 #endif
1520 
1521 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_out_nr[inst]);
1522 		if (ret < 0)
1523 			return ret;
1524 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1525 					  video_device_node_name(vfd));
1526 	}
1527 
1528 	if (dev->has_vbi_cap) {
1529 		vfd = &dev->vbi_cap_dev;
1530 		snprintf(vfd->name, sizeof(vfd->name),
1531 			 "vivid-%03d-vbi-cap", inst);
1532 		vfd->fops = &vivid_fops;
1533 		vfd->ioctl_ops = &vivid_ioctl_ops;
1534 		vfd->device_caps = dev->vbi_cap_caps;
1535 		vfd->release = video_device_release_empty;
1536 		vfd->v4l2_dev = &dev->v4l2_dev;
1537 		vfd->queue = &dev->vb_vbi_cap_q;
1538 		vfd->lock = &dev->mutex;
1539 		vfd->tvnorms = tvnorms_cap;
1540 		video_set_drvdata(vfd, dev);
1541 
1542 #ifdef CONFIG_MEDIA_CONTROLLER
1543 		dev->vbi_cap_pad.flags = MEDIA_PAD_FL_SINK;
1544 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_cap_pad);
1545 		if (ret)
1546 			return ret;
1547 #endif
1548 
1549 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1550 		if (ret < 0)
1551 			return ret;
1552 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1553 					  video_device_node_name(vfd),
1554 					  (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1555 					  "raw and sliced" :
1556 					  (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1557 	}
1558 
1559 	if (dev->has_vbi_out) {
1560 		vfd = &dev->vbi_out_dev;
1561 		snprintf(vfd->name, sizeof(vfd->name),
1562 			 "vivid-%03d-vbi-out", inst);
1563 		vfd->vfl_dir = VFL_DIR_TX;
1564 		vfd->fops = &vivid_fops;
1565 		vfd->ioctl_ops = &vivid_ioctl_ops;
1566 		vfd->device_caps = dev->vbi_out_caps;
1567 		vfd->release = video_device_release_empty;
1568 		vfd->v4l2_dev = &dev->v4l2_dev;
1569 		vfd->queue = &dev->vb_vbi_out_q;
1570 		vfd->lock = &dev->mutex;
1571 		vfd->tvnorms = tvnorms_out;
1572 		video_set_drvdata(vfd, dev);
1573 
1574 #ifdef CONFIG_MEDIA_CONTROLLER
1575 		dev->vbi_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1576 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_out_pad);
1577 		if (ret)
1578 			return ret;
1579 #endif
1580 
1581 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1582 		if (ret < 0)
1583 			return ret;
1584 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1585 					  video_device_node_name(vfd),
1586 					  (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1587 					  "raw and sliced" :
1588 					  (dev->has_raw_vbi_out ? "raw" : "sliced"));
1589 	}
1590 
1591 	if (dev->has_sdr_cap) {
1592 		vfd = &dev->sdr_cap_dev;
1593 		snprintf(vfd->name, sizeof(vfd->name),
1594 			 "vivid-%03d-sdr-cap", inst);
1595 		vfd->fops = &vivid_fops;
1596 		vfd->ioctl_ops = &vivid_ioctl_ops;
1597 		vfd->device_caps = dev->sdr_cap_caps;
1598 		vfd->release = video_device_release_empty;
1599 		vfd->v4l2_dev = &dev->v4l2_dev;
1600 		vfd->queue = &dev->vb_sdr_cap_q;
1601 		vfd->lock = &dev->mutex;
1602 		video_set_drvdata(vfd, dev);
1603 
1604 #ifdef CONFIG_MEDIA_CONTROLLER
1605 		dev->sdr_cap_pad.flags = MEDIA_PAD_FL_SINK;
1606 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->sdr_cap_pad);
1607 		if (ret)
1608 			return ret;
1609 #endif
1610 
1611 		ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1612 		if (ret < 0)
1613 			return ret;
1614 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1615 					  video_device_node_name(vfd));
1616 	}
1617 
1618 	if (dev->has_radio_rx) {
1619 		vfd = &dev->radio_rx_dev;
1620 		snprintf(vfd->name, sizeof(vfd->name),
1621 			 "vivid-%03d-rad-rx", inst);
1622 		vfd->fops = &vivid_radio_fops;
1623 		vfd->ioctl_ops = &vivid_ioctl_ops;
1624 		vfd->device_caps = dev->radio_rx_caps;
1625 		vfd->release = video_device_release_empty;
1626 		vfd->v4l2_dev = &dev->v4l2_dev;
1627 		vfd->lock = &dev->mutex;
1628 		video_set_drvdata(vfd, dev);
1629 
1630 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1631 		if (ret < 0)
1632 			return ret;
1633 		v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1634 					  video_device_node_name(vfd));
1635 	}
1636 
1637 	if (dev->has_radio_tx) {
1638 		vfd = &dev->radio_tx_dev;
1639 		snprintf(vfd->name, sizeof(vfd->name),
1640 			 "vivid-%03d-rad-tx", inst);
1641 		vfd->vfl_dir = VFL_DIR_TX;
1642 		vfd->fops = &vivid_radio_fops;
1643 		vfd->ioctl_ops = &vivid_ioctl_ops;
1644 		vfd->device_caps = dev->radio_tx_caps;
1645 		vfd->release = video_device_release_empty;
1646 		vfd->v4l2_dev = &dev->v4l2_dev;
1647 		vfd->lock = &dev->mutex;
1648 		video_set_drvdata(vfd, dev);
1649 
1650 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1651 		if (ret < 0)
1652 			return ret;
1653 		v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1654 					  video_device_node_name(vfd));
1655 	}
1656 
1657 	if (dev->has_meta_cap) {
1658 		vfd = &dev->meta_cap_dev;
1659 		snprintf(vfd->name, sizeof(vfd->name),
1660 			 "vivid-%03d-meta-cap", inst);
1661 		vfd->fops = &vivid_fops;
1662 		vfd->ioctl_ops = &vivid_ioctl_ops;
1663 		vfd->device_caps = dev->meta_cap_caps;
1664 		vfd->release = video_device_release_empty;
1665 		vfd->v4l2_dev = &dev->v4l2_dev;
1666 		vfd->queue = &dev->vb_meta_cap_q;
1667 		vfd->lock = &dev->mutex;
1668 		vfd->tvnorms = tvnorms_cap;
1669 		video_set_drvdata(vfd, dev);
1670 #ifdef CONFIG_MEDIA_CONTROLLER
1671 		dev->meta_cap_pad.flags = MEDIA_PAD_FL_SINK;
1672 		ret = media_entity_pads_init(&vfd->entity, 1,
1673 					     &dev->meta_cap_pad);
1674 		if (ret)
1675 			return ret;
1676 #endif
1677 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1678 					    meta_cap_nr[inst]);
1679 		if (ret < 0)
1680 			return ret;
1681 		v4l2_info(&dev->v4l2_dev,
1682 			  "V4L2 metadata capture device registered as %s\n",
1683 			  video_device_node_name(vfd));
1684 	}
1685 
1686 	if (dev->has_meta_out) {
1687 		vfd = &dev->meta_out_dev;
1688 		snprintf(vfd->name, sizeof(vfd->name),
1689 			 "vivid-%03d-meta-out", inst);
1690 		vfd->vfl_dir = VFL_DIR_TX;
1691 		vfd->fops = &vivid_fops;
1692 		vfd->ioctl_ops = &vivid_ioctl_ops;
1693 		vfd->device_caps = dev->meta_out_caps;
1694 		vfd->release = video_device_release_empty;
1695 		vfd->v4l2_dev = &dev->v4l2_dev;
1696 		vfd->queue = &dev->vb_meta_out_q;
1697 		vfd->lock = &dev->mutex;
1698 		vfd->tvnorms = tvnorms_out;
1699 		video_set_drvdata(vfd, dev);
1700 #ifdef CONFIG_MEDIA_CONTROLLER
1701 		dev->meta_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1702 		ret = media_entity_pads_init(&vfd->entity, 1,
1703 					     &dev->meta_out_pad);
1704 		if (ret)
1705 			return ret;
1706 #endif
1707 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1708 					    meta_out_nr[inst]);
1709 		if (ret < 0)
1710 			return ret;
1711 		v4l2_info(&dev->v4l2_dev,
1712 			  "V4L2 metadata output device registered as %s\n",
1713 			  video_device_node_name(vfd));
1714 	}
1715 
1716 	if (dev->has_touch_cap) {
1717 		vfd = &dev->touch_cap_dev;
1718 		snprintf(vfd->name, sizeof(vfd->name),
1719 			 "vivid-%03d-touch-cap", inst);
1720 		vfd->fops = &vivid_fops;
1721 		vfd->ioctl_ops = &vivid_ioctl_ops;
1722 		vfd->device_caps = dev->touch_cap_caps;
1723 		vfd->release = video_device_release_empty;
1724 		vfd->v4l2_dev = &dev->v4l2_dev;
1725 		vfd->queue = &dev->vb_touch_cap_q;
1726 		vfd->tvnorms = tvnorms_cap;
1727 		vfd->lock = &dev->mutex;
1728 		video_set_drvdata(vfd, dev);
1729 #ifdef CONFIG_MEDIA_CONTROLLER
1730 		dev->touch_cap_pad.flags = MEDIA_PAD_FL_SINK;
1731 		ret = media_entity_pads_init(&vfd->entity, 1,
1732 					     &dev->touch_cap_pad);
1733 		if (ret)
1734 			return ret;
1735 #endif
1736 		ret = video_register_device(vfd, VFL_TYPE_TOUCH,
1737 					    touch_cap_nr[inst]);
1738 		if (ret < 0)
1739 			return ret;
1740 		v4l2_info(&dev->v4l2_dev,
1741 			  "V4L2 touch capture device registered as %s\n",
1742 			  video_device_node_name(vfd));
1743 	}
1744 
1745 #ifdef CONFIG_MEDIA_CONTROLLER
1746 	/* Register the media device */
1747 	ret = media_device_register(&dev->mdev);
1748 	if (ret) {
1749 		dev_err(dev->mdev.dev,
1750 			"media device register failed (err=%d)\n", ret);
1751 		return ret;
1752 	}
1753 #endif
1754 	return 0;
1755 }
1756 
1757 static void update_hdmi_ctrls_work_handler(struct work_struct *work)
1758 {
1759 	u64 skip_mask;
1760 	u64 update_mask;
1761 
1762 	spin_lock(&hdmi_output_skip_mask_lock);
1763 	skip_mask = hdmi_to_output_menu_skip_mask;
1764 	update_mask = hdmi_input_update_outputs_mask;
1765 	hdmi_input_update_outputs_mask = 0;
1766 	spin_unlock(&hdmi_output_skip_mask_lock);
1767 	for (int i = 0; i < n_devs && vivid_devs[i]; i++) {
1768 		if (update_mask & (1 << i))
1769 			vivid_update_connected_outputs(vivid_devs[i]);
1770 		for (int j = 0; j < vivid_devs[i]->num_hdmi_inputs; j++) {
1771 			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_hdmi_to_output[j];
1772 
1773 			v4l2_ctrl_modify_range(c, c->minimum, c->maximum,
1774 					       skip_mask & ~(1ULL << c->cur.val),
1775 					       c->default_value);
1776 		}
1777 	}
1778 }
1779 
1780 static void update_svid_ctrls_work_handler(struct work_struct *work)
1781 {
1782 	u64 skip_mask;
1783 
1784 	spin_lock(&svid_output_skip_mask_lock);
1785 	skip_mask = svid_to_output_menu_skip_mask;
1786 	spin_unlock(&svid_output_skip_mask_lock);
1787 	for (int i = 0; i < n_devs && vivid_devs[i]; i++) {
1788 		for (int j = 0; j < vivid_devs[i]->num_svid_inputs; j++) {
1789 			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_svid_to_output[j];
1790 
1791 			v4l2_ctrl_modify_range(c, c->minimum, c->maximum,
1792 					       skip_mask & ~(1ULL << c->cur.val),
1793 					       c->default_value);
1794 		}
1795 	}
1796 }
1797 
1798 static int vivid_create_instance(struct platform_device *pdev, int inst)
1799 {
1800 	static const struct v4l2_dv_timings def_dv_timings =
1801 					V4L2_DV_BT_CEA_1280X720P60;
1802 	unsigned in_type_counter[4] = { 0, 0, 0, 0 };
1803 	unsigned out_type_counter[4] = { 0, 0, 0, 0 };
1804 	int ccs_cap = ccs_cap_mode[inst];
1805 	int ccs_out = ccs_out_mode[inst];
1806 	bool has_tuner;
1807 	bool has_modulator;
1808 	struct vivid_dev *dev;
1809 	unsigned node_type = node_types[inst];
1810 	v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
1811 	int ret;
1812 	int i;
1813 
1814 	/* allocate main vivid state structure */
1815 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1816 	if (!dev)
1817 		return -ENOMEM;
1818 
1819 	dev->inst = inst;
1820 
1821 #ifdef CONFIG_MEDIA_CONTROLLER
1822 	dev->v4l2_dev.mdev = &dev->mdev;
1823 
1824 	/* Initialize media device */
1825 	strscpy(dev->mdev.model, VIVID_MODULE_NAME, sizeof(dev->mdev.model));
1826 	snprintf(dev->mdev.bus_info, sizeof(dev->mdev.bus_info),
1827 		 "platform:%s-%03d", VIVID_MODULE_NAME, inst);
1828 	dev->mdev.dev = &pdev->dev;
1829 	media_device_init(&dev->mdev);
1830 	dev->mdev.ops = &vivid_media_ops;
1831 #endif
1832 
1833 	/* register v4l2_device */
1834 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1835 			"%s-%03d", VIVID_MODULE_NAME, inst);
1836 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1837 	if (ret) {
1838 		kfree(dev);
1839 		return ret;
1840 	}
1841 	dev->v4l2_dev.release = vivid_dev_release;
1842 
1843 	ret = vivid_detect_feature_set(dev, inst, node_type,
1844 				       &has_tuner, &has_modulator,
1845 				       &ccs_cap, &ccs_out,
1846 				       in_type_counter, out_type_counter);
1847 	if (ret)
1848 		goto free_dev;
1849 
1850 	vivid_set_capabilities(dev);
1851 
1852 	ret = -ENOMEM;
1853 	/* initialize the test pattern generator */
1854 	tpg_init(&dev->tpg, 640, 360);
1855 	if (tpg_alloc(&dev->tpg, array_size(MAX_WIDTH, MAX_ZOOM)))
1856 		goto free_dev;
1857 	dev->scaled_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1858 	if (!dev->scaled_line)
1859 		goto free_dev;
1860 	dev->blended_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1861 	if (!dev->blended_line)
1862 		goto free_dev;
1863 
1864 	/* load the edid */
1865 	dev->edid = vmalloc(array_size(256, 128));
1866 	if (!dev->edid)
1867 		goto free_dev;
1868 
1869 	ret = vivid_init_dv_timings(dev);
1870 	if (ret < 0)
1871 		goto free_dev;
1872 
1873 	vivid_disable_unused_ioctls(dev, has_tuner, has_modulator,
1874 				    in_type_counter, out_type_counter);
1875 
1876 	/* configure internal data */
1877 	dev->fmt_cap = &vivid_formats[0];
1878 	dev->fmt_out = &vivid_formats[0];
1879 	if (!dev->multiplanar)
1880 		vivid_formats[0].data_offset[0] = 0;
1881 	dev->webcam_size_idx = 1;
1882 	dev->webcam_ival_idx = 3;
1883 	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
1884 	dev->std_out = V4L2_STD_PAL;
1885 	if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
1886 		tvnorms_cap = V4L2_STD_ALL;
1887 	if (dev->output_type[0] == SVID)
1888 		tvnorms_out = V4L2_STD_ALL;
1889 	for (i = 0; i < MAX_INPUTS; i++) {
1890 		dev->dv_timings_cap[i] = def_dv_timings;
1891 		dev->std_cap[i] = V4L2_STD_PAL;
1892 	}
1893 	dev->dv_timings_out = def_dv_timings;
1894 	dev->tv_freq = 2804 /* 175.25 * 16 */;
1895 	dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
1896 	dev->tv_field_cap = V4L2_FIELD_INTERLACED;
1897 	dev->tv_field_out = V4L2_FIELD_INTERLACED;
1898 	dev->radio_rx_freq = 95000 * 16;
1899 	dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
1900 	if (dev->has_radio_tx) {
1901 		dev->radio_tx_freq = 95500 * 16;
1902 		dev->radio_rds_loop = false;
1903 	}
1904 	dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
1905 	dev->sdr_adc_freq = 300000;
1906 	dev->sdr_fm_freq = 50000000;
1907 	dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
1908 	dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
1909 
1910 	dev->edid_max_blocks = dev->edid_blocks = 2;
1911 	memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
1912 	dev->radio_rds_init_time = ktime_get();
1913 	INIT_WORK(&dev->update_hdmi_ctrl_work, update_hdmi_ctrls_work_handler);
1914 	INIT_WORK(&dev->update_svid_ctrl_work, update_svid_ctrls_work_handler);
1915 	for (int j = 0, k = 0; j < dev->num_inputs; ++j)
1916 		if (dev->input_type[j] == HDMI)
1917 			dev->hdmi_index_to_input_index[k++] = j;
1918 	for (int j = 0, k = 0; j < dev->num_outputs; ++j)
1919 		if (dev->output_type[j] == HDMI) {
1920 			dev->output_to_iface_index[j] = k;
1921 			dev->hdmi_index_to_output_index[k++] = j;
1922 		}
1923 	for (int j = 0, k = 0; j < dev->num_inputs; ++j)
1924 		if (dev->input_type[j] == SVID)
1925 			dev->svid_index_to_input_index[k++] = j;
1926 	for (int j = 0, k = 0; j < dev->num_outputs; ++j)
1927 		if (dev->output_type[j] == SVID)
1928 			dev->output_to_iface_index[j] = k++;
1929 
1930 	/* create all controls */
1931 	ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
1932 			in_type_counter[TV] || in_type_counter[SVID] ||
1933 			out_type_counter[SVID],
1934 			in_type_counter[HDMI] || out_type_counter[HDMI]);
1935 	if (ret)
1936 		goto unreg_dev;
1937 
1938 	/* enable/disable interface specific controls */
1939 	if (dev->num_inputs && dev->input_type[0] != HDMI) {
1940 		v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode, false);
1941 		v4l2_ctrl_activate(dev->ctrl_dv_timings, false);
1942 	} else if (dev->num_inputs && dev->input_type[0] == HDMI) {
1943 		v4l2_ctrl_activate(dev->ctrl_std_signal_mode, false);
1944 		v4l2_ctrl_activate(dev->ctrl_standard, false);
1945 	}
1946 
1947 	/*
1948 	 * update the capture and output formats to do a proper initial
1949 	 * configuration.
1950 	 */
1951 	vivid_update_format_cap(dev, false);
1952 	vivid_update_format_out(dev);
1953 
1954 	/* update touch configuration */
1955 	dev->timeperframe_tch_cap.numerator = 1;
1956 	dev->timeperframe_tch_cap.denominator = 10;
1957 	vivid_set_touch(dev, 0);
1958 
1959 	/* initialize locks */
1960 	spin_lock_init(&dev->slock);
1961 	mutex_init(&dev->mutex);
1962 
1963 	/* init dma queues */
1964 	INIT_LIST_HEAD(&dev->vid_cap_active);
1965 	INIT_LIST_HEAD(&dev->vid_out_active);
1966 	INIT_LIST_HEAD(&dev->vbi_cap_active);
1967 	INIT_LIST_HEAD(&dev->vbi_out_active);
1968 	INIT_LIST_HEAD(&dev->sdr_cap_active);
1969 	INIT_LIST_HEAD(&dev->meta_cap_active);
1970 	INIT_LIST_HEAD(&dev->meta_out_active);
1971 	INIT_LIST_HEAD(&dev->touch_cap_active);
1972 
1973 	spin_lock_init(&dev->cec_xfers_slock);
1974 
1975 	if (allocators[inst] == 1)
1976 		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1977 
1978 	ret = vivid_create_queues(dev);
1979 	if (ret)
1980 		goto unreg_dev;
1981 
1982 #ifdef CONFIG_VIDEO_VIVID_CEC
1983 	if (dev->has_vid_cap && in_type_counter[HDMI]) {
1984 		struct cec_adapter *adap;
1985 
1986 		adap = vivid_cec_alloc_adap(dev, 0, false);
1987 		ret = PTR_ERR_OR_ZERO(adap);
1988 		if (ret < 0)
1989 			goto unreg_dev;
1990 		dev->cec_rx_adap = adap;
1991 	}
1992 
1993 	if (dev->has_vid_out) {
1994 		int j;
1995 
1996 		for (i = j = 0; i < dev->num_outputs; i++) {
1997 			struct cec_adapter *adap;
1998 
1999 			if (dev->output_type[i] != HDMI)
2000 				continue;
2001 
2002 			adap = vivid_cec_alloc_adap(dev, j, true);
2003 			ret = PTR_ERR_OR_ZERO(adap);
2004 			if (ret < 0) {
2005 				while (j--)
2006 					cec_delete_adapter(dev->cec_tx_adap[j]);
2007 				goto unreg_dev;
2008 			}
2009 
2010 			dev->cec_tx_adap[j++] = adap;
2011 		}
2012 	}
2013 
2014 	if (dev->cec_rx_adap || dev->num_hdmi_outputs) {
2015 		init_waitqueue_head(&dev->kthread_waitq_cec);
2016 		dev->kthread_cec = kthread_run(vivid_cec_bus_thread, dev,
2017 					       "vivid_cec-%s", dev->v4l2_dev.name);
2018 		if (IS_ERR(dev->kthread_cec)) {
2019 			ret = PTR_ERR(dev->kthread_cec);
2020 			dev->kthread_cec = NULL;
2021 			v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
2022 			goto unreg_dev;
2023 		}
2024 	}
2025 
2026 #endif
2027 
2028 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
2029 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
2030 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
2031 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
2032 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
2033 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
2034 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
2035 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_cap);
2036 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_out);
2037 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_touch_cap);
2038 
2039 	/* finally start creating the device nodes */
2040 	ret = vivid_create_devnodes(pdev, dev, inst,
2041 				    tvnorms_cap, tvnorms_out,
2042 				    in_type_counter, out_type_counter);
2043 	if (ret)
2044 		goto unreg_dev;
2045 
2046 	/* Now that everything is fine, let's add it to device list */
2047 	vivid_devs[inst] = dev;
2048 
2049 	return 0;
2050 
2051 unreg_dev:
2052 	vb2_video_unregister_device(&dev->touch_cap_dev);
2053 	vb2_video_unregister_device(&dev->meta_out_dev);
2054 	vb2_video_unregister_device(&dev->meta_cap_dev);
2055 	video_unregister_device(&dev->radio_tx_dev);
2056 	video_unregister_device(&dev->radio_rx_dev);
2057 	vb2_video_unregister_device(&dev->sdr_cap_dev);
2058 	vb2_video_unregister_device(&dev->vbi_out_dev);
2059 	vb2_video_unregister_device(&dev->vbi_cap_dev);
2060 	vb2_video_unregister_device(&dev->vid_out_dev);
2061 	vb2_video_unregister_device(&dev->vid_cap_dev);
2062 	cec_unregister_adapter(dev->cec_rx_adap);
2063 	for (i = 0; i < MAX_HDMI_OUTPUTS; i++)
2064 		cec_unregister_adapter(dev->cec_tx_adap[i]);
2065 	if (dev->kthread_cec)
2066 		kthread_stop(dev->kthread_cec);
2067 free_dev:
2068 	v4l2_device_put(&dev->v4l2_dev);
2069 	return ret;
2070 }
2071 
2072 /* This routine allocates from 1 to n_devs virtual drivers.
2073 
2074    The real maximum number of virtual drivers will depend on how many drivers
2075    will succeed. This is limited to the maximum number of devices that
2076    videodev supports, which is equal to VIDEO_NUM_DEVICES.
2077  */
2078 static int vivid_probe(struct platform_device *pdev)
2079 {
2080 	const struct font_desc *font = find_font("VGA8x16");
2081 	int ret = 0, i;
2082 
2083 	if (font == NULL) {
2084 		pr_err("vivid: could not find font\n");
2085 		return -ENODEV;
2086 	}
2087 
2088 	tpg_set_font(font->data);
2089 
2090 	n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
2091 
2092 	for (i = 0; i < n_devs; i++) {
2093 		ret = vivid_create_instance(pdev, i);
2094 		if (ret) {
2095 			/* If some instantiations succeeded, keep driver */
2096 			if (i)
2097 				ret = 0;
2098 			break;
2099 		}
2100 	}
2101 
2102 	if (ret < 0) {
2103 		pr_err("vivid: error %d while loading driver\n", ret);
2104 		return ret;
2105 	}
2106 
2107 	/* n_devs will reflect the actual number of allocated devices */
2108 	n_devs = i;
2109 
2110 	/* Determine qmenu items actually in use */
2111 	int hdmi_count = FIXED_MENU_ITEMS;
2112 	int svid_count = FIXED_MENU_ITEMS;
2113 
2114 	for (int i = 0; i < n_devs; i++) {
2115 		struct vivid_dev *dev = vivid_devs[i];
2116 
2117 		if (!dev->has_vid_out)
2118 			continue;
2119 		for (int j = 0; j < dev->num_outputs && hdmi_count < MAX_MENU_ITEMS; ++j) {
2120 			if (dev->output_type[j] == HDMI) {
2121 				vivid_ctrl_hdmi_to_output_instance[hdmi_count] = vivid_devs[i];
2122 				vivid_ctrl_hdmi_to_output_index[hdmi_count++] = j;
2123 			}
2124 		}
2125 		for (int j = 0; j < dev->num_outputs && svid_count < MAX_MENU_ITEMS; ++j) {
2126 			if (dev->output_type[j] == SVID) {
2127 				vivid_ctrl_svid_to_output_instance[svid_count] = vivid_devs[i];
2128 				vivid_ctrl_svid_to_output_index[svid_count++] = j;
2129 			}
2130 		}
2131 	}
2132 	hdmi_count = min(hdmi_count, MAX_MENU_ITEMS);
2133 	svid_count = min(svid_count, MAX_MENU_ITEMS);
2134 	for (int i = 0; i < n_devs; i++) {
2135 		for (int j = 0; j < vivid_devs[i]->num_hdmi_inputs; j++) {
2136 			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_hdmi_to_output[j];
2137 
2138 			v4l2_ctrl_modify_range(c, c->minimum, hdmi_count - 1, 0, c->default_value);
2139 		}
2140 		for (int j = 0; j < vivid_devs[i]->num_svid_inputs; j++) {
2141 			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_svid_to_output[j];
2142 
2143 			v4l2_ctrl_modify_range(c, c->minimum, svid_count - 1, 0, c->default_value);
2144 		}
2145 	}
2146 	return ret;
2147 }
2148 
2149 static void vivid_remove(struct platform_device *pdev)
2150 {
2151 	struct vivid_dev *dev;
2152 	unsigned int i, j;
2153 
2154 	for (i = 0; i < n_devs; i++) {
2155 		dev = vivid_devs[i];
2156 		if (!dev)
2157 			continue;
2158 
2159 		if (dev->disconnect_error)
2160 			vivid_reconnect(dev);
2161 #ifdef CONFIG_MEDIA_CONTROLLER
2162 		media_device_unregister(&dev->mdev);
2163 #endif
2164 
2165 		if (dev->has_vid_cap) {
2166 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2167 				video_device_node_name(&dev->vid_cap_dev));
2168 			vb2_video_unregister_device(&dev->vid_cap_dev);
2169 		}
2170 		if (dev->has_vid_out) {
2171 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2172 				video_device_node_name(&dev->vid_out_dev));
2173 			vb2_video_unregister_device(&dev->vid_out_dev);
2174 		}
2175 		if (dev->has_vbi_cap) {
2176 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2177 				video_device_node_name(&dev->vbi_cap_dev));
2178 			vb2_video_unregister_device(&dev->vbi_cap_dev);
2179 		}
2180 		if (dev->has_vbi_out) {
2181 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2182 				video_device_node_name(&dev->vbi_out_dev));
2183 			vb2_video_unregister_device(&dev->vbi_out_dev);
2184 		}
2185 		if (dev->has_sdr_cap) {
2186 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2187 				video_device_node_name(&dev->sdr_cap_dev));
2188 			vb2_video_unregister_device(&dev->sdr_cap_dev);
2189 		}
2190 		if (dev->has_radio_rx) {
2191 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2192 				video_device_node_name(&dev->radio_rx_dev));
2193 			video_unregister_device(&dev->radio_rx_dev);
2194 		}
2195 		if (dev->has_radio_tx) {
2196 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2197 				video_device_node_name(&dev->radio_tx_dev));
2198 			video_unregister_device(&dev->radio_tx_dev);
2199 		}
2200 		if (dev->has_fb) {
2201 			v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
2202 				dev->fb_info.node);
2203 			unregister_framebuffer(&dev->fb_info);
2204 			vivid_fb_release_buffers(dev);
2205 		}
2206 		if (dev->has_meta_cap) {
2207 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2208 				  video_device_node_name(&dev->meta_cap_dev));
2209 			vb2_video_unregister_device(&dev->meta_cap_dev);
2210 		}
2211 		if (dev->has_meta_out) {
2212 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2213 				  video_device_node_name(&dev->meta_out_dev));
2214 			vb2_video_unregister_device(&dev->meta_out_dev);
2215 		}
2216 		if (dev->has_touch_cap) {
2217 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2218 				  video_device_node_name(&dev->touch_cap_dev));
2219 			vb2_video_unregister_device(&dev->touch_cap_dev);
2220 		}
2221 		cec_unregister_adapter(dev->cec_rx_adap);
2222 		for (j = 0; j < MAX_HDMI_OUTPUTS; j++)
2223 			cec_unregister_adapter(dev->cec_tx_adap[j]);
2224 		if (dev->kthread_cec)
2225 			kthread_stop(dev->kthread_cec);
2226 		v4l2_device_put(&dev->v4l2_dev);
2227 		vivid_devs[i] = NULL;
2228 	}
2229 }
2230 
2231 static void vivid_pdev_release(struct device *dev)
2232 {
2233 }
2234 
2235 static struct platform_device vivid_pdev = {
2236 	.name		= "vivid",
2237 	.dev.release	= vivid_pdev_release,
2238 };
2239 
2240 static struct platform_driver vivid_pdrv = {
2241 	.probe		= vivid_probe,
2242 	.remove_new	= vivid_remove,
2243 	.driver		= {
2244 		.name	= "vivid",
2245 	},
2246 };
2247 
2248 static int __init vivid_init(void)
2249 {
2250 	int hdmi_count = FIXED_MENU_ITEMS;
2251 	int svid_count = FIXED_MENU_ITEMS;
2252 	int ret = -ENOMEM;
2253 	unsigned int ndevs;
2254 
2255 	/* Sanity check, prevent insane number of vivid instances */
2256 	if (n_devs > 64)
2257 		n_devs = 64;
2258 	ndevs = clamp_t(unsigned int, n_devs, 1, VIVID_MAX_DEVS);
2259 
2260 	for (unsigned int i = 0; i < ndevs; i++) {
2261 		if (!(node_types[i] & (1 << 8)))
2262 			continue;
2263 		unsigned int n_outputs = min(num_outputs[i], MAX_OUTPUTS);
2264 
2265 		for (u8 j = 0, k = 0; j < n_outputs && hdmi_count < MAX_MENU_ITEMS &&
2266 		     k < MAX_HDMI_OUTPUTS; ++j) {
2267 			if (output_types[i] & BIT(j)) {
2268 				vivid_ctrl_hdmi_to_output_strings[hdmi_count] =
2269 					kmalloc(MAX_STRING_LENGTH, GFP_KERNEL);
2270 				if (!vivid_ctrl_hdmi_to_output_strings[hdmi_count])
2271 					goto free_output_strings;
2272 				snprintf(vivid_ctrl_hdmi_to_output_strings[hdmi_count],
2273 					 MAX_STRING_LENGTH, "Output HDMI %03d-%d",
2274 					 i & 0xff, k);
2275 				k++;
2276 				hdmi_count++;
2277 			}
2278 		}
2279 		for (u8 j = 0, k = 0; j < n_outputs && svid_count < MAX_MENU_ITEMS; ++j) {
2280 			if (!(output_types[i] & BIT(j))) {
2281 				vivid_ctrl_svid_to_output_strings[svid_count] =
2282 					kmalloc(MAX_STRING_LENGTH, GFP_KERNEL);
2283 				if (!vivid_ctrl_svid_to_output_strings[svid_count])
2284 					goto free_output_strings;
2285 				snprintf(vivid_ctrl_svid_to_output_strings[svid_count],
2286 					 MAX_STRING_LENGTH, "Output S-Video %03d-%d",
2287 					 i & 0xff, k);
2288 				k++;
2289 				svid_count++;
2290 			}
2291 		}
2292 	}
2293 	ret = platform_device_register(&vivid_pdev);
2294 	if (ret)
2295 		goto free_output_strings;
2296 	ret = platform_driver_register(&vivid_pdrv);
2297 	if (ret)
2298 		goto unreg_device;
2299 
2300 	/* Initialize workqueue before module is loaded */
2301 	update_hdmi_ctrls_workqueue = create_workqueue("update_hdmi_ctrls_wq");
2302 	if (!update_hdmi_ctrls_workqueue) {
2303 		ret = -ENOMEM;
2304 		goto unreg_driver;
2305 	}
2306 	update_svid_ctrls_workqueue = create_workqueue("update_svid_ctrls_wq");
2307 	if (!update_svid_ctrls_workqueue) {
2308 		ret = -ENOMEM;
2309 		goto destroy_hdmi_wq;
2310 	}
2311 	return ret;
2312 
2313 destroy_hdmi_wq:
2314 	destroy_workqueue(update_hdmi_ctrls_workqueue);
2315 unreg_driver:
2316 	platform_driver_register(&vivid_pdrv);
2317 unreg_device:
2318 	platform_device_unregister(&vivid_pdev);
2319 free_output_strings:
2320 	for (int i = FIXED_MENU_ITEMS; i < MAX_MENU_ITEMS; i++) {
2321 		kfree(vivid_ctrl_hdmi_to_output_strings[i]);
2322 		kfree(vivid_ctrl_svid_to_output_strings[i]);
2323 	}
2324 	return ret;
2325 }
2326 
2327 static void __exit vivid_exit(void)
2328 {
2329 	for (int i = FIXED_MENU_ITEMS; i < MAX_MENU_ITEMS; i++) {
2330 		kfree(vivid_ctrl_hdmi_to_output_strings[i]);
2331 		kfree(vivid_ctrl_svid_to_output_strings[i]);
2332 	}
2333 	destroy_workqueue(update_svid_ctrls_workqueue);
2334 	destroy_workqueue(update_hdmi_ctrls_workqueue);
2335 	platform_driver_unregister(&vivid_pdrv);
2336 	platform_device_unregister(&vivid_pdev);
2337 }
2338 
2339 module_init(vivid_init);
2340 module_exit(vivid_exit);
2341