2 * vivid-core.c - A Virtual Video Test Driver, core initialization
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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
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/videodev2.h>
30 #include <linux/v4l2-dv-timings.h>
31 #include <media/videobuf2-vmalloc.h>
32 #include <media/v4l2-dv-timings.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-fh.h>
35 #include <media/v4l2-event.h>
37 #include "vivid-core.h"
38 #include "vivid-vid-common.h"
39 #include "vivid-vid-cap.h"
40 #include "vivid-vid-out.h"
41 #include "vivid-radio-common.h"
42 #include "vivid-radio-rx.h"
43 #include "vivid-radio-tx.h"
44 #include "vivid-sdr-cap.h"
45 #include "vivid-vbi-cap.h"
46 #include "vivid-vbi-out.h"
47 #include "vivid-osd.h"
48 #include "vivid-ctrls.h"
50 #define VIVID_MODULE_NAME "vivid"
52 /* The maximum number of vivid devices */
53 #define VIVID_MAX_DEVS 64
55 MODULE_DESCRIPTION("Virtual Video Test Driver");
56 MODULE_AUTHOR("Hans Verkuil");
57 MODULE_LICENSE("GPL");
59 static unsigned n_devs = 1;
60 module_param(n_devs, uint, 0444);
61 MODULE_PARM_DESC(n_devs, " number of driver instances to create");
63 static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
64 module_param_array(vid_cap_nr, int, NULL, 0444);
65 MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
67 static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
68 module_param_array(vid_out_nr, int, NULL, 0444);
69 MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
71 static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
72 module_param_array(vbi_cap_nr, int, NULL, 0444);
73 MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
75 static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
76 module_param_array(vbi_out_nr, int, NULL, 0444);
77 MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
79 static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
80 module_param_array(sdr_cap_nr, int, NULL, 0444);
81 MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
83 static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
84 module_param_array(radio_rx_nr, int, NULL, 0444);
85 MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
87 static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
88 module_param_array(radio_tx_nr, int, NULL, 0444);
89 MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
91 static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
92 module_param_array(ccs_cap_mode, int, NULL, 0444);
93 MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
94 "\t\t bit 0=crop, 1=compose, 2=scale,\n"
95 "\t\t -1=user-controlled (default)");
97 static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
98 module_param_array(ccs_out_mode, int, NULL, 0444);
99 MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
100 "\t\t bit 0=crop, 1=compose, 2=scale,\n"
101 "\t\t -1=user-controlled (default)");
103 static unsigned multiplanar[VIVID_MAX_DEVS];
104 module_param_array(multiplanar, uint, NULL, 0444);
105 MODULE_PARM_DESC(multiplanar, " 0 (default) is alternating single and multiplanar devices,\n"
106 "\t\t 1 is single planar devices,\n"
107 "\t\t 2 is multiplanar devices");
109 /* Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr + vbi-out + vid-out */
110 static unsigned node_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0x1d3d };
111 module_param_array(node_types, uint, NULL, 0444);
112 MODULE_PARM_DESC(node_types, " node types, default is 0x1d3d. Bitmask with the following meaning:\n"
113 "\t\t bit 0: Video Capture node\n"
114 "\t\t bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
115 "\t\t bit 4: Radio Receiver node\n"
116 "\t\t bit 5: Software Defined Radio Receiver node\n"
117 "\t\t bit 8: Video Output node\n"
118 "\t\t bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
119 "\t\t bit 12: Radio Transmitter node\n"
120 "\t\t bit 16: Framebuffer for testing overlays");
122 /* Default: 4 inputs */
123 static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
124 module_param_array(num_inputs, uint, NULL, 0444);
125 MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
127 /* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
128 static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
129 module_param_array(input_types, uint, NULL, 0444);
130 MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
131 "\t\t bits 0-1 == input 0, bits 31-30 == input 15.\n"
132 "\t\t Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
134 /* Default: 2 outputs */
135 static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
136 module_param_array(num_outputs, uint, NULL, 0444);
137 MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
139 /* Default: output 0 = SVID, 1 = HDMI */
140 static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
141 module_param_array(output_types, uint, NULL, 0444);
142 MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
143 "\t\t bit 0 == output 0, bit 15 == output 15.\n"
144 "\t\t Type 0 == S-Video, 1 == HDMI");
146 unsigned vivid_debug;
147 module_param(vivid_debug, uint, 0644);
148 MODULE_PARM_DESC(vivid_debug, " activates debug info");
150 static bool no_error_inj;
151 module_param(no_error_inj, bool, 0444);
152 MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
154 static struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
156 const struct v4l2_rect vivid_min_rect = {
157 0, 0, MIN_WIDTH, MIN_HEIGHT
160 const struct v4l2_rect vivid_max_rect = {
161 0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
164 static const u8 vivid_hdmi_edid[256] = {
165 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
166 0x63, 0x3a, 0xaa, 0x55, 0x00, 0x00, 0x00, 0x00,
167 0x0a, 0x18, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
168 0x0e, 0x00, 0xb2, 0xa0, 0x57, 0x49, 0x9b, 0x26,
169 0x10, 0x48, 0x4f, 0x2f, 0xcf, 0x00, 0x31, 0x59,
170 0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
171 0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x02, 0x3a,
172 0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
173 0x46, 0x00, 0x10, 0x09, 0x00, 0x00, 0x00, 0x1e,
174 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
175 0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
176 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 'v',
177 '4', 'l', '2', '-', 'h', 'd', 'm', 'i',
178 0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x10,
179 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0,
182 0x02, 0x03, 0x1a, 0xc0, 0x48, 0xa2, 0x10, 0x04,
183 0x02, 0x01, 0x21, 0x14, 0x13, 0x23, 0x09, 0x07,
184 0x07, 0x65, 0x03, 0x0c, 0x00, 0x10, 0x00, 0xe2,
185 0x00, 0x2a, 0x01, 0x1d, 0x00, 0x80, 0x51, 0xd0,
186 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0x00, 0x00,
187 0x00, 0x00, 0x00, 0x1e, 0x8c, 0x0a, 0xd0, 0x8a,
188 0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00,
189 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
196 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd7
200 void vivid_lock(struct vb2_queue *vq)
202 struct vivid_dev *dev = vb2_get_drv_priv(vq);
204 mutex_lock(&dev->mutex);
207 void vivid_unlock(struct vb2_queue *vq)
209 struct vivid_dev *dev = vb2_get_drv_priv(vq);
211 mutex_unlock(&dev->mutex);
214 static int vidioc_querycap(struct file *file, void *priv,
215 struct v4l2_capability *cap)
217 struct vivid_dev *dev = video_drvdata(file);
218 struct video_device *vdev = video_devdata(file);
220 strcpy(cap->driver, "vivid");
221 strcpy(cap->card, "vivid");
222 snprintf(cap->bus_info, sizeof(cap->bus_info),
223 "platform:%s", dev->v4l2_dev.name);
225 if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_RX)
226 cap->device_caps = dev->vid_cap_caps;
227 if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_TX)
228 cap->device_caps = dev->vid_out_caps;
229 else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_RX)
230 cap->device_caps = dev->vbi_cap_caps;
231 else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_TX)
232 cap->device_caps = dev->vbi_out_caps;
233 else if (vdev->vfl_type == VFL_TYPE_SDR)
234 cap->device_caps = dev->sdr_cap_caps;
235 else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_RX)
236 cap->device_caps = dev->radio_rx_caps;
237 else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_TX)
238 cap->device_caps = dev->radio_tx_caps;
239 cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
240 dev->vbi_cap_caps | dev->vbi_out_caps |
241 dev->radio_rx_caps | dev->radio_tx_caps |
242 dev->sdr_cap_caps | V4L2_CAP_DEVICE_CAPS;
246 static int vidioc_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
248 struct video_device *vdev = video_devdata(file);
250 if (vdev->vfl_type == VFL_TYPE_RADIO)
251 return vivid_radio_rx_s_hw_freq_seek(file, fh, a);
255 static int vidioc_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
257 struct video_device *vdev = video_devdata(file);
259 if (vdev->vfl_type == VFL_TYPE_RADIO)
260 return vivid_radio_rx_enum_freq_bands(file, fh, band);
261 if (vdev->vfl_type == VFL_TYPE_SDR)
262 return vivid_sdr_enum_freq_bands(file, fh, band);
266 static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
268 struct video_device *vdev = video_devdata(file);
270 if (vdev->vfl_type == VFL_TYPE_RADIO)
271 return vivid_radio_rx_g_tuner(file, fh, vt);
272 if (vdev->vfl_type == VFL_TYPE_SDR)
273 return vivid_sdr_g_tuner(file, fh, vt);
274 return vivid_video_g_tuner(file, fh, vt);
277 static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
279 struct video_device *vdev = video_devdata(file);
281 if (vdev->vfl_type == VFL_TYPE_RADIO)
282 return vivid_radio_rx_s_tuner(file, fh, vt);
283 if (vdev->vfl_type == VFL_TYPE_SDR)
284 return vivid_sdr_s_tuner(file, fh, vt);
285 return vivid_video_s_tuner(file, fh, vt);
288 static int vidioc_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
290 struct vivid_dev *dev = video_drvdata(file);
291 struct video_device *vdev = video_devdata(file);
293 if (vdev->vfl_type == VFL_TYPE_RADIO)
294 return vivid_radio_g_frequency(file,
295 vdev->vfl_dir == VFL_DIR_RX ?
296 &dev->radio_rx_freq : &dev->radio_tx_freq, vf);
297 if (vdev->vfl_type == VFL_TYPE_SDR)
298 return vivid_sdr_g_frequency(file, fh, vf);
299 return vivid_video_g_frequency(file, fh, vf);
302 static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
304 struct vivid_dev *dev = video_drvdata(file);
305 struct video_device *vdev = video_devdata(file);
307 if (vdev->vfl_type == VFL_TYPE_RADIO)
308 return vivid_radio_s_frequency(file,
309 vdev->vfl_dir == VFL_DIR_RX ?
310 &dev->radio_rx_freq : &dev->radio_tx_freq, vf);
311 if (vdev->vfl_type == VFL_TYPE_SDR)
312 return vivid_sdr_s_frequency(file, fh, vf);
313 return vivid_video_s_frequency(file, fh, vf);
316 static int vidioc_overlay(struct file *file, void *fh, unsigned i)
318 struct video_device *vdev = video_devdata(file);
320 if (vdev->vfl_dir == VFL_DIR_RX)
321 return vivid_vid_cap_overlay(file, fh, i);
322 return vivid_vid_out_overlay(file, fh, i);
325 static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *a)
327 struct video_device *vdev = video_devdata(file);
329 if (vdev->vfl_dir == VFL_DIR_RX)
330 return vivid_vid_cap_g_fbuf(file, fh, a);
331 return vivid_vid_out_g_fbuf(file, fh, a);
334 static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
336 struct video_device *vdev = video_devdata(file);
338 if (vdev->vfl_dir == VFL_DIR_RX)
339 return vivid_vid_cap_s_fbuf(file, fh, a);
340 return vivid_vid_out_s_fbuf(file, fh, a);
343 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
345 struct video_device *vdev = video_devdata(file);
347 if (vdev->vfl_dir == VFL_DIR_RX)
348 return vivid_vid_cap_s_std(file, fh, id);
349 return vivid_vid_out_s_std(file, fh, id);
352 static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
354 struct video_device *vdev = video_devdata(file);
356 if (vdev->vfl_dir == VFL_DIR_RX)
357 return vivid_vid_cap_s_dv_timings(file, fh, timings);
358 return vivid_vid_out_s_dv_timings(file, fh, timings);
361 static int vidioc_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cc)
363 struct video_device *vdev = video_devdata(file);
365 if (vdev->vfl_dir == VFL_DIR_RX)
366 return vivid_vid_cap_cropcap(file, fh, cc);
367 return vivid_vid_out_cropcap(file, fh, cc);
370 static int vidioc_g_selection(struct file *file, void *fh,
371 struct v4l2_selection *sel)
373 struct video_device *vdev = video_devdata(file);
375 if (vdev->vfl_dir == VFL_DIR_RX)
376 return vivid_vid_cap_g_selection(file, fh, sel);
377 return vivid_vid_out_g_selection(file, fh, sel);
380 static int vidioc_s_selection(struct file *file, void *fh,
381 struct v4l2_selection *sel)
383 struct video_device *vdev = video_devdata(file);
385 if (vdev->vfl_dir == VFL_DIR_RX)
386 return vivid_vid_cap_s_selection(file, fh, sel);
387 return vivid_vid_out_s_selection(file, fh, sel);
390 static int vidioc_g_parm(struct file *file, void *fh,
391 struct v4l2_streamparm *parm)
393 struct video_device *vdev = video_devdata(file);
395 if (vdev->vfl_dir == VFL_DIR_RX)
396 return vivid_vid_cap_g_parm(file, fh, parm);
397 return vivid_vid_out_g_parm(file, fh, parm);
400 static int vidioc_s_parm(struct file *file, void *fh,
401 struct v4l2_streamparm *parm)
403 struct video_device *vdev = video_devdata(file);
405 if (vdev->vfl_dir == VFL_DIR_RX)
406 return vivid_vid_cap_s_parm(file, fh, parm);
407 return vivid_vid_out_g_parm(file, fh, parm);
410 static ssize_t vivid_radio_read(struct file *file, char __user *buf,
411 size_t size, loff_t *offset)
413 struct video_device *vdev = video_devdata(file);
415 if (vdev->vfl_dir == VFL_DIR_TX)
417 return vivid_radio_rx_read(file, buf, size, offset);
420 static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
421 size_t size, loff_t *offset)
423 struct video_device *vdev = video_devdata(file);
425 if (vdev->vfl_dir == VFL_DIR_RX)
427 return vivid_radio_tx_write(file, buf, size, offset);
430 static unsigned int vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
432 struct video_device *vdev = video_devdata(file);
434 if (vdev->vfl_dir == VFL_DIR_RX)
435 return vivid_radio_rx_poll(file, wait);
436 return vivid_radio_tx_poll(file, wait);
439 static bool vivid_is_in_use(struct video_device *vdev)
444 spin_lock_irqsave(&vdev->fh_lock, flags);
445 res = !list_empty(&vdev->fh_list);
446 spin_unlock_irqrestore(&vdev->fh_lock, flags);
450 static bool vivid_is_last_user(struct vivid_dev *dev)
452 unsigned uses = vivid_is_in_use(&dev->vid_cap_dev) +
453 vivid_is_in_use(&dev->vid_out_dev) +
454 vivid_is_in_use(&dev->vbi_cap_dev) +
455 vivid_is_in_use(&dev->vbi_out_dev) +
456 vivid_is_in_use(&dev->sdr_cap_dev) +
457 vivid_is_in_use(&dev->radio_rx_dev) +
458 vivid_is_in_use(&dev->radio_tx_dev);
463 static int vivid_fop_release(struct file *file)
465 struct vivid_dev *dev = video_drvdata(file);
466 struct video_device *vdev = video_devdata(file);
468 mutex_lock(&dev->mutex);
469 if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
470 !video_is_registered(vdev) && vivid_is_last_user(dev)) {
472 * I am the last user of this driver, and a disconnect
473 * was forced (since this video_device is unregistered),
474 * so re-register all video_device's again.
476 v4l2_info(&dev->v4l2_dev, "reconnect\n");
477 set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
478 set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
479 set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
480 set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
481 set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
482 set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
483 set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
485 mutex_unlock(&dev->mutex);
486 if (file->private_data == dev->overlay_cap_owner)
487 dev->overlay_cap_owner = NULL;
488 if (file->private_data == dev->radio_rx_rds_owner) {
489 dev->radio_rx_rds_last_block = 0;
490 dev->radio_rx_rds_owner = NULL;
492 if (file->private_data == dev->radio_tx_rds_owner) {
493 dev->radio_tx_rds_last_block = 0;
494 dev->radio_tx_rds_owner = NULL;
497 return vb2_fop_release(file);
498 return v4l2_fh_release(file);
501 static const struct v4l2_file_operations vivid_fops = {
502 .owner = THIS_MODULE,
503 .open = v4l2_fh_open,
504 .release = vivid_fop_release,
505 .read = vb2_fop_read,
506 .write = vb2_fop_write,
507 .poll = vb2_fop_poll,
508 .unlocked_ioctl = video_ioctl2,
509 .mmap = vb2_fop_mmap,
512 static const struct v4l2_file_operations vivid_radio_fops = {
513 .owner = THIS_MODULE,
514 .open = v4l2_fh_open,
515 .release = vivid_fop_release,
516 .read = vivid_radio_read,
517 .write = vivid_radio_write,
518 .poll = vivid_radio_poll,
519 .unlocked_ioctl = video_ioctl2,
522 static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
523 .vidioc_querycap = vidioc_querycap,
525 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid,
526 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
527 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
528 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
529 .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_mplane,
530 .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_vid_cap_mplane,
531 .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_vid_cap_mplane,
532 .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_vid_cap_mplane,
534 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid,
535 .vidioc_g_fmt_vid_out = vidioc_g_fmt_vid_out,
536 .vidioc_try_fmt_vid_out = vidioc_try_fmt_vid_out,
537 .vidioc_s_fmt_vid_out = vidioc_s_fmt_vid_out,
538 .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_mplane,
539 .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_vid_out_mplane,
540 .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_vid_out_mplane,
541 .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_vid_out_mplane,
543 .vidioc_g_selection = vidioc_g_selection,
544 .vidioc_s_selection = vidioc_s_selection,
545 .vidioc_cropcap = vidioc_cropcap,
547 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
548 .vidioc_try_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
549 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
551 .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
552 .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_fmt_sliced_vbi_cap,
553 .vidioc_s_fmt_sliced_vbi_cap = vidioc_s_fmt_sliced_vbi_cap,
554 .vidioc_g_sliced_vbi_cap = vidioc_g_sliced_vbi_cap,
556 .vidioc_g_fmt_vbi_out = vidioc_g_fmt_vbi_out,
557 .vidioc_try_fmt_vbi_out = vidioc_g_fmt_vbi_out,
558 .vidioc_s_fmt_vbi_out = vidioc_s_fmt_vbi_out,
560 .vidioc_g_fmt_sliced_vbi_out = vidioc_g_fmt_sliced_vbi_out,
561 .vidioc_try_fmt_sliced_vbi_out = vidioc_try_fmt_sliced_vbi_out,
562 .vidioc_s_fmt_sliced_vbi_out = vidioc_s_fmt_sliced_vbi_out,
564 .vidioc_enum_fmt_sdr_cap = vidioc_enum_fmt_sdr_cap,
565 .vidioc_g_fmt_sdr_cap = vidioc_g_fmt_sdr_cap,
566 .vidioc_try_fmt_sdr_cap = vidioc_g_fmt_sdr_cap,
567 .vidioc_s_fmt_sdr_cap = vidioc_g_fmt_sdr_cap,
569 .vidioc_overlay = vidioc_overlay,
570 .vidioc_enum_framesizes = vidioc_enum_framesizes,
571 .vidioc_enum_frameintervals = vidioc_enum_frameintervals,
572 .vidioc_g_parm = vidioc_g_parm,
573 .vidioc_s_parm = vidioc_s_parm,
575 .vidioc_enum_fmt_vid_overlay = vidioc_enum_fmt_vid_overlay,
576 .vidioc_g_fmt_vid_overlay = vidioc_g_fmt_vid_overlay,
577 .vidioc_try_fmt_vid_overlay = vidioc_try_fmt_vid_overlay,
578 .vidioc_s_fmt_vid_overlay = vidioc_s_fmt_vid_overlay,
579 .vidioc_g_fmt_vid_out_overlay = vidioc_g_fmt_vid_out_overlay,
580 .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_out_overlay,
581 .vidioc_s_fmt_vid_out_overlay = vidioc_s_fmt_vid_out_overlay,
582 .vidioc_g_fbuf = vidioc_g_fbuf,
583 .vidioc_s_fbuf = vidioc_s_fbuf,
585 .vidioc_reqbufs = vb2_ioctl_reqbufs,
586 .vidioc_create_bufs = vb2_ioctl_create_bufs,
587 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
588 .vidioc_querybuf = vb2_ioctl_querybuf,
589 .vidioc_qbuf = vb2_ioctl_qbuf,
590 .vidioc_dqbuf = vb2_ioctl_dqbuf,
591 /* Not yet .vidioc_expbuf = vb2_ioctl_expbuf,*/
592 .vidioc_streamon = vb2_ioctl_streamon,
593 .vidioc_streamoff = vb2_ioctl_streamoff,
595 .vidioc_enum_input = vidioc_enum_input,
596 .vidioc_g_input = vidioc_g_input,
597 .vidioc_s_input = vidioc_s_input,
598 .vidioc_s_audio = vidioc_s_audio,
599 .vidioc_g_audio = vidioc_g_audio,
600 .vidioc_enumaudio = vidioc_enumaudio,
601 .vidioc_s_frequency = vidioc_s_frequency,
602 .vidioc_g_frequency = vidioc_g_frequency,
603 .vidioc_s_tuner = vidioc_s_tuner,
604 .vidioc_g_tuner = vidioc_g_tuner,
605 .vidioc_s_modulator = vidioc_s_modulator,
606 .vidioc_g_modulator = vidioc_g_modulator,
607 .vidioc_s_hw_freq_seek = vidioc_s_hw_freq_seek,
608 .vidioc_enum_freq_bands = vidioc_enum_freq_bands,
610 .vidioc_enum_output = vidioc_enum_output,
611 .vidioc_g_output = vidioc_g_output,
612 .vidioc_s_output = vidioc_s_output,
613 .vidioc_s_audout = vidioc_s_audout,
614 .vidioc_g_audout = vidioc_g_audout,
615 .vidioc_enumaudout = vidioc_enumaudout,
617 .vidioc_querystd = vidioc_querystd,
618 .vidioc_g_std = vidioc_g_std,
619 .vidioc_s_std = vidioc_s_std,
620 .vidioc_s_dv_timings = vidioc_s_dv_timings,
621 .vidioc_g_dv_timings = vidioc_g_dv_timings,
622 .vidioc_query_dv_timings = vidioc_query_dv_timings,
623 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
624 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
625 .vidioc_g_edid = vidioc_g_edid,
626 .vidioc_s_edid = vidioc_s_edid,
628 .vidioc_log_status = v4l2_ctrl_log_status,
629 .vidioc_subscribe_event = vidioc_subscribe_event,
630 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
633 /* -----------------------------------------------------------------
634 Initialization and module stuff
635 ------------------------------------------------------------------*/
637 static int __init vivid_create_instance(int inst)
639 static const struct v4l2_dv_timings def_dv_timings =
640 V4L2_DV_BT_CEA_1280X720P60;
641 unsigned in_type_counter[4] = { 0, 0, 0, 0 };
642 unsigned out_type_counter[4] = { 0, 0, 0, 0 };
643 int ccs_cap = ccs_cap_mode[inst];
644 int ccs_out = ccs_out_mode[inst];
647 struct vivid_dev *dev;
648 struct video_device *vfd;
650 unsigned node_type = node_types[inst];
651 v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
655 /* allocate main vivid state structure */
656 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
662 /* register v4l2_device */
663 snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
664 "%s-%03d", VIVID_MODULE_NAME, inst);
665 ret = v4l2_device_register(NULL, &dev->v4l2_dev);
669 /* start detecting feature set */
671 /* do we use single- or multi-planar? */
672 if (multiplanar[inst] == 0)
673 dev->multiplanar = inst & 1;
675 dev->multiplanar = multiplanar[inst] > 1;
676 v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
677 dev->multiplanar ? "multi" : "single ");
679 /* how many inputs do we have and of what type? */
680 dev->num_inputs = num_inputs[inst];
681 if (dev->num_inputs < 1)
683 if (dev->num_inputs >= MAX_INPUTS)
684 dev->num_inputs = MAX_INPUTS;
685 for (i = 0; i < dev->num_inputs; i++) {
686 dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
687 dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
689 dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
691 /* how many outputs do we have and of what type? */
692 dev->num_outputs = num_outputs[inst];
693 if (dev->num_outputs < 1)
694 dev->num_outputs = 1;
695 if (dev->num_outputs >= MAX_OUTPUTS)
696 dev->num_outputs = MAX_OUTPUTS;
697 for (i = 0; i < dev->num_outputs; i++) {
698 dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
699 dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
701 dev->has_audio_outputs = out_type_counter[SVID];
703 /* do we create a video capture device? */
704 dev->has_vid_cap = node_type & 0x0001;
706 /* do we create a vbi capture device? */
707 if (in_type_counter[TV] || in_type_counter[SVID]) {
708 dev->has_raw_vbi_cap = node_type & 0x0004;
709 dev->has_sliced_vbi_cap = node_type & 0x0008;
710 dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
713 /* do we create a video output device? */
714 dev->has_vid_out = node_type & 0x0100;
716 /* do we create a vbi output device? */
717 if (out_type_counter[SVID]) {
718 dev->has_raw_vbi_out = node_type & 0x0400;
719 dev->has_sliced_vbi_out = node_type & 0x0800;
720 dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
723 /* do we create a radio receiver device? */
724 dev->has_radio_rx = node_type & 0x0010;
726 /* do we create a radio transmitter device? */
727 dev->has_radio_tx = node_type & 0x1000;
729 /* do we create a software defined radio capture device? */
730 dev->has_sdr_cap = node_type & 0x0020;
732 /* do we have a tuner? */
733 has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
734 dev->has_radio_rx || dev->has_sdr_cap;
736 /* do we have a modulator? */
737 has_modulator = dev->has_radio_tx;
739 if (dev->has_vid_cap)
740 /* do we have a framebuffer for overlay testing? */
741 dev->has_fb = node_type & 0x10000;
743 /* can we do crop/compose/scaling while capturing? */
744 if (no_error_inj && ccs_cap == -1)
747 /* if ccs_cap == -1, then the use can select it using controls */
749 dev->has_crop_cap = ccs_cap & 1;
750 dev->has_compose_cap = ccs_cap & 2;
751 dev->has_scaler_cap = ccs_cap & 4;
752 v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
753 dev->has_crop_cap ? 'Y' : 'N',
754 dev->has_compose_cap ? 'Y' : 'N',
755 dev->has_scaler_cap ? 'Y' : 'N');
758 /* can we do crop/compose/scaling with video output? */
759 if (no_error_inj && ccs_out == -1)
762 /* if ccs_out == -1, then the use can select it using controls */
764 dev->has_crop_out = ccs_out & 1;
765 dev->has_compose_out = ccs_out & 2;
766 dev->has_scaler_out = ccs_out & 4;
767 v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
768 dev->has_crop_out ? 'Y' : 'N',
769 dev->has_compose_out ? 'Y' : 'N',
770 dev->has_scaler_out ? 'Y' : 'N');
773 /* end detecting feature set */
775 if (dev->has_vid_cap) {
776 /* set up the capabilities of the video capture device */
777 dev->vid_cap_caps = dev->multiplanar ?
778 V4L2_CAP_VIDEO_CAPTURE_MPLANE :
779 V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY;
780 dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
781 if (dev->has_audio_inputs)
782 dev->vid_cap_caps |= V4L2_CAP_AUDIO;
783 if (in_type_counter[TV])
784 dev->vid_cap_caps |= V4L2_CAP_TUNER;
786 if (dev->has_vid_out) {
787 /* set up the capabilities of the video output device */
788 dev->vid_out_caps = dev->multiplanar ?
789 V4L2_CAP_VIDEO_OUTPUT_MPLANE :
790 V4L2_CAP_VIDEO_OUTPUT;
792 dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
793 dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
794 if (dev->has_audio_outputs)
795 dev->vid_out_caps |= V4L2_CAP_AUDIO;
797 if (dev->has_vbi_cap) {
798 /* set up the capabilities of the vbi capture device */
799 dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
800 (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
801 dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
802 if (dev->has_audio_inputs)
803 dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
804 if (in_type_counter[TV])
805 dev->vbi_cap_caps |= V4L2_CAP_TUNER;
807 if (dev->has_vbi_out) {
808 /* set up the capabilities of the vbi output device */
809 dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
810 (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
811 dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
812 if (dev->has_audio_outputs)
813 dev->vbi_out_caps |= V4L2_CAP_AUDIO;
815 if (dev->has_sdr_cap) {
816 /* set up the capabilities of the sdr capture device */
817 dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
818 dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
820 /* set up the capabilities of the radio receiver device */
821 if (dev->has_radio_rx)
822 dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
823 V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
825 /* set up the capabilities of the radio transmitter device */
826 if (dev->has_radio_tx)
827 dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
830 /* initialize the test pattern generator */
831 tpg_init(&dev->tpg, 640, 360);
832 if (tpg_alloc(&dev->tpg, MAX_ZOOM * MAX_WIDTH))
834 dev->scaled_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
835 if (!dev->scaled_line)
837 dev->blended_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
838 if (!dev->blended_line)
842 dev->edid = vmalloc(256 * 128);
846 /* create a string array containing the names of all the preset timings */
847 while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
848 dev->query_dv_timings_size++;
849 dev->query_dv_timings_qmenu = kmalloc(dev->query_dv_timings_size *
850 (sizeof(void *) + 32), GFP_KERNEL);
851 if (dev->query_dv_timings_qmenu == NULL)
853 for (i = 0; i < dev->query_dv_timings_size; i++) {
854 const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
855 char *p = (char *)&dev->query_dv_timings_qmenu[dev->query_dv_timings_size];
859 dev->query_dv_timings_qmenu[i] = p;
861 htot = V4L2_DV_BT_FRAME_WIDTH(bt);
862 vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
863 snprintf(p, 32, "%ux%u%s%u",
864 bt->width, bt->height, bt->interlaced ? "i" : "p",
865 (u32)bt->pixelclock / (htot * vtot));
868 /* disable invalid ioctls based on the feature set */
869 if (!dev->has_audio_inputs) {
870 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
871 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
872 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
873 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
874 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
875 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
877 if (!dev->has_audio_outputs) {
878 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
879 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
880 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
881 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
882 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
883 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
885 if (!in_type_counter[TV] && !in_type_counter[SVID]) {
886 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
887 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
888 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
889 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
891 if (!out_type_counter[SVID]) {
892 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
893 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
894 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
896 if (!has_tuner && !has_modulator) {
897 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
898 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
899 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
900 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
903 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
904 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
905 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
906 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
908 if (in_type_counter[HDMI] == 0) {
909 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
910 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
911 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
912 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
913 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
914 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
915 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
917 if (out_type_counter[HDMI] == 0) {
918 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
919 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
920 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
921 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
922 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
925 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
926 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
927 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
929 v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
930 v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
931 v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
932 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
933 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
934 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
935 v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
936 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
937 v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
939 /* configure internal data */
940 dev->fmt_cap = &vivid_formats[0];
941 dev->fmt_out = &vivid_formats[0];
942 if (!dev->multiplanar)
943 vivid_formats[0].data_offset[0] = 0;
944 dev->webcam_size_idx = 1;
945 dev->webcam_ival_idx = 3;
946 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
947 dev->std_cap = V4L2_STD_PAL;
948 dev->std_out = V4L2_STD_PAL;
949 if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
950 tvnorms_cap = V4L2_STD_ALL;
951 if (dev->output_type[0] == SVID)
952 tvnorms_out = V4L2_STD_ALL;
953 dev->dv_timings_cap = def_dv_timings;
954 dev->dv_timings_out = def_dv_timings;
955 dev->tv_freq = 2804 /* 175.25 * 16 */;
956 dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
957 dev->tv_field_cap = V4L2_FIELD_INTERLACED;
958 dev->tv_field_out = V4L2_FIELD_INTERLACED;
959 dev->radio_rx_freq = 95000 * 16;
960 dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
961 if (dev->has_radio_tx) {
962 dev->radio_tx_freq = 95500 * 16;
963 dev->radio_rds_loop = false;
965 dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
966 dev->sdr_adc_freq = 300000;
967 dev->sdr_fm_freq = 50000000;
968 dev->edid_max_blocks = dev->edid_blocks = 2;
969 memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
970 ktime_get_ts(&dev->radio_rds_init_ts);
972 /* create all controls */
973 ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
974 in_type_counter[TV] || in_type_counter[SVID] ||
975 out_type_counter[SVID],
976 in_type_counter[HDMI] || out_type_counter[HDMI]);
981 * update the capture and output formats to do a proper initial
984 vivid_update_format_cap(dev, false);
985 vivid_update_format_out(dev);
987 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
988 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
989 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
990 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
991 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
992 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
993 v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
995 /* initialize overlay */
996 dev->fb_cap.fmt.width = dev->src_rect.width;
997 dev->fb_cap.fmt.height = dev->src_rect.height;
998 dev->fb_cap.fmt.pixelformat = dev->fmt_cap->fourcc;
999 dev->fb_cap.fmt.bytesperline = dev->src_rect.width * tpg_g_twopixelsize(&dev->tpg, 0) / 2;
1000 dev->fb_cap.fmt.sizeimage = dev->src_rect.height * dev->fb_cap.fmt.bytesperline;
1002 /* initialize locks */
1003 spin_lock_init(&dev->slock);
1004 mutex_init(&dev->mutex);
1006 /* init dma queues */
1007 INIT_LIST_HEAD(&dev->vid_cap_active);
1008 INIT_LIST_HEAD(&dev->vid_out_active);
1009 INIT_LIST_HEAD(&dev->vbi_cap_active);
1010 INIT_LIST_HEAD(&dev->vbi_out_active);
1011 INIT_LIST_HEAD(&dev->sdr_cap_active);
1013 /* start creating the vb2 queues */
1014 if (dev->has_vid_cap) {
1015 /* initialize vid_cap queue */
1016 q = &dev->vb_vid_cap_q;
1017 q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1018 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1019 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1021 q->buf_struct_size = sizeof(struct vivid_buffer);
1022 q->ops = &vivid_vid_cap_qops;
1023 q->mem_ops = &vb2_vmalloc_memops;
1024 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1025 q->min_buffers_needed = 2;
1027 ret = vb2_queue_init(q);
1032 if (dev->has_vid_out) {
1033 /* initialize vid_out queue */
1034 q = &dev->vb_vid_out_q;
1035 q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1036 V4L2_BUF_TYPE_VIDEO_OUTPUT;
1037 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1039 q->buf_struct_size = sizeof(struct vivid_buffer);
1040 q->ops = &vivid_vid_out_qops;
1041 q->mem_ops = &vb2_vmalloc_memops;
1042 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1043 q->min_buffers_needed = 2;
1045 ret = vb2_queue_init(q);
1050 if (dev->has_vbi_cap) {
1051 /* initialize vbi_cap queue */
1052 q = &dev->vb_vbi_cap_q;
1053 q->type = dev->has_raw_vbi_cap ? V4L2_BUF_TYPE_VBI_CAPTURE :
1054 V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
1055 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1057 q->buf_struct_size = sizeof(struct vivid_buffer);
1058 q->ops = &vivid_vbi_cap_qops;
1059 q->mem_ops = &vb2_vmalloc_memops;
1060 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1061 q->min_buffers_needed = 2;
1063 ret = vb2_queue_init(q);
1068 if (dev->has_vbi_out) {
1069 /* initialize vbi_out queue */
1070 q = &dev->vb_vbi_out_q;
1071 q->type = dev->has_raw_vbi_out ? V4L2_BUF_TYPE_VBI_OUTPUT :
1072 V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
1073 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1075 q->buf_struct_size = sizeof(struct vivid_buffer);
1076 q->ops = &vivid_vbi_out_qops;
1077 q->mem_ops = &vb2_vmalloc_memops;
1078 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1079 q->min_buffers_needed = 2;
1081 ret = vb2_queue_init(q);
1086 if (dev->has_sdr_cap) {
1087 /* initialize sdr_cap queue */
1088 q = &dev->vb_sdr_cap_q;
1089 q->type = V4L2_BUF_TYPE_SDR_CAPTURE;
1090 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1092 q->buf_struct_size = sizeof(struct vivid_buffer);
1093 q->ops = &vivid_sdr_cap_qops;
1094 q->mem_ops = &vb2_vmalloc_memops;
1095 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1096 q->min_buffers_needed = 8;
1098 ret = vb2_queue_init(q);
1104 /* Create framebuffer for testing capture/output overlay */
1105 ret = vivid_fb_init(dev);
1108 v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1112 /* finally start creating the device nodes */
1113 if (dev->has_vid_cap) {
1114 vfd = &dev->vid_cap_dev;
1115 strlcpy(vfd->name, "vivid-vid-cap", sizeof(vfd->name));
1116 vfd->fops = &vivid_fops;
1117 vfd->ioctl_ops = &vivid_ioctl_ops;
1118 vfd->release = video_device_release_empty;
1119 vfd->v4l2_dev = &dev->v4l2_dev;
1120 vfd->queue = &dev->vb_vid_cap_q;
1121 vfd->tvnorms = tvnorms_cap;
1124 * Provide a mutex to v4l2 core. It will be used to protect
1125 * all fops and v4l2 ioctls.
1127 vfd->lock = &dev->mutex;
1128 video_set_drvdata(vfd, dev);
1130 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
1133 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1134 video_device_node_name(vfd));
1137 if (dev->has_vid_out) {
1138 vfd = &dev->vid_out_dev;
1139 strlcpy(vfd->name, "vivid-vid-out", sizeof(vfd->name));
1140 vfd->vfl_dir = VFL_DIR_TX;
1141 vfd->fops = &vivid_fops;
1142 vfd->ioctl_ops = &vivid_ioctl_ops;
1143 vfd->release = video_device_release_empty;
1144 vfd->v4l2_dev = &dev->v4l2_dev;
1145 vfd->queue = &dev->vb_vid_out_q;
1146 vfd->tvnorms = tvnorms_out;
1149 * Provide a mutex to v4l2 core. It will be used to protect
1150 * all fops and v4l2 ioctls.
1152 vfd->lock = &dev->mutex;
1153 video_set_drvdata(vfd, dev);
1155 ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
1158 v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1159 video_device_node_name(vfd));
1162 if (dev->has_vbi_cap) {
1163 vfd = &dev->vbi_cap_dev;
1164 strlcpy(vfd->name, "vivid-vbi-cap", sizeof(vfd->name));
1165 vfd->fops = &vivid_fops;
1166 vfd->ioctl_ops = &vivid_ioctl_ops;
1167 vfd->release = video_device_release_empty;
1168 vfd->v4l2_dev = &dev->v4l2_dev;
1169 vfd->queue = &dev->vb_vbi_cap_q;
1170 vfd->lock = &dev->mutex;
1171 vfd->tvnorms = tvnorms_cap;
1172 video_set_drvdata(vfd, dev);
1174 ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1177 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1178 video_device_node_name(vfd),
1179 (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1181 (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1184 if (dev->has_vbi_out) {
1185 vfd = &dev->vbi_out_dev;
1186 strlcpy(vfd->name, "vivid-vbi-out", sizeof(vfd->name));
1187 vfd->vfl_dir = VFL_DIR_TX;
1188 vfd->fops = &vivid_fops;
1189 vfd->ioctl_ops = &vivid_ioctl_ops;
1190 vfd->release = video_device_release_empty;
1191 vfd->v4l2_dev = &dev->v4l2_dev;
1192 vfd->queue = &dev->vb_vbi_out_q;
1193 vfd->lock = &dev->mutex;
1194 vfd->tvnorms = tvnorms_out;
1195 video_set_drvdata(vfd, dev);
1197 ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1200 v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1201 video_device_node_name(vfd),
1202 (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1204 (dev->has_raw_vbi_out ? "raw" : "sliced"));
1207 if (dev->has_sdr_cap) {
1208 vfd = &dev->sdr_cap_dev;
1209 strlcpy(vfd->name, "vivid-sdr-cap", sizeof(vfd->name));
1210 vfd->fops = &vivid_fops;
1211 vfd->ioctl_ops = &vivid_ioctl_ops;
1212 vfd->release = video_device_release_empty;
1213 vfd->v4l2_dev = &dev->v4l2_dev;
1214 vfd->queue = &dev->vb_sdr_cap_q;
1215 vfd->lock = &dev->mutex;
1216 video_set_drvdata(vfd, dev);
1218 ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1221 v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1222 video_device_node_name(vfd));
1225 if (dev->has_radio_rx) {
1226 vfd = &dev->radio_rx_dev;
1227 strlcpy(vfd->name, "vivid-rad-rx", sizeof(vfd->name));
1228 vfd->fops = &vivid_radio_fops;
1229 vfd->ioctl_ops = &vivid_ioctl_ops;
1230 vfd->release = video_device_release_empty;
1231 vfd->v4l2_dev = &dev->v4l2_dev;
1232 vfd->lock = &dev->mutex;
1233 video_set_drvdata(vfd, dev);
1235 ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1238 v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1239 video_device_node_name(vfd));
1242 if (dev->has_radio_tx) {
1243 vfd = &dev->radio_tx_dev;
1244 strlcpy(vfd->name, "vivid-rad-tx", sizeof(vfd->name));
1245 vfd->vfl_dir = VFL_DIR_TX;
1246 vfd->fops = &vivid_radio_fops;
1247 vfd->ioctl_ops = &vivid_ioctl_ops;
1248 vfd->release = video_device_release_empty;
1249 vfd->v4l2_dev = &dev->v4l2_dev;
1250 vfd->lock = &dev->mutex;
1251 video_set_drvdata(vfd, dev);
1253 ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1256 v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1257 video_device_node_name(vfd));
1260 /* Now that everything is fine, let's add it to device list */
1261 vivid_devs[inst] = dev;
1266 video_unregister_device(&dev->radio_tx_dev);
1267 video_unregister_device(&dev->radio_rx_dev);
1268 video_unregister_device(&dev->sdr_cap_dev);
1269 video_unregister_device(&dev->vbi_out_dev);
1270 video_unregister_device(&dev->vbi_cap_dev);
1271 video_unregister_device(&dev->vid_out_dev);
1272 video_unregister_device(&dev->vid_cap_dev);
1273 vivid_free_controls(dev);
1274 v4l2_device_unregister(&dev->v4l2_dev);
1276 vfree(dev->scaled_line);
1277 vfree(dev->blended_line);
1279 tpg_free(&dev->tpg);
1280 kfree(dev->query_dv_timings_qmenu);
1285 /* This routine allocates from 1 to n_devs virtual drivers.
1287 The real maximum number of virtual drivers will depend on how many drivers
1288 will succeed. This is limited to the maximum number of devices that
1289 videodev supports, which is equal to VIDEO_NUM_DEVICES.
1291 static int __init vivid_init(void)
1293 const struct font_desc *font = find_font("VGA8x16");
1297 pr_err("vivid: could not find font\n");
1301 tpg_set_font(font->data);
1303 n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
1305 for (i = 0; i < n_devs; i++) {
1306 ret = vivid_create_instance(i);
1308 /* If some instantiations succeeded, keep driver */
1316 pr_err("vivid: error %d while loading driver\n", ret);
1320 /* n_devs will reflect the actual number of allocated devices */
1326 static void __exit vivid_exit(void)
1328 struct vivid_dev *dev;
1331 for (i = 0; vivid_devs[i]; i++) {
1332 dev = vivid_devs[i];
1334 if (dev->has_vid_cap) {
1335 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1336 video_device_node_name(&dev->vid_cap_dev));
1337 video_unregister_device(&dev->vid_cap_dev);
1339 if (dev->has_vid_out) {
1340 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1341 video_device_node_name(&dev->vid_out_dev));
1342 video_unregister_device(&dev->vid_out_dev);
1344 if (dev->has_vbi_cap) {
1345 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1346 video_device_node_name(&dev->vbi_cap_dev));
1347 video_unregister_device(&dev->vbi_cap_dev);
1349 if (dev->has_vbi_out) {
1350 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1351 video_device_node_name(&dev->vbi_out_dev));
1352 video_unregister_device(&dev->vbi_out_dev);
1354 if (dev->has_sdr_cap) {
1355 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1356 video_device_node_name(&dev->sdr_cap_dev));
1357 video_unregister_device(&dev->sdr_cap_dev);
1359 if (dev->has_radio_rx) {
1360 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1361 video_device_node_name(&dev->radio_rx_dev));
1362 video_unregister_device(&dev->radio_rx_dev);
1364 if (dev->has_radio_tx) {
1365 v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1366 video_device_node_name(&dev->radio_tx_dev));
1367 video_unregister_device(&dev->radio_tx_dev);
1370 v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
1372 unregister_framebuffer(&dev->fb_info);
1373 vivid_fb_release_buffers(dev);
1375 v4l2_device_unregister(&dev->v4l2_dev);
1376 vivid_free_controls(dev);
1377 vfree(dev->scaled_line);
1378 vfree(dev->blended_line);
1380 vfree(dev->bitmap_cap);
1381 vfree(dev->bitmap_out);
1382 tpg_free(&dev->tpg);
1383 kfree(dev->query_dv_timings_qmenu);
1385 vivid_devs[i] = NULL;
1389 module_init(vivid_init);
1390 module_exit(vivid_exit);