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