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