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