File Index
Files and directories
Sign in
FILE

vivid-core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/test-drivers/vivid/vivid-core.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2
   3/*
   4 * vivid-core.c - A Virtual Video Test Driver, core initialization
   5 *
   6 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   7 */
   8
   9#include <linux/module.h>
  10#include <linux/errno.h>
  11#include <linux/kernel.h>
  12#include <linux/init.h>
  13#include <linux/sched.h>
  14#include <linux/slab.h>
  15#include <linux/vmalloc.h>
  16#include <linux/font.h>
  17#include <linux/mutex.h>
  18#include <linux/platform_device.h>
  19#include <linux/videodev2.h>
  20#include <linux/v4l2-dv-timings.h>
  21#include <media/videobuf2-vmalloc.h>
  22#include <media/videobuf2-dma-contig.h>
  23#include <media/v4l2-dv-timings.h>
  24#include <media/v4l2-ioctl.h>
  25#include <media/v4l2-fh.h>
  26#include <media/v4l2-event.h>
  27
  28#include "vivid-core.h"
  29#include "vivid-vid-common.h"
  30#include "vivid-vid-cap.h"
  31#include "vivid-vid-out.h"
  32#include "vivid-radio-common.h"
  33#include "vivid-radio-rx.h"
  34#include "vivid-radio-tx.h"
  35#include "vivid-sdr-cap.h"
  36#include "vivid-vbi-cap.h"
  37#include "vivid-vbi-out.h"
  38#include "vivid-osd.h"
  39#include "vivid-cec.h"
  40#include "vivid-ctrls.h"
  41#include "vivid-meta-cap.h"
  42#include "vivid-meta-out.h"
  43#include "vivid-touch-cap.h"
  44
  45#define VIVID_MODULE_NAME "vivid"
  46#define MAX_STRING_LENGTH 23
  47
  48MODULE_DESCRIPTION("Virtual Video Test Driver");
  49MODULE_AUTHOR("Hans Verkuil");
  50MODULE_LICENSE("GPL");
  51
  52unsigned int n_devs = 1;
  53module_param(n_devs, uint, 0444);
  54MODULE_PARM_DESC(n_devs, " number of driver instances to create");
  55
  56static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  57module_param_array(vid_cap_nr, int, NULL, 0444);
  58MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
  59
  60static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  61module_param_array(vid_out_nr, int, NULL, 0444);
  62MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
  63
  64static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  65module_param_array(vbi_cap_nr, int, NULL, 0444);
  66MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
  67
  68static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  69module_param_array(vbi_out_nr, int, NULL, 0444);
  70MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
  71
  72static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  73module_param_array(sdr_cap_nr, int, NULL, 0444);
  74MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
  75
  76static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  77module_param_array(radio_rx_nr, int, NULL, 0444);
  78MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
  79
  80static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  81module_param_array(radio_tx_nr, int, NULL, 0444);
  82MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
  83
  84static int meta_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  85module_param_array(meta_cap_nr, int, NULL, 0444);
  86MODULE_PARM_DESC(meta_cap_nr, " videoX start number, -1 is autodetect");
  87
  88static int meta_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  89module_param_array(meta_out_nr, int, NULL, 0444);
  90MODULE_PARM_DESC(meta_out_nr, " videoX start number, -1 is autodetect");
  91
  92static int touch_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  93module_param_array(touch_cap_nr, int, NULL, 0444);
  94MODULE_PARM_DESC(touch_cap_nr, " v4l-touchX start number, -1 is autodetect");
  95
  96static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  97module_param_array(ccs_cap_mode, int, NULL, 0444);
  98MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
  99			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
 100			   "\t\t    -1=user-controlled (default)");
 101
 102static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
 103module_param_array(ccs_out_mode, int, NULL, 0444);
 104MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
 105			   "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
 106			   "\t\t    -1=user-controlled (default)");
 107
 108static unsigned multiplanar[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 1 };
 109module_param_array(multiplanar, uint, NULL, 0444);
 110MODULE_PARM_DESC(multiplanar, " 1 (default) creates a single planar device, 2 creates a multiplanar device.");
 111
 112/*
 113 * Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr +
 114 * vbi-out + vid-out + meta-cap
 115 */
 116static unsigned int node_types[VIVID_MAX_DEVS] = {
 117	[0 ... (VIVID_MAX_DEVS - 1)] = 0xe1d3d
 118};
 119module_param_array(node_types, uint, NULL, 0444);
 120MODULE_PARM_DESC(node_types, " node types, default is 0xe1d3d. Bitmask with the following meaning:\n"
 121			     "\t\t    bit 0: Video Capture node\n"
 122			     "\t\t    bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
 123			     "\t\t    bit 4: Radio Receiver node\n"
 124			     "\t\t    bit 5: Software Defined Radio Receiver node\n"
 125			     "\t\t    bit 8: Video Output node\n"
 126			     "\t\t    bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
 127			     "\t\t    bit 12: Radio Transmitter node\n"
 128#ifdef CONFIG_VIDEO_VIVID_OSD
 129			     "\t\t    bit 16: Framebuffer for testing output overlays\n"
 130#endif
 131			     "\t\t    bit 17: Metadata Capture node\n"
 132			     "\t\t    bit 18: Metadata Output node\n"
 133			     "\t\t    bit 19: Touch Capture node\n");
 134
 135/* Default: 4 inputs */
 136static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
 137module_param_array(num_inputs, uint, NULL, 0444);
 138MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
 139
 140/* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
 141static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
 142module_param_array(input_types, uint, NULL, 0444);
 143MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
 144			      "\t\t    bits 0-1 == input 0, bits 31-30 == input 15.\n"
 145			      "\t\t    Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
 146
 147/* Default: 2 outputs */
 148static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
 149module_param_array(num_outputs, uint, NULL, 0444);
 150MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
 151
 152/* Default: output 0 = SVID, 1 = HDMI */
 153static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
 154module_param_array(output_types, uint, NULL, 0444);
 155MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
 156			      "\t\t    bit 0 == output 0, bit 15 == output 15.\n"
 157			      "\t\t    Type 0 == S-Video, 1 == HDMI");
 158
 159unsigned vivid_debug;
 160module_param(vivid_debug, uint, 0644);
 161MODULE_PARM_DESC(vivid_debug, " activates debug info");
 162
 163static bool no_error_inj;
 164module_param(no_error_inj, bool, 0444);
 165MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
 166
 167static unsigned int allocators[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0 };
 168module_param_array(allocators, uint, NULL, 0444);
 169MODULE_PARM_DESC(allocators, " memory allocator selection, default is 0.\n"
 170			     "\t\t    0 == vmalloc\n"
 171			     "\t\t    1 == dma-contig");
 172
 173static unsigned int cache_hints[VIVID_MAX_DEVS] = {
 174	[0 ... (VIVID_MAX_DEVS - 1)] = 0
 175};
 176module_param_array(cache_hints, uint, NULL, 0444);
 177MODULE_PARM_DESC(cache_hints, " user-space cache hints, default is 0.\n"
 178			     "\t\t    0 == forbid\n"
 179			     "\t\t    1 == allow");
 180
 181static unsigned int supports_requests[VIVID_MAX_DEVS] = {
 182	[0 ... (VIVID_MAX_DEVS - 1)] = 1
 183};
 184module_param_array(supports_requests, uint, NULL, 0444);
 185MODULE_PARM_DESC(supports_requests, " support for requests, default is 1.\n"
 186			     "\t\t    0 == no support\n"
 187			     "\t\t    1 == supports requests\n"
 188			     "\t\t    2 == requires requests");
 189
 190struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
 191
 192DEFINE_SPINLOCK(hdmi_output_skip_mask_lock);
 193struct workqueue_struct *update_hdmi_ctrls_workqueue;
 194u64 hdmi_to_output_menu_skip_mask;
 195u64 hdmi_input_update_outputs_mask;
 196
 197struct vivid_dev *vivid_ctrl_hdmi_to_output_instance[MAX_MENU_ITEMS];
 198unsigned int vivid_ctrl_hdmi_to_output_index[MAX_MENU_ITEMS];
 199
 200char *vivid_ctrl_hdmi_to_output_strings[MAX_MENU_ITEMS + 1] = {
 201	"Test Pattern Generator",
 202	"None"
 203};
 204
 205DEFINE_SPINLOCK(svid_output_skip_mask_lock);
 206struct workqueue_struct *update_svid_ctrls_workqueue;
 207u64 svid_to_output_menu_skip_mask;
 208
 209struct vivid_dev *vivid_ctrl_svid_to_output_instance[MAX_MENU_ITEMS];
 210unsigned int vivid_ctrl_svid_to_output_index[MAX_MENU_ITEMS];
 211
 212char *vivid_ctrl_svid_to_output_strings[MAX_MENU_ITEMS + 1] = {
 213	"Test Pattern Generator",
 214	"None"
 215};
 216
 217const struct v4l2_rect vivid_min_rect = {
 218	0, 0, MIN_WIDTH, MIN_HEIGHT
 219};
 220
 221const struct v4l2_rect vivid_max_rect = {
 222	0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
 223};
 224
 225static const u8 vivid_hdmi_edid[256] = {
 226	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
 227	0x31, 0xd8, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00,
 228	0x22, 0x1a, 0x01, 0x03, 0x80, 0x60, 0x36, 0x78,
 229	0x0f, 0xee, 0x91, 0xa3, 0x54, 0x4c, 0x99, 0x26,
 230	0x0f, 0x50, 0x54, 0x2f, 0xcf, 0x00, 0x31, 0x59,
 231	0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
 232	0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x08, 0xe8,
 233	0x00, 0x30, 0xf2, 0x70, 0x5a, 0x80, 0xb0, 0x58,
 234	0x8a, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00, 0x1e,
 235	0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
 236	0x87, 0x3c, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
 237	0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00, 0x76,
 238	0x69, 0x76, 0x69, 0x64, 0x0a, 0x20, 0x20, 0x20,
 239	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x10,
 240	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 241	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x7b,
 242
 243	0x02, 0x03, 0x3f, 0xf1, 0x51, 0x61, 0x60, 0x5f,
 244	0x5e, 0x5d, 0x10, 0x1f, 0x04, 0x13, 0x22, 0x21,
 245	0x20, 0x05, 0x14, 0x02, 0x11, 0x01, 0x23, 0x09,
 246	0x07, 0x07, 0x83, 0x01, 0x00, 0x00, 0x6d, 0x03,
 247	0x0c, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x21, 0x00,
 248	0x60, 0x01, 0x02, 0x03, 0x67, 0xd8, 0x5d, 0xc4,
 249	0x01, 0x78, 0x00, 0x00, 0xe2, 0x00, 0xca, 0xe3,
 250	0x05, 0x00, 0x00, 0xe3, 0x06, 0x01, 0x00, 0x4d,
 251	0xd0, 0x00, 0xa0, 0xf0, 0x70, 0x3e, 0x80, 0x30,
 252	0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32, 0x00, 0x00,
 253	0x1e, 0x1a, 0x36, 0x80, 0xa0, 0x70, 0x38, 0x1f,
 254	0x40, 0x30, 0x20, 0x35, 0x00, 0xc0, 0x1c, 0x32,
 255	0x00, 0x00, 0x1a, 0x1a, 0x1d, 0x00, 0x80, 0x51,
 256	0xd0, 0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0xc0,
 257	0x1c, 0x32, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00,
 258	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x92,
 259};
 260
 261static int vidioc_querycap(struct file *file, void  *priv,
 262					struct v4l2_capability *cap)
 263{
 264	struct vivid_dev *dev = video_drvdata(file);
 265
 266	strscpy(cap->driver, "vivid", sizeof(cap->driver));
 267	strscpy(cap->card, "vivid", sizeof(cap->card));
 268	snprintf(cap->bus_info, sizeof(cap->bus_info),
 269		 "platform:%s-%03d", VIVID_MODULE_NAME, dev->inst);
 270
 271	cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
 272		dev->vbi_cap_caps | dev->vbi_out_caps |
 273		dev->radio_rx_caps | dev->radio_tx_caps |
 274		dev->sdr_cap_caps | dev->meta_cap_caps |
 275		dev->meta_out_caps | dev->touch_cap_caps |
 276		V4L2_CAP_DEVICE_CAPS;
 277	return 0;
 278}
 279
 280static int vidioc_s_hw_freq_seek(struct file *file, void *priv, const struct v4l2_hw_freq_seek *a)
 281{
 282	struct video_device *vdev = video_devdata(file);
 283
 284	if (vdev->vfl_type == VFL_TYPE_RADIO)
 285		return vivid_radio_rx_s_hw_freq_seek(file, priv, a);
 286	return -ENOTTY;
 287}
 288
 289static int vidioc_enum_freq_bands(struct file *file, void *priv, struct v4l2_frequency_band *band)
 290{
 291	struct video_device *vdev = video_devdata(file);
 292
 293	if (vdev->vfl_type == VFL_TYPE_RADIO)
 294		return vivid_radio_rx_enum_freq_bands(file, priv, band);
 295	if (vdev->vfl_type == VFL_TYPE_SDR)
 296		return vivid_sdr_enum_freq_bands(file, priv, band);
 297	return -ENOTTY;
 298}
 299
 300static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *vt)
 301{
 302	struct video_device *vdev = video_devdata(file);
 303
 304	if (vdev->vfl_type == VFL_TYPE_RADIO)
 305		return vivid_radio_rx_g_tuner(file, priv, vt);
 306	if (vdev->vfl_type == VFL_TYPE_SDR)
 307		return vivid_sdr_g_tuner(file, priv, vt);
 308	return vivid_video_g_tuner(file, priv, vt);
 309}
 310
 311static int vidioc_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *vt)
 312{
 313	struct video_device *vdev = video_devdata(file);
 314
 315	if (vdev->vfl_type == VFL_TYPE_RADIO)
 316		return vivid_radio_rx_s_tuner(file, priv, vt);
 317	if (vdev->vfl_type == VFL_TYPE_SDR)
 318		return vivid_sdr_s_tuner(file, priv, vt);
 319	return vivid_video_s_tuner(file, priv, vt);
 320}
 321
 322static int vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *vf)
 323{
 324	struct vivid_dev *dev = video_drvdata(file);
 325	struct video_device *vdev = video_devdata(file);
 326
 327	if (vdev->vfl_type == VFL_TYPE_RADIO)
 328		return vivid_radio_g_frequency(file,
 329			vdev->vfl_dir == VFL_DIR_RX ?
 330			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
 331	if (vdev->vfl_type == VFL_TYPE_SDR)
 332		return vivid_sdr_g_frequency(file, priv, vf);
 333	return vivid_video_g_frequency(file, priv, vf);
 334}
 335
 336static int vidioc_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *vf)
 337{
 338	struct vivid_dev *dev = video_drvdata(file);
 339	struct video_device *vdev = video_devdata(file);
 340
 341	if (vdev->vfl_type == VFL_TYPE_RADIO)
 342		return vivid_radio_s_frequency(file,
 343			vdev->vfl_dir == VFL_DIR_RX ?
 344			&dev->radio_rx_freq : &dev->radio_tx_freq, vf);
 345	if (vdev->vfl_type == VFL_TYPE_SDR)
 346		return vivid_sdr_s_frequency(file, priv, vf);
 347	return vivid_video_s_frequency(file, priv, vf);
 348}
 349
 350static int vidioc_overlay(struct file *file, void *priv, unsigned i)
 351{
 352	struct video_device *vdev = video_devdata(file);
 353
 354	if (vdev->vfl_dir == VFL_DIR_RX)
 355		return -ENOTTY;
 356	return vivid_vid_out_overlay(file, priv, i);
 357}
 358
 359static int vidioc_g_fbuf(struct file *file, void *priv, struct v4l2_framebuffer *a)
 360{
 361	struct video_device *vdev = video_devdata(file);
 362
 363	if (vdev->vfl_dir == VFL_DIR_RX)
 364		return -ENOTTY;
 365	return vivid_vid_out_g_fbuf(file, priv, a);
 366}
 367
 368static int vidioc_s_fbuf(struct file *file, void *priv, const struct v4l2_framebuffer *a)
 369{
 370	struct video_device *vdev = video_devdata(file);
 371
 372	if (vdev->vfl_dir == VFL_DIR_RX)
 373		return -ENOTTY;
 374	return vivid_vid_out_s_fbuf(file, priv, a);
 375}
 376
 377static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
 378{
 379	struct video_device *vdev = video_devdata(file);
 380
 381	if (vdev->vfl_dir == VFL_DIR_RX)
 382		return vivid_vid_cap_s_std(file, priv, id);
 383	return vivid_vid_out_s_std(file, priv, id);
 384}
 385
 386static int vidioc_s_dv_timings(struct file *file, void *priv, struct v4l2_dv_timings *timings)
 387{
 388	struct video_device *vdev = video_devdata(file);
 389
 390	if (vdev->vfl_dir == VFL_DIR_RX)
 391		return vivid_vid_cap_s_dv_timings(file, priv, timings);
 392	return vivid_vid_out_s_dv_timings(file, priv, timings);
 393}
 394
 395static int vidioc_g_pixelaspect(struct file *file, void *priv,
 396				int type, struct v4l2_fract *f)
 397{
 398	struct video_device *vdev = video_devdata(file);
 399
 400	if (vdev->vfl_dir == VFL_DIR_RX)
 401		return vivid_vid_cap_g_pixelaspect(file, priv, type, f);
 402	return vivid_vid_out_g_pixelaspect(file, priv, type, f);
 403}
 404
 405static int vidioc_g_selection(struct file *file, void *priv,
 406			      struct v4l2_selection *sel)
 407{
 408	struct video_device *vdev = video_devdata(file);
 409
 410	if (vdev->vfl_dir == VFL_DIR_RX)
 411		return vivid_vid_cap_g_selection(file, priv, sel);
 412	return vivid_vid_out_g_selection(file, priv, sel);
 413}
 414
 415static int vidioc_s_selection(struct file *file, void *priv,
 416			      struct v4l2_selection *sel)
 417{
 418	struct video_device *vdev = video_devdata(file);
 419
 420	if (vdev->vfl_dir == VFL_DIR_RX)
 421		return vivid_vid_cap_s_selection(file, priv, sel);
 422	return vivid_vid_out_s_selection(file, priv, sel);
 423}
 424
 425static int vidioc_g_parm(struct file *file, void *priv,
 426			  struct v4l2_streamparm *parm)
 427{
 428	struct video_device *vdev = video_devdata(file);
 429
 430	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 431		return vivid_g_parm_tch(file, priv, parm);
 432	if (vdev->vfl_dir == VFL_DIR_RX)
 433		return vivid_vid_cap_g_parm(file, priv, parm);
 434	return vivid_vid_out_g_parm(file, priv, parm);
 435}
 436
 437static int vidioc_s_parm(struct file *file, void *priv,
 438			  struct v4l2_streamparm *parm)
 439{
 440	struct video_device *vdev = video_devdata(file);
 441
 442	if (vdev->vfl_dir == VFL_DIR_RX)
 443		return vivid_vid_cap_s_parm(file, priv, parm);
 444	return -ENOTTY;
 445}
 446
 447static int vidioc_log_status(struct file *file, void *priv)
 448{
 449	struct vivid_dev *dev = video_drvdata(file);
 450	struct video_device *vdev = video_devdata(file);
 451
 452	v4l2_ctrl_log_status(file, priv);
 453	if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_VIDEO)
 454		tpg_log_status(&dev->tpg);
 455	return 0;
 456}
 457
 458static ssize_t vivid_radio_read(struct file *file, char __user *buf,
 459			 size_t size, loff_t *offset)
 460{
 461	struct video_device *vdev = video_devdata(file);
 462
 463	if (vdev->vfl_dir == VFL_DIR_TX)
 464		return -EINVAL;
 465	return vivid_radio_rx_read(file, buf, size, offset);
 466}
 467
 468static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
 469			  size_t size, loff_t *offset)
 470{
 471	struct video_device *vdev = video_devdata(file);
 472
 473	if (vdev->vfl_dir == VFL_DIR_RX)
 474		return -EINVAL;
 475	return vivid_radio_tx_write(file, buf, size, offset);
 476}
 477
 478static __poll_t vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
 479{
 480	struct video_device *vdev = video_devdata(file);
 481
 482	if (vdev->vfl_dir == VFL_DIR_RX)
 483		return vivid_radio_rx_poll(file, wait);
 484	return vivid_radio_tx_poll(file, wait);
 485}
 486
 487static int vivid_enum_input(struct file *file, void *priv,
 488			    struct v4l2_input *inp)
 489{
 490	struct video_device *vdev = video_devdata(file);
 491
 492	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 493		return vivid_enum_input_tch(file, priv, inp);
 494	return vidioc_enum_input(file, priv, inp);
 495}
 496
 497static int vivid_g_input(struct file *file, void *priv, unsigned int *i)
 498{
 499	struct video_device *vdev = video_devdata(file);
 500
 501	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 502		return vivid_g_input_tch(file, priv, i);
 503	return vidioc_g_input(file, priv, i);
 504}
 505
 506static int vivid_s_input(struct file *file, void *priv, unsigned int i)
 507{
 508	struct video_device *vdev = video_devdata(file);
 509
 510	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 511		return vivid_s_input_tch(file, priv, i);
 512	return vidioc_s_input(file, priv, i);
 513}
 514
 515static int vivid_enum_fmt_cap(struct file *file, void  *priv,
 516			      struct v4l2_fmtdesc *f)
 517{
 518	struct video_device *vdev = video_devdata(file);
 519
 520	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 521		return vivid_enum_fmt_tch(file, priv, f);
 522	return vivid_enum_fmt_vid(file, priv, f);
 523}
 524
 525static int vivid_g_fmt_cap(struct file *file, void *priv,
 526			   struct v4l2_format *f)
 527{
 528	struct video_device *vdev = video_devdata(file);
 529
 530	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 531		return vivid_g_fmt_tch(file, priv, f);
 532	return vidioc_g_fmt_vid_cap(file, priv, f);
 533}
 534
 535static int vivid_try_fmt_cap(struct file *file, void *priv,
 536			     struct v4l2_format *f)
 537{
 538	struct video_device *vdev = video_devdata(file);
 539
 540	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 541		return vivid_g_fmt_tch(file, priv, f);
 542	return vidioc_try_fmt_vid_cap(file, priv, f);
 543}
 544
 545static int vivid_s_fmt_cap(struct file *file, void *priv,
 546			   struct v4l2_format *f)
 547{
 548	struct video_device *vdev = video_devdata(file);
 549
 550	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 551		return vivid_g_fmt_tch(file, priv, f);
 552	return vidioc_s_fmt_vid_cap(file, priv, f);
 553}
 554
 555static int vivid_g_fmt_cap_mplane(struct file *file, void *priv,
 556				  struct v4l2_format *f)
 557{
 558	struct video_device *vdev = video_devdata(file);
 559
 560	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 561		return vivid_g_fmt_tch_mplane(file, priv, f);
 562	return vidioc_g_fmt_vid_cap_mplane(file, priv, f);
 563}
 564
 565static int vivid_try_fmt_cap_mplane(struct file *file, void *priv,
 566				    struct v4l2_format *f)
 567{
 568	struct video_device *vdev = video_devdata(file);
 569
 570	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 571		return vivid_g_fmt_tch_mplane(file, priv, f);
 572	return vidioc_try_fmt_vid_cap_mplane(file, priv, f);
 573}
 574
 575static int vivid_s_fmt_cap_mplane(struct file *file, void *priv,
 576				  struct v4l2_format *f)
 577{
 578	struct video_device *vdev = video_devdata(file);
 579
 580	if (vdev->vfl_type == VFL_TYPE_TOUCH)
 581		return vivid_g_fmt_tch_mplane(file, priv, f);
 582	return vidioc_s_fmt_vid_cap_mplane(file, priv, f);
 583}
 584
 585static bool vivid_is_in_use(bool valid, struct video_device *vdev)
 586{
 587	unsigned long flags;
 588	bool res;
 589
 590	if (!valid)
 591		return false;
 592	spin_lock_irqsave(&vdev->fh_lock, flags);
 593	res = !list_empty(&vdev->fh_list);
 594	spin_unlock_irqrestore(&vdev->fh_lock, flags);
 595	return res;
 596}
 597
 598static bool vivid_is_last_user(struct vivid_dev *dev)
 599{
 600	unsigned int uses =
 601		vivid_is_in_use(dev->has_vid_cap, &dev->vid_cap_dev) +
 602		vivid_is_in_use(dev->has_vid_out, &dev->vid_out_dev) +
 603		vivid_is_in_use(dev->has_vbi_cap, &dev->vbi_cap_dev) +
 604		vivid_is_in_use(dev->has_vbi_out, &dev->vbi_out_dev) +
 605		vivid_is_in_use(dev->has_radio_rx, &dev->radio_rx_dev) +
 606		vivid_is_in_use(dev->has_radio_tx, &dev->radio_tx_dev) +
 607		vivid_is_in_use(dev->has_sdr_cap, &dev->sdr_cap_dev) +
 608		vivid_is_in_use(dev->has_meta_cap, &dev->meta_cap_dev) +
 609		vivid_is_in_use(dev->has_meta_out, &dev->meta_out_dev) +
 610		vivid_is_in_use(dev->has_touch_cap, &dev->touch_cap_dev);
 611
 612	return uses == 1;
 613}
 614
 615static void vivid_reconnect(struct vivid_dev *dev)
 616{
 617	if (dev->has_vid_cap)
 618		set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
 619	if (dev->has_vid_out)
 620		set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
 621	if (dev->has_vbi_cap)
 622		set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
 623	if (dev->has_vbi_out)
 624		set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
 625	if (dev->has_radio_rx)
 626		set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
 627	if (dev->has_radio_tx)
 628		set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
 629	if (dev->has_sdr_cap)
 630		set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
 631	if (dev->has_meta_cap)
 632		set_bit(V4L2_FL_REGISTERED, &dev->meta_cap_dev.flags);
 633	if (dev->has_meta_out)
 634		set_bit(V4L2_FL_REGISTERED, &dev->meta_out_dev.flags);
 635	if (dev->has_touch_cap)
 636		set_bit(V4L2_FL_REGISTERED, &dev->touch_cap_dev.flags);
 637	dev->disconnect_error = false;
 638}
 639
 640static int vivid_fop_release(struct file *file)
 641{
 642	struct vivid_dev *dev = video_drvdata(file);
 643	struct video_device *vdev = video_devdata(file);
 644
 645	mutex_lock(&dev->mutex);
 646	if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
 647	    dev->disconnect_error && !video_is_registered(vdev) &&
 648	    vivid_is_last_user(dev)) {
 649		/*
 650		 * I am the last user of this driver, and a disconnect
 651		 * was forced (since this video_device is unregistered),
 652		 * so re-register all video_device's again.
 653		 */
 654		v4l2_info(&dev->v4l2_dev, "reconnect\n");
 655		vivid_reconnect(dev);
 656	}
 657	if (file_to_v4l2_fh(file) == dev->radio_rx_rds_owner) {
 658		dev->radio_rx_rds_last_block = 0;
 659		dev->radio_rx_rds_owner = NULL;
 660	}
 661	if (file_to_v4l2_fh(file) == dev->radio_tx_rds_owner) {
 662		dev->radio_tx_rds_last_block = 0;
 663		dev->radio_tx_rds_owner = NULL;
 664	}
 665	mutex_unlock(&dev->mutex);
 666	if (vdev->queue)
 667		return vb2_fop_release(file);
 668	return v4l2_fh_release(file);
 669}
 670
 671static const struct v4l2_file_operations vivid_fops = {
 672	.owner		= THIS_MODULE,
 673	.open           = v4l2_fh_open,
 674	.release        = vivid_fop_release,
 675	.read           = vb2_fop_read,
 676	.write          = vb2_fop_write,
 677	.poll		= vb2_fop_poll,
 678	.unlocked_ioctl = video_ioctl2,
 679	.mmap           = vb2_fop_mmap,
 680};
 681
 682static const struct v4l2_file_operations vivid_radio_fops = {
 683	.owner		= THIS_MODULE,
 684	.open           = v4l2_fh_open,
 685	.release        = vivid_fop_release,
 686	.read           = vivid_radio_read,
 687	.write          = vivid_radio_write,
 688	.poll		= vivid_radio_poll,
 689	.unlocked_ioctl = video_ioctl2,
 690};
 691
 692static int vidioc_reqbufs(struct file *file, void *priv,
 693			  struct v4l2_requestbuffers *p)
 694{
 695	struct video_device *vdev = video_devdata(file);
 696	int r;
 697
 698	/*
 699	 * Sliced and raw VBI capture share the same queue so we must
 700	 * change the type.
 701	 */
 702	if (p->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
 703	    p->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
 704		r = vb2_queue_change_type(vdev->queue, p->type);
 705		if (r)
 706			return r;
 707	}
 708
 709	return vb2_ioctl_reqbufs(file, priv, p);
 710}
 711
 712static int vidioc_create_bufs(struct file *file, void *priv,
 713			      struct v4l2_create_buffers *p)
 714{
 715	struct video_device *vdev = video_devdata(file);
 716	int r;
 717
 718	/*
 719	 * Sliced and raw VBI capture share the same queue so we must
 720	 * change the type.
 721	 */
 722	if (p->format.type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE ||
 723	    p->format.type == V4L2_BUF_TYPE_VBI_CAPTURE) {
 724		r = vb2_queue_change_type(vdev->queue, p->format.type);
 725		if (r)
 726			return r;
 727	}
 728
 729	return vb2_ioctl_create_bufs(file, priv, p);
 730}
 731
 732static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
 733	.vidioc_querycap		= vidioc_querycap,
 734
 735	.vidioc_enum_fmt_vid_cap	= vivid_enum_fmt_cap,
 736	.vidioc_g_fmt_vid_cap		= vivid_g_fmt_cap,
 737	.vidioc_try_fmt_vid_cap		= vivid_try_fmt_cap,
 738	.vidioc_s_fmt_vid_cap		= vivid_s_fmt_cap,
 739	.vidioc_g_fmt_vid_cap_mplane	= vivid_g_fmt_cap_mplane,
 740	.vidioc_try_fmt_vid_cap_mplane	= vivid_try_fmt_cap_mplane,
 741	.vidioc_s_fmt_vid_cap_mplane	= vivid_s_fmt_cap_mplane,
 742
 743	.vidioc_enum_fmt_vid_out	= vivid_enum_fmt_vid,
 744	.vidioc_g_fmt_vid_out		= vidioc_g_fmt_vid_out,
 745	.vidioc_try_fmt_vid_out		= vidioc_try_fmt_vid_out,
 746	.vidioc_s_fmt_vid_out		= vidioc_s_fmt_vid_out,
 747	.vidioc_g_fmt_vid_out_mplane	= vidioc_g_fmt_vid_out_mplane,
 748	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out_mplane,
 749	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out_mplane,
 750
 751	.vidioc_g_selection		= vidioc_g_selection,
 752	.vidioc_s_selection		= vidioc_s_selection,
 753	.vidioc_g_pixelaspect		= vidioc_g_pixelaspect,
 754
 755	.vidioc_g_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
 756	.vidioc_try_fmt_vbi_cap		= vidioc_g_fmt_vbi_cap,
 757	.vidioc_s_fmt_vbi_cap		= vidioc_s_fmt_vbi_cap,
 758
 759	.vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
 760	.vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
 761	.vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
 762	.vidioc_g_sliced_vbi_cap	= vidioc_g_sliced_vbi_cap,
 763
 764	.vidioc_g_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
 765	.vidioc_try_fmt_vbi_out		= vidioc_g_fmt_vbi_out,
 766	.vidioc_s_fmt_vbi_out		= vidioc_s_fmt_vbi_out,
 767
 768	.vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
 769	.vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
 770	.vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
 771
 772	.vidioc_enum_fmt_sdr_cap	= vidioc_enum_fmt_sdr_cap,
 773	.vidioc_g_fmt_sdr_cap		= vidioc_g_fmt_sdr_cap,
 774	.vidioc_try_fmt_sdr_cap		= vidioc_try_fmt_sdr_cap,
 775	.vidioc_s_fmt_sdr_cap		= vidioc_s_fmt_sdr_cap,
 776
 777	.vidioc_overlay			= vidioc_overlay,
 778	.vidioc_enum_framesizes		= vidioc_enum_framesizes,
 779	.vidioc_enum_frameintervals	= vidioc_enum_frameintervals,
 780	.vidioc_g_parm			= vidioc_g_parm,
 781	.vidioc_s_parm			= vidioc_s_parm,
 782
 783	.vidioc_g_fmt_vid_out_overlay	= vidioc_g_fmt_vid_out_overlay,
 784	.vidioc_try_fmt_vid_out_overlay	= vidioc_try_fmt_vid_out_overlay,
 785	.vidioc_s_fmt_vid_out_overlay	= vidioc_s_fmt_vid_out_overlay,
 786	.vidioc_g_fbuf			= vidioc_g_fbuf,
 787	.vidioc_s_fbuf			= vidioc_s_fbuf,
 788
 789	.vidioc_reqbufs			= vidioc_reqbufs,
 790	.vidioc_create_bufs		= vidioc_create_bufs,
 791	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
 792	.vidioc_querybuf		= vb2_ioctl_querybuf,
 793	.vidioc_qbuf			= vb2_ioctl_qbuf,
 794	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
 795	.vidioc_expbuf			= vb2_ioctl_expbuf,
 796	.vidioc_streamon		= vb2_ioctl_streamon,
 797	.vidioc_streamoff		= vb2_ioctl_streamoff,
 798	.vidioc_remove_bufs		= vb2_ioctl_remove_bufs,
 799
 800	.vidioc_enum_input		= vivid_enum_input,
 801	.vidioc_g_input			= vivid_g_input,
 802	.vidioc_s_input			= vivid_s_input,
 803	.vidioc_s_audio			= vidioc_s_audio,
 804	.vidioc_g_audio			= vidioc_g_audio,
 805	.vidioc_enumaudio		= vidioc_enumaudio,
 806	.vidioc_s_frequency		= vidioc_s_frequency,
 807	.vidioc_g_frequency		= vidioc_g_frequency,
 808	.vidioc_s_tuner			= vidioc_s_tuner,
 809	.vidioc_g_tuner			= vidioc_g_tuner,
 810	.vidioc_s_modulator		= vidioc_s_modulator,
 811	.vidioc_g_modulator		= vidioc_g_modulator,
 812	.vidioc_s_hw_freq_seek		= vidioc_s_hw_freq_seek,
 813	.vidioc_enum_freq_bands		= vidioc_enum_freq_bands,
 814
 815	.vidioc_enum_output		= vidioc_enum_output,
 816	.vidioc_g_output		= vidioc_g_output,
 817	.vidioc_s_output		= vidioc_s_output,
 818	.vidioc_s_audout		= vidioc_s_audout,
 819	.vidioc_g_audout		= vidioc_g_audout,
 820	.vidioc_enumaudout		= vidioc_enumaudout,
 821
 822	.vidioc_querystd		= vidioc_querystd,
 823	.vidioc_g_std			= vidioc_g_std,
 824	.vidioc_s_std			= vidioc_s_std,
 825	.vidioc_s_dv_timings		= vidioc_s_dv_timings,
 826	.vidioc_g_dv_timings		= vidioc_g_dv_timings,
 827	.vidioc_query_dv_timings	= vidioc_query_dv_timings,
 828	.vidioc_enum_dv_timings		= vidioc_enum_dv_timings,
 829	.vidioc_dv_timings_cap		= vidioc_dv_timings_cap,
 830	.vidioc_g_edid			= vidioc_g_edid,
 831	.vidioc_s_edid			= vidioc_s_edid,
 832
 833	.vidioc_log_status		= vidioc_log_status,
 834	.vidioc_subscribe_event		= vidioc_subscribe_event,
 835	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
 836
 837	.vidioc_enum_fmt_meta_cap	= vidioc_enum_fmt_meta_cap,
 838	.vidioc_g_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
 839	.vidioc_s_fmt_meta_cap		= vidioc_g_fmt_meta_cap,
 840	.vidioc_try_fmt_meta_cap	= vidioc_g_fmt_meta_cap,
 841
 842	.vidioc_enum_fmt_meta_out       = vidioc_enum_fmt_meta_out,
 843	.vidioc_g_fmt_meta_out          = vidioc_g_fmt_meta_out,
 844	.vidioc_s_fmt_meta_out          = vidioc_g_fmt_meta_out,
 845	.vidioc_try_fmt_meta_out        = vidioc_g_fmt_meta_out,
 846};
 847
 848/* -----------------------------------------------------------------
 849	Initialization and module stuff
 850   ------------------------------------------------------------------*/
 851
 852static void vivid_dev_release(struct v4l2_device *v4l2_dev)
 853{
 854	struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
 855
 856	cancel_work_sync(&dev->update_hdmi_ctrl_work);
 857	vivid_free_controls(dev);
 858	v4l2_device_unregister(&dev->v4l2_dev);
 859#ifdef CONFIG_MEDIA_CONTROLLER
 860	media_device_cleanup(&dev->mdev);
 861#endif
 862	vfree(dev->scaled_line);
 863	vfree(dev->blended_line);
 864	vfree(dev->edid);
 865	tpg_free(&dev->tpg);
 866	kfree(dev->query_dv_timings_qmenu);
 867	kfree(dev->query_dv_timings_qmenu_strings);
 868	kfree(dev);
 869}
 870
 871#ifdef CONFIG_MEDIA_CONTROLLER
 872static int vivid_req_validate(struct media_request *req)
 873{
 874	struct vivid_dev *dev = container_of(req->mdev, struct vivid_dev, mdev);
 875
 876	if (dev->req_validate_error) {
 877		dev->req_validate_error = false;
 878		return -EINVAL;
 879	}
 880	return vb2_request_validate(req);
 881}
 882
 883static const struct media_device_ops vivid_media_ops = {
 884	.req_validate = vivid_req_validate,
 885	.req_queue = vb2_request_queue,
 886};
 887#endif
 888
 889static int vivid_create_queue(struct vivid_dev *dev,
 890			      struct vb2_queue *q,
 891			      u32 buf_type,
 892			      unsigned int min_reqbufs_allocation,
 893			      const struct vb2_ops *ops)
 894{
 895	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->multiplanar)
 896		buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 897	else if (buf_type == V4L2_BUF_TYPE_VIDEO_OUTPUT && dev->multiplanar)
 898		buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
 899	else if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE && !dev->has_raw_vbi_cap)
 900		buf_type = V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
 901	else if (buf_type == V4L2_BUF_TYPE_VBI_OUTPUT && !dev->has_raw_vbi_out)
 902		buf_type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
 903
 904	q->type = buf_type;
 905	q->io_modes = VB2_MMAP | VB2_DMABUF;
 906	q->io_modes |= V4L2_TYPE_IS_OUTPUT(buf_type) ?  VB2_WRITE : VB2_READ;
 907
 908	/*
 909	 * The maximum number of buffers is 32768 if PAGE_SHIFT == 12,
 910	 * see also MAX_BUFFER_INDEX in videobuf2-core.c. It will be less if
 911	 * PAGE_SHIFT > 12, but then max_num_buffers will be clamped by
 912	 * videobuf2-core.c to MAX_BUFFER_INDEX.
 913	 */
 914	if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
 915		q->max_num_buffers = MAX_VID_CAP_BUFFERS;
 916	if (buf_type == V4L2_BUF_TYPE_SDR_CAPTURE)
 917		q->max_num_buffers = 1024;
 918	if (buf_type == V4L2_BUF_TYPE_VBI_CAPTURE)
 919		q->max_num_buffers = 32768;
 920
 921	if (allocators[dev->inst] != 1)
 922		q->io_modes |= VB2_USERPTR;
 923	q->drv_priv = dev;
 924	q->buf_struct_size = sizeof(struct vivid_buffer);
 925	q->ops = ops;
 926	q->mem_ops = allocators[dev->inst] == 1 ? &vb2_dma_contig_memops :
 927						  &vb2_vmalloc_memops;
 928	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
 929	q->min_reqbufs_allocation = min_reqbufs_allocation;
 930	q->lock = &dev->mutex;
 931	q->dev = dev->v4l2_dev.dev;
 932	q->supports_requests = supports_requests[dev->inst];
 933	q->requires_requests = supports_requests[dev->inst] >= 2;
 934	q->allow_cache_hints = (cache_hints[dev->inst] == 1);
 935
 936	return vb2_queue_init(q);
 937}
 938
 939static int vivid_detect_feature_set(struct vivid_dev *dev, int inst,
 940				    unsigned node_type,
 941				    bool *has_tuner,
 942				    bool *has_modulator,
 943				    int *ccs_cap,
 944				    int *ccs_out,
 945				    unsigned in_type_counter[4],
 946				    unsigned out_type_counter[4])
 947{
 948	int i;
 949
 950	/* do we use single- or multi-planar? */
 951	dev->multiplanar = multiplanar[inst] > 1;
 952	v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
 953			dev->multiplanar ? "multi" : "single ");
 954
 955	/* how many inputs do we have and of what type? */
 956	dev->num_inputs = num_inputs[inst];
 957	if (node_type & 0x20007) {
 958		if (dev->num_inputs < 1)
 959			dev->num_inputs = 1;
 960	} else {
 961		dev->num_inputs = 0;
 962	}
 963	if (dev->num_inputs >= MAX_INPUTS)
 964		dev->num_inputs = MAX_INPUTS;
 965	for (i = 0; i < dev->num_inputs; i++) {
 966		dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
 967		dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
 968	}
 969	dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
 970	if (in_type_counter[HDMI] == 16) {
 971		/* The CEC physical address only allows for max 15 inputs */
 972		in_type_counter[HDMI]--;
 973		dev->num_inputs--;
 974	}
 975	dev->num_hdmi_inputs = in_type_counter[HDMI];
 976	dev->num_svid_inputs = in_type_counter[SVID];
 977
 978	/* how many outputs do we have and of what type? */
 979	dev->num_outputs = num_outputs[inst];
 980	if (node_type & 0x40300) {
 981		if (dev->num_outputs < 1)
 982			dev->num_outputs = 1;
 983	} else {
 984		dev->num_outputs = 0;
 985	}
 986	if (dev->num_outputs >= MAX_OUTPUTS)
 987		dev->num_outputs = MAX_OUTPUTS;
 988	for (i = 0; i < dev->num_outputs; i++) {
 989		dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
 990		dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
 991	}
 992	dev->has_audio_outputs = out_type_counter[SVID];
 993	if (out_type_counter[HDMI] == 16) {
 994		/*
 995		 * The CEC physical address only allows for max 15 inputs,
 996		 * so outputs are also limited to 15 to allow for easy
 997		 * CEC output to input mapping.
 998		 */
 999		out_type_counter[HDMI]--;
1000		dev->num_outputs--;
1001	}
1002	dev->num_hdmi_outputs = out_type_counter[HDMI];
1003
1004	/* do we create a video capture device? */
1005	dev->has_vid_cap = node_type & 0x0001;
1006
1007	/* do we create a vbi capture device? */
1008	if (in_type_counter[TV] || in_type_counter[SVID]) {
1009		dev->has_raw_vbi_cap = node_type & 0x0004;
1010		dev->has_sliced_vbi_cap = node_type & 0x0008;
1011		dev->has_vbi_cap = dev->has_raw_vbi_cap | dev->has_sliced_vbi_cap;
1012	}
1013
1014	/* do we create a meta capture device */
1015	dev->has_meta_cap = node_type & 0x20000;
1016
1017	/* sanity checks */
1018	if ((in_type_counter[WEBCAM] || in_type_counter[HDMI]) &&
1019	    !dev->has_vid_cap && !dev->has_meta_cap) {
1020		v4l2_warn(&dev->v4l2_dev,
1021			  "Webcam or HDMI input without video or metadata nodes\n");
1022		return -EINVAL;
1023	}
1024	if ((in_type_counter[TV] || in_type_counter[SVID]) &&
1025	    !dev->has_vid_cap && !dev->has_vbi_cap && !dev->has_meta_cap) {
1026		v4l2_warn(&dev->v4l2_dev,
1027			  "TV or S-Video input without video, VBI or metadata nodes\n");
1028		return -EINVAL;
1029	}
1030
1031	/* do we create a video output device? */
1032	dev->has_vid_out = node_type & 0x0100;
1033
1034	/* do we create a vbi output device? */
1035	if (out_type_counter[SVID]) {
1036		dev->has_raw_vbi_out = node_type & 0x0400;
1037		dev->has_sliced_vbi_out = node_type & 0x0800;
1038		dev->has_vbi_out = dev->has_raw_vbi_out | dev->has_sliced_vbi_out;
1039	}
1040
1041	/* do we create a metadata output device */
1042	dev->has_meta_out = node_type & 0x40000;
1043
1044	/* sanity checks */
1045	if (out_type_counter[SVID] &&
1046	    !dev->has_vid_out && !dev->has_vbi_out && !dev->has_meta_out) {
1047		v4l2_warn(&dev->v4l2_dev,
1048			  "S-Video output without video, VBI or metadata nodes\n");
1049		return -EINVAL;
1050	}
1051	if (out_type_counter[HDMI] && !dev->has_vid_out && !dev->has_meta_out) {
1052		v4l2_warn(&dev->v4l2_dev,
1053			  "HDMI output without video or metadata nodes\n");
1054		return -EINVAL;
1055	}
1056
1057	/* do we create a radio receiver device? */
1058	dev->has_radio_rx = node_type & 0x0010;
1059
1060	/* do we create a radio transmitter device? */
1061	dev->has_radio_tx = node_type & 0x1000;
1062
1063	/* do we create a software defined radio capture device? */
1064	dev->has_sdr_cap = node_type & 0x0020;
1065
1066	/* do we have a TV tuner? */
1067	dev->has_tv_tuner = in_type_counter[TV];
1068
1069	/* do we have a tuner? */
1070	*has_tuner = ((dev->has_vid_cap || dev->has_vbi_cap) && in_type_counter[TV]) ||
1071		      dev->has_radio_rx || dev->has_sdr_cap;
1072
1073	/* do we have a modulator? */
1074	*has_modulator = dev->has_radio_tx;
1075
1076#ifdef CONFIG_VIDEO_VIVID_OSD
1077	if (dev->has_vid_cap)
1078		/* do we have a framebuffer for overlay testing? */
1079		dev->has_fb = node_type & 0x10000;
1080#endif
1081
1082	/* can we do crop/compose/scaling while capturing? */
1083	if (no_error_inj && *ccs_cap == -1)
1084		*ccs_cap = 7;
1085
1086	/* if ccs_cap == -1, then the user can select it using controls */
1087	if (*ccs_cap != -1) {
1088		dev->has_crop_cap = *ccs_cap & 1;
1089		dev->has_compose_cap = *ccs_cap & 2;
1090		dev->has_scaler_cap = *ccs_cap & 4;
1091		v4l2_info(&dev->v4l2_dev, "Capture Crop: %c Compose: %c Scaler: %c\n",
1092			dev->has_crop_cap ? 'Y' : 'N',
1093			dev->has_compose_cap ? 'Y' : 'N',
1094			dev->has_scaler_cap ? 'Y' : 'N');
1095	}
1096
1097	/* can we do crop/compose/scaling with video output? */
1098	if (no_error_inj && *ccs_out == -1)
1099		*ccs_out = 7;
1100
1101	/* if ccs_out == -1, then the user can select it using controls */
1102	if (*ccs_out != -1) {
1103		dev->has_crop_out = *ccs_out & 1;
1104		dev->has_compose_out = *ccs_out & 2;
1105		dev->has_scaler_out = *ccs_out & 4;
1106		v4l2_info(&dev->v4l2_dev, "Output Crop: %c Compose: %c Scaler: %c\n",
1107			dev->has_crop_out ? 'Y' : 'N',
1108			dev->has_compose_out ? 'Y' : 'N',
1109			dev->has_scaler_out ? 'Y' : 'N');
1110	}
1111
1112	/* do we create a touch capture device */
1113	dev->has_touch_cap = node_type & 0x80000;
1114
1115	return 0;
1116}
1117
1118static void vivid_set_capabilities(struct vivid_dev *dev)
1119{
1120	if (dev->has_vid_cap) {
1121		/* set up the capabilities of the video capture device */
1122		dev->vid_cap_caps = dev->multiplanar ?
1123			V4L2_CAP_VIDEO_CAPTURE_MPLANE :
1124			V4L2_CAP_VIDEO_CAPTURE;
1125		dev->vid_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1126		if (dev->has_audio_inputs)
1127			dev->vid_cap_caps |= V4L2_CAP_AUDIO;
1128		if (dev->has_tv_tuner)
1129			dev->vid_cap_caps |= V4L2_CAP_TUNER;
1130	}
1131	if (dev->has_vid_out) {
1132		/* set up the capabilities of the video output device */
1133		dev->vid_out_caps = dev->multiplanar ?
1134			V4L2_CAP_VIDEO_OUTPUT_MPLANE :
1135			V4L2_CAP_VIDEO_OUTPUT;
1136		if (dev->has_fb)
1137			dev->vid_out_caps |= V4L2_CAP_VIDEO_OUTPUT_OVERLAY;
1138		dev->vid_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1139		if (dev->has_audio_outputs)
1140			dev->vid_out_caps |= V4L2_CAP_AUDIO;
1141	}
1142	if (dev->has_vbi_cap) {
1143		/* set up the capabilities of the vbi capture device */
1144		dev->vbi_cap_caps = (dev->has_raw_vbi_cap ? V4L2_CAP_VBI_CAPTURE : 0) |
1145				    (dev->has_sliced_vbi_cap ? V4L2_CAP_SLICED_VBI_CAPTURE : 0);
1146		dev->vbi_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1147		if (dev->has_audio_inputs)
1148			dev->vbi_cap_caps |= V4L2_CAP_AUDIO;
1149		if (dev->has_tv_tuner)
1150			dev->vbi_cap_caps |= V4L2_CAP_TUNER;
1151	}
1152	if (dev->has_vbi_out) {
1153		/* set up the capabilities of the vbi output device */
1154		dev->vbi_out_caps = (dev->has_raw_vbi_out ? V4L2_CAP_VBI_OUTPUT : 0) |
1155				    (dev->has_sliced_vbi_out ? V4L2_CAP_SLICED_VBI_OUTPUT : 0);
1156		dev->vbi_out_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1157		if (dev->has_audio_outputs)
1158			dev->vbi_out_caps |= V4L2_CAP_AUDIO;
1159	}
1160	if (dev->has_sdr_cap) {
1161		/* set up the capabilities of the sdr capture device */
1162		dev->sdr_cap_caps = V4L2_CAP_SDR_CAPTURE | V4L2_CAP_TUNER;
1163		dev->sdr_cap_caps |= V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1164	}
1165	/* set up the capabilities of the radio receiver device */
1166	if (dev->has_radio_rx)
1167		dev->radio_rx_caps = V4L2_CAP_RADIO | V4L2_CAP_RDS_CAPTURE |
1168				     V4L2_CAP_HW_FREQ_SEEK | V4L2_CAP_TUNER |
1169				     V4L2_CAP_READWRITE;
1170	/* set up the capabilities of the radio transmitter device */
1171	if (dev->has_radio_tx)
1172		dev->radio_tx_caps = V4L2_CAP_RDS_OUTPUT | V4L2_CAP_MODULATOR |
1173				     V4L2_CAP_READWRITE;
1174
1175	/* set up the capabilities of meta capture device */
1176	if (dev->has_meta_cap) {
1177		dev->meta_cap_caps = V4L2_CAP_META_CAPTURE |
1178				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1179		if (dev->has_audio_inputs)
1180			dev->meta_cap_caps |= V4L2_CAP_AUDIO;
1181		if (dev->has_tv_tuner)
1182			dev->meta_cap_caps |= V4L2_CAP_TUNER;
1183	}
1184	/* set up the capabilities of meta output device */
1185	if (dev->has_meta_out) {
1186		dev->meta_out_caps = V4L2_CAP_META_OUTPUT |
1187				     V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
1188		if (dev->has_audio_outputs)
1189			dev->meta_out_caps |= V4L2_CAP_AUDIO;
1190	}
1191	/* set up the capabilities of the touch capture device */
1192	if (dev->has_touch_cap) {
1193		dev->touch_cap_caps = V4L2_CAP_TOUCH | V4L2_CAP_STREAMING |
1194				      V4L2_CAP_READWRITE;
1195		dev->touch_cap_caps |= dev->multiplanar ?
1196			V4L2_CAP_VIDEO_CAPTURE_MPLANE : V4L2_CAP_VIDEO_CAPTURE;
1197	}
1198}
1199
1200static void vivid_disable_unused_ioctls(struct vivid_dev *dev,
1201					bool has_tuner,
1202					bool has_modulator,
1203					unsigned in_type_counter[4],
1204					unsigned out_type_counter[4])
1205{
1206	/* disable invalid ioctls based on the feature set */
1207	if (!dev->has_audio_inputs) {
1208		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
1209		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
1210		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
1211		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
1212		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
1213		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
1214		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_AUDIO);
1215		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_AUDIO);
1216		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_ENUMAUDIO);
1217	}
1218	if (!dev->has_audio_outputs) {
1219		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
1220		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
1221		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
1222		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
1223		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
1224		v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
1225		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_AUDOUT);
1226		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_AUDOUT);
1227		v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_ENUMAUDOUT);
1228	}
1229	if (!in_type_counter[TV] && !in_type_counter[SVID]) {
1230		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
1231		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
1232		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
1233		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
1234	}
1235	if (!out_type_counter[SVID]) {
1236		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
1237		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
1238		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
1239	}
1240	if (!has_tuner && !has_modulator) {
1241		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
1242		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
1243		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
1244		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
1245		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_FREQUENCY);
1246		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_FREQUENCY);
1247	}
1248	if (!has_tuner) {
1249		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
1250		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
1251		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
1252		v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
1253		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_TUNER);
1254		v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_G_TUNER);
1255	}
1256	if (in_type_counter[HDMI] == 0) {
1257		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
1258		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
1259		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
1260		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
1261		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
1262		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
1263		v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
1264	}
1265	if (out_type_counter[HDMI] == 0) {
1266		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
1267		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
1268		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
1269		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
1270		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
1271	}
1272	if (!dev->has_fb) {
1273		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
1274		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
1275		v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
1276	}
1277	v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1278	v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1279	v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1280	v4l2_disable_ioctl(&dev->meta_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
1281	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
1282	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
1283	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
1284	v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1285	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
1286	v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
1287	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_S_FREQUENCY);
1288	v4l2_disable_ioctl(&dev->meta_out_dev, VIDIOC_G_FREQUENCY);
1289	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_S_PARM);
1290	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMESIZES);
1291	v4l2_disable_ioctl(&dev->touch_cap_dev, VIDIOC_ENUM_FRAMEINTERVALS);
1292}
1293
1294static int vivid_init_dv_timings(struct vivid_dev *dev)
1295{
1296	int i;
1297
1298	while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
1299		dev->query_dv_timings_size++;
1300
1301	/*
1302	 * Create a char pointer array that points to the names of all the
1303	 * preset timings
1304	 */
1305	dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size,
1306						    sizeof(char *), GFP_KERNEL);
1307	/*
1308	 * Create a string array containing the names of all the preset
1309	 * timings. Each name is max 31 chars long (+ terminating 0).
1310	 */
1311	dev->query_dv_timings_qmenu_strings =
1312		kmalloc_array(dev->query_dv_timings_size, 32, GFP_KERNEL);
1313
1314	if (!dev->query_dv_timings_qmenu ||
1315	    !dev->query_dv_timings_qmenu_strings)
1316		return -ENOMEM;
1317
1318	for (i = 0; i < dev->query_dv_timings_size; i++) {
1319		const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
1320		char *p = dev->query_dv_timings_qmenu_strings + i * 32;
1321		u32 htot, vtot;
1322
1323		dev->query_dv_timings_qmenu[i] = p;
1324
1325		htot = V4L2_DV_BT_FRAME_WIDTH(bt);
1326		vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
1327		snprintf(p, 32, "%ux%u%s%u",
1328			bt->width, bt->height, bt->interlaced ? "i" : "p",
1329			(u32)bt->pixelclock / (htot * vtot));
1330	}
1331
1332	return 0;
1333}
1334
1335static int vivid_create_queues(struct vivid_dev *dev)
1336{
1337	int ret;
1338
1339	/* start creating the vb2 queues */
1340	if (dev->has_vid_cap) {
1341		/* initialize vid_cap queue */
1342		ret = vivid_create_queue(dev, &dev->vb_vid_cap_q,
1343					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1344					 &vivid_vid_cap_qops);
1345		if (ret)
1346			return ret;
1347	}
1348
1349	if (dev->has_vid_out) {
1350		/* initialize vid_out queue */
1351		ret = vivid_create_queue(dev, &dev->vb_vid_out_q,
1352					 V4L2_BUF_TYPE_VIDEO_OUTPUT, 2,
1353					 &vivid_vid_out_qops);
1354		if (ret)
1355			return ret;
1356	}
1357
1358	if (dev->has_vbi_cap) {
1359		/* initialize vbi_cap queue */
1360		ret = vivid_create_queue(dev, &dev->vb_vbi_cap_q,
1361					 V4L2_BUF_TYPE_VBI_CAPTURE, 2,
1362					 &vivid_vbi_cap_qops);
1363		if (ret)
1364			return ret;
1365	}
1366
1367	if (dev->has_vbi_out) {
1368		/* initialize vbi_out queue */
1369		ret = vivid_create_queue(dev, &dev->vb_vbi_out_q,
1370					 V4L2_BUF_TYPE_VBI_OUTPUT, 2,
1371					 &vivid_vbi_out_qops);
1372		if (ret)
1373			return ret;
1374	}
1375
1376	if (dev->has_sdr_cap) {
1377		/* initialize sdr_cap queue */
1378		ret = vivid_create_queue(dev, &dev->vb_sdr_cap_q,
1379					 V4L2_BUF_TYPE_SDR_CAPTURE, 8,
1380					 &vivid_sdr_cap_qops);
1381		if (ret)
1382			return ret;
1383	}
1384
1385	if (dev->has_meta_cap) {
1386		/* initialize meta_cap queue */
1387		ret = vivid_create_queue(dev, &dev->vb_meta_cap_q,
1388					 V4L2_BUF_TYPE_META_CAPTURE, 2,
1389					 &vivid_meta_cap_qops);
1390		if (ret)
1391			return ret;
1392	}
1393
1394	if (dev->has_meta_out) {
1395		/* initialize meta_out queue */
1396		ret = vivid_create_queue(dev, &dev->vb_meta_out_q,
1397					 V4L2_BUF_TYPE_META_OUTPUT, 2,
1398					 &vivid_meta_out_qops);
1399		if (ret)
1400			return ret;
1401	}
1402
1403	if (dev->has_touch_cap) {
1404		/* initialize touch_cap queue */
1405		ret = vivid_create_queue(dev, &dev->vb_touch_cap_q,
1406					 V4L2_BUF_TYPE_VIDEO_CAPTURE, 2,
1407					 &vivid_touch_cap_qops);
1408		if (ret)
1409			return ret;
1410	}
1411
1412	if (dev->has_fb) {
1413		/* Create framebuffer for testing output overlay */
1414		ret = vivid_fb_init(dev);
1415		if (ret)
1416			return ret;
1417	}
1418	return 0;
1419}
1420
1421static int vivid_create_devnodes(struct platform_device *pdev,
1422				 struct vivid_dev *dev, int inst,
1423				 v4l2_std_id tvnorms_cap,
1424				 v4l2_std_id tvnorms_out,
1425				 unsigned in_type_counter[4],
1426				 unsigned out_type_counter[4])
1427{
1428	struct video_device *vfd;
1429	int ret;
1430
1431	if (dev->has_vid_cap) {
1432		vfd = &dev->vid_cap_dev;
1433		snprintf(vfd->name, sizeof(vfd->name),
1434			 "vivid-%03d-vid-cap", inst);
1435		vfd->fops = &vivid_fops;
1436		vfd->ioctl_ops = &vivid_ioctl_ops;
1437		vfd->device_caps = dev->vid_cap_caps;
1438		vfd->release = video_device_release_empty;
1439		vfd->v4l2_dev = &dev->v4l2_dev;
1440		vfd->queue = &dev->vb_vid_cap_q;
1441		vfd->tvnorms = tvnorms_cap;
1442
1443		/*
1444		 * Provide a mutex to v4l2 core. It will be used to protect
1445		 * all fops and v4l2 ioctls.
1446		 */
1447		vfd->lock = &dev->mutex;
1448		video_set_drvdata(vfd, dev);
1449
1450#ifdef CONFIG_MEDIA_CONTROLLER
1451		dev->vid_cap_pad.flags = MEDIA_PAD_FL_SINK;
1452		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_cap_pad);
1453		if (ret)
1454			return ret;
1455#endif
1456
1457#ifdef CONFIG_VIDEO_VIVID_CEC
1458		if (in_type_counter[HDMI]) {
1459			ret = cec_register_adapter(dev->cec_rx_adap, &pdev->dev);
1460			if (ret < 0) {
1461				cec_delete_adapter(dev->cec_rx_adap);
1462				dev->cec_rx_adap = NULL;
1463				return ret;
1464			}
1465			cec_s_phys_addr(dev->cec_rx_adap, 0, false);
1466			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI input\n",
1467				  dev_name(&dev->cec_rx_adap->devnode.dev));
1468		}
1469#endif
1470
1471		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_cap_nr[inst]);
1472		if (ret < 0)
1473			return ret;
1474		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1475					  video_device_node_name(vfd));
1476	}
1477
1478	if (dev->has_vid_out) {
1479#ifdef CONFIG_VIDEO_VIVID_CEC
1480		int i;
1481#endif
1482		vfd = &dev->vid_out_dev;
1483		snprintf(vfd->name, sizeof(vfd->name),
1484			 "vivid-%03d-vid-out", inst);
1485		vfd->vfl_dir = VFL_DIR_TX;
1486		vfd->fops = &vivid_fops;
1487		vfd->ioctl_ops = &vivid_ioctl_ops;
1488		vfd->device_caps = dev->vid_out_caps;
1489		vfd->release = video_device_release_empty;
1490		vfd->v4l2_dev = &dev->v4l2_dev;
1491		vfd->queue = &dev->vb_vid_out_q;
1492		vfd->tvnorms = tvnorms_out;
1493
1494		/*
1495		 * Provide a mutex to v4l2 core. It will be used to protect
1496		 * all fops and v4l2 ioctls.
1497		 */
1498		vfd->lock = &dev->mutex;
1499		video_set_drvdata(vfd, dev);
1500
1501#ifdef CONFIG_MEDIA_CONTROLLER
1502		dev->vid_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1503		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vid_out_pad);
1504		if (ret)
1505			return ret;
1506#endif
1507
1508#ifdef CONFIG_VIDEO_VIVID_CEC
1509		for (i = 0; i < dev->num_hdmi_outputs; i++) {
1510			ret = cec_register_adapter(dev->cec_tx_adap[i], &pdev->dev);
1511			if (ret < 0) {
1512				for (; i >= 0; i--) {
1513					cec_delete_adapter(dev->cec_tx_adap[i]);
1514					dev->cec_tx_adap[i] = NULL;
1515				}
1516				return ret;
1517			}
1518			v4l2_info(&dev->v4l2_dev, "CEC adapter %s registered for HDMI output %d\n",
1519				  dev_name(&dev->cec_tx_adap[i]->devnode.dev), i);
1520		}
1521#endif
1522
1523		ret = video_register_device(vfd, VFL_TYPE_VIDEO, vid_out_nr[inst]);
1524		if (ret < 0)
1525			return ret;
1526		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1527					  video_device_node_name(vfd));
1528	}
1529
1530	if (dev->has_vbi_cap) {
1531		vfd = &dev->vbi_cap_dev;
1532		snprintf(vfd->name, sizeof(vfd->name),
1533			 "vivid-%03d-vbi-cap", inst);
1534		vfd->fops = &vivid_fops;
1535		vfd->ioctl_ops = &vivid_ioctl_ops;
1536		vfd->device_caps = dev->vbi_cap_caps;
1537		vfd->release = video_device_release_empty;
1538		vfd->v4l2_dev = &dev->v4l2_dev;
1539		vfd->queue = &dev->vb_vbi_cap_q;
1540		vfd->lock = &dev->mutex;
1541		vfd->tvnorms = tvnorms_cap;
1542		video_set_drvdata(vfd, dev);
1543
1544#ifdef CONFIG_MEDIA_CONTROLLER
1545		dev->vbi_cap_pad.flags = MEDIA_PAD_FL_SINK;
1546		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_cap_pad);
1547		if (ret)
1548			return ret;
1549#endif
1550
1551		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1552		if (ret < 0)
1553			return ret;
1554		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1555					  video_device_node_name(vfd),
1556					  (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1557					  "raw and sliced" :
1558					  (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1559	}
1560
1561	if (dev->has_vbi_out) {
1562		vfd = &dev->vbi_out_dev;
1563		snprintf(vfd->name, sizeof(vfd->name),
1564			 "vivid-%03d-vbi-out", inst);
1565		vfd->vfl_dir = VFL_DIR_TX;
1566		vfd->fops = &vivid_fops;
1567		vfd->ioctl_ops = &vivid_ioctl_ops;
1568		vfd->device_caps = dev->vbi_out_caps;
1569		vfd->release = video_device_release_empty;
1570		vfd->v4l2_dev = &dev->v4l2_dev;
1571		vfd->queue = &dev->vb_vbi_out_q;
1572		vfd->lock = &dev->mutex;
1573		vfd->tvnorms = tvnorms_out;
1574		video_set_drvdata(vfd, dev);
1575
1576#ifdef CONFIG_MEDIA_CONTROLLER
1577		dev->vbi_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1578		ret = media_entity_pads_init(&vfd->entity, 1, &dev->vbi_out_pad);
1579		if (ret)
1580			return ret;
1581#endif
1582
1583		ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1584		if (ret < 0)
1585			return ret;
1586		v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1587					  video_device_node_name(vfd),
1588					  (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1589					  "raw and sliced" :
1590					  (dev->has_raw_vbi_out ? "raw" : "sliced"));
1591	}
1592
1593	if (dev->has_sdr_cap) {
1594		vfd = &dev->sdr_cap_dev;
1595		snprintf(vfd->name, sizeof(vfd->name),
1596			 "vivid-%03d-sdr-cap", inst);
1597		vfd->fops = &vivid_fops;
1598		vfd->ioctl_ops = &vivid_ioctl_ops;
1599		vfd->device_caps = dev->sdr_cap_caps;
1600		vfd->release = video_device_release_empty;
1601		vfd->v4l2_dev = &dev->v4l2_dev;
1602		vfd->queue = &dev->vb_sdr_cap_q;
1603		vfd->lock = &dev->mutex;
1604		video_set_drvdata(vfd, dev);
1605
1606#ifdef CONFIG_MEDIA_CONTROLLER
1607		dev->sdr_cap_pad.flags = MEDIA_PAD_FL_SINK;
1608		ret = media_entity_pads_init(&vfd->entity, 1, &dev->sdr_cap_pad);
1609		if (ret)
1610			return ret;
1611#endif
1612
1613		ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1614		if (ret < 0)
1615			return ret;
1616		v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1617					  video_device_node_name(vfd));
1618	}
1619
1620	if (dev->has_radio_rx) {
1621		vfd = &dev->radio_rx_dev;
1622		snprintf(vfd->name, sizeof(vfd->name),
1623			 "vivid-%03d-rad-rx", inst);
1624		vfd->fops = &vivid_radio_fops;
1625		vfd->ioctl_ops = &vivid_ioctl_ops;
1626		vfd->device_caps = dev->radio_rx_caps;
1627		vfd->release = video_device_release_empty;
1628		vfd->v4l2_dev = &dev->v4l2_dev;
1629		vfd->lock = &dev->mutex;
1630		video_set_drvdata(vfd, dev);
1631
1632		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1633		if (ret < 0)
1634			return ret;
1635		v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1636					  video_device_node_name(vfd));
1637	}
1638
1639	if (dev->has_radio_tx) {
1640		vfd = &dev->radio_tx_dev;
1641		snprintf(vfd->name, sizeof(vfd->name),
1642			 "vivid-%03d-rad-tx", inst);
1643		vfd->vfl_dir = VFL_DIR_TX;
1644		vfd->fops = &vivid_radio_fops;
1645		vfd->ioctl_ops = &vivid_ioctl_ops;
1646		vfd->device_caps = dev->radio_tx_caps;
1647		vfd->release = video_device_release_empty;
1648		vfd->v4l2_dev = &dev->v4l2_dev;
1649		vfd->lock = &dev->mutex;
1650		video_set_drvdata(vfd, dev);
1651
1652		ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1653		if (ret < 0)
1654			return ret;
1655		v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1656					  video_device_node_name(vfd));
1657	}
1658
1659	if (dev->has_meta_cap) {
1660		vfd = &dev->meta_cap_dev;
1661		snprintf(vfd->name, sizeof(vfd->name),
1662			 "vivid-%03d-meta-cap", inst);
1663		vfd->fops = &vivid_fops;
1664		vfd->ioctl_ops = &vivid_ioctl_ops;
1665		vfd->device_caps = dev->meta_cap_caps;
1666		vfd->release = video_device_release_empty;
1667		vfd->v4l2_dev = &dev->v4l2_dev;
1668		vfd->queue = &dev->vb_meta_cap_q;
1669		vfd->lock = &dev->mutex;
1670		vfd->tvnorms = tvnorms_cap;
1671		video_set_drvdata(vfd, dev);
1672#ifdef CONFIG_MEDIA_CONTROLLER
1673		dev->meta_cap_pad.flags = MEDIA_PAD_FL_SINK;
1674		ret = media_entity_pads_init(&vfd->entity, 1,
1675					     &dev->meta_cap_pad);
1676		if (ret)
1677			return ret;
1678#endif
1679		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1680					    meta_cap_nr[inst]);
1681		if (ret < 0)
1682			return ret;
1683		v4l2_info(&dev->v4l2_dev,
1684			  "V4L2 metadata capture device registered as %s\n",
1685			  video_device_node_name(vfd));
1686	}
1687
1688	if (dev->has_meta_out) {
1689		vfd = &dev->meta_out_dev;
1690		snprintf(vfd->name, sizeof(vfd->name),
1691			 "vivid-%03d-meta-out", inst);
1692		vfd->vfl_dir = VFL_DIR_TX;
1693		vfd->fops = &vivid_fops;
1694		vfd->ioctl_ops = &vivid_ioctl_ops;
1695		vfd->device_caps = dev->meta_out_caps;
1696		vfd->release = video_device_release_empty;
1697		vfd->v4l2_dev = &dev->v4l2_dev;
1698		vfd->queue = &dev->vb_meta_out_q;
1699		vfd->lock = &dev->mutex;
1700		vfd->tvnorms = tvnorms_out;
1701		video_set_drvdata(vfd, dev);
1702#ifdef CONFIG_MEDIA_CONTROLLER
1703		dev->meta_out_pad.flags = MEDIA_PAD_FL_SOURCE;
1704		ret = media_entity_pads_init(&vfd->entity, 1,
1705					     &dev->meta_out_pad);
1706		if (ret)
1707			return ret;
1708#endif
1709		ret = video_register_device(vfd, VFL_TYPE_VIDEO,
1710					    meta_out_nr[inst]);
1711		if (ret < 0)
1712			return ret;
1713		v4l2_info(&dev->v4l2_dev,
1714			  "V4L2 metadata output device registered as %s\n",
1715			  video_device_node_name(vfd));
1716	}
1717
1718	if (dev->has_touch_cap) {
1719		vfd = &dev->touch_cap_dev;
1720		snprintf(vfd->name, sizeof(vfd->name),
1721			 "vivid-%03d-touch-cap", inst);
1722		vfd->fops = &vivid_fops;
1723		vfd->ioctl_ops = &vivid_ioctl_ops;
1724		vfd->device_caps = dev->touch_cap_caps;
1725		vfd->release = video_device_release_empty;
1726		vfd->v4l2_dev = &dev->v4l2_dev;
1727		vfd->queue = &dev->vb_touch_cap_q;
1728		vfd->tvnorms = tvnorms_cap;
1729		vfd->lock = &dev->mutex;
1730		video_set_drvdata(vfd, dev);
1731#ifdef CONFIG_MEDIA_CONTROLLER
1732		dev->touch_cap_pad.flags = MEDIA_PAD_FL_SINK;
1733		ret = media_entity_pads_init(&vfd->entity, 1,
1734					     &dev->touch_cap_pad);
1735		if (ret)
1736			return ret;
1737#endif
1738		ret = video_register_device(vfd, VFL_TYPE_TOUCH,
1739					    touch_cap_nr[inst]);
1740		if (ret < 0)
1741			return ret;
1742		v4l2_info(&dev->v4l2_dev,
1743			  "V4L2 touch capture device registered as %s\n",
1744			  video_device_node_name(vfd));
1745	}
1746
1747#ifdef CONFIG_MEDIA_CONTROLLER
1748	/* Register the media device */
1749	ret = media_device_register(&dev->mdev);
1750	if (ret) {
1751		dev_err(dev->mdev.dev,
1752			"media device register failed (err=%d)\n", ret);
1753		return ret;
1754	}
1755#endif
1756	return 0;
1757}
1758
1759static void update_hdmi_ctrls_work_handler(struct work_struct *work)
1760{
1761	u64 skip_mask;
1762	u64 update_mask;
1763
1764	spin_lock(&hdmi_output_skip_mask_lock);
1765	skip_mask = hdmi_to_output_menu_skip_mask;
1766	update_mask = hdmi_input_update_outputs_mask;
1767	hdmi_input_update_outputs_mask = 0;
1768	spin_unlock(&hdmi_output_skip_mask_lock);
1769	for (int i = 0; i < n_devs && vivid_devs[i]; i++) {
1770		if (update_mask & (1 << i))
1771			vivid_update_connected_outputs(vivid_devs[i]);
1772		for (int j = 0; j < vivid_devs[i]->num_hdmi_inputs; j++) {
1773			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_hdmi_to_output[j];
1774
1775			v4l2_ctrl_modify_range(c, c->minimum, c->maximum,
1776					       skip_mask & ~(1ULL << c->cur.val),
1777					       c->default_value);
1778		}
1779	}
1780}
1781
1782static void update_svid_ctrls_work_handler(struct work_struct *work)
1783{
1784	u64 skip_mask;
1785
1786	spin_lock(&svid_output_skip_mask_lock);
1787	skip_mask = svid_to_output_menu_skip_mask;
1788	spin_unlock(&svid_output_skip_mask_lock);
1789	for (int i = 0; i < n_devs && vivid_devs[i]; i++) {
1790		for (int j = 0; j < vivid_devs[i]->num_svid_inputs; j++) {
1791			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_svid_to_output[j];
1792
1793			v4l2_ctrl_modify_range(c, c->minimum, c->maximum,
1794					       skip_mask & ~(1ULL << c->cur.val),
1795					       c->default_value);
1796		}
1797	}
1798}
1799
1800static int vivid_create_instance(struct platform_device *pdev, int inst)
1801{
1802	static const struct v4l2_dv_timings def_dv_timings =
1803					V4L2_DV_BT_CEA_1280X720P60;
1804	unsigned in_type_counter[4] = { 0, 0, 0, 0 };
1805	unsigned out_type_counter[4] = { 0, 0, 0, 0 };
1806	int ccs_cap = ccs_cap_mode[inst];
1807	int ccs_out = ccs_out_mode[inst];
1808	bool has_tuner;
1809	bool has_modulator;
1810	struct vivid_dev *dev;
1811	unsigned node_type = node_types[inst];
1812	v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
1813	int ret;
1814	int i;
1815
1816	/* allocate main vivid state structure */
1817	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1818	if (!dev)
1819		return -ENOMEM;
1820
1821	dev->inst = inst;
1822
1823#ifdef CONFIG_MEDIA_CONTROLLER
1824	dev->v4l2_dev.mdev = &dev->mdev;
1825
1826	/* Initialize media device */
1827	strscpy(dev->mdev.model, VIVID_MODULE_NAME, sizeof(dev->mdev.model));
1828	snprintf(dev->mdev.bus_info, sizeof(dev->mdev.bus_info),
1829		 "platform:%s-%03d", VIVID_MODULE_NAME, inst);
1830	dev->mdev.dev = &pdev->dev;
1831	media_device_init(&dev->mdev);
1832	dev->mdev.ops = &vivid_media_ops;
1833#endif
1834
1835	/* register v4l2_device */
1836	snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
1837			"%s-%03d", VIVID_MODULE_NAME, inst);
1838	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
1839	if (ret) {
1840		kfree(dev);
1841		return ret;
1842	}
1843	dev->v4l2_dev.release = vivid_dev_release;
1844
1845	ret = vivid_detect_feature_set(dev, inst, node_type,
1846				       &has_tuner, &has_modulator,
1847				       &ccs_cap, &ccs_out,
1848				       in_type_counter, out_type_counter);
1849	if (ret)
1850		goto free_dev;
1851
1852	vivid_set_capabilities(dev);
1853
1854	ret = -ENOMEM;
1855	/* initialize the test pattern generator */
1856	tpg_init(&dev->tpg, 640, 360);
1857	if (tpg_alloc(&dev->tpg, array_size(MAX_WIDTH, MAX_ZOOM)))
1858		goto free_dev;
1859	dev->scaled_line = vcalloc(MAX_WIDTH, MAX_ZOOM);
1860	if (!dev->scaled_line)
1861		goto free_dev;
1862	dev->blended_line = vcalloc(MAX_WIDTH, MAX_ZOOM);
1863	if (!dev->blended_line)
1864		goto free_dev;
1865
1866	/* load the edid */
1867	dev->edid = vmalloc_array(256, 128);
1868	if (!dev->edid)
1869		goto free_dev;
1870
1871	ret = vivid_init_dv_timings(dev);
1872	if (ret < 0)
1873		goto free_dev;
1874
1875	vivid_disable_unused_ioctls(dev, has_tuner, has_modulator,
1876				    in_type_counter, out_type_counter);
1877
1878	/* configure internal data */
1879	dev->fmt_cap = &vivid_formats[0];
1880	dev->fmt_out = &vivid_formats[0];
1881	if (!dev->multiplanar)
1882		vivid_formats[0].data_offset[0] = 0;
1883	dev->webcam_size_idx = 1;
1884	dev->webcam_ival_idx = 3;
1885	tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
1886	dev->std_out = V4L2_STD_PAL;
1887	if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
1888		tvnorms_cap = V4L2_STD_ALL;
1889	if (dev->output_type[0] == SVID)
1890		tvnorms_out = V4L2_STD_ALL;
1891	for (i = 0; i < MAX_INPUTS; i++) {
1892		dev->dv_timings_cap[i] = def_dv_timings;
1893		dev->std_cap[i] = V4L2_STD_PAL;
1894	}
1895	dev->dv_timings_out = def_dv_timings;
1896	dev->tv_freq = 2804 /* 175.25 * 16 */;
1897	dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
1898	dev->tv_field_cap = V4L2_FIELD_INTERLACED;
1899	dev->tv_field_out = V4L2_FIELD_INTERLACED;
1900	dev->radio_rx_freq = 95000 * 16;
1901	dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
1902	if (dev->has_radio_tx) {
1903		dev->radio_tx_freq = 95500 * 16;
1904		dev->radio_rds_loop = false;
1905	}
1906	dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
1907	dev->sdr_adc_freq = 300000;
1908	dev->sdr_fm_freq = 50000000;
1909	dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
1910	dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
1911
1912	dev->edid_max_blocks = dev->edid_blocks = 2;
1913	memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
1914	dev->radio_rds_init_time = ktime_get();
1915	INIT_WORK(&dev->update_hdmi_ctrl_work, update_hdmi_ctrls_work_handler);
1916	INIT_WORK(&dev->update_svid_ctrl_work, update_svid_ctrls_work_handler);
1917	for (int j = 0, k = 0; j < dev->num_inputs; ++j)
1918		if (dev->input_type[j] == HDMI)
1919			dev->hdmi_index_to_input_index[k++] = j;
1920	for (int j = 0, k = 0; j < dev->num_outputs; ++j)
1921		if (dev->output_type[j] == HDMI) {
1922			dev->output_to_iface_index[j] = k;
1923			dev->hdmi_index_to_output_index[k++] = j;
1924		}
1925	for (int j = 0, k = 0; j < dev->num_inputs; ++j)
1926		if (dev->input_type[j] == SVID)
1927			dev->svid_index_to_input_index[k++] = j;
1928	for (int j = 0, k = 0; j < dev->num_outputs; ++j)
1929		if (dev->output_type[j] == SVID)
1930			dev->output_to_iface_index[j] = k++;
1931
1932	/* create all controls */
1933	ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
1934			in_type_counter[TV] || in_type_counter[SVID] ||
1935			out_type_counter[SVID],
1936			in_type_counter[HDMI] || out_type_counter[HDMI]);
1937	if (ret)
1938		goto unreg_dev;
1939
1940	/* enable/disable interface specific controls */
1941	if (dev->num_inputs && dev->input_type[0] != HDMI) {
1942		v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode, false);
1943		v4l2_ctrl_activate(dev->ctrl_dv_timings, false);
1944	} else if (dev->num_inputs && dev->input_type[0] == HDMI) {
1945		v4l2_ctrl_activate(dev->ctrl_std_signal_mode, false);
1946		v4l2_ctrl_activate(dev->ctrl_standard, false);
1947	}
1948
1949	/*
1950	 * update the capture and output formats to do a proper initial
1951	 * configuration.
1952	 */
1953	vivid_update_format_cap(dev, false);
1954	vivid_update_format_out(dev);
1955
1956	/* update touch configuration */
1957	dev->timeperframe_tch_cap.numerator = 1;
1958	dev->timeperframe_tch_cap.denominator = 10;
1959	vivid_set_touch(dev, 0);
1960
1961	/* initialize locks */
1962	spin_lock_init(&dev->slock);
1963	mutex_init(&dev->mutex);
1964
1965	/* init dma queues */
1966	INIT_LIST_HEAD(&dev->vid_cap_active);
1967	INIT_LIST_HEAD(&dev->vid_out_active);
1968	INIT_LIST_HEAD(&dev->vbi_cap_active);
1969	INIT_LIST_HEAD(&dev->vbi_out_active);
1970	INIT_LIST_HEAD(&dev->sdr_cap_active);
1971	INIT_LIST_HEAD(&dev->meta_cap_active);
1972	INIT_LIST_HEAD(&dev->meta_out_active);
1973	INIT_LIST_HEAD(&dev->touch_cap_active);
1974
1975	spin_lock_init(&dev->cec_xfers_slock);
1976
1977	if (allocators[inst] == 1)
1978		dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1979
1980	ret = vivid_create_queues(dev);
1981	if (ret)
1982		goto unreg_dev;
1983
1984#ifdef CONFIG_VIDEO_VIVID_CEC
1985	if (dev->has_vid_cap && in_type_counter[HDMI]) {
1986		struct cec_adapter *adap;
1987
1988		adap = vivid_cec_alloc_adap(dev, 0, false);
1989		ret = PTR_ERR_OR_ZERO(adap);
1990		if (ret < 0)
1991			goto unreg_dev;
1992		dev->cec_rx_adap = adap;
1993	}
1994
1995	if (dev->has_vid_out) {
1996		int j;
1997
1998		for (i = j = 0; i < dev->num_outputs; i++) {
1999			struct cec_adapter *adap;
2000
2001			if (dev->output_type[i] != HDMI)
2002				continue;
2003
2004			adap = vivid_cec_alloc_adap(dev, j, true);
2005			ret = PTR_ERR_OR_ZERO(adap);
2006			if (ret < 0) {
2007				while (j--)
2008					cec_delete_adapter(dev->cec_tx_adap[j]);
2009				goto unreg_dev;
2010			}
2011
2012			dev->cec_tx_adap[j++] = adap;
2013		}
2014	}
2015
2016	if (dev->cec_rx_adap || dev->num_hdmi_outputs) {
2017		init_waitqueue_head(&dev->kthread_waitq_cec);
2018		dev->kthread_cec = kthread_run(vivid_cec_bus_thread, dev,
2019					       "vivid_cec-%s", dev->v4l2_dev.name);
2020		if (IS_ERR(dev->kthread_cec)) {
2021			ret = PTR_ERR(dev->kthread_cec);
2022			dev->kthread_cec = NULL;
2023			v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
2024			goto unreg_dev;
2025		}
2026	}
2027
2028#endif
2029
2030	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
2031	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
2032	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
2033	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
2034	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
2035	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
2036	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
2037	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_cap);
2038	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_meta_out);
2039	v4l2_ctrl_handler_setup(&dev->ctrl_hdl_touch_cap);
2040
2041	/* finally start creating the device nodes */
2042	ret = vivid_create_devnodes(pdev, dev, inst,
2043				    tvnorms_cap, tvnorms_out,
2044				    in_type_counter, out_type_counter);
2045	if (ret)
2046		goto unreg_dev;
2047
2048	/* Now that everything is fine, let's add it to device list */
2049	vivid_devs[inst] = dev;
2050
2051	return 0;
2052
2053unreg_dev:
2054	vb2_video_unregister_device(&dev->touch_cap_dev);
2055	vb2_video_unregister_device(&dev->meta_out_dev);
2056	vb2_video_unregister_device(&dev->meta_cap_dev);
2057	video_unregister_device(&dev->radio_tx_dev);
2058	video_unregister_device(&dev->radio_rx_dev);
2059	vb2_video_unregister_device(&dev->sdr_cap_dev);
2060	vb2_video_unregister_device(&dev->vbi_out_dev);
2061	vb2_video_unregister_device(&dev->vbi_cap_dev);
2062	vb2_video_unregister_device(&dev->vid_out_dev);
2063	vb2_video_unregister_device(&dev->vid_cap_dev);
2064	cec_unregister_adapter(dev->cec_rx_adap);
2065	for (i = 0; i < MAX_HDMI_OUTPUTS; i++)
2066		cec_unregister_adapter(dev->cec_tx_adap[i]);
2067	if (dev->kthread_cec)
2068		kthread_stop(dev->kthread_cec);
2069free_dev:
2070	v4l2_device_put(&dev->v4l2_dev);
2071	return ret;
2072}
2073
2074/* This routine allocates from 1 to n_devs virtual drivers.
2075
2076   The real maximum number of virtual drivers will depend on how many drivers
2077   will succeed. This is limited to the maximum number of devices that
2078   videodev supports, which is equal to VIDEO_NUM_DEVICES.
2079 */
2080static int vivid_probe(struct platform_device *pdev)
2081{
2082	const struct font_desc *font = find_font("VGA8x16");
2083	int ret = 0, i;
2084
2085	if (font == NULL) {
2086		pr_err("vivid: could not find font\n");
2087		return -ENODEV;
2088	}
2089
2090	tpg_set_font(font->data);
2091
2092	n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
2093
2094	for (i = 0; i < n_devs; i++) {
2095		ret = vivid_create_instance(pdev, i);
2096		if (ret) {
2097			/* If some instantiations succeeded, keep driver */
2098			if (i)
2099				ret = 0;
2100			break;
2101		}
2102	}
2103
2104	if (ret < 0) {
2105		pr_err("vivid: error %d while loading driver\n", ret);
2106		return ret;
2107	}
2108
2109	/* n_devs will reflect the actual number of allocated devices */
2110	n_devs = i;
2111
2112	/* Determine qmenu items actually in use */
2113	int hdmi_count = FIXED_MENU_ITEMS;
2114	int svid_count = FIXED_MENU_ITEMS;
2115
2116	for (int i = 0; i < n_devs; i++) {
2117		struct vivid_dev *dev = vivid_devs[i];
2118
2119		if (!dev->has_vid_out)
2120			continue;
2121		for (int j = 0; j < dev->num_outputs && hdmi_count < MAX_MENU_ITEMS; ++j) {
2122			if (dev->output_type[j] == HDMI) {
2123				vivid_ctrl_hdmi_to_output_instance[hdmi_count] = vivid_devs[i];
2124				vivid_ctrl_hdmi_to_output_index[hdmi_count++] = j;
2125			}
2126		}
2127		for (int j = 0; j < dev->num_outputs && svid_count < MAX_MENU_ITEMS; ++j) {
2128			if (dev->output_type[j] == SVID) {
2129				vivid_ctrl_svid_to_output_instance[svid_count] = vivid_devs[i];
2130				vivid_ctrl_svid_to_output_index[svid_count++] = j;
2131			}
2132		}
2133	}
2134	hdmi_count = min(hdmi_count, MAX_MENU_ITEMS);
2135	svid_count = min(svid_count, MAX_MENU_ITEMS);
2136	for (int i = 0; i < n_devs; i++) {
2137		for (int j = 0; j < vivid_devs[i]->num_hdmi_inputs; j++) {
2138			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_hdmi_to_output[j];
2139
2140			v4l2_ctrl_modify_range(c, c->minimum, hdmi_count - 1, 0, c->default_value);
2141		}
2142		for (int j = 0; j < vivid_devs[i]->num_svid_inputs; j++) {
2143			struct v4l2_ctrl *c = vivid_devs[i]->ctrl_svid_to_output[j];
2144
2145			v4l2_ctrl_modify_range(c, c->minimum, svid_count - 1, 0, c->default_value);
2146		}
2147	}
2148	return ret;
2149}
2150
2151static void vivid_remove(struct platform_device *pdev)
2152{
2153	struct vivid_dev *dev;
2154	unsigned int i, j;
2155
2156	for (i = 0; i < n_devs; i++) {
2157		dev = vivid_devs[i];
2158		if (!dev)
2159			continue;
2160
2161		if (dev->disconnect_error)
2162			vivid_reconnect(dev);
2163#ifdef CONFIG_MEDIA_CONTROLLER
2164		media_device_unregister(&dev->mdev);
2165#endif
2166
2167		if (dev->has_vid_cap) {
2168			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2169				video_device_node_name(&dev->vid_cap_dev));
2170			vb2_video_unregister_device(&dev->vid_cap_dev);
2171		}
2172		if (dev->has_vid_out) {
2173			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2174				video_device_node_name(&dev->vid_out_dev));
2175			vb2_video_unregister_device(&dev->vid_out_dev);
2176		}
2177		if (dev->has_vbi_cap) {
2178			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2179				video_device_node_name(&dev->vbi_cap_dev));
2180			vb2_video_unregister_device(&dev->vbi_cap_dev);
2181		}
2182		if (dev->has_vbi_out) {
2183			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2184				video_device_node_name(&dev->vbi_out_dev));
2185			vb2_video_unregister_device(&dev->vbi_out_dev);
2186		}
2187		if (dev->has_sdr_cap) {
2188			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2189				video_device_node_name(&dev->sdr_cap_dev));
2190			vb2_video_unregister_device(&dev->sdr_cap_dev);
2191		}
2192		if (dev->has_radio_rx) {
2193			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2194				video_device_node_name(&dev->radio_rx_dev));
2195			video_unregister_device(&dev->radio_rx_dev);
2196		}
2197		if (dev->has_radio_tx) {
2198			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2199				video_device_node_name(&dev->radio_tx_dev));
2200			video_unregister_device(&dev->radio_tx_dev);
2201		}
2202		if (dev->has_fb)
2203			vivid_fb_deinit(dev);
2204		if (dev->has_meta_cap) {
2205			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2206				  video_device_node_name(&dev->meta_cap_dev));
2207			vb2_video_unregister_device(&dev->meta_cap_dev);
2208		}
2209		if (dev->has_meta_out) {
2210			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2211				  video_device_node_name(&dev->meta_out_dev));
2212			vb2_video_unregister_device(&dev->meta_out_dev);
2213		}
2214		if (dev->has_touch_cap) {
2215			v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
2216				  video_device_node_name(&dev->touch_cap_dev));
2217			vb2_video_unregister_device(&dev->touch_cap_dev);
2218		}
2219		cec_unregister_adapter(dev->cec_rx_adap);
2220		for (j = 0; j < MAX_HDMI_OUTPUTS; j++)
2221			cec_unregister_adapter(dev->cec_tx_adap[j]);
2222		if (dev->kthread_cec)
2223			kthread_stop(dev->kthread_cec);
2224		v4l2_device_put(&dev->v4l2_dev);
2225		vivid_devs[i] = NULL;
2226	}
2227}
2228
2229static void vivid_pdev_release(struct device *dev)
2230{
2231}
2232
2233static struct platform_device vivid_pdev = {
2234	.name		= "vivid",
2235	.dev.release	= vivid_pdev_release,
2236};
2237
2238static struct platform_driver vivid_pdrv = {
2239	.probe		= vivid_probe,
2240	.remove		= vivid_remove,
2241	.driver		= {
2242		.name	= "vivid",
2243	},
2244};
2245
2246static int __init vivid_init(void)
2247{
2248	int hdmi_count = FIXED_MENU_ITEMS;
2249	int svid_count = FIXED_MENU_ITEMS;
2250	int ret = -ENOMEM;
2251	unsigned int ndevs;
2252
2253	/* Sanity check, prevent insane number of vivid instances */
2254	if (n_devs > 64)
2255		n_devs = 64;
2256	ndevs = clamp_t(unsigned int, n_devs, 1, VIVID_MAX_DEVS);
2257
2258	for (unsigned int i = 0; i < ndevs; i++) {
2259		if (!(node_types[i] & (1 << 8)))
2260			continue;
2261		unsigned int n_outputs = min(num_outputs[i], MAX_OUTPUTS);
2262
2263		for (u8 j = 0, k = 0; j < n_outputs && hdmi_count < MAX_MENU_ITEMS &&
2264		     k < MAX_HDMI_OUTPUTS; ++j) {
2265			if (output_types[i] & BIT(j)) {
2266				vivid_ctrl_hdmi_to_output_strings[hdmi_count] =
2267					kmalloc(MAX_STRING_LENGTH, GFP_KERNEL);
2268				if (!vivid_ctrl_hdmi_to_output_strings[hdmi_count])
2269					goto free_output_strings;
2270				snprintf(vivid_ctrl_hdmi_to_output_strings[hdmi_count],
2271					 MAX_STRING_LENGTH, "Output HDMI %03d-%d",
2272					 i & 0xff, k);
2273				k++;
2274				hdmi_count++;
2275			}
2276		}
2277		for (u8 j = 0, k = 0; j < n_outputs && svid_count < MAX_MENU_ITEMS; ++j) {
2278			if (!(output_types[i] & BIT(j))) {
2279				vivid_ctrl_svid_to_output_strings[svid_count] =
2280					kmalloc(MAX_STRING_LENGTH, GFP_KERNEL);
2281				if (!vivid_ctrl_svid_to_output_strings[svid_count])
2282					goto free_output_strings;
2283				snprintf(vivid_ctrl_svid_to_output_strings[svid_count],
2284					 MAX_STRING_LENGTH, "Output S-Video %03d-%d",
2285					 i & 0xff, k);
2286				k++;
2287				svid_count++;
2288			}
2289		}
2290	}
2291	ret = platform_device_register(&vivid_pdev);
2292	if (ret)
2293		goto free_output_strings;
2294	ret = platform_driver_register(&vivid_pdrv);
2295	if (ret)
2296		goto unreg_device;
2297
2298	/* Initialize workqueue before module is loaded */
2299	update_hdmi_ctrls_workqueue = create_workqueue("update_hdmi_ctrls_wq");
2300	if (!update_hdmi_ctrls_workqueue) {
2301		ret = -ENOMEM;
2302		goto unreg_driver;
2303	}
2304	update_svid_ctrls_workqueue = create_workqueue("update_svid_ctrls_wq");
2305	if (!update_svid_ctrls_workqueue) {
2306		ret = -ENOMEM;
2307		goto destroy_hdmi_wq;
2308	}
2309	return ret;
2310
2311destroy_hdmi_wq:
2312	destroy_workqueue(update_hdmi_ctrls_workqueue);
2313unreg_driver:
2314	platform_driver_register(&vivid_pdrv);
2315unreg_device:
2316	platform_device_unregister(&vivid_pdev);
2317free_output_strings:
2318	for (int i = FIXED_MENU_ITEMS; i < MAX_MENU_ITEMS; i++) {
2319		kfree(vivid_ctrl_hdmi_to_output_strings[i]);
2320		kfree(vivid_ctrl_svid_to_output_strings[i]);
2321	}
2322	return ret;
2323}
2324
2325static void __exit vivid_exit(void)
2326{
2327	for (int i = FIXED_MENU_ITEMS; i < MAX_MENU_ITEMS; i++) {
2328		kfree(vivid_ctrl_hdmi_to_output_strings[i]);
2329		kfree(vivid_ctrl_svid_to_output_strings[i]);
2330	}
2331	destroy_workqueue(update_svid_ctrls_workqueue);
2332	destroy_workqueue(update_hdmi_ctrls_workqueue);
2333	platform_driver_unregister(&vivid_pdrv);
2334	platform_device_unregister(&vivid_pdev);
2335}
2336
2337module_init(vivid_init);
2338module_exit(vivid_exit);
~/ Read-only file preview.