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