xref: /linux/drivers/media/test-drivers/vivid/vivid-core.c (revision 429508c84d95811dd1300181dfe84743caff9a38)
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 	.vidioc_remove_bufs		= vb2_ioctl_remove_bufs,
773 
774 	.vidioc_enum_input		= vivid_enum_input,
775 	.vidioc_g_input			= vivid_g_input,
776 	.vidioc_s_input			= vivid_s_input,
777 	.vidioc_s_audio			= vidioc_s_audio,
778 	.vidioc_g_audio			= vidioc_g_audio,
779 	.vidioc_enumaudio		= vidioc_enumaudio,
780 	.vidioc_s_frequency		= vidioc_s_frequency,
781 	.vidioc_g_frequency		= vidioc_g_frequency,
782 	.vidioc_s_tuner			= vidioc_s_tuner,
783 	.vidioc_g_tuner			= vidioc_g_tuner,
784 	.vidioc_s_modulator		= vidioc_s_modulator,
785 	.vidioc_g_modulator		= vidioc_g_modulator,
786 	.vidioc_s_hw_freq_seek		= vidioc_s_hw_freq_seek,
787 	.vidioc_enum_freq_bands		= vidioc_enum_freq_bands,
788 
789 	.vidioc_enum_output		= vidioc_enum_output,
790 	.vidioc_g_output		= vidioc_g_output,
791 	.vidioc_s_output		= vidioc_s_output,
792 	.vidioc_s_audout		= vidioc_s_audout,
793 	.vidioc_g_audout		= vidioc_g_audout,
794 	.vidioc_enumaudout		= vidioc_enumaudout,
795 
796 	.vidioc_querystd		= vidioc_querystd,
797 	.vidioc_g_std			= vidioc_g_std,
798 	.vidioc_s_std			= vidioc_s_std,
799 	.vidioc_s_dv_timings		= vidioc_s_dv_timings,
800 	.vidioc_g_dv_timings		= vidioc_g_dv_timings,
801 	.vidioc_query_dv_timings	= vidioc_query_dv_timings,
802 	.vidioc_enum_dv_timings		= vidioc_enum_dv_timings,
803 	.vidioc_dv_timings_cap		= vidioc_dv_timings_cap,
804 	.vidioc_g_edid			= vidioc_g_edid,
805 	.vidioc_s_edid			= vidioc_s_edid,
806 
807 	.vidioc_log_status		= vidioc_log_status,
808 	.vidioc_subscribe_event		= vidioc_subscribe_event,
809 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
810 
811 	.vidioc_enum_fmt_meta_cap	= vidioc_enum_fmt_meta_cap,
812 	.vidioc_g_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
813 	.vidioc_s_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
814 	.vidioc_try_fmt_meta_cap	= vidioc_g_fmt_meta_cap,
815 
816 	.vidioc_enum_fmt_meta_out       = vidioc_enum_fmt_meta_out,
817 	.vidioc_g_fmt_meta_out          = vidioc_g_fmt_meta_out,
818 	.vidioc_s_fmt_meta_out          = vidioc_g_fmt_meta_out,
819 	.vidioc_try_fmt_meta_out        = vidioc_g_fmt_meta_out,
820 };
821 
822 /* -----------------------------------------------------------------
823 	Initialization and module stuff
824    ------------------------------------------------------------------*/
825 
826 static void vivid_dev_release(struct v4l2_device *v4l2_dev)
827 {
828 	struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
829 
830 	vivid_free_controls(dev);
831 	v4l2_device_unregister(&dev->v4l2_dev);
832 #ifdef CONFIG_MEDIA_CONTROLLER
833 	media_device_cleanup(&dev->mdev);
834 #endif
835 	vfree(dev->scaled_line);
836 	vfree(dev->blended_line);
837 	vfree(dev->edid);
838 	tpg_free(&dev->tpg);
839 	kfree(dev->query_dv_timings_qmenu);
840 	kfree(dev->query_dv_timings_qmenu_strings);
841 	kfree(dev);
842 }
843 
844 #ifdef CONFIG_MEDIA_CONTROLLER
845 static int vivid_req_validate(struct media_request *req)
846 {
847 	struct vivid_dev *dev = container_of(req->mdev, struct vivid_dev, mdev);
848 
849 	if (dev->req_validate_error) {
850 		dev->req_validate_error = false;
851 		return -EINVAL;
852 	}
853 	return vb2_request_validate(req);
854 }
855 
856 static const struct media_device_ops vivid_media_ops = {
857 	.req_validate = vivid_req_validate,
858 	.req_queue = vb2_request_queue,
859 };
860 #endif
861 
862 static int vivid_create_queue(struct vivid_dev *dev,
863 			      struct vb2_queue *q,
864 			      u32 buf_type,
865 			      unsigned int min_reqbufs_allocation,
866 			      const struct vb2_ops *ops)
867 {
868 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->multiplanar)
869 		buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
870 	else if (buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT && dev->multiplanar)
871 		buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
872 	else if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE && !dev->has_raw_vbi_cap)
873 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
874 	else if (buf_type == V4L2_BUF_TYPE_VBI_OUTPUT && !dev->has_raw_vbi_out)
875 		buf_type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
876 
877 	q->type = buf_type;
878 	q->io_modes = VB2_MMAP | VB2_DMABUF;
879 	q->io_modes |= V4L2_TYPE_IS_OUTPUT(buf_type) ?  VB2_WRITE : VB2_READ;
880 
881 	/*
882 	 * The maximum number of buffers is 32768 if PAGE_SHIFT == 12,
883 	 * see also MAX_BUFFER_INDEX in videobuf2-core.c. It will be less if
884 	 * PAGE_SHIFT > 12, but then max_num_buffers will be clamped by
885 	 * videobuf2-core.c to MAX_BUFFER_INDEX.
886 	 */
887 	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
888 		q->max_num_buffers = 64;
889 	if (buf_type == V4L2_BUF_TYPE_SDR_CAPTURE)
890 		q->max_num_buffers = 1024;
891 	if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE)
892 		q->max_num_buffers = 32768;
893 
894 	if (allocators[dev->inst] != 1)
895 		q->io_modes |= VB2_USERPTR;
896 	q->drv_priv = dev;
897 	q->buf_struct_size = sizeof(struct vivid_buffer);
898 	q->ops = ops;
899 	q->mem_ops = allocators[dev->inst] == 1 ? &vb2_dma_contig_memops :
900 						  &vb2_vmalloc_memops;
901 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
902 	q->min_reqbufs_allocation = min_reqbufs_allocation;
903 	q->lock = &dev->mutex;
904 	q->dev = dev->v4l2_dev.dev;
905 	q->supports_requests = supports_requests[dev->inst];
906 	q->requires_requests = supports_requests[dev->inst] >= 2;
907 	q->allow_cache_hints = (cache_hints[dev->inst] == 1);
908 
909 	return vb2_queue_init(q);
910 }
911 
912 static int vivid_detect_feature_set(struct vivid_dev *dev, int inst,
913 				    unsigned node_type,
914 				    bool *has_tuner,
915 				    bool *has_modulator,
916 				    int *ccs_cap,
917 				    int *ccs_out,
918 				    unsigned in_type_counter[4],
919 				    unsigned out_type_counter[4])
920 {
921 	int i;
922 
923 	/* do we use single- or multi-planar? */
924 	dev->multiplanar = multiplanar[inst] > 1;
925 	v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
926 			dev->multiplanar ? "multi" : "single ");
927 
928 	/* how many inputs do we have and of what type? */
929 	dev->num_inputs = num_inputs[inst];
930 	if (node_type & 0x20007) {
931 		if (dev->num_inputs < 1)
932 			dev->num_inputs = 1;
933 	} else {
934 		dev->num_inputs = 0;
935 	}
936 	if (dev->num_inputs >= MAX_INPUTS)
937 		dev->num_inputs = MAX_INPUTS;
938 	for (i = 0; i < dev->num_inputs; i++) {
939 		dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
940 		dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
941 	}
942 	dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
943 	if (in_type_counter[HDMI] == 16) {
944 		/* The CEC physical address only allows for max 15 inputs */
945 		in_type_counter[HDMI]--;
946 		dev->num_inputs--;
947 	}
948 	dev->num_hdmi_inputs = in_type_counter[HDMI];
949 
950 	/* how many outputs do we have and of what type? */
951 	dev->num_outputs = num_outputs[inst];
952 	if (node_type & 0x40300) {
953 		if (dev->num_outputs < 1)
954 			dev->num_outputs = 1;
955 	} else {
956 		dev->num_outputs = 0;
957 	}
958 	if (dev->num_outputs >= MAX_OUTPUTS)
959 		dev->num_outputs = MAX_OUTPUTS;
960 	for (i = 0; i < dev->num_outputs; i++) {
961 		dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
962 		dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
963 		dev->display_present[i] = true;
964 	}
965 	dev->has_audio_outputs = out_type_counter[SVID];
966 	if (out_type_counter[HDMI] == 16) {
967 		/*
968 		 * The CEC physical address only allows for max 15 inputs,
969 		 * so outputs are also limited to 15 to allow for easy
970 		 * CEC output to input mapping.
971 		 */
972 		out_type_counter[HDMI]--;
973 		dev->num_outputs--;
974 	}
975 	dev->num_hdmi_outputs = out_type_counter[HDMI];
976 
977 	/* do we create a video capture device? */
978 	dev->has_vid_cap = node_type & 0x0001;
979 
980 	/* do we create a vbi capture device? */
981 	if (in_type_counter[TV] || in_type_counter[SVID]) {
982 		dev->has_raw_vbi_cap = node_type & 0x0004;
983 		dev->has_sliced_vbi_cap = node_type & 0x0008;
984 		dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
985 	}
986 
987 	/* do we create a meta capture device */
988 	dev->has_meta_cap = node_type & 0x20000;
989 
990 	/* sanity checks */
991 	if ((in_type_counter[WEBCAM] || in_type_counter[HDMI]) &&
992 	    !dev->has_vid_cap && !dev->has_meta_cap) {
993 		v4l2_warn(&dev->v4l2_dev,
994 			  "Webcam or HDMI input without video or metadata nodes\n");
995 		return -EINVAL;
996 	}
997 	if ((in_type_counter[TV] || in_type_counter[SVID]) &&
998 	    !dev->has_vid_cap && !dev->has_vbi_cap && !dev->has_meta_cap) {
999 		v4l2_warn(&dev->v4l2_dev,
1000 			  "TV or S-Video input without video, VBI or metadata nodes\n");
1001 		return -EINVAL;
1002 	}
1003 
1004 	/* do we create a video output device? */
1005 	dev->has_vid_out = node_type & 0x0100;
1006 
1007 	/* do we create a vbi output device? */
1008 	if (out_type_counter[SVID]) {
1009 		dev->has_raw_vbi_out = node_type & 0x0400;
1010 		dev->has_sliced_vbi_out = node_type & 0x0800;
1011 		dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
1012 	}
1013 
1014 	/* do we create a metadata output device */
1015 	dev->has_meta_out = node_type & 0x40000;
1016 
1017 	/* sanity checks */
1018 	if (out_type_counter[SVID] &&
1019 	    !dev->has_vid_out && !dev->has_vbi_out && !dev->has_meta_out) {
1020 		v4l2_warn(&dev->v4l2_dev,
1021 			  "S-Video output without video, VBI or metadata nodes\n");
1022 		return -EINVAL;
1023 	}
1024 	if (out_type_counter[HDMI] && !dev->has_vid_out && !dev->has_meta_out) {
1025 		v4l2_warn(&dev->v4l2_dev,
1026 			  "HDMI output without video or metadata nodes\n");
1027 		return -EINVAL;
1028 	}
1029 
1030 	/* do we create a radio receiver device? */
1031 	dev->has_radio_rx = node_type & 0x0010;
1032 
1033 	/* do we create a radio transmitter device? */
1034 	dev->has_radio_tx = node_type & 0x1000;
1035 
1036 	/* do we create a software defined radio capture device? */
1037 	dev->has_sdr_cap = node_type & 0x0020;
1038 
1039 	/* do we have a TV tuner? */
1040 	dev->has_tv_tuner = in_type_counter[TV];
1041 
1042 	/* do we have a tuner? */
1043 	*has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
1044 		      dev->has_radio_rx || dev->has_sdr_cap;
1045 
1046 	/* do we have a modulator? */
1047 	*has_modulator = dev->has_radio_tx;
1048 
1049 	if (dev->has_vid_cap)
1050 		/* do we have a framebuffer for overlay testing? */
1051 		dev->has_fb = node_type & 0x10000;
1052 
1053 	/* can we do crop/compose/scaling while capturing? */
1054 	if (no_error_inj && *ccs_cap == -1)
1055 		*ccs_cap = 7;
1056 
1057 	/* if ccs_cap == -1, then the user can select it using controls */
1058 	if (*ccs_cap != -1) {
1059 		dev->has_crop_cap = *ccs_cap & 1;
1060 		dev->has_compose_cap = *ccs_cap & 2;
1061 		dev->has_scaler_cap = *ccs_cap & 4;
1062 		v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
1063 			dev->has_crop_cap ? 'Y' : 'N',
1064 			dev->has_compose_cap ? 'Y' : 'N',
1065 			dev->has_scaler_cap ? 'Y' : 'N');
1066 	}
1067 
1068 	/* can we do crop/compose/scaling with video output? */
1069 	if (no_error_inj && *ccs_out == -1)
1070 		*ccs_out = 7;
1071 
1072 	/* if ccs_out == -1, then the user can select it using controls */
1073 	if (*ccs_out != -1) {
1074 		dev->has_crop_out = *ccs_out & 1;
1075 		dev->has_compose_out = *ccs_out & 2;
1076 		dev->has_scaler_out = *ccs_out & 4;
1077 		v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
1078 			dev->has_crop_out ? 'Y' : 'N',
1079 			dev->has_compose_out ? 'Y' : 'N',
1080 			dev->has_scaler_out ? 'Y' : 'N');
1081 	}
1082 
1083 	/* do we create a touch capture device */
1084 	dev->has_touch_cap = node_type & 0x80000;
1085 
1086 	return 0;
1087 }
1088 
1089 static void vivid_set_capabilities(struct vivid_dev *dev)
1090 {
1091 	if (dev->has_vid_cap) {
1092 		/* set up the capabilities of the video capture device */
1093 		dev->vid_cap_caps = dev->multiplanar ?
1094 			V4L2_CAP_VIDEO_CAPTURE_MPLANE :
1095 			V4L2_CAP_VIDEO_CAPTURE;
1096 		dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1097 		if (dev->has_audio_inputs)
1098 			dev->vid_cap_caps |= V4L2_CAP_AUDIO;
1099 		if (dev->has_tv_tuner)
1100 			dev->vid_cap_caps |= V4L2_CAP_TUNER;
1101 	}
1102 	if (dev->has_vid_out) {
1103 		/* set up the capabilities of the video output device */
1104 		dev->vid_out_caps = dev->multiplanar ?
1105 			V4L2_CAP_VIDEO_OUTPUT_MPLANE :
1106 			V4L2_CAP_VIDEO_OUTPUT;
1107 		if (dev->has_fb)
1108 			dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1109 		dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1110 		if (dev->has_audio_outputs)
1111 			dev->vid_out_caps |= V4L2_CAP_AUDIO;
1112 	}
1113 	if (dev->has_vbi_cap) {
1114 		/* set up the capabilities of the vbi capture device */
1115 		dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
1116 				    (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
1117 		dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1118 		if (dev->has_audio_inputs)
1119 			dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
1120 		if (dev->has_tv_tuner)
1121 			dev->vbi_cap_caps |= V4L2_CAP_TUNER;
1122 	}
1123 	if (dev->has_vbi_out) {
1124 		/* set up the capabilities of the vbi output device */
1125 		dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
1126 				    (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
1127 		dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1128 		if (dev->has_audio_outputs)
1129 			dev->vbi_out_caps |= V4L2_CAP_AUDIO;
1130 	}
1131 	if (dev->has_sdr_cap) {
1132 		/* set up the capabilities of the sdr capture device */
1133 		dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
1134 		dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1135 	}
1136 	/* set up the capabilities of the radio receiver device */
1137 	if (dev->has_radio_rx)
1138 		dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
1139 				     V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
1140 				     V4L2_CAP_READWRITE;
1141 	/* set up the capabilities of the radio transmitter device */
1142 	if (dev->has_radio_tx)
1143 		dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
1144 				     V4L2_CAP_READWRITE;
1145 
1146 	/* set up the capabilities of meta capture device */
1147 	if (dev->has_meta_cap) {
1148 		dev->meta_cap_caps = V4L2_CAP_META_CAPTURE |
1149 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1150 		if (dev->has_audio_inputs)
1151 			dev->meta_cap_caps |= V4L2_CAP_AUDIO;
1152 		if (dev->has_tv_tuner)
1153 			dev->meta_cap_caps |= V4L2_CAP_TUNER;
1154 	}
1155 	/* set up the capabilities of meta output device */
1156 	if (dev->has_meta_out) {
1157 		dev->meta_out_caps = V4L2_CAP_META_OUTPUT |
1158 				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1159 		if (dev->has_audio_outputs)
1160 			dev->meta_out_caps |= V4L2_CAP_AUDIO;
1161 	}
1162 	/* set up the capabilities of the touch capture device */
1163 	if (dev->has_touch_cap) {
1164 		dev->touch_cap_caps = V4L2_CAP_TOUCH | V4L2_CAP_STREAMING |
1165 				      V4L2_CAP_READWRITE;
1166 		dev->touch_cap_caps |= dev->multiplanar ?
1167 			V4L2_CAP_VIDEO_CAPTURE_MPLANE : V4L2_CAP_VIDEO_CAPTURE;
1168 	}
1169 }
1170 
1171 static void vivid_disable_unused_ioctls(struct vivid_dev *dev,
1172 					bool has_tuner,
1173 					bool has_modulator,
1174 					unsigned in_type_counter[4],
1175 					unsigned out_type_counter[4])
1176 {
1177 	/* disable invalid ioctls based on the feature set */
1178 	if (!dev->has_audio_inputs) {
1179 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
1180 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
1181 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
1182 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
1183 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
1184 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
1185 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_AUDIO);
1186 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_AUDIO);
1187 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_ENUMAUDIO);
1188 	}
1189 	if (!dev->has_audio_outputs) {
1190 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
1191 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
1192 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
1193 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
1194 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
1195 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
1196 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_AUDOUT);
1197 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_AUDOUT);
1198 		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_ENUMAUDOUT);
1199 	}
1200 	if (!in_type_counter[TV] && !in_type_counter[SVID]) {
1201 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
1202 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
1203 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
1204 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
1205 	}
1206 	if (!out_type_counter[SVID]) {
1207 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
1208 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
1209 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
1210 	}
1211 	if (!has_tuner && !has_modulator) {
1212 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
1213 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
1214 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
1215 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
1216 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_FREQUENCY);
1217 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_FREQUENCY);
1218 	}
1219 	if (!has_tuner) {
1220 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
1221 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
1222 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
1223 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
1224 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_TUNER);
1225 		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_TUNER);
1226 	}
1227 	if (in_type_counter[HDMI] == 0) {
1228 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
1229 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
1230 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
1231 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
1232 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
1233 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
1234 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
1235 	}
1236 	if (out_type_counter[HDMI] == 0) {
1237 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
1238 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
1239 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
1240 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
1241 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
1242 	}
1243 	if (!dev->has_fb) {
1244 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
1245 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
1246 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
1247 	}
1248 	v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1249 	v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1250 	v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1251 	v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1252 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
1253 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
1254 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
1255 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1256 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
1257 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
1258 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_FREQUENCY);
1259 	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_FREQUENCY);
1260 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_S_PARM);
1261 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMESIZES);
1262 	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1263 }
1264 
1265 static int vivid_init_dv_timings(struct vivid_dev *dev)
1266 {
1267 	int i;
1268 
1269 	while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
1270 		dev->query_dv_timings_size++;
1271 
1272 	/*
1273 	 * Create a char pointer array that points to the names of all the
1274 	 * preset timings
1275 	 */
1276 	dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
1277 						    sizeof(char *), GFP_KERNEL);
1278 	/*
1279 	 * Create a string array containing the names of all the preset
1280 	 * timings. Each name is max 31 chars long (+ terminating 0).
1281 	 */
1282 	dev->query_dv_timings_qmenu_strings =
1283 		kmalloc_array(dev->query_dv_timings_size, 32, GFP_KERNEL);
1284 
1285 	if (!dev->query_dv_timings_qmenu ||
1286 	    !dev->query_dv_timings_qmenu_strings)
1287 		return -ENOMEM;
1288 
1289 	for (i = 0; i < dev->query_dv_timings_size; i++) {
1290 		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1291 		char *p = dev->query_dv_timings_qmenu_strings + i * 32;
1292 		u32 htot, vtot;
1293 
1294 		dev->query_dv_timings_qmenu[i] = p;
1295 
1296 		htot = V4L2_DV_BT_FRAME_WIDTH(bt);
1297 		vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
1298 		snprintf(p, 32, "%ux%u%s%u",
1299 			bt->width, bt->height, bt->interlaced ? "i" : "p",
1300 			(u32)bt->pixelclock / (htot * vtot));
1301 	}
1302 
1303 	return 0;
1304 }
1305 
1306 static int vivid_create_queues(struct vivid_dev *dev)
1307 {
1308 	int ret;
1309 
1310 	/* start creating the vb2 queues */
1311 	if (dev->has_vid_cap) {
1312 		/* initialize vid_cap queue */
1313 		ret = vivid_create_queue(dev, &dev->vb_vid_cap_q,
1314 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1315 					 &vivid_vid_cap_qops);
1316 		if (ret)
1317 			return ret;
1318 	}
1319 
1320 	if (dev->has_vid_out) {
1321 		/* initialize vid_out queue */
1322 		ret = vivid_create_queue(dev, &dev->vb_vid_out_q,
1323 					 V4L2_BUF_TYPE_VIDEO_OUTPUT, 2,
1324 					 &vivid_vid_out_qops);
1325 		if (ret)
1326 			return ret;
1327 	}
1328 
1329 	if (dev->has_vbi_cap) {
1330 		/* initialize vbi_cap queue */
1331 		ret = vivid_create_queue(dev, &dev->vb_vbi_cap_q,
1332 					 V4L2_BUF_TYPE_VBI_CAPTURE, 2,
1333 					 &vivid_vbi_cap_qops);
1334 		if (ret)
1335 			return ret;
1336 	}
1337 
1338 	if (dev->has_vbi_out) {
1339 		/* initialize vbi_out queue */
1340 		ret = vivid_create_queue(dev, &dev->vb_vbi_out_q,
1341 					 V4L2_BUF_TYPE_VBI_OUTPUT, 2,
1342 					 &vivid_vbi_out_qops);
1343 		if (ret)
1344 			return ret;
1345 	}
1346 
1347 	if (dev->has_sdr_cap) {
1348 		/* initialize sdr_cap queue */
1349 		ret = vivid_create_queue(dev, &dev->vb_sdr_cap_q,
1350 					 V4L2_BUF_TYPE_SDR_CAPTURE, 8,
1351 					 &vivid_sdr_cap_qops);
1352 		if (ret)
1353 			return ret;
1354 	}
1355 
1356 	if (dev->has_meta_cap) {
1357 		/* initialize meta_cap queue */
1358 		ret = vivid_create_queue(dev, &dev->vb_meta_cap_q,
1359 					 V4L2_BUF_TYPE_META_CAPTURE, 2,
1360 					 &vivid_meta_cap_qops);
1361 		if (ret)
1362 			return ret;
1363 	}
1364 
1365 	if (dev->has_meta_out) {
1366 		/* initialize meta_out queue */
1367 		ret = vivid_create_queue(dev, &dev->vb_meta_out_q,
1368 					 V4L2_BUF_TYPE_META_OUTPUT, 2,
1369 					 &vivid_meta_out_qops);
1370 		if (ret)
1371 			return ret;
1372 	}
1373 
1374 	if (dev->has_touch_cap) {
1375 		/* initialize touch_cap queue */
1376 		ret = vivid_create_queue(dev, &dev->vb_touch_cap_q,
1377 					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1378 					 &vivid_touch_cap_qops);
1379 		if (ret)
1380 			return ret;
1381 	}
1382 
1383 	if (dev->has_fb) {
1384 		/* Create framebuffer for testing output overlay */
1385 		ret = vivid_fb_init(dev);
1386 		if (ret)
1387 			return ret;
1388 		v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1389 			  dev->fb_info.node);
1390 	}
1391 	return 0;
1392 }
1393 
1394 static int vivid_create_devnodes(struct platform_device *pdev,
1395 				 struct vivid_dev *dev, int inst,
1396 				 unsigned int cec_tx_bus_cnt,
1397 				 v4l2_std_id tvnorms_cap,
1398 				 v4l2_std_id tvnorms_out,
1399 				 unsigned in_type_counter[4],
1400 				 unsigned out_type_counter[4])
1401 {
1402 	struct video_device *vfd;
1403 	int ret;
1404 
1405 	if (dev->has_vid_cap) {
1406 		vfd = &dev->vid_cap_dev;
1407 		snprintf(vfd->name, sizeof(vfd->name),
1408 			 "vivid-%03d-vid-cap", inst);
1409 		vfd->fops = &vivid_fops;
1410 		vfd->ioctl_ops = &vivid_ioctl_ops;
1411 		vfd->device_caps = dev->vid_cap_caps;
1412 		vfd->release = video_device_release_empty;
1413 		vfd->v4l2_dev = &dev->v4l2_dev;
1414 		vfd->queue = &dev->vb_vid_cap_q;
1415 		vfd->tvnorms = tvnorms_cap;
1416 
1417 		/*
1418 		 * Provide a mutex to v4l2 core. It will be used to protect
1419 		 * all fops and v4l2 ioctls.
1420 		 */
1421 		vfd->lock = &dev->mutex;
1422 		video_set_drvdata(vfd, dev);
1423 
1424 #ifdef CONFIG_MEDIA_CONTROLLER
1425 		dev->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
1426 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_cap_pad);
1427 		if (ret)
1428 			return ret;
1429 #endif
1430 
1431 #ifdef CONFIG_VIDEO_VIVID_CEC
1432 		if (in_type_counter[HDMI]) {
1433 			ret = cec_register_adapter(dev->cec_rx_adap, &pdev->dev);
1434 			if (ret < 0) {
1435 				cec_delete_adapter(dev->cec_rx_adap);
1436 				dev->cec_rx_adap = NULL;
1437 				return ret;
1438 			}
1439 			cec_s_phys_addr(dev->cec_rx_adap, 0, false);
1440 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input 0\n",
1441 				  dev_name(&dev->cec_rx_adap->devnode.dev));
1442 		}
1443 #endif
1444 
1445 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_cap_nr[inst]);
1446 		if (ret < 0)
1447 			return ret;
1448 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1449 					  video_device_node_name(vfd));
1450 	}
1451 
1452 	if (dev->has_vid_out) {
1453 #ifdef CONFIG_VIDEO_VIVID_CEC
1454 		int i;
1455 #endif
1456 		vfd = &dev->vid_out_dev;
1457 		snprintf(vfd->name, sizeof(vfd->name),
1458 			 "vivid-%03d-vid-out", inst);
1459 		vfd->vfl_dir = VFL_DIR_TX;
1460 		vfd->fops = &vivid_fops;
1461 		vfd->ioctl_ops = &vivid_ioctl_ops;
1462 		vfd->device_caps = dev->vid_out_caps;
1463 		vfd->release = video_device_release_empty;
1464 		vfd->v4l2_dev = &dev->v4l2_dev;
1465 		vfd->queue = &dev->vb_vid_out_q;
1466 		vfd->tvnorms = tvnorms_out;
1467 
1468 		/*
1469 		 * Provide a mutex to v4l2 core. It will be used to protect
1470 		 * all fops and v4l2 ioctls.
1471 		 */
1472 		vfd->lock = &dev->mutex;
1473 		video_set_drvdata(vfd, dev);
1474 
1475 #ifdef CONFIG_MEDIA_CONTROLLER
1476 		dev->vid_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1477 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_out_pad);
1478 		if (ret)
1479 			return ret;
1480 #endif
1481 
1482 #ifdef CONFIG_VIDEO_VIVID_CEC
1483 		for (i = 0; i < cec_tx_bus_cnt; i++) {
1484 			ret = cec_register_adapter(dev->cec_tx_adap[i], &pdev->dev);
1485 			if (ret < 0) {
1486 				for (; i < cec_tx_bus_cnt; i++) {
1487 					cec_delete_adapter(dev->cec_tx_adap[i]);
1488 					dev->cec_tx_adap[i] = NULL;
1489 				}
1490 				return ret;
1491 			}
1492 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1493 				  dev_name(&dev->cec_tx_adap[i]->devnode.dev), i);
1494 			if (i < out_type_counter[HDMI])
1495 				cec_s_phys_addr(dev->cec_tx_adap[i], (i + 1) << 12, false);
1496 			else
1497 				cec_s_phys_addr(dev->cec_tx_adap[i], 0x1000, false);
1498 		}
1499 #endif
1500 
1501 		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_out_nr[inst]);
1502 		if (ret < 0)
1503 			return ret;
1504 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1505 					  video_device_node_name(vfd));
1506 	}
1507 
1508 	if (dev->has_vbi_cap) {
1509 		vfd = &dev->vbi_cap_dev;
1510 		snprintf(vfd->name, sizeof(vfd->name),
1511 			 "vivid-%03d-vbi-cap", inst);
1512 		vfd->fops = &vivid_fops;
1513 		vfd->ioctl_ops = &vivid_ioctl_ops;
1514 		vfd->device_caps = dev->vbi_cap_caps;
1515 		vfd->release = video_device_release_empty;
1516 		vfd->v4l2_dev = &dev->v4l2_dev;
1517 		vfd->queue = &dev->vb_vbi_cap_q;
1518 		vfd->lock = &dev->mutex;
1519 		vfd->tvnorms = tvnorms_cap;
1520 		video_set_drvdata(vfd, dev);
1521 
1522 #ifdef CONFIG_MEDIA_CONTROLLER
1523 		dev->vbi_cap_pad.flags = MEDIA_PAD_FL_SINK;
1524 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_cap_pad);
1525 		if (ret)
1526 			return ret;
1527 #endif
1528 
1529 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1530 		if (ret < 0)
1531 			return ret;
1532 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1533 					  video_device_node_name(vfd),
1534 					  (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1535 					  "raw and sliced" :
1536 					  (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1537 	}
1538 
1539 	if (dev->has_vbi_out) {
1540 		vfd = &dev->vbi_out_dev;
1541 		snprintf(vfd->name, sizeof(vfd->name),
1542 			 "vivid-%03d-vbi-out", inst);
1543 		vfd->vfl_dir = VFL_DIR_TX;
1544 		vfd->fops = &vivid_fops;
1545 		vfd->ioctl_ops = &vivid_ioctl_ops;
1546 		vfd->device_caps = dev->vbi_out_caps;
1547 		vfd->release = video_device_release_empty;
1548 		vfd->v4l2_dev = &dev->v4l2_dev;
1549 		vfd->queue = &dev->vb_vbi_out_q;
1550 		vfd->lock = &dev->mutex;
1551 		vfd->tvnorms = tvnorms_out;
1552 		video_set_drvdata(vfd, dev);
1553 
1554 #ifdef CONFIG_MEDIA_CONTROLLER
1555 		dev->vbi_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1556 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_out_pad);
1557 		if (ret)
1558 			return ret;
1559 #endif
1560 
1561 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1562 		if (ret < 0)
1563 			return ret;
1564 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1565 					  video_device_node_name(vfd),
1566 					  (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1567 					  "raw and sliced" :
1568 					  (dev->has_raw_vbi_out ? "raw" : "sliced"));
1569 	}
1570 
1571 	if (dev->has_sdr_cap) {
1572 		vfd = &dev->sdr_cap_dev;
1573 		snprintf(vfd->name, sizeof(vfd->name),
1574 			 "vivid-%03d-sdr-cap", inst);
1575 		vfd->fops = &vivid_fops;
1576 		vfd->ioctl_ops = &vivid_ioctl_ops;
1577 		vfd->device_caps = dev->sdr_cap_caps;
1578 		vfd->release = video_device_release_empty;
1579 		vfd->v4l2_dev = &dev->v4l2_dev;
1580 		vfd->queue = &dev->vb_sdr_cap_q;
1581 		vfd->lock = &dev->mutex;
1582 		video_set_drvdata(vfd, dev);
1583 
1584 #ifdef CONFIG_MEDIA_CONTROLLER
1585 		dev->sdr_cap_pad.flags = MEDIA_PAD_FL_SINK;
1586 		ret = media_entity_pads_init(&vfd->entity, 1, &dev->sdr_cap_pad);
1587 		if (ret)
1588 			return ret;
1589 #endif
1590 
1591 		ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1592 		if (ret < 0)
1593 			return ret;
1594 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1595 					  video_device_node_name(vfd));
1596 	}
1597 
1598 	if (dev->has_radio_rx) {
1599 		vfd = &dev->radio_rx_dev;
1600 		snprintf(vfd->name, sizeof(vfd->name),
1601 			 "vivid-%03d-rad-rx", inst);
1602 		vfd->fops = &vivid_radio_fops;
1603 		vfd->ioctl_ops = &vivid_ioctl_ops;
1604 		vfd->device_caps = dev->radio_rx_caps;
1605 		vfd->release = video_device_release_empty;
1606 		vfd->v4l2_dev = &dev->v4l2_dev;
1607 		vfd->lock = &dev->mutex;
1608 		video_set_drvdata(vfd, dev);
1609 
1610 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1611 		if (ret < 0)
1612 			return ret;
1613 		v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1614 					  video_device_node_name(vfd));
1615 	}
1616 
1617 	if (dev->has_radio_tx) {
1618 		vfd = &dev->radio_tx_dev;
1619 		snprintf(vfd->name, sizeof(vfd->name),
1620 			 "vivid-%03d-rad-tx", inst);
1621 		vfd->vfl_dir = VFL_DIR_TX;
1622 		vfd->fops = &vivid_radio_fops;
1623 		vfd->ioctl_ops = &vivid_ioctl_ops;
1624 		vfd->device_caps = dev->radio_tx_caps;
1625 		vfd->release = video_device_release_empty;
1626 		vfd->v4l2_dev = &dev->v4l2_dev;
1627 		vfd->lock = &dev->mutex;
1628 		video_set_drvdata(vfd, dev);
1629 
1630 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1631 		if (ret < 0)
1632 			return ret;
1633 		v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1634 					  video_device_node_name(vfd));
1635 	}
1636 
1637 	if (dev->has_meta_cap) {
1638 		vfd = &dev->meta_cap_dev;
1639 		snprintf(vfd->name, sizeof(vfd->name),
1640 			 "vivid-%03d-meta-cap", inst);
1641 		vfd->fops = &vivid_fops;
1642 		vfd->ioctl_ops = &vivid_ioctl_ops;
1643 		vfd->device_caps = dev->meta_cap_caps;
1644 		vfd->release = video_device_release_empty;
1645 		vfd->v4l2_dev = &dev->v4l2_dev;
1646 		vfd->queue = &dev->vb_meta_cap_q;
1647 		vfd->lock = &dev->mutex;
1648 		vfd->tvnorms = tvnorms_cap;
1649 		video_set_drvdata(vfd, dev);
1650 #ifdef CONFIG_MEDIA_CONTROLLER
1651 		dev->meta_cap_pad.flags = MEDIA_PAD_FL_SINK;
1652 		ret = media_entity_pads_init(&vfd->entity, 1,
1653 					     &dev->meta_cap_pad);
1654 		if (ret)
1655 			return ret;
1656 #endif
1657 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1658 					    meta_cap_nr[inst]);
1659 		if (ret < 0)
1660 			return ret;
1661 		v4l2_info(&dev->v4l2_dev,
1662 			  "V4L2 metadata capture device registered as %s\n",
1663 			  video_device_node_name(vfd));
1664 	}
1665 
1666 	if (dev->has_meta_out) {
1667 		vfd = &dev->meta_out_dev;
1668 		snprintf(vfd->name, sizeof(vfd->name),
1669 			 "vivid-%03d-meta-out", inst);
1670 		vfd->vfl_dir = VFL_DIR_TX;
1671 		vfd->fops = &vivid_fops;
1672 		vfd->ioctl_ops = &vivid_ioctl_ops;
1673 		vfd->device_caps = dev->meta_out_caps;
1674 		vfd->release = video_device_release_empty;
1675 		vfd->v4l2_dev = &dev->v4l2_dev;
1676 		vfd->queue = &dev->vb_meta_out_q;
1677 		vfd->lock = &dev->mutex;
1678 		vfd->tvnorms = tvnorms_out;
1679 		video_set_drvdata(vfd, dev);
1680 #ifdef CONFIG_MEDIA_CONTROLLER
1681 		dev->meta_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1682 		ret = media_entity_pads_init(&vfd->entity, 1,
1683 					     &dev->meta_out_pad);
1684 		if (ret)
1685 			return ret;
1686 #endif
1687 		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1688 					    meta_out_nr[inst]);
1689 		if (ret < 0)
1690 			return ret;
1691 		v4l2_info(&dev->v4l2_dev,
1692 			  "V4L2 metadata output device registered as %s\n",
1693 			  video_device_node_name(vfd));
1694 	}
1695 
1696 	if (dev->has_touch_cap) {
1697 		vfd = &dev->touch_cap_dev;
1698 		snprintf(vfd->name, sizeof(vfd->name),
1699 			 "vivid-%03d-touch-cap", inst);
1700 		vfd->fops = &vivid_fops;
1701 		vfd->ioctl_ops = &vivid_ioctl_ops;
1702 		vfd->device_caps = dev->touch_cap_caps;
1703 		vfd->release = video_device_release_empty;
1704 		vfd->v4l2_dev = &dev->v4l2_dev;
1705 		vfd->queue = &dev->vb_touch_cap_q;
1706 		vfd->tvnorms = tvnorms_cap;
1707 		vfd->lock = &dev->mutex;
1708 		video_set_drvdata(vfd, dev);
1709 #ifdef CONFIG_MEDIA_CONTROLLER
1710 		dev->touch_cap_pad.flags = MEDIA_PAD_FL_SINK;
1711 		ret = media_entity_pads_init(&vfd->entity, 1,
1712 					     &dev->touch_cap_pad);
1713 		if (ret)
1714 			return ret;
1715 #endif
1716 		ret = video_register_device(vfd, VFL_TYPE_TOUCH,
1717 					    touch_cap_nr[inst]);
1718 		if (ret < 0)
1719 			return ret;
1720 		v4l2_info(&dev->v4l2_dev,
1721 			  "V4L2 touch capture device registered as %s\n",
1722 			  video_device_node_name(vfd));
1723 	}
1724 
1725 #ifdef CONFIG_MEDIA_CONTROLLER
1726 	/* Register the media device */
1727 	ret = media_device_register(&dev->mdev);
1728 	if (ret) {
1729 		dev_err(dev->mdev.dev,
1730 			"media device register failed (err=%d)\n", ret);
1731 		return ret;
1732 	}
1733 #endif
1734 	return 0;
1735 }
1736 
1737 static int vivid_create_instance(struct platform_device *pdev, int inst)
1738 {
1739 	static const struct v4l2_dv_timings def_dv_timings =
1740 					V4L2_DV_BT_CEA_1280X720P60;
1741 	unsigned in_type_counter[4] = { 0, 0, 0, 0 };
1742 	unsigned out_type_counter[4] = { 0, 0, 0, 0 };
1743 	int ccs_cap = ccs_cap_mode[inst];
1744 	int ccs_out = ccs_out_mode[inst];
1745 	bool has_tuner;
1746 	bool has_modulator;
1747 	struct vivid_dev *dev;
1748 	unsigned node_type = node_types[inst];
1749 	v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
1750 	unsigned int cec_tx_bus_cnt = 0;
1751 	int ret;
1752 	int i;
1753 
1754 	/* allocate main vivid state structure */
1755 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1756 	if (!dev)
1757 		return -ENOMEM;
1758 
1759 	dev->inst = inst;
1760 
1761 #ifdef CONFIG_MEDIA_CONTROLLER
1762 	dev->v4l2_dev.mdev = &dev->mdev;
1763 
1764 	/* Initialize media device */
1765 	strscpy(dev->mdev.model, VIVID_MODULE_NAME, sizeof(dev->mdev.model));
1766 	snprintf(dev->mdev.bus_info, sizeof(dev->mdev.bus_info),
1767 		 "platform:%s-%03d", VIVID_MODULE_NAME, inst);
1768 	dev->mdev.dev = &pdev->dev;
1769 	media_device_init(&dev->mdev);
1770 	dev->mdev.ops = &vivid_media_ops;
1771 #endif
1772 
1773 	/* register v4l2_device */
1774 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1775 			"%s-%03d", VIVID_MODULE_NAME, inst);
1776 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1777 	if (ret) {
1778 		kfree(dev);
1779 		return ret;
1780 	}
1781 	dev->v4l2_dev.release = vivid_dev_release;
1782 
1783 	ret = vivid_detect_feature_set(dev, inst, node_type,
1784 				       &has_tuner, &has_modulator,
1785 				       &ccs_cap, &ccs_out,
1786 				       in_type_counter, out_type_counter);
1787 	if (ret)
1788 		goto free_dev;
1789 
1790 	vivid_set_capabilities(dev);
1791 
1792 	ret = -ENOMEM;
1793 	/* initialize the test pattern generator */
1794 	tpg_init(&dev->tpg, 640, 360);
1795 	if (tpg_alloc(&dev->tpg, array_size(MAX_WIDTH, MAX_ZOOM)))
1796 		goto free_dev;
1797 	dev->scaled_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1798 	if (!dev->scaled_line)
1799 		goto free_dev;
1800 	dev->blended_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
1801 	if (!dev->blended_line)
1802 		goto free_dev;
1803 
1804 	/* load the edid */
1805 	dev->edid = vmalloc(array_size(256, 128));
1806 	if (!dev->edid)
1807 		goto free_dev;
1808 
1809 	ret = vivid_init_dv_timings(dev);
1810 	if (ret < 0)
1811 		goto free_dev;
1812 
1813 	vivid_disable_unused_ioctls(dev, has_tuner, has_modulator,
1814 				    in_type_counter, out_type_counter);
1815 
1816 	/* configure internal data */
1817 	dev->fmt_cap = &vivid_formats[0];
1818 	dev->fmt_out = &vivid_formats[0];
1819 	if (!dev->multiplanar)
1820 		vivid_formats[0].data_offset[0] = 0;
1821 	dev->webcam_size_idx = 1;
1822 	dev->webcam_ival_idx = 3;
1823 	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
1824 	dev->std_out = V4L2_STD_PAL;
1825 	if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
1826 		tvnorms_cap = V4L2_STD_ALL;
1827 	if (dev->output_type[0] == SVID)
1828 		tvnorms_out = V4L2_STD_ALL;
1829 	for (i = 0; i < MAX_INPUTS; i++) {
1830 		dev->dv_timings_cap[i] = def_dv_timings;
1831 		dev->std_cap[i] = V4L2_STD_PAL;
1832 	}
1833 	dev->dv_timings_out = def_dv_timings;
1834 	dev->tv_freq = 2804 /* 175.25 * 16 */;
1835 	dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
1836 	dev->tv_field_cap = V4L2_FIELD_INTERLACED;
1837 	dev->tv_field_out = V4L2_FIELD_INTERLACED;
1838 	dev->radio_rx_freq = 95000 * 16;
1839 	dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
1840 	if (dev->has_radio_tx) {
1841 		dev->radio_tx_freq = 95500 * 16;
1842 		dev->radio_rds_loop = false;
1843 	}
1844 	dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
1845 	dev->sdr_adc_freq = 300000;
1846 	dev->sdr_fm_freq = 50000000;
1847 	dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
1848 	dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
1849 
1850 	dev->edid_max_blocks = dev->edid_blocks = 2;
1851 	memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
1852 	dev->radio_rds_init_time = ktime_get();
1853 
1854 	/* create all controls */
1855 	ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
1856 			in_type_counter[TV] || in_type_counter[SVID] ||
1857 			out_type_counter[SVID],
1858 			in_type_counter[HDMI] || out_type_counter[HDMI]);
1859 	if (ret)
1860 		goto unreg_dev;
1861 
1862 	/* enable/disable interface specific controls */
1863 	if (dev->num_outputs && dev->output_type[0] != HDMI)
1864 		v4l2_ctrl_activate(dev->ctrl_display_present, false);
1865 	if (dev->num_inputs && dev->input_type[0] != HDMI) {
1866 		v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode, false);
1867 		v4l2_ctrl_activate(dev->ctrl_dv_timings, false);
1868 	} else if (dev->num_inputs && dev->input_type[0] == HDMI) {
1869 		v4l2_ctrl_activate(dev->ctrl_std_signal_mode, false);
1870 		v4l2_ctrl_activate(dev->ctrl_standard, false);
1871 	}
1872 
1873 	/*
1874 	 * update the capture and output formats to do a proper initial
1875 	 * configuration.
1876 	 */
1877 	vivid_update_format_cap(dev, false);
1878 	vivid_update_format_out(dev);
1879 
1880 	/* update touch configuration */
1881 	dev->timeperframe_tch_cap.numerator = 1;
1882 	dev->timeperframe_tch_cap.denominator = 10;
1883 	vivid_set_touch(dev, 0);
1884 
1885 	/* initialize locks */
1886 	spin_lock_init(&dev->slock);
1887 	mutex_init(&dev->mutex);
1888 
1889 	/* init dma queues */
1890 	INIT_LIST_HEAD(&dev->vid_cap_active);
1891 	INIT_LIST_HEAD(&dev->vid_out_active);
1892 	INIT_LIST_HEAD(&dev->vbi_cap_active);
1893 	INIT_LIST_HEAD(&dev->vbi_out_active);
1894 	INIT_LIST_HEAD(&dev->sdr_cap_active);
1895 	INIT_LIST_HEAD(&dev->meta_cap_active);
1896 	INIT_LIST_HEAD(&dev->meta_out_active);
1897 	INIT_LIST_HEAD(&dev->touch_cap_active);
1898 
1899 	spin_lock_init(&dev->cec_xfers_slock);
1900 
1901 	if (allocators[inst] == 1)
1902 		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1903 
1904 	ret = vivid_create_queues(dev);
1905 	if (ret)
1906 		goto unreg_dev;
1907 
1908 #ifdef CONFIG_VIDEO_VIVID_CEC
1909 	if (dev->has_vid_cap && in_type_counter[HDMI]) {
1910 		struct cec_adapter *adap;
1911 
1912 		adap = vivid_cec_alloc_adap(dev, 0, false);
1913 		ret = PTR_ERR_OR_ZERO(adap);
1914 		if (ret < 0)
1915 			goto unreg_dev;
1916 		dev->cec_rx_adap = adap;
1917 	}
1918 
1919 	if (dev->has_vid_out) {
1920 		for (i = 0; i < dev->num_outputs; i++) {
1921 			struct cec_adapter *adap;
1922 
1923 			if (dev->output_type[i] != HDMI)
1924 				continue;
1925 
1926 			dev->cec_output2bus_map[i] = cec_tx_bus_cnt;
1927 			adap = vivid_cec_alloc_adap(dev, cec_tx_bus_cnt, true);
1928 			ret = PTR_ERR_OR_ZERO(adap);
1929 			if (ret < 0) {
1930 				for (i = 0; i < dev->num_outputs; i++)
1931 					cec_delete_adapter(dev->cec_tx_adap[i]);
1932 				goto unreg_dev;
1933 			}
1934 
1935 			dev->cec_tx_adap[cec_tx_bus_cnt] = adap;
1936 			cec_tx_bus_cnt++;
1937 		}
1938 	}
1939 
1940 	if (dev->cec_rx_adap || cec_tx_bus_cnt) {
1941 		init_waitqueue_head(&dev->kthread_waitq_cec);
1942 		dev->kthread_cec = kthread_run(vivid_cec_bus_thread, dev,
1943 					       "vivid_cec-%s", dev->v4l2_dev.name);
1944 		if (IS_ERR(dev->kthread_cec)) {
1945 			ret = PTR_ERR(dev->kthread_cec);
1946 			dev->kthread_cec = NULL;
1947 			v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
1948 			goto unreg_dev;
1949 		}
1950 	}
1951 
1952 #endif
1953 
1954 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
1955 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
1956 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
1957 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
1958 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
1959 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
1960 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
1961 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_cap);
1962 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_out);
1963 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_touch_cap);
1964 
1965 	/* finally start creating the device nodes */
1966 	ret = vivid_create_devnodes(pdev, dev, inst, cec_tx_bus_cnt,
1967 				    tvnorms_cap, tvnorms_out,
1968 				    in_type_counter, out_type_counter);
1969 	if (ret)
1970 		goto unreg_dev;
1971 
1972 	/* Now that everything is fine, let's add it to device list */
1973 	vivid_devs[inst] = dev;
1974 
1975 	return 0;
1976 
1977 unreg_dev:
1978 	vb2_video_unregister_device(&dev->touch_cap_dev);
1979 	vb2_video_unregister_device(&dev->meta_out_dev);
1980 	vb2_video_unregister_device(&dev->meta_cap_dev);
1981 	video_unregister_device(&dev->radio_tx_dev);
1982 	video_unregister_device(&dev->radio_rx_dev);
1983 	vb2_video_unregister_device(&dev->sdr_cap_dev);
1984 	vb2_video_unregister_device(&dev->vbi_out_dev);
1985 	vb2_video_unregister_device(&dev->vbi_cap_dev);
1986 	vb2_video_unregister_device(&dev->vid_out_dev);
1987 	vb2_video_unregister_device(&dev->vid_cap_dev);
1988 	cec_unregister_adapter(dev->cec_rx_adap);
1989 	for (i = 0; i < MAX_OUTPUTS; i++)
1990 		cec_unregister_adapter(dev->cec_tx_adap[i]);
1991 	if (dev->kthread_cec)
1992 		kthread_stop(dev->kthread_cec);
1993 free_dev:
1994 	v4l2_device_put(&dev->v4l2_dev);
1995 	return ret;
1996 }
1997 
1998 /* This routine allocates from 1 to n_devs virtual drivers.
1999 
2000    The real maximum number of virtual drivers will depend on how many drivers
2001    will succeed. This is limited to the maximum number of devices that
2002    videodev supports, which is equal to VIDEO_NUM_DEVICES.
2003  */
2004 static int vivid_probe(struct platform_device *pdev)
2005 {
2006 	const struct font_desc *font = find_font("VGA8x16");
2007 	int ret = 0, i;
2008 
2009 	if (font == NULL) {
2010 		pr_err("vivid: could not find font\n");
2011 		return -ENODEV;
2012 	}
2013 
2014 	tpg_set_font(font->data);
2015 
2016 	n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
2017 
2018 	for (i = 0; i < n_devs; i++) {
2019 		ret = vivid_create_instance(pdev, i);
2020 		if (ret) {
2021 			/* If some instantiations succeeded, keep driver */
2022 			if (i)
2023 				ret = 0;
2024 			break;
2025 		}
2026 	}
2027 
2028 	if (ret < 0) {
2029 		pr_err("vivid: error %d while loading driver\n", ret);
2030 		return ret;
2031 	}
2032 
2033 	/* n_devs will reflect the actual number of allocated devices */
2034 	n_devs = i;
2035 
2036 	return ret;
2037 }
2038 
2039 static void vivid_remove(struct platform_device *pdev)
2040 {
2041 	struct vivid_dev *dev;
2042 	unsigned int i, j;
2043 
2044 	for (i = 0; i < n_devs; i++) {
2045 		dev = vivid_devs[i];
2046 		if (!dev)
2047 			continue;
2048 
2049 		if (dev->disconnect_error)
2050 			vivid_reconnect(dev);
2051 #ifdef CONFIG_MEDIA_CONTROLLER
2052 		media_device_unregister(&dev->mdev);
2053 #endif
2054 
2055 		if (dev->has_vid_cap) {
2056 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2057 				video_device_node_name(&dev->vid_cap_dev));
2058 			vb2_video_unregister_device(&dev->vid_cap_dev);
2059 		}
2060 		if (dev->has_vid_out) {
2061 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2062 				video_device_node_name(&dev->vid_out_dev));
2063 			vb2_video_unregister_device(&dev->vid_out_dev);
2064 		}
2065 		if (dev->has_vbi_cap) {
2066 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2067 				video_device_node_name(&dev->vbi_cap_dev));
2068 			vb2_video_unregister_device(&dev->vbi_cap_dev);
2069 		}
2070 		if (dev->has_vbi_out) {
2071 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2072 				video_device_node_name(&dev->vbi_out_dev));
2073 			vb2_video_unregister_device(&dev->vbi_out_dev);
2074 		}
2075 		if (dev->has_sdr_cap) {
2076 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2077 				video_device_node_name(&dev->sdr_cap_dev));
2078 			vb2_video_unregister_device(&dev->sdr_cap_dev);
2079 		}
2080 		if (dev->has_radio_rx) {
2081 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2082 				video_device_node_name(&dev->radio_rx_dev));
2083 			video_unregister_device(&dev->radio_rx_dev);
2084 		}
2085 		if (dev->has_radio_tx) {
2086 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2087 				video_device_node_name(&dev->radio_tx_dev));
2088 			video_unregister_device(&dev->radio_tx_dev);
2089 		}
2090 		if (dev->has_fb) {
2091 			v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
2092 				dev->fb_info.node);
2093 			unregister_framebuffer(&dev->fb_info);
2094 			vivid_fb_release_buffers(dev);
2095 		}
2096 		if (dev->has_meta_cap) {
2097 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2098 				  video_device_node_name(&dev->meta_cap_dev));
2099 			vb2_video_unregister_device(&dev->meta_cap_dev);
2100 		}
2101 		if (dev->has_meta_out) {
2102 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2103 				  video_device_node_name(&dev->meta_out_dev));
2104 			vb2_video_unregister_device(&dev->meta_out_dev);
2105 		}
2106 		if (dev->has_touch_cap) {
2107 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2108 				  video_device_node_name(&dev->touch_cap_dev));
2109 			vb2_video_unregister_device(&dev->touch_cap_dev);
2110 		}
2111 		cec_unregister_adapter(dev->cec_rx_adap);
2112 		for (j = 0; j < MAX_OUTPUTS; j++)
2113 			cec_unregister_adapter(dev->cec_tx_adap[j]);
2114 		if (dev->kthread_cec)
2115 			kthread_stop(dev->kthread_cec);
2116 		v4l2_device_put(&dev->v4l2_dev);
2117 		vivid_devs[i] = NULL;
2118 	}
2119 }
2120 
2121 static void vivid_pdev_release(struct device *dev)
2122 {
2123 }
2124 
2125 static struct platform_device vivid_pdev = {
2126 	.name		= "vivid",
2127 	.dev.release	= vivid_pdev_release,
2128 };
2129 
2130 static struct platform_driver vivid_pdrv = {
2131 	.probe		= vivid_probe,
2132 	.remove_new	= vivid_remove,
2133 	.driver		= {
2134 		.name	= "vivid",
2135 	},
2136 };
2137 
2138 static int __init vivid_init(void)
2139 {
2140 	int ret;
2141 
2142 	ret = platform_device_register(&vivid_pdev);
2143 	if (ret)
2144 		return ret;
2145 
2146 	ret = platform_driver_register(&vivid_pdrv);
2147 	if (ret)
2148 		platform_device_unregister(&vivid_pdev);
2149 
2150 	return ret;
2151 }
2152 
2153 static void __exit vivid_exit(void)
2154 {
2155 	platform_driver_unregister(&vivid_pdrv);
2156 	platform_device_unregister(&vivid_pdev);
2157 }
2158 
2159 module_init(vivid_init);
2160 module_exit(vivid_exit);
2161