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