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