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