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