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