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 
41 #define VIVID_MODULE_NAME "vivid"
42 
43 /* The maximum number of vivid devices */
44 #define VIVID_MAX_DEVS CONFIG_VIDEO_VIVID_MAX_DEVS
45 
46 MODULE_DESCRIPTION("Virtual Video Test Driver");
47 MODULE_AUTHOR("Hans Verkuil");
48 MODULE_LICENSE("GPL");
49 
50 static unsigned n_devs = 1;
51 module_param(n_devs, uint, 0444);
52 MODULE_PARM_DESC(n_devs, " number of driver instances to create");
53 
54 static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
55 module_param_array(vid_cap_nr, int, NULL, 0444);
56 MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
57 
58 static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
59 module_param_array(vid_out_nr, int, NULL, 0444);
60 MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
61 
62 static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
63 module_param_array(vbi_cap_nr, int, NULL, 0444);
64 MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
65 
66 static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
67 module_param_array(vbi_out_nr, int, NULL, 0444);
68 MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
69 
70 static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
71 module_param_array(sdr_cap_nr, int, NULL, 0444);
72 MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
73 
74 static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
75 module_param_array(radio_rx_nr, int, NULL, 0444);
76 MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
77 
78 static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
79 module_param_array(radio_tx_nr, int, NULL, 0444);
80 MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
81 
82 static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
83 module_param_array(ccs_cap_mode, int, NULL, 0444);
84 MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
85 			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
86 			   "\t\t    -1=user-controlled (default)");
87 
88 static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
89 module_param_array(ccs_out_mode, int, NULL, 0444);
90 MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
91 			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
92 			   "\t\t    -1=user-controlled (default)");
93 
94 static unsigned multiplanar[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 1 };
95 module_param_array(multiplanar, uint, NULL, 0444);
96 MODULE_PARM_DESC(multiplanar, " 1 (default) creates a single planar device, 2 creates a multiplanar device.");
97 
98 /* Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr + vbi-out + vid-out */
99 static unsigned node_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0x1d3d };
100 module_param_array(node_types, uint, NULL, 0444);
101 MODULE_PARM_DESC(node_types, " node types, default is 0x1d3d. Bitmask with the following meaning:\n"
102 			     "\t\t    bit 0: Video Capture node\n"
103 			     "\t\t    bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
104 			     "\t\t    bit 4: Radio Receiver node\n"
105 			     "\t\t    bit 5: Software Defined Radio Receiver node\n"
106 			     "\t\t    bit 8: Video Output node\n"
107 			     "\t\t    bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
108 			     "\t\t    bit 12: Radio Transmitter node\n"
109 			     "\t\t    bit 16: Framebuffer for testing overlays");
110 
111 /* Default: 4 inputs */
112 static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
113 module_param_array(num_inputs, uint, NULL, 0444);
114 MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
115 
116 /* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
117 static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
118 module_param_array(input_types, uint, NULL, 0444);
119 MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
120 			      "\t\t    bits 0-1 == input 0, bits 31-30 == input 15.\n"
121 			      "\t\t    Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
122 
123 /* Default: 2 outputs */
124 static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
125 module_param_array(num_outputs, uint, NULL, 0444);
126 MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
127 
128 /* Default: output 0 = SVID, 1 = HDMI */
129 static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
130 module_param_array(output_types, uint, NULL, 0444);
131 MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
132 			      "\t\t    bit 0 == output 0, bit 15 == output 15.\n"
133 			      "\t\t    Type 0 == S-Video, 1 == HDMI");
134 
135 unsigned vivid_debug;
136 module_param(vivid_debug, uint, 0644);
137 MODULE_PARM_DESC(vivid_debug, " activates debug info");
138 
139 static bool no_error_inj;
140 module_param(no_error_inj, bool, 0444);
141 MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
142 
143 static unsigned int allocators[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0 };
144 module_param_array(allocators, uint, NULL, 0444);
145 MODULE_PARM_DESC(allocators, " memory allocator selection, default is 0.\n"
146 			     "\t\t    0 == vmalloc\n"
147 			     "\t\t    1 == dma-contig");
148 
149 static struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
150 
151 const struct v4l2_rect vivid_min_rect = {
152 	0, 0, MIN_WIDTH, MIN_HEIGHT
153 };
154 
155 const struct v4l2_rect vivid_max_rect = {
156 	0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
157 };
158 
159 static const u8 vivid_hdmi_edid[256] = {
160 	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
161 	0x31, 0xd8, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
162 	0x22, 0x1a, 0x01, 0x03, 0x80, 0x60, 0x36, 0x78,
163 	0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
164 	0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
165 	0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
166 	0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x08, 0xe8,
167 	0x00, 0x30, 0xf2, 0x70, 0x5a, 0x80, 0xb0, 0x58,
168 	0x8a, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00, 0x1e,
169 	0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
170 	0x87, 0x3c, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
171 	0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x76,
172 	0x69, 0x76, 0x69, 0x64, 0x0a, 0x20, 0x20, 0x20,
173 	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
174 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
175 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7b,
176 
177 	0x02, 0x03, 0x3f, 0xf0, 0x51, 0x61, 0x60, 0x5f,
178 	0x5e, 0x5d, 0x10, 0x1f, 0x04, 0x13, 0x22, 0x21,
179 	0x20, 0x05, 0x14, 0x02, 0x11, 0x01, 0x23, 0x09,
180 	0x07, 0x07, 0x83, 0x01, 0x00, 0x00, 0x6d, 0x03,
181 	0x0c, 0x00, 0x10, 0x00, 0x00, 0x3c, 0x21, 0x00,
182 	0x60, 0x01, 0x02, 0x03, 0x67, 0xd8, 0x5d, 0xc4,
183 	0x01, 0x78, 0x00, 0x00, 0xe2, 0x00, 0xea, 0xe3,
184 	0x05, 0x00, 0x00, 0xe3, 0x06, 0x01, 0x00, 0x4d,
185 	0xd0, 0x00, 0xa0, 0xf0, 0x70, 0x3e, 0x80, 0x30,
186 	0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00,
187 	0x1e, 0x1a, 0x36, 0x80, 0xa0, 0x70, 0x38, 0x1f,
188 	0x40, 0x30, 0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32,
189 	0x00, 0x00, 0x1a, 0x1a, 0x1d, 0x00, 0x80, 0x51,
190 	0xd0, 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0xc0,
191 	0x1c, 0x32, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
192 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63,
193 };
194 
vidioc_querycap(struct file * file,void * priv,struct v4l2_capability * cap)195 static int vidioc_querycap(struct file *file, void  *priv,
196 					struct v4l2_capability *cap)
197 {
198 	struct vivid_dev *dev = video_drvdata(file);
199 
200 	strcpy(cap->driver, "vivid");
201 	strcpy(cap->card, "vivid");
202 	snprintf(cap->bus_info, sizeof(cap->bus_info),
203 			"platform:%s", dev->v4l2_dev.name);
204 
205 	cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
206 		dev->vbi_cap_caps | dev->vbi_out_caps |
207 		dev->radio_rx_caps | dev->radio_tx_caps |
208 		dev->sdr_cap_caps | V4L2_CAP_DEVICE_CAPS;
209 	return 0;
210 }
211 
vidioc_s_hw_freq_seek(struct file * file,void * fh,const struct v4l2_hw_freq_seek * a)212 static int vidioc_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
213 {
214 	struct video_device *vdev = video_devdata(file);
215 
216 	if (vdev->vfl_type == VFL_TYPE_RADIO)
217 		return vivid_radio_rx_s_hw_freq_seek(file, fh, a);
218 	return -ENOTTY;
219 }
220 
vidioc_enum_freq_bands(struct file * file,void * fh,struct v4l2_frequency_band * band)221 static int vidioc_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
222 {
223 	struct video_device *vdev = video_devdata(file);
224 
225 	if (vdev->vfl_type == VFL_TYPE_RADIO)
226 		return vivid_radio_rx_enum_freq_bands(file, fh, band);
227 	if (vdev->vfl_type == VFL_TYPE_SDR)
228 		return vivid_sdr_enum_freq_bands(file, fh, band);
229 	return -ENOTTY;
230 }
231 
vidioc_g_tuner(struct file * file,void * fh,struct v4l2_tuner * vt)232 static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
233 {
234 	struct video_device *vdev = video_devdata(file);
235 
236 	if (vdev->vfl_type == VFL_TYPE_RADIO)
237 		return vivid_radio_rx_g_tuner(file, fh, vt);
238 	if (vdev->vfl_type == VFL_TYPE_SDR)
239 		return vivid_sdr_g_tuner(file, fh, vt);
240 	return vivid_video_g_tuner(file, fh, vt);
241 }
242 
vidioc_s_tuner(struct file * file,void * fh,const struct v4l2_tuner * vt)243 static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
244 {
245 	struct video_device *vdev = video_devdata(file);
246 
247 	if (vdev->vfl_type == VFL_TYPE_RADIO)
248 		return vivid_radio_rx_s_tuner(file, fh, vt);
249 	if (vdev->vfl_type == VFL_TYPE_SDR)
250 		return vivid_sdr_s_tuner(file, fh, vt);
251 	return vivid_video_s_tuner(file, fh, vt);
252 }
253 
vidioc_g_frequency(struct file * file,void * fh,struct v4l2_frequency * vf)254 static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
255 {
256 	struct vivid_dev *dev = video_drvdata(file);
257 	struct video_device *vdev = video_devdata(file);
258 
259 	if (vdev->vfl_type == VFL_TYPE_RADIO)
260 		return vivid_radio_g_frequency(file,
261 			vdev->vfl_dir == VFL_DIR_RX ?
262 			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
263 	if (vdev->vfl_type == VFL_TYPE_SDR)
264 		return vivid_sdr_g_frequency(file, fh, vf);
265 	return vivid_video_g_frequency(file, fh, vf);
266 }
267 
vidioc_s_frequency(struct file * file,void * fh,const struct v4l2_frequency * vf)268 static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
269 {
270 	struct vivid_dev *dev = video_drvdata(file);
271 	struct video_device *vdev = video_devdata(file);
272 
273 	if (vdev->vfl_type == VFL_TYPE_RADIO)
274 		return vivid_radio_s_frequency(file,
275 			vdev->vfl_dir == VFL_DIR_RX ?
276 			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
277 	if (vdev->vfl_type == VFL_TYPE_SDR)
278 		return vivid_sdr_s_frequency(file, fh, vf);
279 	return vivid_video_s_frequency(file, fh, vf);
280 }
281 
vidioc_overlay(struct file * file,void * fh,unsigned i)282 static int vidioc_overlay(struct file *file, void *fh, unsigned i)
283 {
284 	struct video_device *vdev = video_devdata(file);
285 
286 	if (vdev->vfl_dir == VFL_DIR_RX)
287 		return vivid_vid_cap_overlay(file, fh, i);
288 	return vivid_vid_out_overlay(file, fh, i);
289 }
290 
vidioc_g_fbuf(struct file * file,void * fh,struct v4l2_framebuffer * a)291 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *a)
292 {
293 	struct video_device *vdev = video_devdata(file);
294 
295 	if (vdev->vfl_dir == VFL_DIR_RX)
296 		return vivid_vid_cap_g_fbuf(file, fh, a);
297 	return vivid_vid_out_g_fbuf(file, fh, a);
298 }
299 
vidioc_s_fbuf(struct file * file,void * fh,const struct v4l2_framebuffer * a)300 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
301 {
302 	struct video_device *vdev = video_devdata(file);
303 
304 	if (vdev->vfl_dir == VFL_DIR_RX)
305 		return vivid_vid_cap_s_fbuf(file, fh, a);
306 	return vivid_vid_out_s_fbuf(file, fh, a);
307 }
308 
vidioc_s_std(struct file * file,void * fh,v4l2_std_id id)309 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
310 {
311 	struct video_device *vdev = video_devdata(file);
312 
313 	if (vdev->vfl_dir == VFL_DIR_RX)
314 		return vivid_vid_cap_s_std(file, fh, id);
315 	return vivid_vid_out_s_std(file, fh, id);
316 }
317 
vidioc_s_dv_timings(struct file * file,void * fh,struct v4l2_dv_timings * timings)318 static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
319 {
320 	struct video_device *vdev = video_devdata(file);
321 
322 	if (vdev->vfl_dir == VFL_DIR_RX)
323 		return vivid_vid_cap_s_dv_timings(file, fh, timings);
324 	return vivid_vid_out_s_dv_timings(file, fh, timings);
325 }
326 
vidioc_cropcap(struct file * file,void * fh,struct v4l2_cropcap * cc)327 static int vidioc_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cc)
328 {
329 	struct video_device *vdev = video_devdata(file);
330 
331 	if (vdev->vfl_dir == VFL_DIR_RX)
332 		return vivid_vid_cap_cropcap(file, fh, cc);
333 	return vivid_vid_out_cropcap(file, fh, cc);
334 }
335 
vidioc_g_selection(struct file * file,void * fh,struct v4l2_selection * sel)336 static int vidioc_g_selection(struct file *file, void *fh,
337 			      struct v4l2_selection *sel)
338 {
339 	struct video_device *vdev = video_devdata(file);
340 
341 	if (vdev->vfl_dir == VFL_DIR_RX)
342 		return vivid_vid_cap_g_selection(file, fh, sel);
343 	return vivid_vid_out_g_selection(file, fh, sel);
344 }
345 
vidioc_s_selection(struct file * file,void * fh,struct v4l2_selection * sel)346 static int vidioc_s_selection(struct file *file, void *fh,
347 			      struct v4l2_selection *sel)
348 {
349 	struct video_device *vdev = video_devdata(file);
350 
351 	if (vdev->vfl_dir == VFL_DIR_RX)
352 		return vivid_vid_cap_s_selection(file, fh, sel);
353 	return vivid_vid_out_s_selection(file, fh, sel);
354 }
355 
vidioc_g_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)356 static int vidioc_g_parm(struct file *file, void *fh,
357 			  struct v4l2_streamparm *parm)
358 {
359 	struct video_device *vdev = video_devdata(file);
360 
361 	if (vdev->vfl_dir == VFL_DIR_RX)
362 		return vivid_vid_cap_g_parm(file, fh, parm);
363 	return vivid_vid_out_g_parm(file, fh, parm);
364 }
365 
vidioc_s_parm(struct file * file,void * fh,struct v4l2_streamparm * parm)366 static int vidioc_s_parm(struct file *file, void *fh,
367 			  struct v4l2_streamparm *parm)
368 {
369 	struct video_device *vdev = video_devdata(file);
370 
371 	if (vdev->vfl_dir == VFL_DIR_RX)
372 		return vivid_vid_cap_s_parm(file, fh, parm);
373 	return vivid_vid_out_g_parm(file, fh, parm);
374 }
375 
vidioc_log_status(struct file * file,void * fh)376 static int vidioc_log_status(struct file *file, void *fh)
377 {
378 	struct vivid_dev *dev = video_drvdata(file);
379 	struct video_device *vdev = video_devdata(file);
380 
381 	v4l2_ctrl_log_status(file, fh);
382 	if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_GRABBER)
383 		tpg_log_status(&dev->tpg);
384 	return 0;
385 }
386 
vivid_radio_read(struct file * file,char __user * buf,size_t size,loff_t * offset)387 static ssize_t vivid_radio_read(struct file *file, char __user *buf,
388 			 size_t size, loff_t *offset)
389 {
390 	struct video_device *vdev = video_devdata(file);
391 
392 	if (vdev->vfl_dir == VFL_DIR_TX)
393 		return -EINVAL;
394 	return vivid_radio_rx_read(file, buf, size, offset);
395 }
396 
vivid_radio_write(struct file * file,const char __user * buf,size_t size,loff_t * offset)397 static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
398 			  size_t size, loff_t *offset)
399 {
400 	struct video_device *vdev = video_devdata(file);
401 
402 	if (vdev->vfl_dir == VFL_DIR_RX)
403 		return -EINVAL;
404 	return vivid_radio_tx_write(file, buf, size, offset);
405 }
406 
vivid_radio_poll(struct file * file,struct poll_table_struct * wait)407 static __poll_t vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
408 {
409 	struct video_device *vdev = video_devdata(file);
410 
411 	if (vdev->vfl_dir == VFL_DIR_RX)
412 		return vivid_radio_rx_poll(file, wait);
413 	return vivid_radio_tx_poll(file, wait);
414 }
415 
vivid_is_in_use(struct video_device * vdev)416 static bool vivid_is_in_use(struct video_device *vdev)
417 {
418 	unsigned long flags;
419 	bool res;
420 
421 	spin_lock_irqsave(&vdev->fh_lock, flags);
422 	res = !list_empty(&vdev->fh_list);
423 	spin_unlock_irqrestore(&vdev->fh_lock, flags);
424 	return res;
425 }
426 
vivid_is_last_user(struct vivid_dev * dev)427 static bool vivid_is_last_user(struct vivid_dev *dev)
428 {
429 	unsigned uses = vivid_is_in_use(&dev->vid_cap_dev) +
430 			vivid_is_in_use(&dev->vid_out_dev) +
431 			vivid_is_in_use(&dev->vbi_cap_dev) +
432 			vivid_is_in_use(&dev->vbi_out_dev) +
433 			vivid_is_in_use(&dev->sdr_cap_dev) +
434 			vivid_is_in_use(&dev->radio_rx_dev) +
435 			vivid_is_in_use(&dev->radio_tx_dev);
436 
437 	return uses == 1;
438 }
439 
vivid_fop_release(struct file * file)440 static int vivid_fop_release(struct file *file)
441 {
442 	struct vivid_dev *dev = video_drvdata(file);
443 	struct video_device *vdev = video_devdata(file);
444 
445 	mutex_lock(&dev->mutex);
446 	if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
447 	    !video_is_registered(vdev) && vivid_is_last_user(dev)) {
448 		/*
449 		 * I am the last user of this driver, and a disconnect
450 		 * was forced (since this video_device is unregistered),
451 		 * so re-register all video_device's again.
452 		 */
453 		v4l2_info(&dev->v4l2_dev, "reconnect\n");
454 		set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
455 		set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
456 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
457 		set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
458 		set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
459 		set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
460 		set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
461 	}
462 	mutex_unlock(&dev->mutex);
463 	if (file->private_data == dev->overlay_cap_owner)
464 		dev->overlay_cap_owner = NULL;
465 	if (file->private_data == dev->radio_rx_rds_owner) {
466 		dev->radio_rx_rds_last_block = 0;
467 		dev->radio_rx_rds_owner = NULL;
468 	}
469 	if (file->private_data == dev->radio_tx_rds_owner) {
470 		dev->radio_tx_rds_last_block = 0;
471 		dev->radio_tx_rds_owner = NULL;
472 	}
473 	if (vdev->queue)
474 		return vb2_fop_release(file);
475 	return v4l2_fh_release(file);
476 }
477 
478 static const struct v4l2_file_operations vivid_fops = {
479 	.owner		= THIS_MODULE,
480 	.open           = v4l2_fh_open,
481 	.release        = vivid_fop_release,
482 	.read           = vb2_fop_read,
483 	.write          = vb2_fop_write,
484 	.poll		= vb2_fop_poll,
485 	.unlocked_ioctl = video_ioctl2,
486 	.mmap           = vb2_fop_mmap,
487 };
488 
489 static const struct v4l2_file_operations vivid_radio_fops = {
490 	.owner		= THIS_MODULE,
491 	.open           = v4l2_fh_open,
492 	.release        = vivid_fop_release,
493 	.read           = vivid_radio_read,
494 	.write          = vivid_radio_write,
495 	.poll		= vivid_radio_poll,
496 	.unlocked_ioctl = video_ioctl2,
497 };
498 
499 static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
500 	.vidioc_querycap		= vidioc_querycap,
501 
502 	.vidioc_enum_fmt_vid_cap	= vidioc_enum_fmt_vid,
503 	.vidioc_g_fmt_vid_cap		= vidioc_g_fmt_vid_cap,
504 	.vidioc_try_fmt_vid_cap		= vidioc_try_fmt_vid_cap,
505 	.vidioc_s_fmt_vid_cap		= vidioc_s_fmt_vid_cap,
506 	.vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_mplane,
507 	.vidioc_g_fmt_vid_cap_mplane	= vidioc_g_fmt_vid_cap_mplane,
508 	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap_mplane,
509 	.vidioc_s_fmt_vid_cap_mplane	= vidioc_s_fmt_vid_cap_mplane,
510 
511 	.vidioc_enum_fmt_vid_out	= vidioc_enum_fmt_vid,
512 	.vidioc_g_fmt_vid_out		= vidioc_g_fmt_vid_out,
513 	.vidioc_try_fmt_vid_out		= vidioc_try_fmt_vid_out,
514 	.vidioc_s_fmt_vid_out		= vidioc_s_fmt_vid_out,
515 	.vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_mplane,
516 	.vidioc_g_fmt_vid_out_mplane	= vidioc_g_fmt_vid_out_mplane,
517 	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
518 	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out_mplane,
519 
520 	.vidioc_g_selection		= vidioc_g_selection,
521 	.vidioc_s_selection		= vidioc_s_selection,
522 	.vidioc_cropcap			= vidioc_cropcap,
523 
524 	.vidioc_g_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
525 	.vidioc_try_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
526 	.vidioc_s_fmt_vbi_cap		= vidioc_s_fmt_vbi_cap,
527 
528 	.vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
529 	.vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
530 	.vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
531 	.vidioc_g_sliced_vbi_cap	= vidioc_g_sliced_vbi_cap,
532 
533 	.vidioc_g_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
534 	.vidioc_try_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
535 	.vidioc_s_fmt_vbi_out		= vidioc_s_fmt_vbi_out,
536 
537 	.vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
538 	.vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
539 	.vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
540 
541 	.vidioc_enum_fmt_sdr_cap	= vidioc_enum_fmt_sdr_cap,
542 	.vidioc_g_fmt_sdr_cap		= vidioc_g_fmt_sdr_cap,
543 	.vidioc_try_fmt_sdr_cap		= vidioc_try_fmt_sdr_cap,
544 	.vidioc_s_fmt_sdr_cap		= vidioc_s_fmt_sdr_cap,
545 
546 	.vidioc_overlay			= vidioc_overlay,
547 	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
548 	.vidioc_enum_frameintervals	= vidioc_enum_frameintervals,
549 	.vidioc_g_parm			= vidioc_g_parm,
550 	.vidioc_s_parm			= vidioc_s_parm,
551 
552 	.vidioc_enum_fmt_vid_overlay	= vidioc_enum_fmt_vid_overlay,
553 	.vidioc_g_fmt_vid_overlay	= vidioc_g_fmt_vid_overlay,
554 	.vidioc_try_fmt_vid_overlay	= vidioc_try_fmt_vid_overlay,
555 	.vidioc_s_fmt_vid_overlay	= vidioc_s_fmt_vid_overlay,
556 	.vidioc_g_fmt_vid_out_overlay	= vidioc_g_fmt_vid_out_overlay,
557 	.vidioc_try_fmt_vid_out_overlay	= vidioc_try_fmt_vid_out_overlay,
558 	.vidioc_s_fmt_vid_out_overlay	= vidioc_s_fmt_vid_out_overlay,
559 	.vidioc_g_fbuf			= vidioc_g_fbuf,
560 	.vidioc_s_fbuf			= vidioc_s_fbuf,
561 
562 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
563 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
564 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
565 	.vidioc_querybuf		= vb2_ioctl_querybuf,
566 	.vidioc_qbuf			= vb2_ioctl_qbuf,
567 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
568 	.vidioc_expbuf			= vb2_ioctl_expbuf,
569 	.vidioc_streamon		= vb2_ioctl_streamon,
570 	.vidioc_streamoff		= vb2_ioctl_streamoff,
571 
572 	.vidioc_enum_input		= vidioc_enum_input,
573 	.vidioc_g_input			= vidioc_g_input,
574 	.vidioc_s_input			= vidioc_s_input,
575 	.vidioc_s_audio			= vidioc_s_audio,
576 	.vidioc_g_audio			= vidioc_g_audio,
577 	.vidioc_enumaudio		= vidioc_enumaudio,
578 	.vidioc_s_frequency		= vidioc_s_frequency,
579 	.vidioc_g_frequency		= vidioc_g_frequency,
580 	.vidioc_s_tuner			= vidioc_s_tuner,
581 	.vidioc_g_tuner			= vidioc_g_tuner,
582 	.vidioc_s_modulator		= vidioc_s_modulator,
583 	.vidioc_g_modulator		= vidioc_g_modulator,
584 	.vidioc_s_hw_freq_seek		= vidioc_s_hw_freq_seek,
585 	.vidioc_enum_freq_bands		= vidioc_enum_freq_bands,
586 
587 	.vidioc_enum_output		= vidioc_enum_output,
588 	.vidioc_g_output		= vidioc_g_output,
589 	.vidioc_s_output		= vidioc_s_output,
590 	.vidioc_s_audout		= vidioc_s_audout,
591 	.vidioc_g_audout		= vidioc_g_audout,
592 	.vidioc_enumaudout		= vidioc_enumaudout,
593 
594 	.vidioc_querystd		= vidioc_querystd,
595 	.vidioc_g_std			= vidioc_g_std,
596 	.vidioc_s_std			= vidioc_s_std,
597 	.vidioc_s_dv_timings		= vidioc_s_dv_timings,
598 	.vidioc_g_dv_timings		= vidioc_g_dv_timings,
599 	.vidioc_query_dv_timings	= vidioc_query_dv_timings,
600 	.vidioc_enum_dv_timings		= vidioc_enum_dv_timings,
601 	.vidioc_dv_timings_cap		= vidioc_dv_timings_cap,
602 	.vidioc_g_edid			= vidioc_g_edid,
603 	.vidioc_s_edid			= vidioc_s_edid,
604 
605 	.vidioc_log_status		= vidioc_log_status,
606 	.vidioc_subscribe_event		= vidioc_subscribe_event,
607 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
608 };
609 
610 /* -----------------------------------------------------------------
611 	Initialization and module stuff
612    ------------------------------------------------------------------*/
613 
vivid_dev_release(struct v4l2_device * v4l2_dev)614 static void vivid_dev_release(struct v4l2_device *v4l2_dev)
615 {
616 	struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
617 
618 	vivid_free_controls(dev);
619 	v4l2_device_unregister(&dev->v4l2_dev);
620 	vfree(dev->scaled_line);
621 	vfree(dev->blended_line);
622 	vfree(dev->edid);
623 	vfree(dev->bitmap_cap);
624 	vfree(dev->bitmap_out);
625 	tpg_free(&dev->tpg);
626 	kfree(dev->query_dv_timings_qmenu);
627 	kfree(dev);
628 }
629 
vivid_create_instance(struct platform_device * pdev,int inst)630 static int vivid_create_instance(struct platform_device *pdev, int inst)
631 {
632 	static const struct v4l2_dv_timings def_dv_timings =
633 					V4L2_DV_BT_CEA_1280X720P60;
634 	static const struct vb2_mem_ops * const vivid_mem_ops[2] = {
635 		&vb2_vmalloc_memops,
636 		&vb2_dma_contig_memops,
637 	};
638 	unsigned in_type_counter[4] = { 0, 0, 0, 0 };
639 	unsigned out_type_counter[4] = { 0, 0, 0, 0 };
640 	int ccs_cap = ccs_cap_mode[inst];
641 	int ccs_out = ccs_out_mode[inst];
642 	bool has_tuner;
643 	bool has_modulator;
644 	struct vivid_dev *dev;
645 	struct video_device *vfd;
646 	struct vb2_queue *q;
647 	unsigned node_type = node_types[inst];
648 	unsigned int allocator = allocators[inst];
649 	v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
650 	int ret;
651 	int i;
652 
653 	/* allocate main vivid state structure */
654 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
655 	if (!dev)
656 		return -ENOMEM;
657 
658 	dev->inst = inst;
659 
660 	/* register v4l2_device */
661 	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
662 			"%s-%03d", VIVID_MODULE_NAME, inst);
663 	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
664 	if (ret) {
665 		kfree(dev);
666 		return ret;
667 	}
668 	dev->v4l2_dev.release = vivid_dev_release;
669 
670 	/* start detecting feature set */
671 
672 	/* do we use single- or multi-planar? */
673 	dev->multiplanar = multiplanar[inst] > 1;
674 	v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
675 			dev->multiplanar ? "multi" : "single ");
676 
677 	/* how many inputs do we have and of what type? */
678 	dev->num_inputs = num_inputs[inst];
679 	if (dev->num_inputs < 1)
680 		dev->num_inputs = 1;
681 	if (dev->num_inputs >= MAX_INPUTS)
682 		dev->num_inputs = MAX_INPUTS;
683 	for (i = 0; i < dev->num_inputs; i++) {
684 		dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
685 		dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
686 	}
687 	dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
688 	if (in_type_counter[HDMI] == 16) {
689 		/* The CEC physical address only allows for max 15 inputs */
690 		in_type_counter[HDMI]--;
691 		dev->num_inputs--;
692 	}
693 
694 	/* how many outputs do we have and of what type? */
695 	dev->num_outputs = num_outputs[inst];
696 	if (dev->num_outputs < 1)
697 		dev->num_outputs = 1;
698 	if (dev->num_outputs >= MAX_OUTPUTS)
699 		dev->num_outputs = MAX_OUTPUTS;
700 	for (i = 0; i < dev->num_outputs; i++) {
701 		dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
702 		dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
703 	}
704 	dev->has_audio_outputs = out_type_counter[SVID];
705 	if (out_type_counter[HDMI] == 16) {
706 		/*
707 		 * The CEC physical address only allows for max 15 inputs,
708 		 * so outputs are also limited to 15 to allow for easy
709 		 * CEC output to input mapping.
710 		 */
711 		out_type_counter[HDMI]--;
712 		dev->num_outputs--;
713 	}
714 
715 	/* do we create a video capture device? */
716 	dev->has_vid_cap = node_type & 0x0001;
717 
718 	/* do we create a vbi capture device? */
719 	if (in_type_counter[TV] || in_type_counter[SVID]) {
720 		dev->has_raw_vbi_cap = node_type & 0x0004;
721 		dev->has_sliced_vbi_cap = node_type & 0x0008;
722 		dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
723 	}
724 
725 	/* do we create a video output device? */
726 	dev->has_vid_out = node_type & 0x0100;
727 
728 	/* do we create a vbi output device? */
729 	if (out_type_counter[SVID]) {
730 		dev->has_raw_vbi_out = node_type & 0x0400;
731 		dev->has_sliced_vbi_out = node_type & 0x0800;
732 		dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
733 	}
734 
735 	/* do we create a radio receiver device? */
736 	dev->has_radio_rx = node_type & 0x0010;
737 
738 	/* do we create a radio transmitter device? */
739 	dev->has_radio_tx = node_type & 0x1000;
740 
741 	/* do we create a software defined radio capture device? */
742 	dev->has_sdr_cap = node_type & 0x0020;
743 
744 	/* do we have a tuner? */
745 	has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
746 		    dev->has_radio_rx || dev->has_sdr_cap;
747 
748 	/* do we have a modulator? */
749 	has_modulator = dev->has_radio_tx;
750 
751 	if (dev->has_vid_cap)
752 		/* do we have a framebuffer for overlay testing? */
753 		dev->has_fb = node_type & 0x10000;
754 
755 	/* can we do crop/compose/scaling while capturing? */
756 	if (no_error_inj && ccs_cap == -1)
757 		ccs_cap = 7;
758 
759 	/* if ccs_cap == -1, then the use can select it using controls */
760 	if (ccs_cap != -1) {
761 		dev->has_crop_cap = ccs_cap & 1;
762 		dev->has_compose_cap = ccs_cap & 2;
763 		dev->has_scaler_cap = ccs_cap & 4;
764 		v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
765 			dev->has_crop_cap ? 'Y' : 'N',
766 			dev->has_compose_cap ? 'Y' : 'N',
767 			dev->has_scaler_cap ? 'Y' : 'N');
768 	}
769 
770 	/* can we do crop/compose/scaling with video output? */
771 	if (no_error_inj && ccs_out == -1)
772 		ccs_out = 7;
773 
774 	/* if ccs_out == -1, then the use can select it using controls */
775 	if (ccs_out != -1) {
776 		dev->has_crop_out = ccs_out & 1;
777 		dev->has_compose_out = ccs_out & 2;
778 		dev->has_scaler_out = ccs_out & 4;
779 		v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
780 			dev->has_crop_out ? 'Y' : 'N',
781 			dev->has_compose_out ? 'Y' : 'N',
782 			dev->has_scaler_out ? 'Y' : 'N');
783 	}
784 
785 	/* end detecting feature set */
786 
787 	if (dev->has_vid_cap) {
788 		/* set up the capabilities of the video capture device */
789 		dev->vid_cap_caps = dev->multiplanar ?
790 			V4L2_CAP_VIDEO_CAPTURE_MPLANE :
791 			V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY;
792 		dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
793 		if (dev->has_audio_inputs)
794 			dev->vid_cap_caps |= V4L2_CAP_AUDIO;
795 		if (in_type_counter[TV])
796 			dev->vid_cap_caps |= V4L2_CAP_TUNER;
797 	}
798 	if (dev->has_vid_out) {
799 		/* set up the capabilities of the video output device */
800 		dev->vid_out_caps = dev->multiplanar ?
801 			V4L2_CAP_VIDEO_OUTPUT_MPLANE :
802 			V4L2_CAP_VIDEO_OUTPUT;
803 		if (dev->has_fb)
804 			dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
805 		dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
806 		if (dev->has_audio_outputs)
807 			dev->vid_out_caps |= V4L2_CAP_AUDIO;
808 	}
809 	if (dev->has_vbi_cap) {
810 		/* set up the capabilities of the vbi capture device */
811 		dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
812 				    (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
813 		dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
814 		if (dev->has_audio_inputs)
815 			dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
816 		if (in_type_counter[TV])
817 			dev->vbi_cap_caps |= V4L2_CAP_TUNER;
818 	}
819 	if (dev->has_vbi_out) {
820 		/* set up the capabilities of the vbi output device */
821 		dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
822 				    (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
823 		dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
824 		if (dev->has_audio_outputs)
825 			dev->vbi_out_caps |= V4L2_CAP_AUDIO;
826 	}
827 	if (dev->has_sdr_cap) {
828 		/* set up the capabilities of the sdr capture device */
829 		dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
830 		dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
831 	}
832 	/* set up the capabilities of the radio receiver device */
833 	if (dev->has_radio_rx)
834 		dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
835 				     V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
836 				     V4L2_CAP_READWRITE;
837 	/* set up the capabilities of the radio transmitter device */
838 	if (dev->has_radio_tx)
839 		dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
840 				     V4L2_CAP_READWRITE;
841 
842 	ret = -ENOMEM;
843 	/* initialize the test pattern generator */
844 	tpg_init(&dev->tpg, 640, 360);
845 	if (tpg_alloc(&dev->tpg, MAX_ZOOM * MAX_WIDTH))
846 		goto free_dev;
847 	dev->scaled_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
848 	if (!dev->scaled_line)
849 		goto free_dev;
850 	dev->blended_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
851 	if (!dev->blended_line)
852 		goto free_dev;
853 
854 	/* load the edid */
855 	dev->edid = vmalloc(256 * 128);
856 	if (!dev->edid)
857 		goto free_dev;
858 
859 	/* create a string array containing the names of all the preset timings */
860 	while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
861 		dev->query_dv_timings_size++;
862 	dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
863 						    (sizeof(void *) + 32),
864 						    GFP_KERNEL);
865 	if (dev->query_dv_timings_qmenu == NULL)
866 		goto free_dev;
867 	for (i = 0; i < dev->query_dv_timings_size; i++) {
868 		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
869 		char *p = (char *)&dev->query_dv_timings_qmenu[dev->query_dv_timings_size];
870 		u32 htot, vtot;
871 
872 		p += i * 32;
873 		dev->query_dv_timings_qmenu[i] = p;
874 
875 		htot = V4L2_DV_BT_FRAME_WIDTH(bt);
876 		vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
877 		snprintf(p, 32, "%ux%u%s%u",
878 			bt->width, bt->height, bt->interlaced ? "i" : "p",
879 			(u32)bt->pixelclock / (htot * vtot));
880 	}
881 
882 	/* disable invalid ioctls based on the feature set */
883 	if (!dev->has_audio_inputs) {
884 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
885 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
886 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
887 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
888 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
889 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
890 	}
891 	if (!dev->has_audio_outputs) {
892 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
893 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
894 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
895 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
896 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
897 		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
898 	}
899 	if (!in_type_counter[TV] && !in_type_counter[SVID]) {
900 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
901 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
902 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
903 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
904 	}
905 	if (!out_type_counter[SVID]) {
906 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
907 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
908 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
909 	}
910 	if (!has_tuner && !has_modulator) {
911 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
912 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
913 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
914 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
915 	}
916 	if (!has_tuner) {
917 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
918 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
919 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
920 		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
921 	}
922 	if (in_type_counter[HDMI] == 0) {
923 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
924 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
925 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
926 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
927 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
928 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
929 		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
930 	}
931 	if (out_type_counter[HDMI] == 0) {
932 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
933 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
934 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
935 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
936 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
937 	}
938 	if (!dev->has_fb) {
939 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
940 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
941 		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
942 	}
943 	v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
944 	v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
945 	v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
946 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
947 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
948 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
949 	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
950 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
951 	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
952 
953 	/* configure internal data */
954 	dev->fmt_cap = &vivid_formats[0];
955 	dev->fmt_out = &vivid_formats[0];
956 	if (!dev->multiplanar)
957 		vivid_formats[0].data_offset[0] = 0;
958 	dev->webcam_size_idx = 1;
959 	dev->webcam_ival_idx = 3;
960 	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
961 	dev->std_cap = V4L2_STD_PAL;
962 	dev->std_out = V4L2_STD_PAL;
963 	if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
964 		tvnorms_cap = V4L2_STD_ALL;
965 	if (dev->output_type[0] == SVID)
966 		tvnorms_out = V4L2_STD_ALL;
967 	dev->dv_timings_cap = def_dv_timings;
968 	dev->dv_timings_out = def_dv_timings;
969 	dev->tv_freq = 2804 /* 175.25 * 16 */;
970 	dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
971 	dev->tv_field_cap = V4L2_FIELD_INTERLACED;
972 	dev->tv_field_out = V4L2_FIELD_INTERLACED;
973 	dev->radio_rx_freq = 95000 * 16;
974 	dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
975 	if (dev->has_radio_tx) {
976 		dev->radio_tx_freq = 95500 * 16;
977 		dev->radio_rds_loop = false;
978 	}
979 	dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
980 	dev->sdr_adc_freq = 300000;
981 	dev->sdr_fm_freq = 50000000;
982 	dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
983 	dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
984 
985 	dev->edid_max_blocks = dev->edid_blocks = 2;
986 	memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
987 	dev->radio_rds_init_time = ktime_get();
988 
989 	/* create all controls */
990 	ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
991 			in_type_counter[TV] || in_type_counter[SVID] ||
992 			out_type_counter[SVID],
993 			in_type_counter[HDMI] || out_type_counter[HDMI]);
994 	if (ret)
995 		goto unreg_dev;
996 
997 	/*
998 	 * update the capture and output formats to do a proper initial
999 	 * configuration.
1000 	 */
1001 	vivid_update_format_cap(dev, false);
1002 	vivid_update_format_out(dev);
1003 
1004 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
1005 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
1006 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
1007 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
1008 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
1009 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
1010 	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
1011 
1012 	/* initialize overlay */
1013 	dev->fb_cap.fmt.width = dev->src_rect.width;
1014 	dev->fb_cap.fmt.height = dev->src_rect.height;
1015 	dev->fb_cap.fmt.pixelformat = dev->fmt_cap->fourcc;
1016 	dev->fb_cap.fmt.bytesperline = dev->src_rect.width * tpg_g_twopixelsize(&dev->tpg, 0) / 2;
1017 	dev->fb_cap.fmt.sizeimage = dev->src_rect.height * dev->fb_cap.fmt.bytesperline;
1018 
1019 	/* initialize locks */
1020 	spin_lock_init(&dev->slock);
1021 	mutex_init(&dev->mutex);
1022 
1023 	/* init dma queues */
1024 	INIT_LIST_HEAD(&dev->vid_cap_active);
1025 	INIT_LIST_HEAD(&dev->vid_out_active);
1026 	INIT_LIST_HEAD(&dev->vbi_cap_active);
1027 	INIT_LIST_HEAD(&dev->vbi_out_active);
1028 	INIT_LIST_HEAD(&dev->sdr_cap_active);
1029 
1030 	INIT_LIST_HEAD(&dev->cec_work_list);
1031 	spin_lock_init(&dev->cec_slock);
1032 	/*
1033 	 * Same as create_singlethread_workqueue, but now I can use the
1034 	 * string formatting of alloc_ordered_workqueue.
1035 	 */
1036 	dev->cec_workqueue =
1037 		alloc_ordered_workqueue("vivid-%03d-cec", WQ_MEM_RECLAIM, inst);
1038 	if (!dev->cec_workqueue) {
1039 		ret = -ENOMEM;
1040 		goto unreg_dev;
1041 	}
1042 
1043 	if (allocator == 1)
1044 		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1045 	else if (allocator >= ARRAY_SIZE(vivid_mem_ops))
1046 		allocator = 0;
1047 
1048 	/* start creating the vb2 queues */
1049 	if (dev->has_vid_cap) {
1050 		/* initialize vid_cap queue */
1051 		q = &dev->vb_vid_cap_q;
1052 		q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1053 			V4L2_BUF_TYPE_VIDEO_CAPTURE;
1054 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1055 		q->drv_priv = dev;
1056 		q->buf_struct_size = sizeof(struct vivid_buffer);
1057 		q->ops = &vivid_vid_cap_qops;
1058 		q->mem_ops = vivid_mem_ops[allocator];
1059 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1060 		q->min_buffers_needed = 2;
1061 		q->lock = &dev->mutex;
1062 		q->dev = dev->v4l2_dev.dev;
1063 
1064 		ret = vb2_queue_init(q);
1065 		if (ret)
1066 			goto unreg_dev;
1067 	}
1068 
1069 	if (dev->has_vid_out) {
1070 		/* initialize vid_out queue */
1071 		q = &dev->vb_vid_out_q;
1072 		q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1073 			V4L2_BUF_TYPE_VIDEO_OUTPUT;
1074 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1075 		q->drv_priv = dev;
1076 		q->buf_struct_size = sizeof(struct vivid_buffer);
1077 		q->ops = &vivid_vid_out_qops;
1078 		q->mem_ops = vivid_mem_ops[allocator];
1079 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1080 		q->min_buffers_needed = 2;
1081 		q->lock = &dev->mutex;
1082 		q->dev = dev->v4l2_dev.dev;
1083 
1084 		ret = vb2_queue_init(q);
1085 		if (ret)
1086 			goto unreg_dev;
1087 	}
1088 
1089 	if (dev->has_vbi_cap) {
1090 		/* initialize vbi_cap queue */
1091 		q = &dev->vb_vbi_cap_q;
1092 		q->type = dev->has_raw_vbi_cap ? V4L2_BUF_TYPE_VBI_CAPTURE :
1093 					      V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
1094 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1095 		q->drv_priv = dev;
1096 		q->buf_struct_size = sizeof(struct vivid_buffer);
1097 		q->ops = &vivid_vbi_cap_qops;
1098 		q->mem_ops = vivid_mem_ops[allocator];
1099 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1100 		q->min_buffers_needed = 2;
1101 		q->lock = &dev->mutex;
1102 		q->dev = dev->v4l2_dev.dev;
1103 
1104 		ret = vb2_queue_init(q);
1105 		if (ret)
1106 			goto unreg_dev;
1107 	}
1108 
1109 	if (dev->has_vbi_out) {
1110 		/* initialize vbi_out queue */
1111 		q = &dev->vb_vbi_out_q;
1112 		q->type = dev->has_raw_vbi_out ? V4L2_BUF_TYPE_VBI_OUTPUT :
1113 					      V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
1114 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1115 		q->drv_priv = dev;
1116 		q->buf_struct_size = sizeof(struct vivid_buffer);
1117 		q->ops = &vivid_vbi_out_qops;
1118 		q->mem_ops = vivid_mem_ops[allocator];
1119 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1120 		q->min_buffers_needed = 2;
1121 		q->lock = &dev->mutex;
1122 		q->dev = dev->v4l2_dev.dev;
1123 
1124 		ret = vb2_queue_init(q);
1125 		if (ret)
1126 			goto unreg_dev;
1127 	}
1128 
1129 	if (dev->has_sdr_cap) {
1130 		/* initialize sdr_cap queue */
1131 		q = &dev->vb_sdr_cap_q;
1132 		q->type = V4L2_BUF_TYPE_SDR_CAPTURE;
1133 		q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1134 		q->drv_priv = dev;
1135 		q->buf_struct_size = sizeof(struct vivid_buffer);
1136 		q->ops = &vivid_sdr_cap_qops;
1137 		q->mem_ops = vivid_mem_ops[allocator];
1138 		q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1139 		q->min_buffers_needed = 8;
1140 		q->lock = &dev->mutex;
1141 		q->dev = dev->v4l2_dev.dev;
1142 
1143 		ret = vb2_queue_init(q);
1144 		if (ret)
1145 			goto unreg_dev;
1146 	}
1147 
1148 	if (dev->has_fb) {
1149 		/* Create framebuffer for testing capture/output overlay */
1150 		ret = vivid_fb_init(dev);
1151 		if (ret)
1152 			goto unreg_dev;
1153 		v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1154 				dev->fb_info.node);
1155 	}
1156 
1157 	/* finally start creating the device nodes */
1158 	if (dev->has_vid_cap) {
1159 		vfd = &dev->vid_cap_dev;
1160 		snprintf(vfd->name, sizeof(vfd->name),
1161 			 "vivid-%03d-vid-cap", inst);
1162 		vfd->fops = &vivid_fops;
1163 		vfd->ioctl_ops = &vivid_ioctl_ops;
1164 		vfd->device_caps = dev->vid_cap_caps;
1165 		vfd->release = video_device_release_empty;
1166 		vfd->v4l2_dev = &dev->v4l2_dev;
1167 		vfd->queue = &dev->vb_vid_cap_q;
1168 		vfd->tvnorms = tvnorms_cap;
1169 
1170 		/*
1171 		 * Provide a mutex to v4l2 core. It will be used to protect
1172 		 * all fops and v4l2 ioctls.
1173 		 */
1174 		vfd->lock = &dev->mutex;
1175 		video_set_drvdata(vfd, dev);
1176 
1177 #ifdef CONFIG_VIDEO_VIVID_CEC
1178 		if (in_type_counter[HDMI]) {
1179 			struct cec_adapter *adap;
1180 
1181 			adap = vivid_cec_alloc_adap(dev, 0, false);
1182 			ret = PTR_ERR_OR_ZERO(adap);
1183 			if (ret < 0)
1184 				goto unreg_dev;
1185 			dev->cec_rx_adap = adap;
1186 			ret = cec_register_adapter(adap, &pdev->dev);
1187 			if (ret < 0) {
1188 				cec_delete_adapter(adap);
1189 				dev->cec_rx_adap = NULL;
1190 				goto unreg_dev;
1191 			}
1192 			cec_s_phys_addr(adap, 0, false);
1193 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input 0\n",
1194 				  dev_name(&adap->devnode.dev));
1195 		}
1196 #endif
1197 
1198 		ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
1199 		if (ret < 0)
1200 			goto unreg_dev;
1201 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1202 					  video_device_node_name(vfd));
1203 	}
1204 
1205 	if (dev->has_vid_out) {
1206 #ifdef CONFIG_VIDEO_VIVID_CEC
1207 		unsigned int bus_cnt = 0;
1208 #endif
1209 
1210 		vfd = &dev->vid_out_dev;
1211 		snprintf(vfd->name, sizeof(vfd->name),
1212 			 "vivid-%03d-vid-out", inst);
1213 		vfd->vfl_dir = VFL_DIR_TX;
1214 		vfd->fops = &vivid_fops;
1215 		vfd->ioctl_ops = &vivid_ioctl_ops;
1216 		vfd->device_caps = dev->vid_out_caps;
1217 		vfd->release = video_device_release_empty;
1218 		vfd->v4l2_dev = &dev->v4l2_dev;
1219 		vfd->queue = &dev->vb_vid_out_q;
1220 		vfd->tvnorms = tvnorms_out;
1221 
1222 		/*
1223 		 * Provide a mutex to v4l2 core. It will be used to protect
1224 		 * all fops and v4l2 ioctls.
1225 		 */
1226 		vfd->lock = &dev->mutex;
1227 		video_set_drvdata(vfd, dev);
1228 
1229 #ifdef CONFIG_VIDEO_VIVID_CEC
1230 		for (i = 0; i < dev->num_outputs; i++) {
1231 			struct cec_adapter *adap;
1232 
1233 			if (dev->output_type[i] != HDMI)
1234 				continue;
1235 			dev->cec_output2bus_map[i] = bus_cnt;
1236 			adap = vivid_cec_alloc_adap(dev, bus_cnt, true);
1237 			ret = PTR_ERR_OR_ZERO(adap);
1238 			if (ret < 0)
1239 				goto unreg_dev;
1240 			dev->cec_tx_adap[bus_cnt] = adap;
1241 			ret = cec_register_adapter(adap, &pdev->dev);
1242 			if (ret < 0) {
1243 				cec_delete_adapter(adap);
1244 				dev->cec_tx_adap[bus_cnt] = NULL;
1245 				goto unreg_dev;
1246 			}
1247 			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1248 				  dev_name(&adap->devnode.dev), bus_cnt);
1249 			bus_cnt++;
1250 			if (bus_cnt <= out_type_counter[HDMI])
1251 				cec_s_phys_addr(adap, bus_cnt << 12, false);
1252 			else
1253 				cec_s_phys_addr(adap, 0x1000, false);
1254 		}
1255 #endif
1256 
1257 		ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
1258 		if (ret < 0)
1259 			goto unreg_dev;
1260 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1261 					  video_device_node_name(vfd));
1262 	}
1263 
1264 	if (dev->has_vbi_cap) {
1265 		vfd = &dev->vbi_cap_dev;
1266 		snprintf(vfd->name, sizeof(vfd->name),
1267 			 "vivid-%03d-vbi-cap", inst);
1268 		vfd->fops = &vivid_fops;
1269 		vfd->ioctl_ops = &vivid_ioctl_ops;
1270 		vfd->device_caps = dev->vbi_cap_caps;
1271 		vfd->release = video_device_release_empty;
1272 		vfd->v4l2_dev = &dev->v4l2_dev;
1273 		vfd->queue = &dev->vb_vbi_cap_q;
1274 		vfd->lock = &dev->mutex;
1275 		vfd->tvnorms = tvnorms_cap;
1276 		video_set_drvdata(vfd, dev);
1277 
1278 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1279 		if (ret < 0)
1280 			goto unreg_dev;
1281 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1282 					  video_device_node_name(vfd),
1283 					  (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1284 					  "raw and sliced" :
1285 					  (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1286 	}
1287 
1288 	if (dev->has_vbi_out) {
1289 		vfd = &dev->vbi_out_dev;
1290 		snprintf(vfd->name, sizeof(vfd->name),
1291 			 "vivid-%03d-vbi-out", inst);
1292 		vfd->vfl_dir = VFL_DIR_TX;
1293 		vfd->fops = &vivid_fops;
1294 		vfd->ioctl_ops = &vivid_ioctl_ops;
1295 		vfd->device_caps = dev->vbi_out_caps;
1296 		vfd->release = video_device_release_empty;
1297 		vfd->v4l2_dev = &dev->v4l2_dev;
1298 		vfd->queue = &dev->vb_vbi_out_q;
1299 		vfd->lock = &dev->mutex;
1300 		vfd->tvnorms = tvnorms_out;
1301 		video_set_drvdata(vfd, dev);
1302 
1303 		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1304 		if (ret < 0)
1305 			goto unreg_dev;
1306 		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1307 					  video_device_node_name(vfd),
1308 					  (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1309 					  "raw and sliced" :
1310 					  (dev->has_raw_vbi_out ? "raw" : "sliced"));
1311 	}
1312 
1313 	if (dev->has_sdr_cap) {
1314 		vfd = &dev->sdr_cap_dev;
1315 		snprintf(vfd->name, sizeof(vfd->name),
1316 			 "vivid-%03d-sdr-cap", inst);
1317 		vfd->fops = &vivid_fops;
1318 		vfd->ioctl_ops = &vivid_ioctl_ops;
1319 		vfd->device_caps = dev->sdr_cap_caps;
1320 		vfd->release = video_device_release_empty;
1321 		vfd->v4l2_dev = &dev->v4l2_dev;
1322 		vfd->queue = &dev->vb_sdr_cap_q;
1323 		vfd->lock = &dev->mutex;
1324 		video_set_drvdata(vfd, dev);
1325 
1326 		ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1327 		if (ret < 0)
1328 			goto unreg_dev;
1329 		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1330 					  video_device_node_name(vfd));
1331 	}
1332 
1333 	if (dev->has_radio_rx) {
1334 		vfd = &dev->radio_rx_dev;
1335 		snprintf(vfd->name, sizeof(vfd->name),
1336 			 "vivid-%03d-rad-rx", inst);
1337 		vfd->fops = &vivid_radio_fops;
1338 		vfd->ioctl_ops = &vivid_ioctl_ops;
1339 		vfd->device_caps = dev->radio_rx_caps;
1340 		vfd->release = video_device_release_empty;
1341 		vfd->v4l2_dev = &dev->v4l2_dev;
1342 		vfd->lock = &dev->mutex;
1343 		video_set_drvdata(vfd, dev);
1344 
1345 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1346 		if (ret < 0)
1347 			goto unreg_dev;
1348 		v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1349 					  video_device_node_name(vfd));
1350 	}
1351 
1352 	if (dev->has_radio_tx) {
1353 		vfd = &dev->radio_tx_dev;
1354 		snprintf(vfd->name, sizeof(vfd->name),
1355 			 "vivid-%03d-rad-tx", inst);
1356 		vfd->vfl_dir = VFL_DIR_TX;
1357 		vfd->fops = &vivid_radio_fops;
1358 		vfd->ioctl_ops = &vivid_ioctl_ops;
1359 		vfd->device_caps = dev->radio_tx_caps;
1360 		vfd->release = video_device_release_empty;
1361 		vfd->v4l2_dev = &dev->v4l2_dev;
1362 		vfd->lock = &dev->mutex;
1363 		video_set_drvdata(vfd, dev);
1364 
1365 		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1366 		if (ret < 0)
1367 			goto unreg_dev;
1368 		v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1369 					  video_device_node_name(vfd));
1370 	}
1371 
1372 	/* Now that everything is fine, let's add it to device list */
1373 	vivid_devs[inst] = dev;
1374 
1375 	return 0;
1376 
1377 unreg_dev:
1378 	video_unregister_device(&dev->radio_tx_dev);
1379 	video_unregister_device(&dev->radio_rx_dev);
1380 	video_unregister_device(&dev->sdr_cap_dev);
1381 	video_unregister_device(&dev->vbi_out_dev);
1382 	video_unregister_device(&dev->vbi_cap_dev);
1383 	video_unregister_device(&dev->vid_out_dev);
1384 	video_unregister_device(&dev->vid_cap_dev);
1385 	cec_unregister_adapter(dev->cec_rx_adap);
1386 	for (i = 0; i < MAX_OUTPUTS; i++)
1387 		cec_unregister_adapter(dev->cec_tx_adap[i]);
1388 	if (dev->cec_workqueue) {
1389 		vivid_cec_bus_free_work(dev);
1390 		destroy_workqueue(dev->cec_workqueue);
1391 	}
1392 free_dev:
1393 	v4l2_device_put(&dev->v4l2_dev);
1394 	return ret;
1395 }
1396 
1397 /* This routine allocates from 1 to n_devs virtual drivers.
1398 
1399    The real maximum number of virtual drivers will depend on how many drivers
1400    will succeed. This is limited to the maximum number of devices that
1401    videodev supports, which is equal to VIDEO_NUM_DEVICES.
1402  */
vivid_probe(struct platform_device * pdev)1403 static int vivid_probe(struct platform_device *pdev)
1404 {
1405 	const struct font_desc *font = find_font("VGA8x16");
1406 	int ret = 0, i;
1407 
1408 	if (font == NULL) {
1409 		pr_err("vivid: could not find font\n");
1410 		return -ENODEV;
1411 	}
1412 
1413 	tpg_set_font(font->data);
1414 
1415 	n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
1416 
1417 	for (i = 0; i < n_devs; i++) {
1418 		ret = vivid_create_instance(pdev, i);
1419 		if (ret) {
1420 			/* If some instantiations succeeded, keep driver */
1421 			if (i)
1422 				ret = 0;
1423 			break;
1424 		}
1425 	}
1426 
1427 	if (ret < 0) {
1428 		pr_err("vivid: error %d while loading driver\n", ret);
1429 		return ret;
1430 	}
1431 
1432 	/* n_devs will reflect the actual number of allocated devices */
1433 	n_devs = i;
1434 
1435 	return ret;
1436 }
1437 
vivid_remove(struct platform_device * pdev)1438 static int vivid_remove(struct platform_device *pdev)
1439 {
1440 	struct vivid_dev *dev;
1441 	unsigned int i, j;
1442 
1443 	for (i = 0; i < n_devs; i++) {
1444 		dev = vivid_devs[i];
1445 		if (!dev)
1446 			continue;
1447 
1448 		if (dev->has_vid_cap) {
1449 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1450 				video_device_node_name(&dev->vid_cap_dev));
1451 			video_unregister_device(&dev->vid_cap_dev);
1452 		}
1453 		if (dev->has_vid_out) {
1454 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1455 				video_device_node_name(&dev->vid_out_dev));
1456 			video_unregister_device(&dev->vid_out_dev);
1457 		}
1458 		if (dev->has_vbi_cap) {
1459 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1460 				video_device_node_name(&dev->vbi_cap_dev));
1461 			video_unregister_device(&dev->vbi_cap_dev);
1462 		}
1463 		if (dev->has_vbi_out) {
1464 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1465 				video_device_node_name(&dev->vbi_out_dev));
1466 			video_unregister_device(&dev->vbi_out_dev);
1467 		}
1468 		if (dev->has_sdr_cap) {
1469 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1470 				video_device_node_name(&dev->sdr_cap_dev));
1471 			video_unregister_device(&dev->sdr_cap_dev);
1472 		}
1473 		if (dev->has_radio_rx) {
1474 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1475 				video_device_node_name(&dev->radio_rx_dev));
1476 			video_unregister_device(&dev->radio_rx_dev);
1477 		}
1478 		if (dev->has_radio_tx) {
1479 			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1480 				video_device_node_name(&dev->radio_tx_dev));
1481 			video_unregister_device(&dev->radio_tx_dev);
1482 		}
1483 		if (dev->has_fb) {
1484 			v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
1485 				dev->fb_info.node);
1486 			unregister_framebuffer(&dev->fb_info);
1487 			vivid_fb_release_buffers(dev);
1488 		}
1489 		cec_unregister_adapter(dev->cec_rx_adap);
1490 		for (j = 0; j < MAX_OUTPUTS; j++)
1491 			cec_unregister_adapter(dev->cec_tx_adap[j]);
1492 		if (dev->cec_workqueue) {
1493 			vivid_cec_bus_free_work(dev);
1494 			destroy_workqueue(dev->cec_workqueue);
1495 		}
1496 		v4l2_device_put(&dev->v4l2_dev);
1497 		vivid_devs[i] = NULL;
1498 	}
1499 	return 0;
1500 }
1501 
vivid_pdev_release(struct device * dev)1502 static void vivid_pdev_release(struct device *dev)
1503 {
1504 }
1505 
1506 static struct platform_device vivid_pdev = {
1507 	.name		= "vivid",
1508 	.dev.release	= vivid_pdev_release,
1509 };
1510 
1511 static struct platform_driver vivid_pdrv = {
1512 	.probe		= vivid_probe,
1513 	.remove		= vivid_remove,
1514 	.driver		= {
1515 		.name	= "vivid",
1516 	},
1517 };
1518 
vivid_init(void)1519 static int __init vivid_init(void)
1520 {
1521 	int ret;
1522 
1523 	ret = platform_device_register(&vivid_pdev);
1524 	if (ret)
1525 		return ret;
1526 
1527 	ret = platform_driver_register(&vivid_pdrv);
1528 	if (ret)
1529 		platform_device_unregister(&vivid_pdev);
1530 
1531 	return ret;
1532 }
1533 
vivid_exit(void)1534 static void __exit vivid_exit(void)
1535 {
1536 	platform_driver_unregister(&vivid_pdrv);
1537 	platform_device_unregister(&vivid_pdev);
1538 }
1539 
1540 module_init(vivid_init);
1541 module_exit(vivid_exit);
1542