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