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