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