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