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