xref: /linux/drivers/media/test-drivers/vivid/vivid-core.c (revision eed4edda910fe34dfae8c6bfbcf57f4593a54295)
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 output 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-%03d", VIVID_MODULE_NAME, dev->inst);
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 -ENOTTY;
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 -ENOTTY;
339 	return vivid_vid_out_g_fbuf(file, fh, a);
340 }
341 
342 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
343 {
344 	struct video_device *vdev = video_devdata(file);
345 
346 	if (vdev->vfl_dir == VFL_DIR_RX)
347 		return -ENOTTY;
348 	return vivid_vid_out_s_fbuf(file, fh, a);
349 }
350 
351 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
352 {
353 	struct video_device *vdev = video_devdata(file);
354 
355 	if (vdev->vfl_dir == VFL_DIR_RX)
356 		return vivid_vid_cap_s_std(file, fh, id);
357 	return vivid_vid_out_s_std(file, fh, id);
358 }
359 
360 static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
361 {
362 	struct video_device *vdev = video_devdata(file);
363 
364 	if (vdev->vfl_dir == VFL_DIR_RX)
365 		return vivid_vid_cap_s_dv_timings(file, fh, timings);
366 	return vivid_vid_out_s_dv_timings(file, fh, timings);
367 }
368 
369 static int vidioc_g_pixelaspect(struct file *file, void *fh,
370 				int type, struct v4l2_fract *f)
371 {
372 	struct video_device *vdev = video_devdata(file);
373 
374 	if (vdev->vfl_dir == VFL_DIR_RX)
375 		return vivid_vid_cap_g_pixelaspect(file, fh, type, f);
376 	return vivid_vid_out_g_pixelaspect(file, fh, type, f);
377 }
378 
379 static int vidioc_g_selection(struct file *file, void *fh,
380 			      struct v4l2_selection *sel)
381 {
382 	struct video_device *vdev = video_devdata(file);
383 
384 	if (vdev->vfl_dir == VFL_DIR_RX)
385 		return vivid_vid_cap_g_selection(file, fh, sel);
386 	return vivid_vid_out_g_selection(file, fh, sel);
387 }
388 
389 static int vidioc_s_selection(struct file *file, void *fh,
390 			      struct v4l2_selection *sel)
391 {
392 	struct video_device *vdev = video_devdata(file);
393 
394 	if (vdev->vfl_dir == VFL_DIR_RX)
395 		return vivid_vid_cap_s_selection(file, fh, sel);
396 	return vivid_vid_out_s_selection(file, fh, sel);
397 }
398 
399 static int vidioc_g_parm(struct file *file, void *fh,
400 			  struct v4l2_streamparm *parm)
401 {
402 	struct video_device *vdev = video_devdata(file);
403 
404 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
405 		return vivid_g_parm_tch(file, fh, parm);
406 	if (vdev->vfl_dir == VFL_DIR_RX)
407 		return vivid_vid_cap_g_parm(file, fh, parm);
408 	return vivid_vid_out_g_parm(file, fh, parm);
409 }
410 
411 static int vidioc_s_parm(struct file *file, void *fh,
412 			  struct v4l2_streamparm *parm)
413 {
414 	struct video_device *vdev = video_devdata(file);
415 
416 	if (vdev->vfl_dir == VFL_DIR_RX)
417 		return vivid_vid_cap_s_parm(file, fh, parm);
418 	return -ENOTTY;
419 }
420 
421 static int vidioc_log_status(struct file *file, void *fh)
422 {
423 	struct vivid_dev *dev = video_drvdata(file);
424 	struct video_device *vdev = video_devdata(file);
425 
426 	v4l2_ctrl_log_status(file, fh);
427 	if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_VIDEO)
428 		tpg_log_status(&dev->tpg);
429 	return 0;
430 }
431 
432 static ssize_t vivid_radio_read(struct file *file, char __user *buf,
433 			 size_t size, loff_t *offset)
434 {
435 	struct video_device *vdev = video_devdata(file);
436 
437 	if (vdev->vfl_dir == VFL_DIR_TX)
438 		return -EINVAL;
439 	return vivid_radio_rx_read(file, buf, size, offset);
440 }
441 
442 static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
443 			  size_t size, loff_t *offset)
444 {
445 	struct video_device *vdev = video_devdata(file);
446 
447 	if (vdev->vfl_dir == VFL_DIR_RX)
448 		return -EINVAL;
449 	return vivid_radio_tx_write(file, buf, size, offset);
450 }
451 
452 static __poll_t vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
453 {
454 	struct video_device *vdev = video_devdata(file);
455 
456 	if (vdev->vfl_dir == VFL_DIR_RX)
457 		return vivid_radio_rx_poll(file, wait);
458 	return vivid_radio_tx_poll(file, wait);
459 }
460 
461 static int vivid_enum_input(struct file *file, void *priv,
462 			    struct v4l2_input *inp)
463 {
464 	struct video_device *vdev = video_devdata(file);
465 
466 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
467 		return vivid_enum_input_tch(file, priv, inp);
468 	return vidioc_enum_input(file, priv, inp);
469 }
470 
471 static int vivid_g_input(struct file *file, void *priv, unsigned int *i)
472 {
473 	struct video_device *vdev = video_devdata(file);
474 
475 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
476 		return vivid_g_input_tch(file, priv, i);
477 	return vidioc_g_input(file, priv, i);
478 }
479 
480 static int vivid_s_input(struct file *file, void *priv, unsigned int i)
481 {
482 	struct video_device *vdev = video_devdata(file);
483 
484 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
485 		return vivid_s_input_tch(file, priv, i);
486 	return vidioc_s_input(file, priv, i);
487 }
488 
489 static int vivid_enum_fmt_cap(struct file *file, void  *priv,
490 			      struct v4l2_fmtdesc *f)
491 {
492 	struct video_device *vdev = video_devdata(file);
493 
494 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
495 		return vivid_enum_fmt_tch(file, priv, f);
496 	return vivid_enum_fmt_vid(file, priv, f);
497 }
498 
499 static int vivid_g_fmt_cap(struct file *file, void *priv,
500 			   struct v4l2_format *f)
501 {
502 	struct video_device *vdev = video_devdata(file);
503 
504 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
505 		return vivid_g_fmt_tch(file, priv, f);
506 	return vidioc_g_fmt_vid_cap(file, priv, f);
507 }
508 
509 static int vivid_try_fmt_cap(struct file *file, void *priv,
510 			     struct v4l2_format *f)
511 {
512 	struct video_device *vdev = video_devdata(file);
513 
514 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
515 		return vivid_g_fmt_tch(file, priv, f);
516 	return vidioc_try_fmt_vid_cap(file, priv, f);
517 }
518 
519 static int vivid_s_fmt_cap(struct file *file, void *priv,
520 			   struct v4l2_format *f)
521 {
522 	struct video_device *vdev = video_devdata(file);
523 
524 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
525 		return vivid_g_fmt_tch(file, priv, f);
526 	return vidioc_s_fmt_vid_cap(file, priv, f);
527 }
528 
529 static int vivid_g_fmt_cap_mplane(struct file *file, void *priv,
530 				  struct v4l2_format *f)
531 {
532 	struct video_device *vdev = video_devdata(file);
533 
534 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
535 		return vivid_g_fmt_tch_mplane(file, priv, f);
536 	return vidioc_g_fmt_vid_cap_mplane(file, priv, f);
537 }
538 
539 static int vivid_try_fmt_cap_mplane(struct file *file, void *priv,
540 				    struct v4l2_format *f)
541 {
542 	struct video_device *vdev = video_devdata(file);
543 
544 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
545 		return vivid_g_fmt_tch_mplane(file, priv, f);
546 	return vidioc_try_fmt_vid_cap_mplane(file, priv, f);
547 }
548 
549 static int vivid_s_fmt_cap_mplane(struct file *file, void *priv,
550 				  struct v4l2_format *f)
551 {
552 	struct video_device *vdev = video_devdata(file);
553 
554 	if (vdev->vfl_type == VFL_TYPE_TOUCH)
555 		return vivid_g_fmt_tch_mplane(file, priv, f);
556 	return vidioc_s_fmt_vid_cap_mplane(file, priv, f);
557 }
558 
559 static bool vivid_is_in_use(bool valid, struct video_device *vdev)
560 {
561 	unsigned long flags;
562 	bool res;
563 
564 	if (!valid)
565 		return false;
566 	spin_lock_irqsave(&vdev->fh_lock, flags);
567 	res = !list_empty(&vdev->fh_list);
568 	spin_unlock_irqrestore(&vdev->fh_lock, flags);
569 	return res;
570 }
571 
572 static bool vivid_is_last_user(struct vivid_dev *dev)
573 {
574 	unsigned int uses =
575 		vivid_is_in_use(dev->has_vid_cap, &dev->vid_cap_dev) +
576 		vivid_is_in_use(dev->has_vid_out, &dev->vid_out_dev) +
577 		vivid_is_in_use(dev->has_vbi_cap, &dev->vbi_cap_dev) +
578 		vivid_is_in_use(dev->has_vbi_out, &dev->vbi_out_dev) +
579 		vivid_is_in_use(dev->has_radio_rx, &dev->radio_rx_dev) +
580 		vivid_is_in_use(dev->has_radio_tx, &dev->radio_tx_dev) +
581 		vivid_is_in_use(dev->has_sdr_cap, &dev->sdr_cap_dev) +
582 		vivid_is_in_use(dev->has_meta_cap, &dev->meta_cap_dev) +
583 		vivid_is_in_use(dev->has_meta_out, &dev->meta_out_dev) +
584 		vivid_is_in_use(dev->has_touch_cap, &dev->touch_cap_dev);
585 
586 	return uses == 1;
587 }
588 
589 static void vivid_reconnect(struct vivid_dev *dev)
590 {
591 	if (dev->has_vid_cap)
592 		set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
593 	if (dev->has_vid_out)
594 		set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
595 	if (dev->has_vbi_cap)
596 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
597 	if (dev->has_vbi_out)
598 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
599 	if (dev->has_radio_rx)
600 		set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
601 	if (dev->has_radio_tx)
602 		set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
603 	if (dev->has_sdr_cap)
604 		set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
605 	if (dev->has_meta_cap)
606 		set_bit(V4L2_FL_REGISTERED, &dev->meta_cap_dev.flags);
607 	if (dev->has_meta_out)
608 		set_bit(V4L2_FL_REGISTERED, &dev->meta_out_dev.flags);
609 	if (dev->has_touch_cap)
610 		set_bit(V4L2_FL_REGISTERED, &dev->touch_cap_dev.flags);
611 	dev->disconnect_error = false;
612 }
613 
614 static int vivid_fop_release(struct file *file)
615 {
616 	struct vivid_dev *dev = video_drvdata(file);
617 	struct video_device *vdev = video_devdata(file);
618 
619 	mutex_lock(&dev->mutex);
620 	if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
621 	    dev->disconnect_error && !video_is_registered(vdev) &&
622 	    vivid_is_last_user(dev)) {
623 		/*
624 		 * I am the last user of this driver, and a disconnect
625 		 * was forced (since this video_device is unregistered),
626 		 * so re-register all video_device's again.
627 		 */
628 		v4l2_info(&dev->v4l2_dev, "reconnect\n");
629 		vivid_reconnect(dev);
630 	}
631 	if (file->private_data == dev->radio_rx_rds_owner) {
632 		dev->radio_rx_rds_last_block = 0;
633 		dev->radio_rx_rds_owner = NULL;
634 	}
635 	if (file->private_data == dev->radio_tx_rds_owner) {
636 		dev->radio_tx_rds_last_block = 0;
637 		dev->radio_tx_rds_owner = NULL;
638 	}
639 	mutex_unlock(&dev->mutex);
640 	if (vdev->queue)
641 		return vb2_fop_release(file);
642 	return v4l2_fh_release(file);
643 }
644 
645 static const struct v4l2_file_operations vivid_fops = {
646 	.owner		= THIS_MODULE,
647 	.open           = v4l2_fh_open,
648 	.release        = vivid_fop_release,
649 	.read           = vb2_fop_read,
650 	.write          = vb2_fop_write,
651 	.poll		= vb2_fop_poll,
652 	.unlocked_ioctl = video_ioctl2,
653 	.mmap           = vb2_fop_mmap,
654 };
655 
656 static const struct v4l2_file_operations vivid_radio_fops = {
657 	.owner		= THIS_MODULE,
658 	.open           = v4l2_fh_open,
659 	.release        = vivid_fop_release,
660 	.read           = vivid_radio_read,
661 	.write          = vivid_radio_write,
662 	.poll		= vivid_radio_poll,
663 	.unlocked_ioctl = video_ioctl2,
664 };
665 
666 static int vidioc_reqbufs(struct file *file, void *priv,
667 			  struct v4l2_requestbuffers *p)
668 {
669 	struct video_device *vdev = video_devdata(file);
670 	int r;
671 
672 	/*
673 	 * Sliced and raw VBI capture share the same queue so we must
674 	 * change the type.
675 	 */
676 	if (p->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
677 	    p->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
678 		r = vb2_queue_change_type(vdev->queue, p->type);
679 		if (r)
680 			return r;
681 	}
682 
683 	return vb2_ioctl_reqbufs(file, priv, p);
684 }
685 
686 static int vidioc_create_bufs(struct file *file, void *priv,
687 			      struct v4l2_create_buffers *p)
688 {
689 	struct video_device *vdev = video_devdata(file);
690 	int r;
691 
692 	/*
693 	 * Sliced and raw VBI capture share the same queue so we must
694 	 * change the type.
695 	 */
696 	if (p->format.type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
697 	    p->format.type == V4L2_BUF_TYPE_VBI_CAPTURE) {
698 		r = vb2_queue_change_type(vdev->queue, p->format.type);
699 		if (r)
700 			return r;
701 	}
702 
703 	return vb2_ioctl_create_bufs(file, priv, p);
704 }
705 
706 static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
707 	.vidioc_querycap		= vidioc_querycap,
708 
709 	.vidioc_enum_fmt_vid_cap	= vivid_enum_fmt_cap,
710 	.vidioc_g_fmt_vid_cap		= vivid_g_fmt_cap,
711 	.vidioc_try_fmt_vid_cap		= vivid_try_fmt_cap,
712 	.vidioc_s_fmt_vid_cap		= vivid_s_fmt_cap,
713 	.vidioc_g_fmt_vid_cap_mplane	= vivid_g_fmt_cap_mplane,
714 	.vidioc_try_fmt_vid_cap_mplane	= vivid_try_fmt_cap_mplane,
715 	.vidioc_s_fmt_vid_cap_mplane	= vivid_s_fmt_cap_mplane,
716 
717 	.vidioc_enum_fmt_vid_out	= vivid_enum_fmt_vid,
718 	.vidioc_g_fmt_vid_out		= vidioc_g_fmt_vid_out,
719 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt_vid_out,
720 	.vidioc_s_fmt_vid_out		= vidioc_s_fmt_vid_out,
721 	.vidioc_g_fmt_vid_out_mplane	= vidioc_g_fmt_vid_out_mplane,
722 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
723 	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out_mplane,
724 
725 	.vidioc_g_selection		= vidioc_g_selection,
726 	.vidioc_s_selection		= vidioc_s_selection,
727 	.vidioc_g_pixelaspect		= vidioc_g_pixelaspect,
728 
729 	.vidioc_g_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
730 	.vidioc_try_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
731 	.vidioc_s_fmt_vbi_cap		= vidioc_s_fmt_vbi_cap,
732 
733 	.vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
734 	.vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
735 	.vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
736 	.vidioc_g_sliced_vbi_cap	= vidioc_g_sliced_vbi_cap,
737 
738 	.vidioc_g_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
739 	.vidioc_try_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
740 	.vidioc_s_fmt_vbi_out		= vidioc_s_fmt_vbi_out,
741 
742 	.vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
743 	.vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
744 	.vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
745 
746 	.vidioc_enum_fmt_sdr_cap	= vidioc_enum_fmt_sdr_cap,
747 	.vidioc_g_fmt_sdr_cap		= vidioc_g_fmt_sdr_cap,
748 	.vidioc_try_fmt_sdr_cap		= vidioc_try_fmt_sdr_cap,
749 	.vidioc_s_fmt_sdr_cap		= vidioc_s_fmt_sdr_cap,
750 
751 	.vidioc_overlay			= vidioc_overlay,
752 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
753 	.vidioc_enum_frameintervals	= vidioc_enum_frameintervals,
754 	.vidioc_g_parm			= vidioc_g_parm,
755 	.vidioc_s_parm			= vidioc_s_parm,
756 
757 	.vidioc_g_fmt_vid_out_overlay	= vidioc_g_fmt_vid_out_overlay,
758 	.vidioc_try_fmt_vid_out_overlay	= vidioc_try_fmt_vid_out_overlay,
759 	.vidioc_s_fmt_vid_out_overlay	= vidioc_s_fmt_vid_out_overlay,
760 	.vidioc_g_fbuf			= vidioc_g_fbuf,
761 	.vidioc_s_fbuf			= vidioc_s_fbuf,
762 
763 	.vidioc_reqbufs			= vidioc_reqbufs,
764 	.vidioc_create_bufs		= vidioc_create_bufs,
765 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
766 	.vidioc_querybuf		= vb2_ioctl_querybuf,
767 	.vidioc_qbuf			= vb2_ioctl_qbuf,
768 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
769 	.vidioc_expbuf			= vb2_ioctl_expbuf,
770 	.vidioc_streamon		= vb2_ioctl_streamon,
771 	.vidioc_streamoff		= vb2_ioctl_streamoff,
772 
773 	.vidioc_enum_input		= vivid_enum_input,
774 	.vidioc_g_input			= vivid_g_input,
775 	.vidioc_s_input			= vivid_s_input,
776 	.vidioc_s_audio			= vidioc_s_audio,
777 	.vidioc_g_audio			= vidioc_g_audio,
778 	.vidioc_enumaudio		= vidioc_enumaudio,
779 	.vidioc_s_frequency		= vidioc_s_frequency,
780 	.vidioc_g_frequency		= vidioc_g_frequency,
781 	.vidioc_s_tuner			= vidioc_s_tuner,
782 	.vidioc_g_tuner			= vidioc_g_tuner,
783 	.vidioc_s_modulator		= vidioc_s_modulator,
784 	.vidioc_g_modulator		= vidioc_g_modulator,
785 	.vidioc_s_hw_freq_seek		= vidioc_s_hw_freq_seek,
786 	.vidioc_enum_freq_bands		= vidioc_enum_freq_bands,
787 
788 	.vidioc_enum_output		= vidioc_enum_output,
789 	.vidioc_g_output		= vidioc_g_output,
790 	.vidioc_s_output		= vidioc_s_output,
791 	.vidioc_s_audout		= vidioc_s_audout,
792 	.vidioc_g_audout		= vidioc_g_audout,
793 	.vidioc_enumaudout		= vidioc_enumaudout,
794 
795 	.vidioc_querystd		= vidioc_querystd,
796 	.vidioc_g_std			= vidioc_g_std,
797 	.vidioc_s_std			= vidioc_s_std,
798 	.vidioc_s_dv_timings		= vidioc_s_dv_timings,
799 	.vidioc_g_dv_timings		= vidioc_g_dv_timings,
800 	.vidioc_query_dv_timings	= vidioc_query_dv_timings,
801 	.vidioc_enum_dv_timings		= vidioc_enum_dv_timings,
802 	.vidioc_dv_timings_cap		= vidioc_dv_timings_cap,
803 	.vidioc_g_edid			= vidioc_g_edid,
804 	.vidioc_s_edid			= vidioc_s_edid,
805 
806 	.vidioc_log_status		= vidioc_log_status,
807 	.vidioc_subscribe_event		= vidioc_subscribe_event,
808 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
809 
810 	.vidioc_enum_fmt_meta_cap	= vidioc_enum_fmt_meta_cap,
811 	.vidioc_g_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
812 	.vidioc_s_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
813 	.vidioc_try_fmt_meta_cap	= vidioc_g_fmt_meta_cap,
814 
815 	.vidioc_enum_fmt_meta_out       = vidioc_enum_fmt_meta_out,
816 	.vidioc_g_fmt_meta_out          = vidioc_g_fmt_meta_out,
817 	.vidioc_s_fmt_meta_out          = vidioc_g_fmt_meta_out,
818 	.vidioc_try_fmt_meta_out        = vidioc_g_fmt_meta_out,
819 };
820 
821 /* -----------------------------------------------------------------
822 	Initialization and module stuff
823    ------------------------------------------------------------------*/
824 
825 static void vivid_dev_release(struct v4l2_device *v4l2_dev)
826 {
827 	struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
828 
829 	vivid_free_controls(dev);
830 	v4l2_device_unregister(&dev->v4l2_dev);
831 #ifdef CONFIG_MEDIA_CONTROLLER
832 	media_device_cleanup(&dev->mdev);
833 #endif
834 	vfree(dev->scaled_line);
835 	vfree(dev->blended_line);
836 	vfree(dev->edid);
837 	tpg_free(&dev->tpg);
838 	kfree(dev->query_dv_timings_qmenu);
839 	kfree(dev->query_dv_timings_qmenu_strings);
840 	kfree(dev);
841 }
842 
843 #ifdef CONFIG_MEDIA_CONTROLLER
844 static int vivid_req_validate(struct media_request *req)
845 {
846 	struct vivid_dev *dev = container_of(req->mdev, struct vivid_dev, mdev);
847 
848 	if (dev->req_validate_error) {
849 		dev->req_validate_error = false;
850 		return -EINVAL;
851 	}
852 	return vb2_request_validate(req);
853 }
854 
855 static const struct media_device_ops vivid_media_ops = {
856 	.req_validate = vivid_req_validate,
857 	.req_queue = vb2_request_queue,
858 };
859 #endif
860 
861 static int vivid_create_queue(struct vivid_dev *dev,
862 			      struct vb2_queue *q,
863 			      u32 buf_type,
864 			      unsigned int min_queued_buffers,
865 			      const struct vb2_ops *ops)
866 {
867 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->multiplanar)
868 		buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
869 	else if (buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT && dev->multiplanar)
870 		buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
871 	else if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE && !dev->has_raw_vbi_cap)
872 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
873 	else if (buf_type == V4L2_BUF_TYPE_VBI_OUTPUT && !dev->has_raw_vbi_out)
874 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
875 
876 	q->type = buf_type;
877 	q->io_modes = VB2_MMAP | VB2_DMABUF;
878 	q->io_modes |= V4L2_TYPE_IS_OUTPUT(buf_type) ?  VB2_WRITE : VB2_READ;
879 
880 	/*
881 	 * The maximum number of buffers is 32768 if PAGE_SHIFT == 12,
882 	 * see also MAX_BUFFER_INDEX in videobuf2-core.c. It will be less if
883 	 * PAGE_SHIFT > 12, but then max_num_buffers will be clamped by
884 	 * videobuf2-core.c to MAX_BUFFER_INDEX.
885 	 */
886 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
887 		q->max_num_buffers = 64;
888 	if (buf_type == V4L2_BUF_TYPE_SDR_CAPTURE)
889 		q->max_num_buffers = 1024;
890 	if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE)
891 		q->max_num_buffers = 32768;
892 
893 	if (allocators[dev->inst] != 1)
894 		q->io_modes |= VB2_USERPTR;
895 	q->drv_priv = dev;
896 	q->buf_struct_size = sizeof(struct vivid_buffer);
897 	q->ops = ops;
898 	q->mem_ops = allocators[dev->inst] == 1 ? &vb2_dma_contig_memops :
899 						  &vb2_vmalloc_memops;
900 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
901 	q->min_queued_buffers = supports_requests[dev->inst] ? 0 : min_queued_buffers;
902 	q->lock = &dev->mutex;
903 	q->dev = dev->v4l2_dev.dev;
904 	q->supports_requests = supports_requests[dev->inst];
905 	q->requires_requests = supports_requests[dev->inst] >= 2;
906 	q->allow_cache_hints = (cache_hints[dev->inst] == 1);
907 
908 	return vb2_queue_init(q);
909 }
910 
911 static int vivid_detect_feature_set(struct vivid_dev *dev, int inst,
912 				    unsigned node_type,
913 				    bool *has_tuner,
914 				    bool *has_modulator,
915 				    int *ccs_cap,
916 				    int *ccs_out,
917 				    unsigned in_type_counter[4],
918 				    unsigned out_type_counter[4])
919 {
920 	int i;
921 
922 	/* do we use single- or multi-planar? */
923 	dev->multiplanar = multiplanar[inst] > 1;
924 	v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
925 			dev->multiplanar ? "multi" : "single ");
926 
927 	/* how many inputs do we have and of what type? */
928 	dev->num_inputs = num_inputs[inst];
929 	if (node_type & 0x20007) {
930 		if (dev->num_inputs < 1)
931 			dev->num_inputs = 1;
932 	} else {
933 		dev->num_inputs = 0;
934 	}
935 	if (dev->num_inputs >= MAX_INPUTS)
936 		dev->num_inputs = MAX_INPUTS;
937 	for (i = 0; i < dev->num_inputs; i++) {
938 		dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
939 		dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
940 	}
941 	dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
942 	if (in_type_counter[HDMI] == 16) {
943 		/* The CEC physical address only allows for max 15 inputs */
944 		in_type_counter[HDMI]--;
945 		dev->num_inputs--;
946 	}
947 	dev->num_hdmi_inputs = in_type_counter[HDMI];
948 
949 	/* how many outputs do we have and of what type? */
950 	dev->num_outputs = num_outputs[inst];
951 	if (node_type & 0x40300) {
952 		if (dev->num_outputs < 1)
953 			dev->num_outputs = 1;
954 	} else {
955 		dev->num_outputs = 0;
956 	}
957 	if (dev->num_outputs >= MAX_OUTPUTS)
958 		dev->num_outputs = MAX_OUTPUTS;
959 	for (i = 0; i < dev->num_outputs; i++) {
960 		dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
961 		dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
962 		dev->display_present[i] = true;
963 	}
964 	dev->has_audio_outputs = out_type_counter[SVID];
965 	if (out_type_counter[HDMI] == 16) {
966 		/*
967 		 * The CEC physical address only allows for max 15 inputs,
968 		 * so outputs are also limited to 15 to allow for easy
969 		 * CEC output to input mapping.
970 		 */
971 		out_type_counter[HDMI]--;
972 		dev->num_outputs--;
973 	}
974 	dev->num_hdmi_outputs = out_type_counter[HDMI];
975 
976 	/* do we create a video capture device? */
977 	dev->has_vid_cap = node_type & 0x0001;
978 
979 	/* do we create a vbi capture device? */
980 	if (in_type_counter[TV] || in_type_counter[SVID]) {
981 		dev->has_raw_vbi_cap = node_type & 0x0004;
982 		dev->has_sliced_vbi_cap = node_type & 0x0008;
983 		dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
984 	}
985 
986 	/* do we create a meta capture device */
987 	dev->has_meta_cap = node_type & 0x20000;
988 
989 	/* sanity checks */
990 	if ((in_type_counter[WEBCAM] || in_type_counter[HDMI]) &&
991 	    !dev->has_vid_cap && !dev->has_meta_cap) {
992 		v4l2_warn(&dev->v4l2_dev,
993 			  "Webcam or HDMI input without video or metadata nodes\n");
994 		return -EINVAL;
995 	}
996 	if ((in_type_counter[TV] || in_type_counter[SVID]) &&
997 	    !dev->has_vid_cap && !dev->has_vbi_cap && !dev->has_meta_cap) {
998 		v4l2_warn(&dev->v4l2_dev,
999 			  "TV or S-Video input without video, VBI or metadata nodes\n");
1000 		return -EINVAL;
1001 	}
1002 
1003 	/* do we create a video output device? */
1004 	dev->has_vid_out = node_type & 0x0100;
1005 
1006 	/* do we create a vbi output device? */
1007 	if (out_type_counter[SVID]) {
1008 		dev->has_raw_vbi_out = node_type & 0x0400;
1009 		dev->has_sliced_vbi_out = node_type & 0x0800;
1010 		dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
1011 	}
1012 
1013 	/* do we create a metadata output device */
1014 	dev->has_meta_out = node_type & 0x40000;
1015 
1016 	/* sanity checks */
1017 	if (out_type_counter[SVID] &&
1018 	    !dev->has_vid_out && !dev->has_vbi_out && !dev->has_meta_out) {
1019 		v4l2_warn(&dev->v4l2_dev,
1020 			  "S-Video output without video, VBI or metadata nodes\n");
1021 		return -EINVAL;
1022 	}
1023 	if (out_type_counter[HDMI] && !dev->has_vid_out && !dev->has_meta_out) {
1024 		v4l2_warn(&dev->v4l2_dev,
1025 			  "HDMI output without video or metadata nodes\n");
1026 		return -EINVAL;
1027 	}
1028 
1029 	/* do we create a radio receiver device? */
1030 	dev->has_radio_rx = node_type & 0x0010;
1031 
1032 	/* do we create a radio transmitter device? */
1033 	dev->has_radio_tx = node_type & 0x1000;
1034 
1035 	/* do we create a software defined radio capture device? */
1036 	dev->has_sdr_cap = node_type & 0x0020;
1037 
1038 	/* do we have a TV tuner? */
1039 	dev->has_tv_tuner = in_type_counter[TV];
1040 
1041 	/* do we have a tuner? */
1042 	*has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
1043 		      dev->has_radio_rx || dev->has_sdr_cap;
1044 
1045 	/* do we have a modulator? */
1046 	*has_modulator = dev->has_radio_tx;
1047 
1048 	if (dev->has_vid_cap)
1049 		/* do we have a framebuffer for overlay testing? */
1050 		dev->has_fb = node_type & 0x10000;
1051 
1052 	/* can we do crop/compose/scaling while capturing? */
1053 	if (no_error_inj && *ccs_cap == -1)
1054 		*ccs_cap = 7;
1055 
1056 	/* if ccs_cap == -1, then the user can select it using controls */
1057 	if (*ccs_cap != -1) {
1058 		dev->has_crop_cap = *ccs_cap & 1;
1059 		dev->has_compose_cap = *ccs_cap & 2;
1060 		dev->has_scaler_cap = *ccs_cap & 4;
1061 		v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
1062 			dev->has_crop_cap ? 'Y' : 'N',
1063 			dev->has_compose_cap ? 'Y' : 'N',
1064 			dev->has_scaler_cap ? 'Y' : 'N');
1065 	}
1066 
1067 	/* can we do crop/compose/scaling with video output? */
1068 	if (no_error_inj && *ccs_out == -1)
1069 		*ccs_out = 7;
1070 
1071 	/* if ccs_out == -1, then the user can select it using controls */
1072 	if (*ccs_out != -1) {
1073 		dev->has_crop_out = *ccs_out & 1;
1074 		dev->has_compose_out = *ccs_out & 2;
1075 		dev->has_scaler_out = *ccs_out & 4;
1076 		v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
1077 			dev->has_crop_out ? 'Y' : 'N',
1078 			dev->has_compose_out ? 'Y' : 'N',
1079 			dev->has_scaler_out ? 'Y' : 'N');
1080 	}
1081 
1082 	/* do we create a touch capture device */
1083 	dev->has_touch_cap = node_type & 0x80000;
1084 
1085 	return 0;
1086 }
1087 
1088 static void vivid_set_capabilities(struct vivid_dev *dev)
1089 {
1090 	if (dev->has_vid_cap) {
1091 		/* set up the capabilities of the video capture device */
1092 		dev->vid_cap_caps = dev->multiplanar ?
1093 			V4L2_CAP_VIDEO_CAPTURE_MPLANE :
1094 			V4L2_CAP_VIDEO_CAPTURE;
1095 		dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1096 		if (dev->has_audio_inputs)
1097 			dev->vid_cap_caps |= V4L2_CAP_AUDIO;
1098 		if (dev->has_tv_tuner)
1099 			dev->vid_cap_caps |= V4L2_CAP_TUNER;
1100 	}
1101 	if (dev->has_vid_out) {
1102 		/* set up the capabilities of the video output device */
1103 		dev->vid_out_caps = dev->multiplanar ?
1104 			V4L2_CAP_VIDEO_OUTPUT_MPLANE :
1105 			V4L2_CAP_VIDEO_OUTPUT;
1106 		if (dev->has_fb)
1107 			dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1108 		dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1109 		if (dev->has_audio_outputs)
1110 			dev->vid_out_caps |= V4L2_CAP_AUDIO;
1111 	}
1112 	if (dev->has_vbi_cap) {
1113 		/* set up the capabilities of the vbi capture device */
1114 		dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
1115 				    (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
1116 		dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1117 		if (dev->has_audio_inputs)
1118 			dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
1119 		if (dev->has_tv_tuner)
1120 			dev->vbi_cap_caps |= V4L2_CAP_TUNER;
1121 	}
1122 	if (dev->has_vbi_out) {
1123 		/* set up the capabilities of the vbi output device */
1124 		dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
1125 				    (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
1126 		dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1127 		if (dev->has_audio_outputs)
1128 			dev->vbi_out_caps |= V4L2_CAP_AUDIO;
1129 	}
1130 	if (dev->has_sdr_cap) {
1131 		/* set up the capabilities of the sdr capture device */
1132 		dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
1133 		dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1134 	}
1135 	/* set up the capabilities of the radio receiver device */
1136 	if (dev->has_radio_rx)
1137 		dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
1138 				     V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
1139 				     V4L2_CAP_READWRITE;
1140 	/* set up the capabilities of the radio transmitter device */
1141 	if (dev->has_radio_tx)
1142 		dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
1143 				     V4L2_CAP_READWRITE;
1144 
1145 	/* set up the capabilities of meta capture device */
1146 	if (dev->has_meta_cap) {
1147 		dev->meta_cap_caps = V4L2_CAP_META_CAPTURE |
1148 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1149 		if (dev->has_audio_inputs)
1150 			dev->meta_cap_caps |= V4L2_CAP_AUDIO;
1151 		if (dev->has_tv_tuner)
1152 			dev->meta_cap_caps |= V4L2_CAP_TUNER;
1153 	}
1154 	/* set up the capabilities of meta output device */
1155 	if (dev->has_meta_out) {
1156 		dev->meta_out_caps = V4L2_CAP_META_OUTPUT |
1157 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1158 		if (dev->has_audio_outputs)
1159 			dev->meta_out_caps |= V4L2_CAP_AUDIO;
1160 	}
1161 	/* set up the capabilities of the touch capture device */
1162 	if (dev->has_touch_cap) {
1163 		dev->touch_cap_caps = V4L2_CAP_TOUCH | V4L2_CAP_STREAMING |
1164 				      V4L2_CAP_READWRITE;
1165 		dev->touch_cap_caps |= dev->multiplanar ?
1166 			V4L2_CAP_VIDEO_CAPTURE_MPLANE : V4L2_CAP_VIDEO_CAPTURE;
1167 	}
1168 }
1169 
1170 static void vivid_disable_unused_ioctls(struct vivid_dev *dev,
1171 					bool has_tuner,
1172 					bool has_modulator,
1173 					unsigned in_type_counter[4],
1174 					unsigned out_type_counter[4])
1175 {
1176 	/* disable invalid ioctls based on the feature set */
1177 	if (!dev->has_audio_inputs) {
1178 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
1179 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
1180 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
1181 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
1182 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
1183 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
1184 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_AUDIO);
1185 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_AUDIO);
1186 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_ENUMAUDIO);
1187 	}
1188 	if (!dev->has_audio_outputs) {
1189 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
1190 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
1191 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
1192 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
1193 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
1194 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
1195 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_AUDOUT);
1196 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_AUDOUT);
1197 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_ENUMAUDOUT);
1198 	}
1199 	if (!in_type_counter[TV] && !in_type_counter[SVID]) {
1200 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
1201 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
1202 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
1203 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
1204 	}
1205 	if (!out_type_counter[SVID]) {
1206 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
1207 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
1208 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
1209 	}
1210 	if (!has_tuner && !has_modulator) {
1211 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
1212 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
1213 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
1214 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
1215 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_FREQUENCY);
1216 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_FREQUENCY);
1217 	}
1218 	if (!has_tuner) {
1219 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
1220 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
1221 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
1222 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
1223 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_TUNER);
1224 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_TUNER);
1225 	}
1226 	if (in_type_counter[HDMI] == 0) {
1227 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
1228 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
1229 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
1230 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
1231 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
1232 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
1233 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
1234 	}
1235 	if (out_type_counter[HDMI] == 0) {
1236 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
1237 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
1238 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
1239 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
1240 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
1241 	}
1242 	if (!dev->has_fb) {
1243 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
1244 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
1245 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
1246 	}
1247 	v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1248 	v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1249 	v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1250 	v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1251 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
1252 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
1253 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
1254 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1255 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
1256 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
1257 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_FREQUENCY);
1258 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_FREQUENCY);
1259 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_S_PARM);
1260 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMESIZES);
1261 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1262 }
1263 
1264 static int vivid_init_dv_timings(struct vivid_dev *dev)
1265 {
1266 	int i;
1267 
1268 	while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
1269 		dev->query_dv_timings_size++;
1270 
1271 	/*
1272 	 * Create a char pointer array that points to the names of all the
1273 	 * preset timings
1274 	 */
1275 	dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
1276 						    sizeof(char *), GFP_KERNEL);
1277 	/*
1278 	 * Create a string array containing the names of all the preset
1279 	 * timings. Each name is max 31 chars long (+ terminating 0).
1280 	 */
1281 	dev->query_dv_timings_qmenu_strings =
1282 		kmalloc_array(dev->query_dv_timings_size, 32, GFP_KERNEL);
1283 
1284 	if (!dev->query_dv_timings_qmenu ||
1285 	    !dev->query_dv_timings_qmenu_strings)
1286 		return -ENOMEM;
1287 
1288 	for (i = 0; i < dev->query_dv_timings_size; i++) {
1289 		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1290 		char *p = dev->query_dv_timings_qmenu_strings + i * 32;
1291 		u32 htot, vtot;
1292 
1293 		dev->query_dv_timings_qmenu[i] = p;
1294 
1295 		htot = V4L2_DV_BT_FRAME_WIDTH(bt);
1296 		vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
1297 		snprintf(p, 32, "%ux%u%s%u",
1298 			bt->width, bt->height, bt->interlaced ? "i" : "p",
1299 			(u32)bt->pixelclock / (htot * vtot));
1300 	}
1301 
1302 	return 0;
1303 }
1304 
1305 static int vivid_create_queues(struct vivid_dev *dev)
1306 {
1307 	int ret;
1308 
1309 	/* start creating the vb2 queues */
1310 	if (dev->has_vid_cap) {
1311 		/* initialize vid_cap queue */
1312 		ret = vivid_create_queue(dev, &dev->vb_vid_cap_q,
1313 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1314 					 &vivid_vid_cap_qops);
1315 		if (ret)
1316 			return ret;
1317 	}
1318 
1319 	if (dev->has_vid_out) {
1320 		/* initialize vid_out queue */
1321 		ret = vivid_create_queue(dev, &dev->vb_vid_out_q,
1322 					 V4L2_BUF_TYPE_VIDEO_OUTPUT, 2,
1323 					 &vivid_vid_out_qops);
1324 		if (ret)
1325 			return ret;
1326 	}
1327 
1328 	if (dev->has_vbi_cap) {
1329 		/* initialize vbi_cap queue */
1330 		ret = vivid_create_queue(dev, &dev->vb_vbi_cap_q,
1331 					 V4L2_BUF_TYPE_VBI_CAPTURE, 2,
1332 					 &vivid_vbi_cap_qops);
1333 		if (ret)
1334 			return ret;
1335 	}
1336 
1337 	if (dev->has_vbi_out) {
1338 		/* initialize vbi_out queue */
1339 		ret = vivid_create_queue(dev, &dev->vb_vbi_out_q,
1340 					 V4L2_BUF_TYPE_VBI_OUTPUT, 2,
1341 					 &vivid_vbi_out_qops);
1342 		if (ret)
1343 			return ret;
1344 	}
1345 
1346 	if (dev->has_sdr_cap) {
1347 		/* initialize sdr_cap queue */
1348 		ret = vivid_create_queue(dev, &dev->vb_sdr_cap_q,
1349 					 V4L2_BUF_TYPE_SDR_CAPTURE, 8,
1350 					 &vivid_sdr_cap_qops);
1351 		if (ret)
1352 			return ret;
1353 	}
1354 
1355 	if (dev->has_meta_cap) {
1356 		/* initialize meta_cap queue */
1357 		ret = vivid_create_queue(dev, &dev->vb_meta_cap_q,
1358 					 V4L2_BUF_TYPE_META_CAPTURE, 2,
1359 					 &vivid_meta_cap_qops);
1360 		if (ret)
1361 			return ret;
1362 	}
1363 
1364 	if (dev->has_meta_out) {
1365 		/* initialize meta_out queue */
1366 		ret = vivid_create_queue(dev, &dev->vb_meta_out_q,
1367 					 V4L2_BUF_TYPE_META_OUTPUT, 1,
1368 					 &vivid_meta_out_qops);
1369 		if (ret)
1370 			return ret;
1371 	}
1372 
1373 	if (dev->has_touch_cap) {
1374 		/* initialize touch_cap queue */
1375 		ret = vivid_create_queue(dev, &dev->vb_touch_cap_q,
1376 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 1,
1377 					 &vivid_touch_cap_qops);
1378 		if (ret)
1379 			return ret;
1380 	}
1381 
1382 	if (dev->has_fb) {
1383 		/* Create framebuffer for testing output overlay */
1384 		ret = vivid_fb_init(dev);
1385 		if (ret)
1386 			return ret;
1387 		v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1388 			  dev->fb_info.node);
1389 	}
1390 	return 0;
1391 }
1392 
1393 static int vivid_create_devnodes(struct platform_device *pdev,
1394 				 struct vivid_dev *dev, int inst,
1395 				 unsigned int cec_tx_bus_cnt,
1396 				 v4l2_std_id tvnorms_cap,
1397 				 v4l2_std_id tvnorms_out,
1398 				 unsigned in_type_counter[4],
1399 				 unsigned out_type_counter[4])
1400 {
1401 	struct video_device *vfd;
1402 	int ret;
1403 
1404 	if (dev->has_vid_cap) {
1405 		vfd = &dev->vid_cap_dev;
1406 		snprintf(vfd->name, sizeof(vfd->name),
1407 			 "vivid-%03d-vid-cap", inst);
1408 		vfd->fops = &vivid_fops;
1409 		vfd->ioctl_ops = &vivid_ioctl_ops;
1410 		vfd->device_caps = dev->vid_cap_caps;
1411 		vfd->release = video_device_release_empty;
1412 		vfd->v4l2_dev = &dev->v4l2_dev;
1413 		vfd->queue = &dev->vb_vid_cap_q;
1414 		vfd->tvnorms = tvnorms_cap;
1415 
1416 		/*
1417 		 * Provide a mutex to v4l2 core. It will be used to protect
1418 		 * all fops and v4l2 ioctls.
1419 		 */
1420 		vfd->lock = &dev->mutex;
1421 		video_set_drvdata(vfd, dev);
1422 
1423 #ifdef CONFIG_MEDIA_CONTROLLER
1424 		dev->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
1425 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_cap_pad);
1426 		if (ret)
1427 			return ret;
1428 #endif
1429 
1430 #ifdef CONFIG_VIDEO_VIVID_CEC
1431 		if (in_type_counter[HDMI]) {
1432 			ret = cec_register_adapter(dev->cec_rx_adap, &pdev->dev);
1433 			if (ret < 0) {
1434 				cec_delete_adapter(dev->cec_rx_adap);
1435 				dev->cec_rx_adap = NULL;
1436 				return ret;
1437 			}
1438 			cec_s_phys_addr(dev->cec_rx_adap, 0, false);
1439 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input 0\n",
1440 				  dev_name(&dev->cec_rx_adap->devnode.dev));
1441 		}
1442 #endif
1443 
1444 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_cap_nr[inst]);
1445 		if (ret < 0)
1446 			return ret;
1447 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1448 					  video_device_node_name(vfd));
1449 	}
1450 
1451 	if (dev->has_vid_out) {
1452 #ifdef CONFIG_VIDEO_VIVID_CEC
1453 		int i;
1454 #endif
1455 		vfd = &dev->vid_out_dev;
1456 		snprintf(vfd->name, sizeof(vfd->name),
1457 			 "vivid-%03d-vid-out", inst);
1458 		vfd->vfl_dir = VFL_DIR_TX;
1459 		vfd->fops = &vivid_fops;
1460 		vfd->ioctl_ops = &vivid_ioctl_ops;
1461 		vfd->device_caps = dev->vid_out_caps;
1462 		vfd->release = video_device_release_empty;
1463 		vfd->v4l2_dev = &dev->v4l2_dev;
1464 		vfd->queue = &dev->vb_vid_out_q;
1465 		vfd->tvnorms = tvnorms_out;
1466 
1467 		/*
1468 		 * Provide a mutex to v4l2 core. It will be used to protect
1469 		 * all fops and v4l2 ioctls.
1470 		 */
1471 		vfd->lock = &dev->mutex;
1472 		video_set_drvdata(vfd, dev);
1473 
1474 #ifdef CONFIG_MEDIA_CONTROLLER
1475 		dev->vid_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1476 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_out_pad);
1477 		if (ret)
1478 			return ret;
1479 #endif
1480 
1481 #ifdef CONFIG_VIDEO_VIVID_CEC
1482 		for (i = 0; i < cec_tx_bus_cnt; i++) {
1483 			ret = cec_register_adapter(dev->cec_tx_adap[i], &pdev->dev);
1484 			if (ret < 0) {
1485 				for (; i < cec_tx_bus_cnt; i++) {
1486 					cec_delete_adapter(dev->cec_tx_adap[i]);
1487 					dev->cec_tx_adap[i] = NULL;
1488 				}
1489 				return ret;
1490 			}
1491 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1492 				  dev_name(&dev->cec_tx_adap[i]->devnode.dev), i);
1493 			if (i < out_type_counter[HDMI])
1494 				cec_s_phys_addr(dev->cec_tx_adap[i], (i + 1) << 12, false);
1495 			else
1496 				cec_s_phys_addr(dev->cec_tx_adap[i], 0x1000, false);
1497 		}
1498 #endif
1499 
1500 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_out_nr[inst]);
1501 		if (ret < 0)
1502 			return ret;
1503 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1504 					  video_device_node_name(vfd));
1505 	}
1506 
1507 	if (dev->has_vbi_cap) {
1508 		vfd = &dev->vbi_cap_dev;
1509 		snprintf(vfd->name, sizeof(vfd->name),
1510 			 "vivid-%03d-vbi-cap", inst);
1511 		vfd->fops = &vivid_fops;
1512 		vfd->ioctl_ops = &vivid_ioctl_ops;
1513 		vfd->device_caps = dev->vbi_cap_caps;
1514 		vfd->release = video_device_release_empty;
1515 		vfd->v4l2_dev = &dev->v4l2_dev;
1516 		vfd->queue = &dev->vb_vbi_cap_q;
1517 		vfd->lock = &dev->mutex;
1518 		vfd->tvnorms = tvnorms_cap;
1519 		video_set_drvdata(vfd, dev);
1520 
1521 #ifdef CONFIG_MEDIA_CONTROLLER
1522 		dev->vbi_cap_pad.flags = MEDIA_PAD_FL_SINK;
1523 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_cap_pad);
1524 		if (ret)
1525 			return ret;
1526 #endif
1527 
1528 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1529 		if (ret < 0)
1530 			return ret;
1531 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1532 					  video_device_node_name(vfd),
1533 					  (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1534 					  "raw and sliced" :
1535 					  (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1536 	}
1537 
1538 	if (dev->has_vbi_out) {
1539 		vfd = &dev->vbi_out_dev;
1540 		snprintf(vfd->name, sizeof(vfd->name),
1541 			 "vivid-%03d-vbi-out", inst);
1542 		vfd->vfl_dir = VFL_DIR_TX;
1543 		vfd->fops = &vivid_fops;
1544 		vfd->ioctl_ops = &vivid_ioctl_ops;
1545 		vfd->device_caps = dev->vbi_out_caps;
1546 		vfd->release = video_device_release_empty;
1547 		vfd->v4l2_dev = &dev->v4l2_dev;
1548 		vfd->queue = &dev->vb_vbi_out_q;
1549 		vfd->lock = &dev->mutex;
1550 		vfd->tvnorms = tvnorms_out;
1551 		video_set_drvdata(vfd, dev);
1552 
1553 #ifdef CONFIG_MEDIA_CONTROLLER
1554 		dev->vbi_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1555 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_out_pad);
1556 		if (ret)
1557 			return ret;
1558 #endif
1559 
1560 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1561 		if (ret < 0)
1562 			return ret;
1563 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1564 					  video_device_node_name(vfd),
1565 					  (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1566 					  "raw and sliced" :
1567 					  (dev->has_raw_vbi_out ? "raw" : "sliced"));
1568 	}
1569 
1570 	if (dev->has_sdr_cap) {
1571 		vfd = &dev->sdr_cap_dev;
1572 		snprintf(vfd->name, sizeof(vfd->name),
1573 			 "vivid-%03d-sdr-cap", inst);
1574 		vfd->fops = &vivid_fops;
1575 		vfd->ioctl_ops = &vivid_ioctl_ops;
1576 		vfd->device_caps = dev->sdr_cap_caps;
1577 		vfd->release = video_device_release_empty;
1578 		vfd->v4l2_dev = &dev->v4l2_dev;
1579 		vfd->queue = &dev->vb_sdr_cap_q;
1580 		vfd->lock = &dev->mutex;
1581 		video_set_drvdata(vfd, dev);
1582 
1583 #ifdef CONFIG_MEDIA_CONTROLLER
1584 		dev->sdr_cap_pad.flags = MEDIA_PAD_FL_SINK;
1585 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->sdr_cap_pad);
1586 		if (ret)
1587 			return ret;
1588 #endif
1589 
1590 		ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1591 		if (ret < 0)
1592 			return ret;
1593 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1594 					  video_device_node_name(vfd));
1595 	}
1596 
1597 	if (dev->has_radio_rx) {
1598 		vfd = &dev->radio_rx_dev;
1599 		snprintf(vfd->name, sizeof(vfd->name),
1600 			 "vivid-%03d-rad-rx", inst);
1601 		vfd->fops = &vivid_radio_fops;
1602 		vfd->ioctl_ops = &vivid_ioctl_ops;
1603 		vfd->device_caps = dev->radio_rx_caps;
1604 		vfd->release = video_device_release_empty;
1605 		vfd->v4l2_dev = &dev->v4l2_dev;
1606 		vfd->lock = &dev->mutex;
1607 		video_set_drvdata(vfd, dev);
1608 
1609 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1610 		if (ret < 0)
1611 			return ret;
1612 		v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1613 					  video_device_node_name(vfd));
1614 	}
1615 
1616 	if (dev->has_radio_tx) {
1617 		vfd = &dev->radio_tx_dev;
1618 		snprintf(vfd->name, sizeof(vfd->name),
1619 			 "vivid-%03d-rad-tx", inst);
1620 		vfd->vfl_dir = VFL_DIR_TX;
1621 		vfd->fops = &vivid_radio_fops;
1622 		vfd->ioctl_ops = &vivid_ioctl_ops;
1623 		vfd->device_caps = dev->radio_tx_caps;
1624 		vfd->release = video_device_release_empty;
1625 		vfd->v4l2_dev = &dev->v4l2_dev;
1626 		vfd->lock = &dev->mutex;
1627 		video_set_drvdata(vfd, dev);
1628 
1629 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1630 		if (ret < 0)
1631 			return ret;
1632 		v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1633 					  video_device_node_name(vfd));
1634 	}
1635 
1636 	if (dev->has_meta_cap) {
1637 		vfd = &dev->meta_cap_dev;
1638 		snprintf(vfd->name, sizeof(vfd->name),
1639 			 "vivid-%03d-meta-cap", inst);
1640 		vfd->fops = &vivid_fops;
1641 		vfd->ioctl_ops = &vivid_ioctl_ops;
1642 		vfd->device_caps = dev->meta_cap_caps;
1643 		vfd->release = video_device_release_empty;
1644 		vfd->v4l2_dev = &dev->v4l2_dev;
1645 		vfd->queue = &dev->vb_meta_cap_q;
1646 		vfd->lock = &dev->mutex;
1647 		vfd->tvnorms = tvnorms_cap;
1648 		video_set_drvdata(vfd, dev);
1649 #ifdef CONFIG_MEDIA_CONTROLLER
1650 		dev->meta_cap_pad.flags = MEDIA_PAD_FL_SINK;
1651 		ret = media_entity_pads_init(&vfd->entity, 1,
1652 					     &dev->meta_cap_pad);
1653 		if (ret)
1654 			return ret;
1655 #endif
1656 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1657 					    meta_cap_nr[inst]);
1658 		if (ret < 0)
1659 			return ret;
1660 		v4l2_info(&dev->v4l2_dev,
1661 			  "V4L2 metadata capture device registered as %s\n",
1662 			  video_device_node_name(vfd));
1663 	}
1664 
1665 	if (dev->has_meta_out) {
1666 		vfd = &dev->meta_out_dev;
1667 		snprintf(vfd->name, sizeof(vfd->name),
1668 			 "vivid-%03d-meta-out", inst);
1669 		vfd->vfl_dir = VFL_DIR_TX;
1670 		vfd->fops = &vivid_fops;
1671 		vfd->ioctl_ops = &vivid_ioctl_ops;
1672 		vfd->device_caps = dev->meta_out_caps;
1673 		vfd->release = video_device_release_empty;
1674 		vfd->v4l2_dev = &dev->v4l2_dev;
1675 		vfd->queue = &dev->vb_meta_out_q;
1676 		vfd->lock = &dev->mutex;
1677 		vfd->tvnorms = tvnorms_out;
1678 		video_set_drvdata(vfd, dev);
1679 #ifdef CONFIG_MEDIA_CONTROLLER
1680 		dev->meta_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1681 		ret = media_entity_pads_init(&vfd->entity, 1,
1682 					     &dev->meta_out_pad);
1683 		if (ret)
1684 			return ret;
1685 #endif
1686 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1687 					    meta_out_nr[inst]);
1688 		if (ret < 0)
1689 			return ret;
1690 		v4l2_info(&dev->v4l2_dev,
1691 			  "V4L2 metadata output device registered as %s\n",
1692 			  video_device_node_name(vfd));
1693 	}
1694 
1695 	if (dev->has_touch_cap) {
1696 		vfd = &dev->touch_cap_dev;
1697 		snprintf(vfd->name, sizeof(vfd->name),
1698 			 "vivid-%03d-touch-cap", inst);
1699 		vfd->fops = &vivid_fops;
1700 		vfd->ioctl_ops = &vivid_ioctl_ops;
1701 		vfd->device_caps = dev->touch_cap_caps;
1702 		vfd->release = video_device_release_empty;
1703 		vfd->v4l2_dev = &dev->v4l2_dev;
1704 		vfd->queue = &dev->vb_touch_cap_q;
1705 		vfd->tvnorms = tvnorms_cap;
1706 		vfd->lock = &dev->mutex;
1707 		video_set_drvdata(vfd, dev);
1708 #ifdef CONFIG_MEDIA_CONTROLLER
1709 		dev->touch_cap_pad.flags = MEDIA_PAD_FL_SINK;
1710 		ret = media_entity_pads_init(&vfd->entity, 1,
1711 					     &dev->touch_cap_pad);
1712 		if (ret)
1713 			return ret;
1714 #endif
1715 		ret = video_register_device(vfd, VFL_TYPE_TOUCH,
1716 					    touch_cap_nr[inst]);
1717 		if (ret < 0)
1718 			return ret;
1719 		v4l2_info(&dev->v4l2_dev,
1720 			  "V4L2 touch capture device registered as %s\n",
1721 			  video_device_node_name(vfd));
1722 	}
1723 
1724 #ifdef CONFIG_MEDIA_CONTROLLER
1725 	/* Register the media device */
1726 	ret = media_device_register(&dev->mdev);
1727 	if (ret) {
1728 		dev_err(dev->mdev.dev,
1729 			"media device register failed (err=%d)\n", ret);
1730 		return ret;
1731 	}
1732 #endif
1733 	return 0;
1734 }
1735 
1736 static int vivid_create_instance(struct platform_device *pdev, int inst)
1737 {
1738 	static const struct v4l2_dv_timings def_dv_timings =
1739 					V4L2_DV_BT_CEA_1280X720P60;
1740 	unsigned in_type_counter[4] = { 0, 0, 0, 0 };
1741 	unsigned out_type_counter[4] = { 0, 0, 0, 0 };
1742 	int ccs_cap = ccs_cap_mode[inst];
1743 	int ccs_out = ccs_out_mode[inst];
1744 	bool has_tuner;
1745 	bool has_modulator;
1746 	struct vivid_dev *dev;
1747 	unsigned node_type = node_types[inst];
1748 	v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
1749 	unsigned int cec_tx_bus_cnt = 0;
1750 	int ret;
1751 	int i;
1752 
1753 	/* allocate main vivid state structure */
1754 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1755 	if (!dev)
1756 		return -ENOMEM;
1757 
1758 	dev->inst = inst;
1759 
1760 #ifdef CONFIG_MEDIA_CONTROLLER
1761 	dev->v4l2_dev.mdev = &dev->mdev;
1762 
1763 	/* Initialize media device */
1764 	strscpy(dev->mdev.model, VIVID_MODULE_NAME, sizeof(dev->mdev.model));
1765 	snprintf(dev->mdev.bus_info, sizeof(dev->mdev.bus_info),
1766 		 "platform:%s-%03d", VIVID_MODULE_NAME, inst);
1767 	dev->mdev.dev = &pdev->dev;
1768 	media_device_init(&dev->mdev);
1769 	dev->mdev.ops = &vivid_media_ops;
1770 #endif
1771 
1772 	/* register v4l2_device */
1773 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1774 			"%s-%03d", VIVID_MODULE_NAME, inst);
1775 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1776 	if (ret) {
1777 		kfree(dev);
1778 		return ret;
1779 	}
1780 	dev->v4l2_dev.release = vivid_dev_release;
1781 
1782 	ret = vivid_detect_feature_set(dev, inst, node_type,
1783 				       &has_tuner, &has_modulator,
1784 				       &ccs_cap, &ccs_out,
1785 				       in_type_counter, out_type_counter);
1786 	if (ret)
1787 		goto free_dev;
1788 
1789 	vivid_set_capabilities(dev);
1790 
1791 	ret = -ENOMEM;
1792 	/* initialize the test pattern generator */
1793 	tpg_init(&dev->tpg, 640, 360);
1794 	if (tpg_alloc(&dev->tpg, array_size(MAX_WIDTH, MAX_ZOOM)))
1795 		goto free_dev;
1796 	dev->scaled_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1797 	if (!dev->scaled_line)
1798 		goto free_dev;
1799 	dev->blended_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1800 	if (!dev->blended_line)
1801 		goto free_dev;
1802 
1803 	/* load the edid */
1804 	dev->edid = vmalloc(array_size(256, 128));
1805 	if (!dev->edid)
1806 		goto free_dev;
1807 
1808 	ret = vivid_init_dv_timings(dev);
1809 	if (ret < 0)
1810 		goto free_dev;
1811 
1812 	vivid_disable_unused_ioctls(dev, has_tuner, has_modulator,
1813 				    in_type_counter, out_type_counter);
1814 
1815 	/* configure internal data */
1816 	dev->fmt_cap = &vivid_formats[0];
1817 	dev->fmt_out = &vivid_formats[0];
1818 	if (!dev->multiplanar)
1819 		vivid_formats[0].data_offset[0] = 0;
1820 	dev->webcam_size_idx = 1;
1821 	dev->webcam_ival_idx = 3;
1822 	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
1823 	dev->std_out = V4L2_STD_PAL;
1824 	if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
1825 		tvnorms_cap = V4L2_STD_ALL;
1826 	if (dev->output_type[0] == SVID)
1827 		tvnorms_out = V4L2_STD_ALL;
1828 	for (i = 0; i < MAX_INPUTS; i++) {
1829 		dev->dv_timings_cap[i] = def_dv_timings;
1830 		dev->std_cap[i] = V4L2_STD_PAL;
1831 	}
1832 	dev->dv_timings_out = def_dv_timings;
1833 	dev->tv_freq = 2804 /* 175.25 * 16 */;
1834 	dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
1835 	dev->tv_field_cap = V4L2_FIELD_INTERLACED;
1836 	dev->tv_field_out = V4L2_FIELD_INTERLACED;
1837 	dev->radio_rx_freq = 95000 * 16;
1838 	dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
1839 	if (dev->has_radio_tx) {
1840 		dev->radio_tx_freq = 95500 * 16;
1841 		dev->radio_rds_loop = false;
1842 	}
1843 	dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
1844 	dev->sdr_adc_freq = 300000;
1845 	dev->sdr_fm_freq = 50000000;
1846 	dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
1847 	dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
1848 
1849 	dev->edid_max_blocks = dev->edid_blocks = 2;
1850 	memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
1851 	dev->radio_rds_init_time = ktime_get();
1852 
1853 	/* create all controls */
1854 	ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
1855 			in_type_counter[TV] || in_type_counter[SVID] ||
1856 			out_type_counter[SVID],
1857 			in_type_counter[HDMI] || out_type_counter[HDMI]);
1858 	if (ret)
1859 		goto unreg_dev;
1860 
1861 	/* enable/disable interface specific controls */
1862 	if (dev->num_outputs && dev->output_type[0] != HDMI)
1863 		v4l2_ctrl_activate(dev->ctrl_display_present, false);
1864 	if (dev->num_inputs && dev->input_type[0] != HDMI) {
1865 		v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode, false);
1866 		v4l2_ctrl_activate(dev->ctrl_dv_timings, false);
1867 	} else if (dev->num_inputs && dev->input_type[0] == HDMI) {
1868 		v4l2_ctrl_activate(dev->ctrl_std_signal_mode, false);
1869 		v4l2_ctrl_activate(dev->ctrl_standard, false);
1870 	}
1871 
1872 	/*
1873 	 * update the capture and output formats to do a proper initial
1874 	 * configuration.
1875 	 */
1876 	vivid_update_format_cap(dev, false);
1877 	vivid_update_format_out(dev);
1878 
1879 	/* update touch configuration */
1880 	dev->timeperframe_tch_cap.numerator = 1;
1881 	dev->timeperframe_tch_cap.denominator = 10;
1882 	vivid_set_touch(dev, 0);
1883 
1884 	/* initialize locks */
1885 	spin_lock_init(&dev->slock);
1886 	mutex_init(&dev->mutex);
1887 
1888 	/* init dma queues */
1889 	INIT_LIST_HEAD(&dev->vid_cap_active);
1890 	INIT_LIST_HEAD(&dev->vid_out_active);
1891 	INIT_LIST_HEAD(&dev->vbi_cap_active);
1892 	INIT_LIST_HEAD(&dev->vbi_out_active);
1893 	INIT_LIST_HEAD(&dev->sdr_cap_active);
1894 	INIT_LIST_HEAD(&dev->meta_cap_active);
1895 	INIT_LIST_HEAD(&dev->meta_out_active);
1896 	INIT_LIST_HEAD(&dev->touch_cap_active);
1897 
1898 	spin_lock_init(&dev->cec_xfers_slock);
1899 
1900 	if (allocators[inst] == 1)
1901 		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1902 
1903 	ret = vivid_create_queues(dev);
1904 	if (ret)
1905 		goto unreg_dev;
1906 
1907 #ifdef CONFIG_VIDEO_VIVID_CEC
1908 	if (dev->has_vid_cap && in_type_counter[HDMI]) {
1909 		struct cec_adapter *adap;
1910 
1911 		adap = vivid_cec_alloc_adap(dev, 0, false);
1912 		ret = PTR_ERR_OR_ZERO(adap);
1913 		if (ret < 0)
1914 			goto unreg_dev;
1915 		dev->cec_rx_adap = adap;
1916 	}
1917 
1918 	if (dev->has_vid_out) {
1919 		for (i = 0; i < dev->num_outputs; i++) {
1920 			struct cec_adapter *adap;
1921 
1922 			if (dev->output_type[i] != HDMI)
1923 				continue;
1924 
1925 			dev->cec_output2bus_map[i] = cec_tx_bus_cnt;
1926 			adap = vivid_cec_alloc_adap(dev, cec_tx_bus_cnt, true);
1927 			ret = PTR_ERR_OR_ZERO(adap);
1928 			if (ret < 0) {
1929 				for (i = 0; i < dev->num_outputs; i++)
1930 					cec_delete_adapter(dev->cec_tx_adap[i]);
1931 				goto unreg_dev;
1932 			}
1933 
1934 			dev->cec_tx_adap[cec_tx_bus_cnt] = adap;
1935 			cec_tx_bus_cnt++;
1936 		}
1937 	}
1938 
1939 	if (dev->cec_rx_adap || cec_tx_bus_cnt) {
1940 		init_waitqueue_head(&dev->kthread_waitq_cec);
1941 		dev->kthread_cec = kthread_run(vivid_cec_bus_thread, dev,
1942 					       "vivid_cec-%s", dev->v4l2_dev.name);
1943 		if (IS_ERR(dev->kthread_cec)) {
1944 			ret = PTR_ERR(dev->kthread_cec);
1945 			dev->kthread_cec = NULL;
1946 			v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
1947 			goto unreg_dev;
1948 		}
1949 	}
1950 
1951 #endif
1952 
1953 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
1954 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
1955 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
1956 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
1957 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
1958 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
1959 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
1960 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_cap);
1961 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_out);
1962 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_touch_cap);
1963 
1964 	/* finally start creating the device nodes */
1965 	ret = vivid_create_devnodes(pdev, dev, inst, cec_tx_bus_cnt,
1966 				    tvnorms_cap, tvnorms_out,
1967 				    in_type_counter, out_type_counter);
1968 	if (ret)
1969 		goto unreg_dev;
1970 
1971 	/* Now that everything is fine, let's add it to device list */
1972 	vivid_devs[inst] = dev;
1973 
1974 	return 0;
1975 
1976 unreg_dev:
1977 	vb2_video_unregister_device(&dev->touch_cap_dev);
1978 	vb2_video_unregister_device(&dev->meta_out_dev);
1979 	vb2_video_unregister_device(&dev->meta_cap_dev);
1980 	video_unregister_device(&dev->radio_tx_dev);
1981 	video_unregister_device(&dev->radio_rx_dev);
1982 	vb2_video_unregister_device(&dev->sdr_cap_dev);
1983 	vb2_video_unregister_device(&dev->vbi_out_dev);
1984 	vb2_video_unregister_device(&dev->vbi_cap_dev);
1985 	vb2_video_unregister_device(&dev->vid_out_dev);
1986 	vb2_video_unregister_device(&dev->vid_cap_dev);
1987 	cec_unregister_adapter(dev->cec_rx_adap);
1988 	for (i = 0; i < MAX_OUTPUTS; i++)
1989 		cec_unregister_adapter(dev->cec_tx_adap[i]);
1990 	if (dev->kthread_cec)
1991 		kthread_stop(dev->kthread_cec);
1992 free_dev:
1993 	v4l2_device_put(&dev->v4l2_dev);
1994 	return ret;
1995 }
1996 
1997 /* This routine allocates from 1 to n_devs virtual drivers.
1998 
1999    The real maximum number of virtual drivers will depend on how many drivers
2000    will succeed. This is limited to the maximum number of devices that
2001    videodev supports, which is equal to VIDEO_NUM_DEVICES.
2002  */
2003 static int vivid_probe(struct platform_device *pdev)
2004 {
2005 	const struct font_desc *font = find_font("VGA8x16");
2006 	int ret = 0, i;
2007 
2008 	if (font == NULL) {
2009 		pr_err("vivid: could not find font\n");
2010 		return -ENODEV;
2011 	}
2012 
2013 	tpg_set_font(font->data);
2014 
2015 	n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
2016 
2017 	for (i = 0; i < n_devs; i++) {
2018 		ret = vivid_create_instance(pdev, i);
2019 		if (ret) {
2020 			/* If some instantiations succeeded, keep driver */
2021 			if (i)
2022 				ret = 0;
2023 			break;
2024 		}
2025 	}
2026 
2027 	if (ret < 0) {
2028 		pr_err("vivid: error %d while loading driver\n", ret);
2029 		return ret;
2030 	}
2031 
2032 	/* n_devs will reflect the actual number of allocated devices */
2033 	n_devs = i;
2034 
2035 	return ret;
2036 }
2037 
2038 static void vivid_remove(struct platform_device *pdev)
2039 {
2040 	struct vivid_dev *dev;
2041 	unsigned int i, j;
2042 
2043 	for (i = 0; i < n_devs; i++) {
2044 		dev = vivid_devs[i];
2045 		if (!dev)
2046 			continue;
2047 
2048 		if (dev->disconnect_error)
2049 			vivid_reconnect(dev);
2050 #ifdef CONFIG_MEDIA_CONTROLLER
2051 		media_device_unregister(&dev->mdev);
2052 #endif
2053 
2054 		if (dev->has_vid_cap) {
2055 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2056 				video_device_node_name(&dev->vid_cap_dev));
2057 			vb2_video_unregister_device(&dev->vid_cap_dev);
2058 		}
2059 		if (dev->has_vid_out) {
2060 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2061 				video_device_node_name(&dev->vid_out_dev));
2062 			vb2_video_unregister_device(&dev->vid_out_dev);
2063 		}
2064 		if (dev->has_vbi_cap) {
2065 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2066 				video_device_node_name(&dev->vbi_cap_dev));
2067 			vb2_video_unregister_device(&dev->vbi_cap_dev);
2068 		}
2069 		if (dev->has_vbi_out) {
2070 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2071 				video_device_node_name(&dev->vbi_out_dev));
2072 			vb2_video_unregister_device(&dev->vbi_out_dev);
2073 		}
2074 		if (dev->has_sdr_cap) {
2075 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2076 				video_device_node_name(&dev->sdr_cap_dev));
2077 			vb2_video_unregister_device(&dev->sdr_cap_dev);
2078 		}
2079 		if (dev->has_radio_rx) {
2080 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2081 				video_device_node_name(&dev->radio_rx_dev));
2082 			video_unregister_device(&dev->radio_rx_dev);
2083 		}
2084 		if (dev->has_radio_tx) {
2085 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2086 				video_device_node_name(&dev->radio_tx_dev));
2087 			video_unregister_device(&dev->radio_tx_dev);
2088 		}
2089 		if (dev->has_fb) {
2090 			v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
2091 				dev->fb_info.node);
2092 			unregister_framebuffer(&dev->fb_info);
2093 			vivid_fb_release_buffers(dev);
2094 		}
2095 		if (dev->has_meta_cap) {
2096 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2097 				  video_device_node_name(&dev->meta_cap_dev));
2098 			vb2_video_unregister_device(&dev->meta_cap_dev);
2099 		}
2100 		if (dev->has_meta_out) {
2101 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2102 				  video_device_node_name(&dev->meta_out_dev));
2103 			vb2_video_unregister_device(&dev->meta_out_dev);
2104 		}
2105 		if (dev->has_touch_cap) {
2106 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2107 				  video_device_node_name(&dev->touch_cap_dev));
2108 			vb2_video_unregister_device(&dev->touch_cap_dev);
2109 		}
2110 		cec_unregister_adapter(dev->cec_rx_adap);
2111 		for (j = 0; j < MAX_OUTPUTS; j++)
2112 			cec_unregister_adapter(dev->cec_tx_adap[j]);
2113 		if (dev->kthread_cec)
2114 			kthread_stop(dev->kthread_cec);
2115 		v4l2_device_put(&dev->v4l2_dev);
2116 		vivid_devs[i] = NULL;
2117 	}
2118 }
2119 
2120 static void vivid_pdev_release(struct device *dev)
2121 {
2122 }
2123 
2124 static struct platform_device vivid_pdev = {
2125 	.name		= "vivid",
2126 	.dev.release	= vivid_pdev_release,
2127 };
2128 
2129 static struct platform_driver vivid_pdrv = {
2130 	.probe		= vivid_probe,
2131 	.remove_new	= vivid_remove,
2132 	.driver		= {
2133 		.name	= "vivid",
2134 	},
2135 };
2136 
2137 static int __init vivid_init(void)
2138 {
2139 	int ret;
2140 
2141 	ret = platform_device_register(&vivid_pdev);
2142 	if (ret)
2143 		return ret;
2144 
2145 	ret = platform_driver_register(&vivid_pdrv);
2146 	if (ret)
2147 		platform_device_unregister(&vivid_pdev);
2148 
2149 	return ret;
2150 }
2151 
2152 static void __exit vivid_exit(void)
2153 {
2154 	platform_driver_unregister(&vivid_pdrv);
2155 	platform_device_unregister(&vivid_pdev);
2156 }
2157 
2158 module_init(vivid_init);
2159 module_exit(vivid_exit);
2160