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