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