root/drivers/media/platform/vivid/vivid-core.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. vidioc_querycap
  2. vidioc_s_hw_freq_seek
  3. vidioc_enum_freq_bands
  4. vidioc_g_tuner
  5. vidioc_s_tuner
  6. vidioc_g_frequency
  7. vidioc_s_frequency
  8. vidioc_overlay
  9. vidioc_g_fbuf
  10. vidioc_s_fbuf
  11. vidioc_s_std
  12. vidioc_s_dv_timings
  13. vidioc_g_pixelaspect
  14. vidioc_g_selection
  15. vidioc_s_selection
  16. vidioc_g_parm
  17. vidioc_s_parm
  18. vidioc_log_status
  19. vivid_radio_read
  20. vivid_radio_write
  21. vivid_radio_poll
  22. vivid_is_in_use
  23. vivid_is_last_user
  24. vivid_fop_release
  25. vivid_dev_release
  26. vivid_req_validate
  27. vivid_create_instance
  28. vivid_probe
  29. vivid_remove
  30. vivid_pdev_release
  31. vivid_init
  32. vivid_exit

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

/* [<][>][^][v][top][bottom][index][help] */