a6fa9edd4c7ea2aca090034b08b809c13af5b2d2
[linux-2.6-block.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, 0xf0, 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, 0xea, 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, 0x63,
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         strscpy(cap->driver, "vivid", sizeof(cap->driver));
201         strscpy(cap->card, "vivid", sizeof(cap->card));
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 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
301 {
302         struct video_device *vdev = video_devdata(file);
303
304         if (vdev->vfl_dir == VFL_DIR_RX)
305                 return vivid_vid_cap_s_fbuf(file, fh, a);
306         return vivid_vid_out_s_fbuf(file, fh, a);
307 }
308
309 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
310 {
311         struct video_device *vdev = video_devdata(file);
312
313         if (vdev->vfl_dir == VFL_DIR_RX)
314                 return vivid_vid_cap_s_std(file, fh, id);
315         return vivid_vid_out_s_std(file, fh, id);
316 }
317
318 static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
319 {
320         struct video_device *vdev = video_devdata(file);
321
322         if (vdev->vfl_dir == VFL_DIR_RX)
323                 return vivid_vid_cap_s_dv_timings(file, fh, timings);
324         return vivid_vid_out_s_dv_timings(file, fh, timings);
325 }
326
327 static int vidioc_g_pixelaspect(struct file *file, void *fh,
328                                 int type, struct v4l2_fract *f)
329 {
330         struct video_device *vdev = video_devdata(file);
331
332         if (vdev->vfl_dir == VFL_DIR_RX)
333                 return vivid_vid_cap_g_pixelaspect(file, fh, type, f);
334         return vivid_vid_out_g_pixelaspect(file, fh, type, f);
335 }
336
337 static int vidioc_g_selection(struct file *file, void *fh,
338                               struct v4l2_selection *sel)
339 {
340         struct video_device *vdev = video_devdata(file);
341
342         if (vdev->vfl_dir == VFL_DIR_RX)
343                 return vivid_vid_cap_g_selection(file, fh, sel);
344         return vivid_vid_out_g_selection(file, fh, sel);
345 }
346
347 static int vidioc_s_selection(struct file *file, void *fh,
348                               struct v4l2_selection *sel)
349 {
350         struct video_device *vdev = video_devdata(file);
351
352         if (vdev->vfl_dir == VFL_DIR_RX)
353                 return vivid_vid_cap_s_selection(file, fh, sel);
354         return vivid_vid_out_s_selection(file, fh, sel);
355 }
356
357 static int vidioc_g_parm(struct file *file, void *fh,
358                           struct v4l2_streamparm *parm)
359 {
360         struct video_device *vdev = video_devdata(file);
361
362         if (vdev->vfl_dir == VFL_DIR_RX)
363                 return vivid_vid_cap_g_parm(file, fh, parm);
364         return vivid_vid_out_g_parm(file, fh, parm);
365 }
366
367 static int vidioc_s_parm(struct file *file, void *fh,
368                           struct v4l2_streamparm *parm)
369 {
370         struct video_device *vdev = video_devdata(file);
371
372         if (vdev->vfl_dir == VFL_DIR_RX)
373                 return vivid_vid_cap_s_parm(file, fh, parm);
374         return vivid_vid_out_g_parm(file, fh, parm);
375 }
376
377 static int vidioc_log_status(struct file *file, void *fh)
378 {
379         struct vivid_dev *dev = video_drvdata(file);
380         struct video_device *vdev = video_devdata(file);
381
382         v4l2_ctrl_log_status(file, fh);
383         if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_GRABBER)
384                 tpg_log_status(&dev->tpg);
385         return 0;
386 }
387
388 static ssize_t vivid_radio_read(struct file *file, char __user *buf,
389                          size_t size, loff_t *offset)
390 {
391         struct video_device *vdev = video_devdata(file);
392
393         if (vdev->vfl_dir == VFL_DIR_TX)
394                 return -EINVAL;
395         return vivid_radio_rx_read(file, buf, size, offset);
396 }
397
398 static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
399                           size_t size, loff_t *offset)
400 {
401         struct video_device *vdev = video_devdata(file);
402
403         if (vdev->vfl_dir == VFL_DIR_RX)
404                 return -EINVAL;
405         return vivid_radio_tx_write(file, buf, size, offset);
406 }
407
408 static __poll_t vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
409 {
410         struct video_device *vdev = video_devdata(file);
411
412         if (vdev->vfl_dir == VFL_DIR_RX)
413                 return vivid_radio_rx_poll(file, wait);
414         return vivid_radio_tx_poll(file, wait);
415 }
416
417 static bool vivid_is_in_use(struct video_device *vdev)
418 {
419         unsigned long flags;
420         bool res;
421
422         spin_lock_irqsave(&vdev->fh_lock, flags);
423         res = !list_empty(&vdev->fh_list);
424         spin_unlock_irqrestore(&vdev->fh_lock, flags);
425         return res;
426 }
427
428 static bool vivid_is_last_user(struct vivid_dev *dev)
429 {
430         unsigned uses = vivid_is_in_use(&dev->vid_cap_dev) +
431                         vivid_is_in_use(&dev->vid_out_dev) +
432                         vivid_is_in_use(&dev->vbi_cap_dev) +
433                         vivid_is_in_use(&dev->vbi_out_dev) +
434                         vivid_is_in_use(&dev->sdr_cap_dev) +
435                         vivid_is_in_use(&dev->radio_rx_dev) +
436                         vivid_is_in_use(&dev->radio_tx_dev);
437
438         return uses == 1;
439 }
440
441 static int vivid_fop_release(struct file *file)
442 {
443         struct vivid_dev *dev = video_drvdata(file);
444         struct video_device *vdev = video_devdata(file);
445
446         mutex_lock(&dev->mutex);
447         if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
448             !video_is_registered(vdev) && vivid_is_last_user(dev)) {
449                 /*
450                  * I am the last user of this driver, and a disconnect
451                  * was forced (since this video_device is unregistered),
452                  * so re-register all video_device's again.
453                  */
454                 v4l2_info(&dev->v4l2_dev, "reconnect\n");
455                 set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
456                 set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
457                 set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
458                 set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
459                 set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
460                 set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
461                 set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
462         }
463         mutex_unlock(&dev->mutex);
464         if (file->private_data == dev->overlay_cap_owner)
465                 dev->overlay_cap_owner = NULL;
466         if (file->private_data == dev->radio_rx_rds_owner) {
467                 dev->radio_rx_rds_last_block = 0;
468                 dev->radio_rx_rds_owner = NULL;
469         }
470         if (file->private_data == dev->radio_tx_rds_owner) {
471                 dev->radio_tx_rds_last_block = 0;
472                 dev->radio_tx_rds_owner = NULL;
473         }
474         if (vdev->queue)
475                 return vb2_fop_release(file);
476         return v4l2_fh_release(file);
477 }
478
479 static const struct v4l2_file_operations vivid_fops = {
480         .owner          = THIS_MODULE,
481         .open           = v4l2_fh_open,
482         .release        = vivid_fop_release,
483         .read           = vb2_fop_read,
484         .write          = vb2_fop_write,
485         .poll           = vb2_fop_poll,
486         .unlocked_ioctl = video_ioctl2,
487         .mmap           = vb2_fop_mmap,
488 };
489
490 static const struct v4l2_file_operations vivid_radio_fops = {
491         .owner          = THIS_MODULE,
492         .open           = v4l2_fh_open,
493         .release        = vivid_fop_release,
494         .read           = vivid_radio_read,
495         .write          = vivid_radio_write,
496         .poll           = vivid_radio_poll,
497         .unlocked_ioctl = video_ioctl2,
498 };
499
500 static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
501         .vidioc_querycap                = vidioc_querycap,
502
503         .vidioc_enum_fmt_vid_cap        = vidioc_enum_fmt_vid,
504         .vidioc_g_fmt_vid_cap           = vidioc_g_fmt_vid_cap,
505         .vidioc_try_fmt_vid_cap         = vidioc_try_fmt_vid_cap,
506         .vidioc_s_fmt_vid_cap           = vidioc_s_fmt_vid_cap,
507         .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_mplane,
508         .vidioc_g_fmt_vid_cap_mplane    = vidioc_g_fmt_vid_cap_mplane,
509         .vidioc_try_fmt_vid_cap_mplane  = vidioc_try_fmt_vid_cap_mplane,
510         .vidioc_s_fmt_vid_cap_mplane    = vidioc_s_fmt_vid_cap_mplane,
511
512         .vidioc_enum_fmt_vid_out        = vidioc_enum_fmt_vid,
513         .vidioc_g_fmt_vid_out           = vidioc_g_fmt_vid_out,
514         .vidioc_try_fmt_vid_out         = vidioc_try_fmt_vid_out,
515         .vidioc_s_fmt_vid_out           = vidioc_s_fmt_vid_out,
516         .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_mplane,
517         .vidioc_g_fmt_vid_out_mplane    = vidioc_g_fmt_vid_out_mplane,
518         .vidioc_try_fmt_vid_out_mplane  = vidioc_try_fmt_vid_out_mplane,
519         .vidioc_s_fmt_vid_out_mplane    = vidioc_s_fmt_vid_out_mplane,
520
521         .vidioc_g_selection             = vidioc_g_selection,
522         .vidioc_s_selection             = vidioc_s_selection,
523         .vidioc_g_pixelaspect           = vidioc_g_pixelaspect,
524
525         .vidioc_g_fmt_vbi_cap           = vidioc_g_fmt_vbi_cap,
526         .vidioc_try_fmt_vbi_cap         = vidioc_g_fmt_vbi_cap,
527         .vidioc_s_fmt_vbi_cap           = vidioc_s_fmt_vbi_cap,
528
529         .vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
530         .vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
531         .vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
532         .vidioc_g_sliced_vbi_cap        = vidioc_g_sliced_vbi_cap,
533
534         .vidioc_g_fmt_vbi_out           = vidioc_g_fmt_vbi_out,
535         .vidioc_try_fmt_vbi_out         = vidioc_g_fmt_vbi_out,
536         .vidioc_s_fmt_vbi_out           = vidioc_s_fmt_vbi_out,
537
538         .vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
539         .vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
540         .vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
541
542         .vidioc_enum_fmt_sdr_cap        = vidioc_enum_fmt_sdr_cap,
543         .vidioc_g_fmt_sdr_cap           = vidioc_g_fmt_sdr_cap,
544         .vidioc_try_fmt_sdr_cap         = vidioc_try_fmt_sdr_cap,
545         .vidioc_s_fmt_sdr_cap           = vidioc_s_fmt_sdr_cap,
546
547         .vidioc_overlay                 = vidioc_overlay,
548         .vidioc_enum_framesizes         = vidioc_enum_framesizes,
549         .vidioc_enum_frameintervals     = vidioc_enum_frameintervals,
550         .vidioc_g_parm                  = vidioc_g_parm,
551         .vidioc_s_parm                  = vidioc_s_parm,
552
553         .vidioc_enum_fmt_vid_overlay    = vidioc_enum_fmt_vid_overlay,
554         .vidioc_g_fmt_vid_overlay       = vidioc_g_fmt_vid_overlay,
555         .vidioc_try_fmt_vid_overlay     = vidioc_try_fmt_vid_overlay,
556         .vidioc_s_fmt_vid_overlay       = vidioc_s_fmt_vid_overlay,
557         .vidioc_g_fmt_vid_out_overlay   = vidioc_g_fmt_vid_out_overlay,
558         .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_out_overlay,
559         .vidioc_s_fmt_vid_out_overlay   = vidioc_s_fmt_vid_out_overlay,
560         .vidioc_g_fbuf                  = vidioc_g_fbuf,
561         .vidioc_s_fbuf                  = vidioc_s_fbuf,
562
563         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
564         .vidioc_create_bufs             = vb2_ioctl_create_bufs,
565         .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
566         .vidioc_querybuf                = vb2_ioctl_querybuf,
567         .vidioc_qbuf                    = vb2_ioctl_qbuf,
568         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
569         .vidioc_expbuf                  = vb2_ioctl_expbuf,
570         .vidioc_streamon                = vb2_ioctl_streamon,
571         .vidioc_streamoff               = vb2_ioctl_streamoff,
572
573         .vidioc_enum_input              = vidioc_enum_input,
574         .vidioc_g_input                 = vidioc_g_input,
575         .vidioc_s_input                 = vidioc_s_input,
576         .vidioc_s_audio                 = vidioc_s_audio,
577         .vidioc_g_audio                 = vidioc_g_audio,
578         .vidioc_enumaudio               = vidioc_enumaudio,
579         .vidioc_s_frequency             = vidioc_s_frequency,
580         .vidioc_g_frequency             = vidioc_g_frequency,
581         .vidioc_s_tuner                 = vidioc_s_tuner,
582         .vidioc_g_tuner                 = vidioc_g_tuner,
583         .vidioc_s_modulator             = vidioc_s_modulator,
584         .vidioc_g_modulator             = vidioc_g_modulator,
585         .vidioc_s_hw_freq_seek          = vidioc_s_hw_freq_seek,
586         .vidioc_enum_freq_bands         = vidioc_enum_freq_bands,
587
588         .vidioc_enum_output             = vidioc_enum_output,
589         .vidioc_g_output                = vidioc_g_output,
590         .vidioc_s_output                = vidioc_s_output,
591         .vidioc_s_audout                = vidioc_s_audout,
592         .vidioc_g_audout                = vidioc_g_audout,
593         .vidioc_enumaudout              = vidioc_enumaudout,
594
595         .vidioc_querystd                = vidioc_querystd,
596         .vidioc_g_std                   = vidioc_g_std,
597         .vidioc_s_std                   = vidioc_s_std,
598         .vidioc_s_dv_timings            = vidioc_s_dv_timings,
599         .vidioc_g_dv_timings            = vidioc_g_dv_timings,
600         .vidioc_query_dv_timings        = vidioc_query_dv_timings,
601         .vidioc_enum_dv_timings         = vidioc_enum_dv_timings,
602         .vidioc_dv_timings_cap          = vidioc_dv_timings_cap,
603         .vidioc_g_edid                  = vidioc_g_edid,
604         .vidioc_s_edid                  = vidioc_s_edid,
605
606         .vidioc_log_status              = vidioc_log_status,
607         .vidioc_subscribe_event         = vidioc_subscribe_event,
608         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
609 };
610
611 /* -----------------------------------------------------------------
612         Initialization and module stuff
613    ------------------------------------------------------------------*/
614
615 static void vivid_dev_release(struct v4l2_device *v4l2_dev)
616 {
617         struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
618
619         vivid_free_controls(dev);
620         v4l2_device_unregister(&dev->v4l2_dev);
621         vfree(dev->scaled_line);
622         vfree(dev->blended_line);
623         vfree(dev->edid);
624         vfree(dev->bitmap_cap);
625         vfree(dev->bitmap_out);
626         tpg_free(&dev->tpg);
627         kfree(dev->query_dv_timings_qmenu);
628         kfree(dev->query_dv_timings_qmenu_strings);
629         kfree(dev);
630 }
631
632 #ifdef CONFIG_MEDIA_CONTROLLER
633 static const struct media_device_ops vivid_media_ops = {
634         .req_validate = vb2_request_validate,
635         .req_queue = vb2_request_queue,
636 };
637 #endif
638
639 static int vivid_create_instance(struct platform_device *pdev, int inst)
640 {
641         static const struct v4l2_dv_timings def_dv_timings =
642                                         V4L2_DV_BT_CEA_1280X720P60;
643         static const struct vb2_mem_ops * const vivid_mem_ops[2] = {
644                 &vb2_vmalloc_memops,
645                 &vb2_dma_contig_memops,
646         };
647         unsigned in_type_counter[4] = { 0, 0, 0, 0 };
648         unsigned out_type_counter[4] = { 0, 0, 0, 0 };
649         int ccs_cap = ccs_cap_mode[inst];
650         int ccs_out = ccs_out_mode[inst];
651         bool has_tuner;
652         bool has_modulator;
653         struct vivid_dev *dev;
654         struct video_device *vfd;
655         struct vb2_queue *q;
656         unsigned node_type = node_types[inst];
657         unsigned int allocator = allocators[inst];
658         v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
659         int ret;
660         int i;
661
662         /* allocate main vivid state structure */
663         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
664         if (!dev)
665                 return -ENOMEM;
666
667         dev->inst = inst;
668
669 #ifdef CONFIG_MEDIA_CONTROLLER
670         dev->v4l2_dev.mdev = &dev->mdev;
671
672         /* Initialize media device */
673         strlcpy(dev->mdev.model, VIVID_MODULE_NAME, sizeof(dev->mdev.model));
674         snprintf(dev->mdev.bus_info, sizeof(dev->mdev.bus_info),
675                  "platform:%s-%03d", VIVID_MODULE_NAME, inst);
676         dev->mdev.dev = &pdev->dev;
677         media_device_init(&dev->mdev);
678         dev->mdev.ops = &vivid_media_ops;
679 #endif
680
681         /* register v4l2_device */
682         snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
683                         "%s-%03d", VIVID_MODULE_NAME, inst);
684         ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
685         if (ret) {
686                 kfree(dev);
687                 return ret;
688         }
689         dev->v4l2_dev.release = vivid_dev_release;
690
691         /* start detecting feature set */
692
693         /* do we use single- or multi-planar? */
694         dev->multiplanar = multiplanar[inst] > 1;
695         v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
696                         dev->multiplanar ? "multi" : "single ");
697
698         /* how many inputs do we have and of what type? */
699         dev->num_inputs = num_inputs[inst];
700         if (dev->num_inputs < 1)
701                 dev->num_inputs = 1;
702         if (dev->num_inputs >= MAX_INPUTS)
703                 dev->num_inputs = MAX_INPUTS;
704         for (i = 0; i < dev->num_inputs; i++) {
705                 dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
706                 dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
707         }
708         dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
709         if (in_type_counter[HDMI] == 16) {
710                 /* The CEC physical address only allows for max 15 inputs */
711                 in_type_counter[HDMI]--;
712                 dev->num_inputs--;
713         }
714
715         /* how many outputs do we have and of what type? */
716         dev->num_outputs = num_outputs[inst];
717         if (dev->num_outputs < 1)
718                 dev->num_outputs = 1;
719         if (dev->num_outputs >= MAX_OUTPUTS)
720                 dev->num_outputs = MAX_OUTPUTS;
721         for (i = 0; i < dev->num_outputs; i++) {
722                 dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
723                 dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
724         }
725         dev->has_audio_outputs = out_type_counter[SVID];
726         if (out_type_counter[HDMI] == 16) {
727                 /*
728                  * The CEC physical address only allows for max 15 inputs,
729                  * so outputs are also limited to 15 to allow for easy
730                  * CEC output to input mapping.
731                  */
732                 out_type_counter[HDMI]--;
733                 dev->num_outputs--;
734         }
735
736         /* do we create a video capture device? */
737         dev->has_vid_cap = node_type & 0x0001;
738
739         /* do we create a vbi capture device? */
740         if (in_type_counter[TV] || in_type_counter[SVID]) {
741                 dev->has_raw_vbi_cap = node_type & 0x0004;
742                 dev->has_sliced_vbi_cap = node_type & 0x0008;
743                 dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
744         }
745
746         /* do we create a video output device? */
747         dev->has_vid_out = node_type & 0x0100;
748
749         /* do we create a vbi output device? */
750         if (out_type_counter[SVID]) {
751                 dev->has_raw_vbi_out = node_type & 0x0400;
752                 dev->has_sliced_vbi_out = node_type & 0x0800;
753                 dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
754         }
755
756         /* do we create a radio receiver device? */
757         dev->has_radio_rx = node_type & 0x0010;
758
759         /* do we create a radio transmitter device? */
760         dev->has_radio_tx = node_type & 0x1000;
761
762         /* do we create a software defined radio capture device? */
763         dev->has_sdr_cap = node_type & 0x0020;
764
765         /* do we have a tuner? */
766         has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
767                     dev->has_radio_rx || dev->has_sdr_cap;
768
769         /* do we have a modulator? */
770         has_modulator = dev->has_radio_tx;
771
772         if (dev->has_vid_cap)
773                 /* do we have a framebuffer for overlay testing? */
774                 dev->has_fb = node_type & 0x10000;
775
776         /* can we do crop/compose/scaling while capturing? */
777         if (no_error_inj && ccs_cap == -1)
778                 ccs_cap = 7;
779
780         /* if ccs_cap == -1, then the use can select it using controls */
781         if (ccs_cap != -1) {
782                 dev->has_crop_cap = ccs_cap & 1;
783                 dev->has_compose_cap = ccs_cap & 2;
784                 dev->has_scaler_cap = ccs_cap & 4;
785                 v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
786                         dev->has_crop_cap ? 'Y' : 'N',
787                         dev->has_compose_cap ? 'Y' : 'N',
788                         dev->has_scaler_cap ? 'Y' : 'N');
789         }
790
791         /* can we do crop/compose/scaling with video output? */
792         if (no_error_inj && ccs_out == -1)
793                 ccs_out = 7;
794
795         /* if ccs_out == -1, then the use can select it using controls */
796         if (ccs_out != -1) {
797                 dev->has_crop_out = ccs_out & 1;
798                 dev->has_compose_out = ccs_out & 2;
799                 dev->has_scaler_out = ccs_out & 4;
800                 v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
801                         dev->has_crop_out ? 'Y' : 'N',
802                         dev->has_compose_out ? 'Y' : 'N',
803                         dev->has_scaler_out ? 'Y' : 'N');
804         }
805
806         /* end detecting feature set */
807
808         if (dev->has_vid_cap) {
809                 /* set up the capabilities of the video capture device */
810                 dev->vid_cap_caps = dev->multiplanar ?
811                         V4L2_CAP_VIDEO_CAPTURE_MPLANE :
812                         V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY;
813                 dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
814                 if (dev->has_audio_inputs)
815                         dev->vid_cap_caps |= V4L2_CAP_AUDIO;
816                 if (in_type_counter[TV])
817                         dev->vid_cap_caps |= V4L2_CAP_TUNER;
818         }
819         if (dev->has_vid_out) {
820                 /* set up the capabilities of the video output device */
821                 dev->vid_out_caps = dev->multiplanar ?
822                         V4L2_CAP_VIDEO_OUTPUT_MPLANE :
823                         V4L2_CAP_VIDEO_OUTPUT;
824                 if (dev->has_fb)
825                         dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
826                 dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
827                 if (dev->has_audio_outputs)
828                         dev->vid_out_caps |= V4L2_CAP_AUDIO;
829         }
830         if (dev->has_vbi_cap) {
831                 /* set up the capabilities of the vbi capture device */
832                 dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
833                                     (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
834                 dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
835                 if (dev->has_audio_inputs)
836                         dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
837                 if (in_type_counter[TV])
838                         dev->vbi_cap_caps |= V4L2_CAP_TUNER;
839         }
840         if (dev->has_vbi_out) {
841                 /* set up the capabilities of the vbi output device */
842                 dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
843                                     (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
844                 dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
845                 if (dev->has_audio_outputs)
846                         dev->vbi_out_caps |= V4L2_CAP_AUDIO;
847         }
848         if (dev->has_sdr_cap) {
849                 /* set up the capabilities of the sdr capture device */
850                 dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
851                 dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
852         }
853         /* set up the capabilities of the radio receiver device */
854         if (dev->has_radio_rx)
855                 dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
856                                      V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
857                                      V4L2_CAP_READWRITE;
858         /* set up the capabilities of the radio transmitter device */
859         if (dev->has_radio_tx)
860                 dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
861                                      V4L2_CAP_READWRITE;
862
863         ret = -ENOMEM;
864         /* initialize the test pattern generator */
865         tpg_init(&dev->tpg, 640, 360);
866         if (tpg_alloc(&dev->tpg, MAX_ZOOM * MAX_WIDTH))
867                 goto free_dev;
868         dev->scaled_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
869         if (!dev->scaled_line)
870                 goto free_dev;
871         dev->blended_line = vzalloc(array_size(MAX_WIDTH, MAX_ZOOM));
872         if (!dev->blended_line)
873                 goto free_dev;
874
875         /* load the edid */
876         dev->edid = vmalloc(256 * 128);
877         if (!dev->edid)
878                 goto free_dev;
879
880         while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
881                 dev->query_dv_timings_size++;
882
883         /*
884          * Create a char pointer array that points to the names of all the
885          * preset timings
886          */
887         dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
888                                                     sizeof(char *), GFP_KERNEL);
889         /*
890          * Create a string array containing the names of all the preset
891          * timings. Each name is max 31 chars long (+ terminating 0).
892          */
893         dev->query_dv_timings_qmenu_strings =
894                 kmalloc_array(dev->query_dv_timings_size, 32, GFP_KERNEL);
895
896         if (!dev->query_dv_timings_qmenu ||
897             !dev->query_dv_timings_qmenu_strings)
898                 goto free_dev;
899
900         for (i = 0; i < dev->query_dv_timings_size; i++) {
901                 const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
902                 char *p = dev->query_dv_timings_qmenu_strings + i * 32;
903                 u32 htot, vtot;
904
905                 dev->query_dv_timings_qmenu[i] = p;
906
907                 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
908                 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
909                 snprintf(p, 32, "%ux%u%s%u",
910                         bt->width, bt->height, bt->interlaced ? "i" : "p",
911                         (u32)bt->pixelclock / (htot * vtot));
912         }
913
914         /* disable invalid ioctls based on the feature set */
915         if (!dev->has_audio_inputs) {
916                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
917                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
918                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
919                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
920                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
921                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
922         }
923         if (!dev->has_audio_outputs) {
924                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
925                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
926                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
927                 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
928                 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
929                 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
930         }
931         if (!in_type_counter[TV] && !in_type_counter[SVID]) {
932                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
933                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
934                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
935                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
936         }
937         if (!out_type_counter[SVID]) {
938                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
939                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
940                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
941         }
942         if (!has_tuner && !has_modulator) {
943                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
944                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
945                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
946                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
947         }
948         if (!has_tuner) {
949                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
950                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
951                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
952                 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
953         }
954         if (in_type_counter[HDMI] == 0) {
955                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
956                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
957                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
958                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
959                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
960                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
961                 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
962         }
963         if (out_type_counter[HDMI] == 0) {
964                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
965                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
966                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
967                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
968                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
969         }
970         if (!dev->has_fb) {
971                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
972                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
973                 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
974         }
975         v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
976         v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
977         v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
978         v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
979         v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
980         v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
981         v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
982         v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
983         v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
984
985         /* configure internal data */
986         dev->fmt_cap = &vivid_formats[0];
987         dev->fmt_out = &vivid_formats[0];
988         if (!dev->multiplanar)
989                 vivid_formats[0].data_offset[0] = 0;
990         dev->webcam_size_idx = 1;
991         dev->webcam_ival_idx = 3;
992         tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
993         dev->std_cap = V4L2_STD_PAL;
994         dev->std_out = V4L2_STD_PAL;
995         if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
996                 tvnorms_cap = V4L2_STD_ALL;
997         if (dev->output_type[0] == SVID)
998                 tvnorms_out = V4L2_STD_ALL;
999         dev->dv_timings_cap = def_dv_timings;
1000         dev->dv_timings_out = def_dv_timings;
1001         dev->tv_freq = 2804 /* 175.25 * 16 */;
1002         dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
1003         dev->tv_field_cap = V4L2_FIELD_INTERLACED;
1004         dev->tv_field_out = V4L2_FIELD_INTERLACED;
1005         dev->radio_rx_freq = 95000 * 16;
1006         dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
1007         if (dev->has_radio_tx) {
1008                 dev->radio_tx_freq = 95500 * 16;
1009                 dev->radio_rds_loop = false;
1010         }
1011         dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
1012         dev->sdr_adc_freq = 300000;
1013         dev->sdr_fm_freq = 50000000;
1014         dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
1015         dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
1016
1017         dev->edid_max_blocks = dev->edid_blocks = 2;
1018         memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
1019         dev->radio_rds_init_time = ktime_get();
1020
1021         /* create all controls */
1022         ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
1023                         in_type_counter[TV] || in_type_counter[SVID] ||
1024                         out_type_counter[SVID],
1025                         in_type_counter[HDMI] || out_type_counter[HDMI]);
1026         if (ret)
1027                 goto unreg_dev;
1028
1029         /*
1030          * update the capture and output formats to do a proper initial
1031          * configuration.
1032          */
1033         vivid_update_format_cap(dev, false);
1034         vivid_update_format_out(dev);
1035
1036         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
1037         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
1038         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
1039         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
1040         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
1041         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
1042         v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
1043
1044         /* initialize overlay */
1045         dev->fb_cap.fmt.width = dev->src_rect.width;
1046         dev->fb_cap.fmt.height = dev->src_rect.height;
1047         dev->fb_cap.fmt.pixelformat = dev->fmt_cap->fourcc;
1048         dev->fb_cap.fmt.bytesperline = dev->src_rect.width * tpg_g_twopixelsize(&dev->tpg, 0) / 2;
1049         dev->fb_cap.fmt.sizeimage = dev->src_rect.height * dev->fb_cap.fmt.bytesperline;
1050
1051         /* initialize locks */
1052         spin_lock_init(&dev->slock);
1053         mutex_init(&dev->mutex);
1054
1055         /* init dma queues */
1056         INIT_LIST_HEAD(&dev->vid_cap_active);
1057         INIT_LIST_HEAD(&dev->vid_out_active);
1058         INIT_LIST_HEAD(&dev->vbi_cap_active);
1059         INIT_LIST_HEAD(&dev->vbi_out_active);
1060         INIT_LIST_HEAD(&dev->sdr_cap_active);
1061
1062         INIT_LIST_HEAD(&dev->cec_work_list);
1063         spin_lock_init(&dev->cec_slock);
1064         /*
1065          * Same as create_singlethread_workqueue, but now I can use the
1066          * string formatting of alloc_ordered_workqueue.
1067          */
1068         dev->cec_workqueue =
1069                 alloc_ordered_workqueue("vivid-%03d-cec", WQ_MEM_RECLAIM, inst);
1070         if (!dev->cec_workqueue) {
1071                 ret = -ENOMEM;
1072                 goto unreg_dev;
1073         }
1074
1075         if (allocator == 1)
1076                 dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1077         else if (allocator >= ARRAY_SIZE(vivid_mem_ops))
1078                 allocator = 0;
1079
1080         /* start creating the vb2 queues */
1081         if (dev->has_vid_cap) {
1082                 /* initialize vid_cap queue */
1083                 q = &dev->vb_vid_cap_q;
1084                 q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1085                         V4L2_BUF_TYPE_VIDEO_CAPTURE;
1086                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1087                 q->drv_priv = dev;
1088                 q->buf_struct_size = sizeof(struct vivid_buffer);
1089                 q->ops = &vivid_vid_cap_qops;
1090                 q->mem_ops = vivid_mem_ops[allocator];
1091                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1092                 q->min_buffers_needed = 2;
1093                 q->lock = &dev->mutex;
1094                 q->dev = dev->v4l2_dev.dev;
1095                 q->supports_requests = true;
1096
1097                 ret = vb2_queue_init(q);
1098                 if (ret)
1099                         goto unreg_dev;
1100         }
1101
1102         if (dev->has_vid_out) {
1103                 /* initialize vid_out queue */
1104                 q = &dev->vb_vid_out_q;
1105                 q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1106                         V4L2_BUF_TYPE_VIDEO_OUTPUT;
1107                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1108                 q->drv_priv = dev;
1109                 q->buf_struct_size = sizeof(struct vivid_buffer);
1110                 q->ops = &vivid_vid_out_qops;
1111                 q->mem_ops = vivid_mem_ops[allocator];
1112                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1113                 q->min_buffers_needed = 2;
1114                 q->lock = &dev->mutex;
1115                 q->dev = dev->v4l2_dev.dev;
1116                 q->supports_requests = true;
1117
1118                 ret = vb2_queue_init(q);
1119                 if (ret)
1120                         goto unreg_dev;
1121         }
1122
1123         if (dev->has_vbi_cap) {
1124                 /* initialize vbi_cap queue */
1125                 q = &dev->vb_vbi_cap_q;
1126                 q->type = dev->has_raw_vbi_cap ? V4L2_BUF_TYPE_VBI_CAPTURE :
1127                                               V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
1128                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1129                 q->drv_priv = dev;
1130                 q->buf_struct_size = sizeof(struct vivid_buffer);
1131                 q->ops = &vivid_vbi_cap_qops;
1132                 q->mem_ops = vivid_mem_ops[allocator];
1133                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1134                 q->min_buffers_needed = 2;
1135                 q->lock = &dev->mutex;
1136                 q->dev = dev->v4l2_dev.dev;
1137                 q->supports_requests = true;
1138
1139                 ret = vb2_queue_init(q);
1140                 if (ret)
1141                         goto unreg_dev;
1142         }
1143
1144         if (dev->has_vbi_out) {
1145                 /* initialize vbi_out queue */
1146                 q = &dev->vb_vbi_out_q;
1147                 q->type = dev->has_raw_vbi_out ? V4L2_BUF_TYPE_VBI_OUTPUT :
1148                                               V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
1149                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1150                 q->drv_priv = dev;
1151                 q->buf_struct_size = sizeof(struct vivid_buffer);
1152                 q->ops = &vivid_vbi_out_qops;
1153                 q->mem_ops = vivid_mem_ops[allocator];
1154                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1155                 q->min_buffers_needed = 2;
1156                 q->lock = &dev->mutex;
1157                 q->dev = dev->v4l2_dev.dev;
1158                 q->supports_requests = true;
1159
1160                 ret = vb2_queue_init(q);
1161                 if (ret)
1162                         goto unreg_dev;
1163         }
1164
1165         if (dev->has_sdr_cap) {
1166                 /* initialize sdr_cap queue */
1167                 q = &dev->vb_sdr_cap_q;
1168                 q->type = V4L2_BUF_TYPE_SDR_CAPTURE;
1169                 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1170                 q->drv_priv = dev;
1171                 q->buf_struct_size = sizeof(struct vivid_buffer);
1172                 q->ops = &vivid_sdr_cap_qops;
1173                 q->mem_ops = vivid_mem_ops[allocator];
1174                 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1175                 q->min_buffers_needed = 8;
1176                 q->lock = &dev->mutex;
1177                 q->dev = dev->v4l2_dev.dev;
1178                 q->supports_requests = true;
1179
1180                 ret = vb2_queue_init(q);
1181                 if (ret)
1182                         goto unreg_dev;
1183         }
1184
1185         if (dev->has_fb) {
1186                 /* Create framebuffer for testing capture/output overlay */
1187                 ret = vivid_fb_init(dev);
1188                 if (ret)
1189                         goto unreg_dev;
1190                 v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1191                                 dev->fb_info.node);
1192         }
1193
1194         /* finally start creating the device nodes */
1195         if (dev->has_vid_cap) {
1196                 vfd = &dev->vid_cap_dev;
1197                 snprintf(vfd->name, sizeof(vfd->name),
1198                          "vivid-%03d-vid-cap", inst);
1199                 vfd->fops = &vivid_fops;
1200                 vfd->ioctl_ops = &vivid_ioctl_ops;
1201                 vfd->device_caps = dev->vid_cap_caps;
1202                 vfd->release = video_device_release_empty;
1203                 vfd->v4l2_dev = &dev->v4l2_dev;
1204                 vfd->queue = &dev->vb_vid_cap_q;
1205                 vfd->tvnorms = tvnorms_cap;
1206
1207                 /*
1208                  * Provide a mutex to v4l2 core. It will be used to protect
1209                  * all fops and v4l2 ioctls.
1210                  */
1211                 vfd->lock = &dev->mutex;
1212                 video_set_drvdata(vfd, dev);
1213
1214 #ifdef CONFIG_MEDIA_CONTROLLER
1215                 dev->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
1216                 ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_cap_pad);
1217                 if (ret)
1218                         goto unreg_dev;
1219 #endif
1220
1221 #ifdef CONFIG_VIDEO_VIVID_CEC
1222                 if (in_type_counter[HDMI]) {
1223                         struct cec_adapter *adap;
1224
1225                         adap = vivid_cec_alloc_adap(dev, 0, false);
1226                         ret = PTR_ERR_OR_ZERO(adap);
1227                         if (ret < 0)
1228                                 goto unreg_dev;
1229                         dev->cec_rx_adap = adap;
1230                         ret = cec_register_adapter(adap, &pdev->dev);
1231                         if (ret < 0) {
1232                                 cec_delete_adapter(adap);
1233                                 dev->cec_rx_adap = NULL;
1234                                 goto unreg_dev;
1235                         }
1236                         cec_s_phys_addr(adap, 0, false);
1237                         v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input 0\n",
1238                                   dev_name(&adap->devnode.dev));
1239                 }
1240 #endif
1241
1242                 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
1243                 if (ret < 0)
1244                         goto unreg_dev;
1245                 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1246                                           video_device_node_name(vfd));
1247         }
1248
1249         if (dev->has_vid_out) {
1250 #ifdef CONFIG_VIDEO_VIVID_CEC
1251                 unsigned int bus_cnt = 0;
1252 #endif
1253
1254                 vfd = &dev->vid_out_dev;
1255                 snprintf(vfd->name, sizeof(vfd->name),
1256                          "vivid-%03d-vid-out", inst);
1257                 vfd->vfl_dir = VFL_DIR_TX;
1258                 vfd->fops = &vivid_fops;
1259                 vfd->ioctl_ops = &vivid_ioctl_ops;
1260                 vfd->device_caps = dev->vid_out_caps;
1261                 vfd->release = video_device_release_empty;
1262                 vfd->v4l2_dev = &dev->v4l2_dev;
1263                 vfd->queue = &dev->vb_vid_out_q;
1264                 vfd->tvnorms = tvnorms_out;
1265
1266                 /*
1267                  * Provide a mutex to v4l2 core. It will be used to protect
1268                  * all fops and v4l2 ioctls.
1269                  */
1270                 vfd->lock = &dev->mutex;
1271                 video_set_drvdata(vfd, dev);
1272
1273 #ifdef CONFIG_MEDIA_CONTROLLER
1274                 dev->vid_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1275                 ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_out_pad);
1276                 if (ret)
1277                         goto unreg_dev;
1278 #endif
1279
1280 #ifdef CONFIG_VIDEO_VIVID_CEC
1281                 for (i = 0; i < dev->num_outputs; i++) {
1282                         struct cec_adapter *adap;
1283
1284                         if (dev->output_type[i] != HDMI)
1285                                 continue;
1286                         dev->cec_output2bus_map[i] = bus_cnt;
1287                         adap = vivid_cec_alloc_adap(dev, bus_cnt, true);
1288                         ret = PTR_ERR_OR_ZERO(adap);
1289                         if (ret < 0)
1290                                 goto unreg_dev;
1291                         dev->cec_tx_adap[bus_cnt] = adap;
1292                         ret = cec_register_adapter(adap, &pdev->dev);
1293                         if (ret < 0) {
1294                                 cec_delete_adapter(adap);
1295                                 dev->cec_tx_adap[bus_cnt] = NULL;
1296                                 goto unreg_dev;
1297                         }
1298                         v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1299                                   dev_name(&adap->devnode.dev), bus_cnt);
1300                         bus_cnt++;
1301                         if (bus_cnt <= out_type_counter[HDMI])
1302                                 cec_s_phys_addr(adap, bus_cnt << 12, false);
1303                         else
1304                                 cec_s_phys_addr(adap, 0x1000, false);
1305                 }
1306 #endif
1307
1308                 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
1309                 if (ret < 0)
1310                         goto unreg_dev;
1311                 v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1312                                           video_device_node_name(vfd));
1313         }
1314
1315         if (dev->has_vbi_cap) {
1316                 vfd = &dev->vbi_cap_dev;
1317                 snprintf(vfd->name, sizeof(vfd->name),
1318                          "vivid-%03d-vbi-cap", inst);
1319                 vfd->fops = &vivid_fops;
1320                 vfd->ioctl_ops = &vivid_ioctl_ops;
1321                 vfd->device_caps = dev->vbi_cap_caps;
1322                 vfd->release = video_device_release_empty;
1323                 vfd->v4l2_dev = &dev->v4l2_dev;
1324                 vfd->queue = &dev->vb_vbi_cap_q;
1325                 vfd->lock = &dev->mutex;
1326                 vfd->tvnorms = tvnorms_cap;
1327                 video_set_drvdata(vfd, dev);
1328
1329 #ifdef CONFIG_MEDIA_CONTROLLER
1330                 dev->vbi_cap_pad.flags = MEDIA_PAD_FL_SINK;
1331                 ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_cap_pad);
1332                 if (ret)
1333                         goto unreg_dev;
1334 #endif
1335
1336                 ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1337                 if (ret < 0)
1338                         goto unreg_dev;
1339                 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1340                                           video_device_node_name(vfd),
1341                                           (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1342                                           "raw and sliced" :
1343                                           (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1344         }
1345
1346         if (dev->has_vbi_out) {
1347                 vfd = &dev->vbi_out_dev;
1348                 snprintf(vfd->name, sizeof(vfd->name),
1349                          "vivid-%03d-vbi-out", inst);
1350                 vfd->vfl_dir = VFL_DIR_TX;
1351                 vfd->fops = &vivid_fops;
1352                 vfd->ioctl_ops = &vivid_ioctl_ops;
1353                 vfd->device_caps = dev->vbi_out_caps;
1354                 vfd->release = video_device_release_empty;
1355                 vfd->v4l2_dev = &dev->v4l2_dev;
1356                 vfd->queue = &dev->vb_vbi_out_q;
1357                 vfd->lock = &dev->mutex;
1358                 vfd->tvnorms = tvnorms_out;
1359                 video_set_drvdata(vfd, dev);
1360
1361 #ifdef CONFIG_MEDIA_CONTROLLER
1362                 dev->vbi_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1363                 ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_out_pad);
1364                 if (ret)
1365                         goto unreg_dev;
1366 #endif
1367
1368                 ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1369                 if (ret < 0)
1370                         goto unreg_dev;
1371                 v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1372                                           video_device_node_name(vfd),
1373                                           (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1374                                           "raw and sliced" :
1375                                           (dev->has_raw_vbi_out ? "raw" : "sliced"));
1376         }
1377
1378         if (dev->has_sdr_cap) {
1379                 vfd = &dev->sdr_cap_dev;
1380                 snprintf(vfd->name, sizeof(vfd->name),
1381                          "vivid-%03d-sdr-cap", inst);
1382                 vfd->fops = &vivid_fops;
1383                 vfd->ioctl_ops = &vivid_ioctl_ops;
1384                 vfd->device_caps = dev->sdr_cap_caps;
1385                 vfd->release = video_device_release_empty;
1386                 vfd->v4l2_dev = &dev->v4l2_dev;
1387                 vfd->queue = &dev->vb_sdr_cap_q;
1388                 vfd->lock = &dev->mutex;
1389                 video_set_drvdata(vfd, dev);
1390
1391 #ifdef CONFIG_MEDIA_CONTROLLER
1392                 dev->sdr_cap_pad.flags = MEDIA_PAD_FL_SINK;
1393                 ret = media_entity_pads_init(&vfd->entity, 1, &dev->sdr_cap_pad);
1394                 if (ret)
1395                         goto unreg_dev;
1396 #endif
1397
1398                 ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1399                 if (ret < 0)
1400                         goto unreg_dev;
1401                 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1402                                           video_device_node_name(vfd));
1403         }
1404
1405         if (dev->has_radio_rx) {
1406                 vfd = &dev->radio_rx_dev;
1407                 snprintf(vfd->name, sizeof(vfd->name),
1408                          "vivid-%03d-rad-rx", inst);
1409                 vfd->fops = &vivid_radio_fops;
1410                 vfd->ioctl_ops = &vivid_ioctl_ops;
1411                 vfd->device_caps = dev->radio_rx_caps;
1412                 vfd->release = video_device_release_empty;
1413                 vfd->v4l2_dev = &dev->v4l2_dev;
1414                 vfd->lock = &dev->mutex;
1415                 video_set_drvdata(vfd, dev);
1416
1417                 ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1418                 if (ret < 0)
1419                         goto unreg_dev;
1420                 v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1421                                           video_device_node_name(vfd));
1422         }
1423
1424         if (dev->has_radio_tx) {
1425                 vfd = &dev->radio_tx_dev;
1426                 snprintf(vfd->name, sizeof(vfd->name),
1427                          "vivid-%03d-rad-tx", inst);
1428                 vfd->vfl_dir = VFL_DIR_TX;
1429                 vfd->fops = &vivid_radio_fops;
1430                 vfd->ioctl_ops = &vivid_ioctl_ops;
1431                 vfd->device_caps = dev->radio_tx_caps;
1432                 vfd->release = video_device_release_empty;
1433                 vfd->v4l2_dev = &dev->v4l2_dev;
1434                 vfd->lock = &dev->mutex;
1435                 video_set_drvdata(vfd, dev);
1436
1437                 ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1438                 if (ret < 0)
1439                         goto unreg_dev;
1440                 v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1441                                           video_device_node_name(vfd));
1442         }
1443
1444 #ifdef CONFIG_MEDIA_CONTROLLER
1445         /* Register the media device */
1446         ret = media_device_register(&dev->mdev);
1447         if (ret) {
1448                 dev_err(dev->mdev.dev,
1449                         "media device register failed (err=%d)\n", ret);
1450                 goto unreg_dev;
1451         }
1452 #endif
1453
1454         /* Now that everything is fine, let's add it to device list */
1455         vivid_devs[inst] = dev;
1456
1457         return 0;
1458
1459 unreg_dev:
1460 #ifdef CONFIG_MEDIA_CONTROLLER
1461         media_device_unregister(&dev->mdev);
1462 #endif
1463         video_unregister_device(&dev->radio_tx_dev);
1464         video_unregister_device(&dev->radio_rx_dev);
1465         video_unregister_device(&dev->sdr_cap_dev);
1466         video_unregister_device(&dev->vbi_out_dev);
1467         video_unregister_device(&dev->vbi_cap_dev);
1468         video_unregister_device(&dev->vid_out_dev);
1469         video_unregister_device(&dev->vid_cap_dev);
1470         cec_unregister_adapter(dev->cec_rx_adap);
1471         for (i = 0; i < MAX_OUTPUTS; i++)
1472                 cec_unregister_adapter(dev->cec_tx_adap[i]);
1473         if (dev->cec_workqueue) {
1474                 vivid_cec_bus_free_work(dev);
1475                 destroy_workqueue(dev->cec_workqueue);
1476         }
1477 free_dev:
1478         v4l2_device_put(&dev->v4l2_dev);
1479         return ret;
1480 }
1481
1482 /* This routine allocates from 1 to n_devs virtual drivers.
1483
1484    The real maximum number of virtual drivers will depend on how many drivers
1485    will succeed. This is limited to the maximum number of devices that
1486    videodev supports, which is equal to VIDEO_NUM_DEVICES.
1487  */
1488 static int vivid_probe(struct platform_device *pdev)
1489 {
1490         const struct font_desc *font = find_font("VGA8x16");
1491         int ret = 0, i;
1492
1493         if (font == NULL) {
1494                 pr_err("vivid: could not find font\n");
1495                 return -ENODEV;
1496         }
1497
1498         tpg_set_font(font->data);
1499
1500         n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
1501
1502         for (i = 0; i < n_devs; i++) {
1503                 ret = vivid_create_instance(pdev, i);
1504                 if (ret) {
1505                         /* If some instantiations succeeded, keep driver */
1506                         if (i)
1507                                 ret = 0;
1508                         break;
1509                 }
1510         }
1511
1512         if (ret < 0) {
1513                 pr_err("vivid: error %d while loading driver\n", ret);
1514                 return ret;
1515         }
1516
1517         /* n_devs will reflect the actual number of allocated devices */
1518         n_devs = i;
1519
1520         return ret;
1521 }
1522
1523 static int vivid_remove(struct platform_device *pdev)
1524 {
1525         struct vivid_dev *dev;
1526         unsigned int i, j;
1527
1528         for (i = 0; i < n_devs; i++) {
1529                 dev = vivid_devs[i];
1530                 if (!dev)
1531                         continue;
1532
1533 #ifdef CONFIG_MEDIA_CONTROLLER
1534                 media_device_unregister(&dev->mdev);
1535 #endif
1536
1537                 if (dev->has_vid_cap) {
1538                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1539                                 video_device_node_name(&dev->vid_cap_dev));
1540                         video_unregister_device(&dev->vid_cap_dev);
1541                 }
1542                 if (dev->has_vid_out) {
1543                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1544                                 video_device_node_name(&dev->vid_out_dev));
1545                         video_unregister_device(&dev->vid_out_dev);
1546                 }
1547                 if (dev->has_vbi_cap) {
1548                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1549                                 video_device_node_name(&dev->vbi_cap_dev));
1550                         video_unregister_device(&dev->vbi_cap_dev);
1551                 }
1552                 if (dev->has_vbi_out) {
1553                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1554                                 video_device_node_name(&dev->vbi_out_dev));
1555                         video_unregister_device(&dev->vbi_out_dev);
1556                 }
1557                 if (dev->has_sdr_cap) {
1558                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1559                                 video_device_node_name(&dev->sdr_cap_dev));
1560                         video_unregister_device(&dev->sdr_cap_dev);
1561                 }
1562                 if (dev->has_radio_rx) {
1563                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1564                                 video_device_node_name(&dev->radio_rx_dev));
1565                         video_unregister_device(&dev->radio_rx_dev);
1566                 }
1567                 if (dev->has_radio_tx) {
1568                         v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1569                                 video_device_node_name(&dev->radio_tx_dev));
1570                         video_unregister_device(&dev->radio_tx_dev);
1571                 }
1572                 if (dev->has_fb) {
1573                         v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
1574                                 dev->fb_info.node);
1575                         unregister_framebuffer(&dev->fb_info);
1576                         vivid_fb_release_buffers(dev);
1577                 }
1578                 cec_unregister_adapter(dev->cec_rx_adap);
1579                 for (j = 0; j < MAX_OUTPUTS; j++)
1580                         cec_unregister_adapter(dev->cec_tx_adap[j]);
1581                 if (dev->cec_workqueue) {
1582                         vivid_cec_bus_free_work(dev);
1583                         destroy_workqueue(dev->cec_workqueue);
1584                 }
1585                 v4l2_device_put(&dev->v4l2_dev);
1586                 vivid_devs[i] = NULL;
1587         }
1588         return 0;
1589 }
1590
1591 static void vivid_pdev_release(struct device *dev)
1592 {
1593 }
1594
1595 static struct platform_device vivid_pdev = {
1596         .name           = "vivid",
1597         .dev.release    = vivid_pdev_release,
1598 };
1599
1600 static struct platform_driver vivid_pdrv = {
1601         .probe          = vivid_probe,
1602         .remove         = vivid_remove,
1603         .driver         = {
1604                 .name   = "vivid",
1605         },
1606 };
1607
1608 static int __init vivid_init(void)
1609 {
1610         int ret;
1611
1612         ret = platform_device_register(&vivid_pdev);
1613         if (ret)
1614                 return ret;
1615
1616         ret = platform_driver_register(&vivid_pdrv);
1617         if (ret)
1618                 platform_device_unregister(&vivid_pdev);
1619
1620         return ret;
1621 }
1622
1623 static void __exit vivid_exit(void)
1624 {
1625         platform_driver_unregister(&vivid_pdrv);
1626         platform_device_unregister(&vivid_pdev);
1627 }
1628
1629 module_init(vivid_init);
1630 module_exit(vivid_exit);