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