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-ctrls.h"
  50
  51#define VIVID_MODULE_NAME "vivid"
  52
  53/* The maximum number of vivid devices */
  54#define VIVID_MAX_DEVS CONFIG_VIDEO_VIVID_MAX_DEVS
  55
  56MODULE_DESCRIPTION("Virtual Video Test Driver");
  57MODULE_AUTHOR("Hans Verkuil");
  58MODULE_LICENSE("GPL");
  59
  60static unsigned n_devs = 1;
  61module_param(n_devs, uint, 0444);
  62MODULE_PARM_DESC(n_devs, " number of driver instances to create");
  63
  64static int vid_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  65module_param_array(vid_cap_nr, int, NULL, 0444);
  66MODULE_PARM_DESC(vid_cap_nr, " videoX start number, -1 is autodetect");
  67
  68static int vid_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  69module_param_array(vid_out_nr, int, NULL, 0444);
  70MODULE_PARM_DESC(vid_out_nr, " videoX start number, -1 is autodetect");
  71
  72static int vbi_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  73module_param_array(vbi_cap_nr, int, NULL, 0444);
  74MODULE_PARM_DESC(vbi_cap_nr, " vbiX start number, -1 is autodetect");
  75
  76static int vbi_out_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  77module_param_array(vbi_out_nr, int, NULL, 0444);
  78MODULE_PARM_DESC(vbi_out_nr, " vbiX start number, -1 is autodetect");
  79
  80static int sdr_cap_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  81module_param_array(sdr_cap_nr, int, NULL, 0444);
  82MODULE_PARM_DESC(sdr_cap_nr, " swradioX start number, -1 is autodetect");
  83
  84static int radio_rx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  85module_param_array(radio_rx_nr, int, NULL, 0444);
  86MODULE_PARM_DESC(radio_rx_nr, " radioX start number, -1 is autodetect");
  87
  88static int radio_tx_nr[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  89module_param_array(radio_tx_nr, int, NULL, 0444);
  90MODULE_PARM_DESC(radio_tx_nr, " radioX start number, -1 is autodetect");
  91
  92static int ccs_cap_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  93module_param_array(ccs_cap_mode, int, NULL, 0444);
  94MODULE_PARM_DESC(ccs_cap_mode, " capture crop/compose/scale mode:\n"
  95                           "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
  96                           "\t\t    -1=user-controlled (default)");
  97
  98static int ccs_out_mode[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = -1 };
  99module_param_array(ccs_out_mode, int, NULL, 0444);
 100MODULE_PARM_DESC(ccs_out_mode, " output crop/compose/scale mode:\n"
 101                           "\t\t    bit 0=crop, 1=compose, 2=scale,\n"
 102                           "\t\t    -1=user-controlled (default)");
 103
 104static unsigned multiplanar[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 1 };
 105module_param_array(multiplanar, uint, NULL, 0444);
 106MODULE_PARM_DESC(multiplanar, " 1 (default) creates a single planar device, 2 creates a multiplanar device.");
 107
 108/* Default: video + vbi-cap (raw and sliced) + radio rx + radio tx + sdr + vbi-out + vid-out */
 109static unsigned node_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0x1d3d };
 110module_param_array(node_types, uint, NULL, 0444);
 111MODULE_PARM_DESC(node_types, " node types, default is 0x1d3d. Bitmask with the following meaning:\n"
 112                             "\t\t    bit 0: Video Capture node\n"
 113                             "\t\t    bit 2-3: VBI Capture node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
 114                             "\t\t    bit 4: Radio Receiver node\n"
 115                             "\t\t    bit 5: Software Defined Radio Receiver node\n"
 116                             "\t\t    bit 8: Video Output node\n"
 117                             "\t\t    bit 10-11: VBI Output node: 0 = none, 1 = raw vbi, 2 = sliced vbi, 3 = both\n"
 118                             "\t\t    bit 12: Radio Transmitter node\n"
 119                             "\t\t    bit 16: Framebuffer for testing overlays");
 120
 121/* Default: 4 inputs */
 122static unsigned num_inputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 4 };
 123module_param_array(num_inputs, uint, NULL, 0444);
 124MODULE_PARM_DESC(num_inputs, " number of inputs, default is 4");
 125
 126/* Default: input 0 = WEBCAM, 1 = TV, 2 = SVID, 3 = HDMI */
 127static unsigned input_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 0xe4 };
 128module_param_array(input_types, uint, NULL, 0444);
 129MODULE_PARM_DESC(input_types, " input types, default is 0xe4. Two bits per input,\n"
 130                              "\t\t    bits 0-1 == input 0, bits 31-30 == input 15.\n"
 131                              "\t\t    Type 0 == webcam, 1 == TV, 2 == S-Video, 3 == HDMI");
 132
 133/* Default: 2 outputs */
 134static unsigned num_outputs[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
 135module_param_array(num_outputs, uint, NULL, 0444);
 136MODULE_PARM_DESC(num_outputs, " number of outputs, default is 2");
 137
 138/* Default: output 0 = SVID, 1 = HDMI */
 139static unsigned output_types[VIVID_MAX_DEVS] = { [0 ... (VIVID_MAX_DEVS - 1)] = 2 };
 140module_param_array(output_types, uint, NULL, 0444);
 141MODULE_PARM_DESC(output_types, " output types, default is 0x02. One bit per output,\n"
 142                              "\t\t    bit 0 == output 0, bit 15 == output 15.\n"
 143                              "\t\t    Type 0 == S-Video, 1 == HDMI");
 144
 145unsigned vivid_debug;
 146module_param(vivid_debug, uint, 0644);
 147MODULE_PARM_DESC(vivid_debug, " activates debug info");
 148
 149static bool no_error_inj;
 150module_param(no_error_inj, bool, 0444);
 151MODULE_PARM_DESC(no_error_inj, " if set disable the error injecting controls");
 152
 153static struct vivid_dev *vivid_devs[VIVID_MAX_DEVS];
 154
 155const struct v4l2_rect vivid_min_rect = {
 156        0, 0, MIN_WIDTH, MIN_HEIGHT
 157};
 158
 159const struct v4l2_rect vivid_max_rect = {
 160        0, 0, MAX_WIDTH * MAX_ZOOM, MAX_HEIGHT * MAX_ZOOM
 161};
 162
 163static const u8 vivid_hdmi_edid[256] = {
 164        0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
 165        0x63, 0x3a, 0xaa, 0x55, 0x00, 0x00, 0x00, 0x00,
 166        0x0a, 0x18, 0x01, 0x03, 0x80, 0x10, 0x09, 0x78,
 167        0x0e, 0x00, 0xb2, 0xa0, 0x57, 0x49, 0x9b, 0x26,
 168        0x10, 0x48, 0x4f, 0x2f, 0xcf, 0x00, 0x31, 0x59,
 169        0x45, 0x59, 0x81, 0x80, 0x81, 0x40, 0x90, 0x40,
 170        0x95, 0x00, 0xa9, 0x40, 0xb3, 0x00, 0x02, 0x3a,
 171        0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
 172        0x46, 0x00, 0x10, 0x09, 0x00, 0x00, 0x00, 0x1e,
 173        0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x55, 0x18,
 174        0x5e, 0x11, 0x00, 0x0a, 0x20, 0x20, 0x20, 0x20,
 175        0x20, 0x20, 0x00, 0x00, 0x00, 0xfc, 0x00,  'v',
 176        '4',   'l',  '2',  '-',  'h',  'd',  'm',  'i',
 177        0x0a, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x00, 0x10,
 178        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 179        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf0,
 180
 181        0x02, 0x03, 0x1a, 0xc0, 0x48, 0xa2, 0x10, 0x04,
 182        0x02, 0x01, 0x21, 0x14, 0x13, 0x23, 0x09, 0x07,
 183        0x07, 0x65, 0x03, 0x0c, 0x00, 0x10, 0x00, 0xe2,
 184        0x00, 0x2a, 0x01, 0x1d, 0x00, 0x80, 0x51, 0xd0,
 185        0x1c, 0x20, 0x40, 0x80, 0x35, 0x00, 0x00, 0x00,
 186        0x00, 0x00, 0x00, 0x1e, 0x8c, 0x0a, 0xd0, 0x8a,
 187        0x20, 0xe0, 0x2d, 0x10, 0x10, 0x3e, 0x96, 0x00,
 188        0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00,
 189        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 190        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 191        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 192        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 193        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 194        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 195        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 196        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd7
 197};
 198
 199static int vidioc_querycap(struct file *file, void  *priv,
 200                                        struct v4l2_capability *cap)
 201{
 202        struct vivid_dev *dev = video_drvdata(file);
 203        struct video_device *vdev = video_devdata(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        if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_RX)
 211                cap->device_caps = dev->vid_cap_caps;
 212        if (vdev->vfl_type == VFL_TYPE_GRABBER && vdev->vfl_dir == VFL_DIR_TX)
 213                cap->device_caps = dev->vid_out_caps;
 214        else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_RX)
 215                cap->device_caps = dev->vbi_cap_caps;
 216        else if (vdev->vfl_type == VFL_TYPE_VBI && vdev->vfl_dir == VFL_DIR_TX)
 217                cap->device_caps = dev->vbi_out_caps;
 218        else if (vdev->vfl_type == VFL_TYPE_SDR)
 219                cap->device_caps = dev->sdr_cap_caps;
 220        else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_RX)
 221                cap->device_caps = dev->radio_rx_caps;
 222        else if (vdev->vfl_type == VFL_TYPE_RADIO && vdev->vfl_dir == VFL_DIR_TX)
 223                cap->device_caps = dev->radio_tx_caps;
 224        cap->capabilities = dev->vid_cap_caps | dev->vid_out_caps |
 225                dev->vbi_cap_caps | dev->vbi_out_caps |
 226                dev->radio_rx_caps | dev->radio_tx_caps |
 227                dev->sdr_cap_caps | V4L2_CAP_DEVICE_CAPS;
 228        return 0;
 229}
 230
 231static int vidioc_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
 232{
 233        struct video_device *vdev = video_devdata(file);
 234
 235        if (vdev->vfl_type == VFL_TYPE_RADIO)
 236                return vivid_radio_rx_s_hw_freq_seek(file, fh, a);
 237        return -ENOTTY;
 238}
 239
 240static int vidioc_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
 241{
 242        struct video_device *vdev = video_devdata(file);
 243
 244        if (vdev->vfl_type == VFL_TYPE_RADIO)
 245                return vivid_radio_rx_enum_freq_bands(file, fh, band);
 246        if (vdev->vfl_type == VFL_TYPE_SDR)
 247                return vivid_sdr_enum_freq_bands(file, fh, band);
 248        return -ENOTTY;
 249}
 250
 251static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
 252{
 253        struct video_device *vdev = video_devdata(file);
 254
 255        if (vdev->vfl_type == VFL_TYPE_RADIO)
 256                return vivid_radio_rx_g_tuner(file, fh, vt);
 257        if (vdev->vfl_type == VFL_TYPE_SDR)
 258                return vivid_sdr_g_tuner(file, fh, vt);
 259        return vivid_video_g_tuner(file, fh, vt);
 260}
 261
 262static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
 263{
 264        struct video_device *vdev = video_devdata(file);
 265
 266        if (vdev->vfl_type == VFL_TYPE_RADIO)
 267                return vivid_radio_rx_s_tuner(file, fh, vt);
 268        if (vdev->vfl_type == VFL_TYPE_SDR)
 269                return vivid_sdr_s_tuner(file, fh, vt);
 270        return vivid_video_s_tuner(file, fh, vt);
 271}
 272
 273static int vidioc_g_frequency(struct file *file, void *fh, 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_g_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_g_frequency(file, fh, vf);
 284        return vivid_video_g_frequency(file, fh, vf);
 285}
 286
 287static int vidioc_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
 288{
 289        struct vivid_dev *dev = video_drvdata(file);
 290        struct video_device *vdev = video_devdata(file);
 291
 292        if (vdev->vfl_type == VFL_TYPE_RADIO)
 293                return vivid_radio_s_frequency(file,
 294                        vdev->vfl_dir == VFL_DIR_RX ?
 295                        &dev->radio_rx_freq : &dev->radio_tx_freq, vf);
 296        if (vdev->vfl_type == VFL_TYPE_SDR)
 297                return vivid_sdr_s_frequency(file, fh, vf);
 298        return vivid_video_s_frequency(file, fh, vf);
 299}
 300
 301static int vidioc_overlay(struct file *file, void *fh, unsigned i)
 302{
 303        struct video_device *vdev = video_devdata(file);
 304
 305        if (vdev->vfl_dir == VFL_DIR_RX)
 306                return vivid_vid_cap_overlay(file, fh, i);
 307        return vivid_vid_out_overlay(file, fh, i);
 308}
 309
 310static int vidioc_g_fbuf(struct file *file, void *fh, struct v4l2_framebuffer *a)
 311{
 312        struct video_device *vdev = video_devdata(file);
 313
 314        if (vdev->vfl_dir == VFL_DIR_RX)
 315                return vivid_vid_cap_g_fbuf(file, fh, a);
 316        return vivid_vid_out_g_fbuf(file, fh, a);
 317}
 318
 319static int vidioc_s_fbuf(struct file *file, void *fh, const struct v4l2_framebuffer *a)
 320{
 321        struct video_device *vdev = video_devdata(file);
 322
 323        if (vdev->vfl_dir == VFL_DIR_RX)
 324                return vivid_vid_cap_s_fbuf(file, fh, a);
 325        return vivid_vid_out_s_fbuf(file, fh, a);
 326}
 327
 328static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id)
 329{
 330        struct video_device *vdev = video_devdata(file);
 331
 332        if (vdev->vfl_dir == VFL_DIR_RX)
 333                return vivid_vid_cap_s_std(file, fh, id);
 334        return vivid_vid_out_s_std(file, fh, id);
 335}
 336
 337static int vidioc_s_dv_timings(struct file *file, void *fh, struct v4l2_dv_timings *timings)
 338{
 339        struct video_device *vdev = video_devdata(file);
 340
 341        if (vdev->vfl_dir == VFL_DIR_RX)
 342                return vivid_vid_cap_s_dv_timings(file, fh, timings);
 343        return vivid_vid_out_s_dv_timings(file, fh, timings);
 344}
 345
 346static int vidioc_cropcap(struct file *file, void *fh, struct v4l2_cropcap *cc)
 347{
 348        struct video_device *vdev = video_devdata(file);
 349
 350        if (vdev->vfl_dir == VFL_DIR_RX)
 351                return vivid_vid_cap_cropcap(file, fh, cc);
 352        return vivid_vid_out_cropcap(file, fh, cc);
 353}
 354
 355static int vidioc_g_selection(struct file *file, void *fh,
 356                              struct v4l2_selection *sel)
 357{
 358        struct video_device *vdev = video_devdata(file);
 359
 360        if (vdev->vfl_dir == VFL_DIR_RX)
 361                return vivid_vid_cap_g_selection(file, fh, sel);
 362        return vivid_vid_out_g_selection(file, fh, sel);
 363}
 364
 365static int vidioc_s_selection(struct file *file, void *fh,
 366                              struct v4l2_selection *sel)
 367{
 368        struct video_device *vdev = video_devdata(file);
 369
 370        if (vdev->vfl_dir == VFL_DIR_RX)
 371                return vivid_vid_cap_s_selection(file, fh, sel);
 372        return vivid_vid_out_s_selection(file, fh, sel);
 373}
 374
 375static int vidioc_g_parm(struct file *file, void *fh,
 376                          struct v4l2_streamparm *parm)
 377{
 378        struct video_device *vdev = video_devdata(file);
 379
 380        if (vdev->vfl_dir == VFL_DIR_RX)
 381                return vivid_vid_cap_g_parm(file, fh, parm);
 382        return vivid_vid_out_g_parm(file, fh, parm);
 383}
 384
 385static int vidioc_s_parm(struct file *file, void *fh,
 386                          struct v4l2_streamparm *parm)
 387{
 388        struct video_device *vdev = video_devdata(file);
 389
 390        if (vdev->vfl_dir == VFL_DIR_RX)
 391                return vivid_vid_cap_s_parm(file, fh, parm);
 392        return vivid_vid_out_g_parm(file, fh, parm);
 393}
 394
 395static int vidioc_log_status(struct file *file, void *fh)
 396{
 397        struct vivid_dev *dev = video_drvdata(file);
 398        struct video_device *vdev = video_devdata(file);
 399
 400        v4l2_ctrl_log_status(file, fh);
 401        if (vdev->vfl_dir == VFL_DIR_RX && vdev->vfl_type == VFL_TYPE_GRABBER)
 402                tpg_log_status(&dev->tpg);
 403        return 0;
 404}
 405
 406static ssize_t vivid_radio_read(struct file *file, char __user *buf,
 407                         size_t size, loff_t *offset)
 408{
 409        struct video_device *vdev = video_devdata(file);
 410
 411        if (vdev->vfl_dir == VFL_DIR_TX)
 412                return -EINVAL;
 413        return vivid_radio_rx_read(file, buf, size, offset);
 414}
 415
 416static ssize_t vivid_radio_write(struct file *file, const char __user *buf,
 417                          size_t size, loff_t *offset)
 418{
 419        struct video_device *vdev = video_devdata(file);
 420
 421        if (vdev->vfl_dir == VFL_DIR_RX)
 422                return -EINVAL;
 423        return vivid_radio_tx_write(file, buf, size, offset);
 424}
 425
 426static unsigned int vivid_radio_poll(struct file *file, struct poll_table_struct *wait)
 427{
 428        struct video_device *vdev = video_devdata(file);
 429
 430        if (vdev->vfl_dir == VFL_DIR_RX)
 431                return vivid_radio_rx_poll(file, wait);
 432        return vivid_radio_tx_poll(file, wait);
 433}
 434
 435static bool vivid_is_in_use(struct video_device *vdev)
 436{
 437        unsigned long flags;
 438        bool res;
 439
 440        spin_lock_irqsave(&vdev->fh_lock, flags);
 441        res = !list_empty(&vdev->fh_list);
 442        spin_unlock_irqrestore(&vdev->fh_lock, flags);
 443        return res;
 444}
 445
 446static bool vivid_is_last_user(struct vivid_dev *dev)
 447{
 448        unsigned uses = vivid_is_in_use(&dev->vid_cap_dev) +
 449                        vivid_is_in_use(&dev->vid_out_dev) +
 450                        vivid_is_in_use(&dev->vbi_cap_dev) +
 451                        vivid_is_in_use(&dev->vbi_out_dev) +
 452                        vivid_is_in_use(&dev->sdr_cap_dev) +
 453                        vivid_is_in_use(&dev->radio_rx_dev) +
 454                        vivid_is_in_use(&dev->radio_tx_dev);
 455
 456        return uses == 1;
 457}
 458
 459static int vivid_fop_release(struct file *file)
 460{
 461        struct vivid_dev *dev = video_drvdata(file);
 462        struct video_device *vdev = video_devdata(file);
 463
 464        mutex_lock(&dev->mutex);
 465        if (!no_error_inj && v4l2_fh_is_singular_file(file) &&
 466            !video_is_registered(vdev) && vivid_is_last_user(dev)) {
 467                /*
 468                 * I am the last user of this driver, and a disconnect
 469                 * was forced (since this video_device is unregistered),
 470                 * so re-register all video_device's again.
 471                 */
 472                v4l2_info(&dev->v4l2_dev, "reconnect\n");
 473                set_bit(V4L2_FL_REGISTERED, &dev->vid_cap_dev.flags);
 474                set_bit(V4L2_FL_REGISTERED, &dev->vid_out_dev.flags);
 475                set_bit(V4L2_FL_REGISTERED, &dev->vbi_cap_dev.flags);
 476                set_bit(V4L2_FL_REGISTERED, &dev->vbi_out_dev.flags);
 477                set_bit(V4L2_FL_REGISTERED, &dev->sdr_cap_dev.flags);
 478                set_bit(V4L2_FL_REGISTERED, &dev->radio_rx_dev.flags);
 479                set_bit(V4L2_FL_REGISTERED, &dev->radio_tx_dev.flags);
 480        }
 481        mutex_unlock(&dev->mutex);
 482        if (file->private_data == dev->overlay_cap_owner)
 483                dev->overlay_cap_owner = NULL;
 484        if (file->private_data == dev->radio_rx_rds_owner) {
 485                dev->radio_rx_rds_last_block = 0;
 486                dev->radio_rx_rds_owner = NULL;
 487        }
 488        if (file->private_data == dev->radio_tx_rds_owner) {
 489                dev->radio_tx_rds_last_block = 0;
 490                dev->radio_tx_rds_owner = NULL;
 491        }
 492        if (vdev->queue)
 493                return vb2_fop_release(file);
 494        return v4l2_fh_release(file);
 495}
 496
 497static const struct v4l2_file_operations vivid_fops = {
 498        .owner          = THIS_MODULE,
 499        .open           = v4l2_fh_open,
 500        .release        = vivid_fop_release,
 501        .read           = vb2_fop_read,
 502        .write          = vb2_fop_write,
 503        .poll           = vb2_fop_poll,
 504        .unlocked_ioctl = video_ioctl2,
 505        .mmap           = vb2_fop_mmap,
 506};
 507
 508static const struct v4l2_file_operations vivid_radio_fops = {
 509        .owner          = THIS_MODULE,
 510        .open           = v4l2_fh_open,
 511        .release        = vivid_fop_release,
 512        .read           = vivid_radio_read,
 513        .write          = vivid_radio_write,
 514        .poll           = vivid_radio_poll,
 515        .unlocked_ioctl = video_ioctl2,
 516};
 517
 518static const struct v4l2_ioctl_ops vivid_ioctl_ops = {
 519        .vidioc_querycap                = vidioc_querycap,
 520
 521        .vidioc_enum_fmt_vid_cap        = vidioc_enum_fmt_vid,
 522        .vidioc_g_fmt_vid_cap           = vidioc_g_fmt_vid_cap,
 523        .vidioc_try_fmt_vid_cap         = vidioc_try_fmt_vid_cap,
 524        .vidioc_s_fmt_vid_cap           = vidioc_s_fmt_vid_cap,
 525        .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_mplane,
 526        .vidioc_g_fmt_vid_cap_mplane    = vidioc_g_fmt_vid_cap_mplane,
 527        .vidioc_try_fmt_vid_cap_mplane  = vidioc_try_fmt_vid_cap_mplane,
 528        .vidioc_s_fmt_vid_cap_mplane    = vidioc_s_fmt_vid_cap_mplane,
 529
 530        .vidioc_enum_fmt_vid_out        = vidioc_enum_fmt_vid,
 531        .vidioc_g_fmt_vid_out           = vidioc_g_fmt_vid_out,
 532        .vidioc_try_fmt_vid_out         = vidioc_try_fmt_vid_out,
 533        .vidioc_s_fmt_vid_out           = vidioc_s_fmt_vid_out,
 534        .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_mplane,
 535        .vidioc_g_fmt_vid_out_mplane    = vidioc_g_fmt_vid_out_mplane,
 536        .vidioc_try_fmt_vid_out_mplane  = vidioc_try_fmt_vid_out_mplane,
 537        .vidioc_s_fmt_vid_out_mplane    = vidioc_s_fmt_vid_out_mplane,
 538
 539        .vidioc_g_selection             = vidioc_g_selection,
 540        .vidioc_s_selection             = vidioc_s_selection,
 541        .vidioc_cropcap                 = vidioc_cropcap,
 542
 543        .vidioc_g_fmt_vbi_cap           = vidioc_g_fmt_vbi_cap,
 544        .vidioc_try_fmt_vbi_cap         = vidioc_g_fmt_vbi_cap,
 545        .vidioc_s_fmt_vbi_cap           = vidioc_s_fmt_vbi_cap,
 546
 547        .vidioc_g_fmt_sliced_vbi_cap    = vidioc_g_fmt_sliced_vbi_cap,
 548        .vidioc_try_fmt_sliced_vbi_cap  = vidioc_try_fmt_sliced_vbi_cap,
 549        .vidioc_s_fmt_sliced_vbi_cap    = vidioc_s_fmt_sliced_vbi_cap,
 550        .vidioc_g_sliced_vbi_cap        = vidioc_g_sliced_vbi_cap,
 551
 552        .vidioc_g_fmt_vbi_out           = vidioc_g_fmt_vbi_out,
 553        .vidioc_try_fmt_vbi_out         = vidioc_g_fmt_vbi_out,
 554        .vidioc_s_fmt_vbi_out           = vidioc_s_fmt_vbi_out,
 555
 556        .vidioc_g_fmt_sliced_vbi_out    = vidioc_g_fmt_sliced_vbi_out,
 557        .vidioc_try_fmt_sliced_vbi_out  = vidioc_try_fmt_sliced_vbi_out,
 558        .vidioc_s_fmt_sliced_vbi_out    = vidioc_s_fmt_sliced_vbi_out,
 559
 560        .vidioc_enum_fmt_sdr_cap        = vidioc_enum_fmt_sdr_cap,
 561        .vidioc_g_fmt_sdr_cap           = vidioc_g_fmt_sdr_cap,
 562        .vidioc_try_fmt_sdr_cap         = vidioc_try_fmt_sdr_cap,
 563        .vidioc_s_fmt_sdr_cap           = vidioc_s_fmt_sdr_cap,
 564
 565        .vidioc_overlay                 = vidioc_overlay,
 566        .vidioc_enum_framesizes         = vidioc_enum_framesizes,
 567        .vidioc_enum_frameintervals     = vidioc_enum_frameintervals,
 568        .vidioc_g_parm                  = vidioc_g_parm,
 569        .vidioc_s_parm                  = vidioc_s_parm,
 570
 571        .vidioc_enum_fmt_vid_overlay    = vidioc_enum_fmt_vid_overlay,
 572        .vidioc_g_fmt_vid_overlay       = vidioc_g_fmt_vid_overlay,
 573        .vidioc_try_fmt_vid_overlay     = vidioc_try_fmt_vid_overlay,
 574        .vidioc_s_fmt_vid_overlay       = vidioc_s_fmt_vid_overlay,
 575        .vidioc_g_fmt_vid_out_overlay   = vidioc_g_fmt_vid_out_overlay,
 576        .vidioc_try_fmt_vid_out_overlay = vidioc_try_fmt_vid_out_overlay,
 577        .vidioc_s_fmt_vid_out_overlay   = vidioc_s_fmt_vid_out_overlay,
 578        .vidioc_g_fbuf                  = vidioc_g_fbuf,
 579        .vidioc_s_fbuf                  = vidioc_s_fbuf,
 580
 581        .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
 582        .vidioc_create_bufs             = vb2_ioctl_create_bufs,
 583        .vidioc_prepare_buf             = vb2_ioctl_prepare_buf,
 584        .vidioc_querybuf                = vb2_ioctl_querybuf,
 585        .vidioc_qbuf                    = vb2_ioctl_qbuf,
 586        .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
 587        .vidioc_expbuf                  = vb2_ioctl_expbuf,
 588        .vidioc_streamon                = vb2_ioctl_streamon,
 589        .vidioc_streamoff               = vb2_ioctl_streamoff,
 590
 591        .vidioc_enum_input              = vidioc_enum_input,
 592        .vidioc_g_input                 = vidioc_g_input,
 593        .vidioc_s_input                 = vidioc_s_input,
 594        .vidioc_s_audio                 = vidioc_s_audio,
 595        .vidioc_g_audio                 = vidioc_g_audio,
 596        .vidioc_enumaudio               = vidioc_enumaudio,
 597        .vidioc_s_frequency             = vidioc_s_frequency,
 598        .vidioc_g_frequency             = vidioc_g_frequency,
 599        .vidioc_s_tuner                 = vidioc_s_tuner,
 600        .vidioc_g_tuner                 = vidioc_g_tuner,
 601        .vidioc_s_modulator             = vidioc_s_modulator,
 602        .vidioc_g_modulator             = vidioc_g_modulator,
 603        .vidioc_s_hw_freq_seek          = vidioc_s_hw_freq_seek,
 604        .vidioc_enum_freq_bands         = vidioc_enum_freq_bands,
 605
 606        .vidioc_enum_output             = vidioc_enum_output,
 607        .vidioc_g_output                = vidioc_g_output,
 608        .vidioc_s_output                = vidioc_s_output,
 609        .vidioc_s_audout                = vidioc_s_audout,
 610        .vidioc_g_audout                = vidioc_g_audout,
 611        .vidioc_enumaudout              = vidioc_enumaudout,
 612
 613        .vidioc_querystd                = vidioc_querystd,
 614        .vidioc_g_std                   = vidioc_g_std,
 615        .vidioc_s_std                   = vidioc_s_std,
 616        .vidioc_s_dv_timings            = vidioc_s_dv_timings,
 617        .vidioc_g_dv_timings            = vidioc_g_dv_timings,
 618        .vidioc_query_dv_timings        = vidioc_query_dv_timings,
 619        .vidioc_enum_dv_timings         = vidioc_enum_dv_timings,
 620        .vidioc_dv_timings_cap          = vidioc_dv_timings_cap,
 621        .vidioc_g_edid                  = vidioc_g_edid,
 622        .vidioc_s_edid                  = vidioc_s_edid,
 623
 624        .vidioc_log_status              = vidioc_log_status,
 625        .vidioc_subscribe_event         = vidioc_subscribe_event,
 626        .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
 627};
 628
 629/* -----------------------------------------------------------------
 630        Initialization and module stuff
 631   ------------------------------------------------------------------*/
 632
 633static void vivid_dev_release(struct v4l2_device *v4l2_dev)
 634{
 635        struct vivid_dev *dev = container_of(v4l2_dev, struct vivid_dev, v4l2_dev);
 636
 637        vivid_free_controls(dev);
 638        v4l2_device_unregister(&dev->v4l2_dev);
 639        vfree(dev->scaled_line);
 640        vfree(dev->blended_line);
 641        vfree(dev->edid);
 642        vfree(dev->bitmap_cap);
 643        vfree(dev->bitmap_out);
 644        tpg_free(&dev->tpg);
 645        kfree(dev->query_dv_timings_qmenu);
 646        kfree(dev);
 647}
 648
 649static int vivid_create_instance(struct platform_device *pdev, int inst)
 650{
 651        static const struct v4l2_dv_timings def_dv_timings =
 652                                        V4L2_DV_BT_CEA_1280X720P60;
 653        unsigned in_type_counter[4] = { 0, 0, 0, 0 };
 654        unsigned out_type_counter[4] = { 0, 0, 0, 0 };
 655        int ccs_cap = ccs_cap_mode[inst];
 656        int ccs_out = ccs_out_mode[inst];
 657        bool has_tuner;
 658        bool has_modulator;
 659        struct vivid_dev *dev;
 660        struct video_device *vfd;
 661        struct vb2_queue *q;
 662        unsigned node_type = node_types[inst];
 663        v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0;
 664        int ret;
 665        int i;
 666
 667        /* allocate main vivid state structure */
 668        dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 669        if (!dev)
 670                return -ENOMEM;
 671
 672        dev->inst = inst;
 673
 674        /* register v4l2_device */
 675        snprintf(dev->v4l2_dev.name, sizeof(dev->v4l2_dev.name),
 676                        "%s-%03d", VIVID_MODULE_NAME, inst);
 677        ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
 678        if (ret) {
 679                kfree(dev);
 680                return ret;
 681        }
 682        dev->v4l2_dev.release = vivid_dev_release;
 683
 684        /* start detecting feature set */
 685
 686        /* do we use single- or multi-planar? */
 687        dev->multiplanar = multiplanar[inst] > 1;
 688        v4l2_info(&dev->v4l2_dev, "using %splanar format API\n",
 689                        dev->multiplanar ? "multi" : "single ");
 690
 691        /* how many inputs do we have and of what type? */
 692        dev->num_inputs = num_inputs[inst];
 693        if (dev->num_inputs < 1)
 694                dev->num_inputs = 1;
 695        if (dev->num_inputs >= MAX_INPUTS)
 696                dev->num_inputs = MAX_INPUTS;
 697        for (i = 0; i < dev->num_inputs; i++) {
 698                dev->input_type[i] = (input_types[inst] >> (i * 2)) & 0x3;
 699                dev->input_name_counter[i] = in_type_counter[dev->input_type[i]]++;
 700        }
 701        dev->has_audio_inputs = in_type_counter[TV] && in_type_counter[SVID];
 702
 703        /* how many outputs do we have and of what type? */
 704        dev->num_outputs = num_outputs[inst];
 705        if (dev->num_outputs < 1)
 706                dev->num_outputs = 1;
 707        if (dev->num_outputs >= MAX_OUTPUTS)
 708                dev->num_outputs = MAX_OUTPUTS;
 709        for (i = 0; i < dev->num_outputs; i++) {
 710                dev->output_type[i] = ((output_types[inst] >> i) & 1) ? HDMI : SVID;
 711                dev->output_name_counter[i] = out_type_counter[dev->output_type[i]]++;
 712        }
 713        dev->has_audio_outputs = out_type_counter[SVID];
 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        /* initialize the test pattern generator */
 843        tpg_init(&dev->tpg, 640, 360);
 844        if (tpg_alloc(&dev->tpg, MAX_ZOOM * MAX_WIDTH))
 845                goto free_dev;
 846        dev->scaled_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
 847        if (!dev->scaled_line)
 848                goto free_dev;
 849        dev->blended_line = vzalloc(MAX_ZOOM * MAX_WIDTH);
 850        if (!dev->blended_line)
 851                goto free_dev;
 852
 853        /* load the edid */
 854        dev->edid = vmalloc(256 * 128);
 855        if (!dev->edid)
 856                goto free_dev;
 857
 858        /* create a string array containing the names of all the preset timings */
 859        while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width)
 860                dev->query_dv_timings_size++;
 861        dev->query_dv_timings_qmenu = kmalloc(dev->query_dv_timings_size *
 862                                           (sizeof(void *) + 32), GFP_KERNEL);
 863        if (dev->query_dv_timings_qmenu == NULL)
 864                goto free_dev;
 865        for (i = 0; i < dev->query_dv_timings_size; i++) {
 866                const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt;
 867                char *p = (char *)&dev->query_dv_timings_qmenu[dev->query_dv_timings_size];
 868                u32 htot, vtot;
 869
 870                p += i * 32;
 871                dev->query_dv_timings_qmenu[i] = p;
 872
 873                htot = V4L2_DV_BT_FRAME_WIDTH(bt);
 874                vtot = V4L2_DV_BT_FRAME_HEIGHT(bt);
 875                snprintf(p, 32, "%ux%u%s%u",
 876                        bt->width, bt->height, bt->interlaced ? "i" : "p",
 877                        (u32)bt->pixelclock / (htot * vtot));
 878        }
 879
 880        /* disable invalid ioctls based on the feature set */
 881        if (!dev->has_audio_inputs) {
 882                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_AUDIO);
 883                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_AUDIO);
 884                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMAUDIO);
 885                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_AUDIO);
 886                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_AUDIO);
 887                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_ENUMAUDIO);
 888        }
 889        if (!dev->has_audio_outputs) {
 890                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_AUDOUT);
 891                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_AUDOUT);
 892                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMAUDOUT);
 893                v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_AUDOUT);
 894                v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_AUDOUT);
 895                v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_ENUMAUDOUT);
 896        }
 897        if (!in_type_counter[TV] && !in_type_counter[SVID]) {
 898                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_STD);
 899                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_STD);
 900                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUMSTD);
 901                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERYSTD);
 902        }
 903        if (!out_type_counter[SVID]) {
 904                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_STD);
 905                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_STD);
 906                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUMSTD);
 907        }
 908        if (!has_tuner && !has_modulator) {
 909                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_FREQUENCY);
 910                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_FREQUENCY);
 911                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_FREQUENCY);
 912                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_FREQUENCY);
 913        }
 914        if (!has_tuner) {
 915                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_TUNER);
 916                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_TUNER);
 917                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_TUNER);
 918                v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_G_TUNER);
 919        }
 920        if (in_type_counter[HDMI] == 0) {
 921                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_EDID);
 922                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_EDID);
 923                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_DV_TIMINGS_CAP);
 924                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_G_DV_TIMINGS);
 925                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_DV_TIMINGS);
 926                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_ENUM_DV_TIMINGS);
 927                v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_QUERY_DV_TIMINGS);
 928        }
 929        if (out_type_counter[HDMI] == 0) {
 930                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_EDID);
 931                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_DV_TIMINGS_CAP);
 932                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_DV_TIMINGS);
 933                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_DV_TIMINGS);
 934                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_DV_TIMINGS);
 935        }
 936        if (!dev->has_fb) {
 937                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FBUF);
 938                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FBUF);
 939                v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_OVERLAY);
 940        }
 941        v4l2_disable_ioctl(&dev->vid_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
 942        v4l2_disable_ioctl(&dev->vbi_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
 943        v4l2_disable_ioctl(&dev->sdr_cap_dev, VIDIOC_S_HW_FREQ_SEEK);
 944        v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_S_FREQUENCY);
 945        v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_G_FREQUENCY);
 946        v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMESIZES);
 947        v4l2_disable_ioctl(&dev->vid_out_dev, VIDIOC_ENUM_FRAMEINTERVALS);
 948        v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_S_FREQUENCY);
 949        v4l2_disable_ioctl(&dev->vbi_out_dev, VIDIOC_G_FREQUENCY);
 950
 951        /* configure internal data */
 952        dev->fmt_cap = &vivid_formats[0];
 953        dev->fmt_out = &vivid_formats[0];
 954        if (!dev->multiplanar)
 955                vivid_formats[0].data_offset[0] = 0;
 956        dev->webcam_size_idx = 1;
 957        dev->webcam_ival_idx = 3;
 958        tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
 959        dev->std_cap = V4L2_STD_PAL;
 960        dev->std_out = V4L2_STD_PAL;
 961        if (dev->input_type[0] == TV || dev->input_type[0] == SVID)
 962                tvnorms_cap = V4L2_STD_ALL;
 963        if (dev->output_type[0] == SVID)
 964                tvnorms_out = V4L2_STD_ALL;
 965        dev->dv_timings_cap = def_dv_timings;
 966        dev->dv_timings_out = def_dv_timings;
 967        dev->tv_freq = 2804 /* 175.25 * 16 */;
 968        dev->tv_audmode = V4L2_TUNER_MODE_STEREO;
 969        dev->tv_field_cap = V4L2_FIELD_INTERLACED;
 970        dev->tv_field_out = V4L2_FIELD_INTERLACED;
 971        dev->radio_rx_freq = 95000 * 16;
 972        dev->radio_rx_audmode = V4L2_TUNER_MODE_STEREO;
 973        if (dev->has_radio_tx) {
 974                dev->radio_tx_freq = 95500 * 16;
 975                dev->radio_rds_loop = false;
 976        }
 977        dev->radio_tx_subchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_RDS;
 978        dev->sdr_adc_freq = 300000;
 979        dev->sdr_fm_freq = 50000000;
 980        dev->sdr_pixelformat = V4L2_SDR_FMT_CU8;
 981        dev->sdr_buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
 982
 983        dev->edid_max_blocks = dev->edid_blocks = 2;
 984        memcpy(dev->edid, vivid_hdmi_edid, sizeof(vivid_hdmi_edid));
 985        ktime_get_ts(&dev->radio_rds_init_ts);
 986
 987        /* create all controls */
 988        ret = vivid_create_controls(dev, ccs_cap == -1, ccs_out == -1, no_error_inj,
 989                        in_type_counter[TV] || in_type_counter[SVID] ||
 990                        out_type_counter[SVID],
 991                        in_type_counter[HDMI] || out_type_counter[HDMI]);
 992        if (ret)
 993                goto unreg_dev;
 994
 995        /*
 996         * update the capture and output formats to do a proper initial
 997         * configuration.
 998         */
 999        vivid_update_format_cap(dev, false);
1000        vivid_update_format_out(dev);
1001
1002        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_cap);
1003        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vid_out);
1004        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_cap);
1005        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_vbi_out);
1006        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_rx);
1007        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_radio_tx);
1008        v4l2_ctrl_handler_setup(&dev->ctrl_hdl_sdr_cap);
1009
1010        /* initialize overlay */
1011        dev->fb_cap.fmt.width = dev->src_rect.width;
1012        dev->fb_cap.fmt.height = dev->src_rect.height;
1013        dev->fb_cap.fmt.pixelformat = dev->fmt_cap->fourcc;
1014        dev->fb_cap.fmt.bytesperline = dev->src_rect.width * tpg_g_twopixelsize(&dev->tpg, 0) / 2;
1015        dev->fb_cap.fmt.sizeimage = dev->src_rect.height * dev->fb_cap.fmt.bytesperline;
1016
1017        /* initialize locks */
1018        spin_lock_init(&dev->slock);
1019        mutex_init(&dev->mutex);
1020
1021        /* init dma queues */
1022        INIT_LIST_HEAD(&dev->vid_cap_active);
1023        INIT_LIST_HEAD(&dev->vid_out_active);
1024        INIT_LIST_HEAD(&dev->vbi_cap_active);
1025        INIT_LIST_HEAD(&dev->vbi_out_active);
1026        INIT_LIST_HEAD(&dev->sdr_cap_active);
1027
1028        /* start creating the vb2 queues */
1029        if (dev->has_vid_cap) {
1030                /* initialize vid_cap queue */
1031                q = &dev->vb_vid_cap_q;
1032                q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1033                        V4L2_BUF_TYPE_VIDEO_CAPTURE;
1034                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1035                q->drv_priv = dev;
1036                q->buf_struct_size = sizeof(struct vivid_buffer);
1037                q->ops = &vivid_vid_cap_qops;
1038                q->mem_ops = &vb2_vmalloc_memops;
1039                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1040                q->min_buffers_needed = 2;
1041                q->lock = &dev->mutex;
1042
1043                ret = vb2_queue_init(q);
1044                if (ret)
1045                        goto unreg_dev;
1046        }
1047
1048        if (dev->has_vid_out) {
1049                /* initialize vid_out queue */
1050                q = &dev->vb_vid_out_q;
1051                q->type = dev->multiplanar ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1052                        V4L2_BUF_TYPE_VIDEO_OUTPUT;
1053                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1054                q->drv_priv = dev;
1055                q->buf_struct_size = sizeof(struct vivid_buffer);
1056                q->ops = &vivid_vid_out_qops;
1057                q->mem_ops = &vb2_vmalloc_memops;
1058                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1059                q->min_buffers_needed = 2;
1060                q->lock = &dev->mutex;
1061
1062                ret = vb2_queue_init(q);
1063                if (ret)
1064                        goto unreg_dev;
1065        }
1066
1067        if (dev->has_vbi_cap) {
1068                /* initialize vbi_cap queue */
1069                q = &dev->vb_vbi_cap_q;
1070                q->type = dev->has_raw_vbi_cap ? V4L2_BUF_TYPE_VBI_CAPTURE :
1071                                              V4L2_BUF_TYPE_SLICED_VBI_CAPTURE;
1072                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1073                q->drv_priv = dev;
1074                q->buf_struct_size = sizeof(struct vivid_buffer);
1075                q->ops = &vivid_vbi_cap_qops;
1076                q->mem_ops = &vb2_vmalloc_memops;
1077                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1078                q->min_buffers_needed = 2;
1079                q->lock = &dev->mutex;
1080
1081                ret = vb2_queue_init(q);
1082                if (ret)
1083                        goto unreg_dev;
1084        }
1085
1086        if (dev->has_vbi_out) {
1087                /* initialize vbi_out queue */
1088                q = &dev->vb_vbi_out_q;
1089                q->type = dev->has_raw_vbi_out ? V4L2_BUF_TYPE_VBI_OUTPUT :
1090                                              V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
1091                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_WRITE;
1092                q->drv_priv = dev;
1093                q->buf_struct_size = sizeof(struct vivid_buffer);
1094                q->ops = &vivid_vbi_out_qops;
1095                q->mem_ops = &vb2_vmalloc_memops;
1096                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1097                q->min_buffers_needed = 2;
1098                q->lock = &dev->mutex;
1099
1100                ret = vb2_queue_init(q);
1101                if (ret)
1102                        goto unreg_dev;
1103        }
1104
1105        if (dev->has_sdr_cap) {
1106                /* initialize sdr_cap queue */
1107                q = &dev->vb_sdr_cap_q;
1108                q->type = V4L2_BUF_TYPE_SDR_CAPTURE;
1109                q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1110                q->drv_priv = dev;
1111                q->buf_struct_size = sizeof(struct vivid_buffer);
1112                q->ops = &vivid_sdr_cap_qops;
1113                q->mem_ops = &vb2_vmalloc_memops;
1114                q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1115                q->min_buffers_needed = 8;
1116                q->lock = &dev->mutex;
1117
1118                ret = vb2_queue_init(q);
1119                if (ret)
1120                        goto unreg_dev;
1121        }
1122
1123        if (dev->has_fb) {
1124                /* Create framebuffer for testing capture/output overlay */
1125                ret = vivid_fb_init(dev);
1126                if (ret)
1127                        goto unreg_dev;
1128                v4l2_info(&dev->v4l2_dev, "Framebuffer device registered as fb%d\n",
1129                                dev->fb_info.node);
1130        }
1131
1132        /* finally start creating the device nodes */
1133        if (dev->has_vid_cap) {
1134                vfd = &dev->vid_cap_dev;
1135                strlcpy(vfd->name, "vivid-vid-cap", sizeof(vfd->name));
1136                vfd->fops = &vivid_fops;
1137                vfd->ioctl_ops = &vivid_ioctl_ops;
1138                vfd->release = video_device_release_empty;
1139                vfd->v4l2_dev = &dev->v4l2_dev;
1140                vfd->queue = &dev->vb_vid_cap_q;
1141                vfd->tvnorms = tvnorms_cap;
1142
1143                /*
1144                 * Provide a mutex to v4l2 core. It will be used to protect
1145                 * all fops and v4l2 ioctls.
1146                 */
1147                vfd->lock = &dev->mutex;
1148                video_set_drvdata(vfd, dev);
1149
1150                ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_cap_nr[inst]);
1151                if (ret < 0)
1152                        goto unreg_dev;
1153                v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1154                                          video_device_node_name(vfd));
1155        }
1156
1157        if (dev->has_vid_out) {
1158                vfd = &dev->vid_out_dev;
1159                strlcpy(vfd->name, "vivid-vid-out", sizeof(vfd->name));
1160                vfd->vfl_dir = VFL_DIR_TX;
1161                vfd->fops = &vivid_fops;
1162                vfd->ioctl_ops = &vivid_ioctl_ops;
1163                vfd->release = video_device_release_empty;
1164                vfd->v4l2_dev = &dev->v4l2_dev;
1165                vfd->queue = &dev->vb_vid_out_q;
1166                vfd->tvnorms = tvnorms_out;
1167
1168                /*
1169                 * Provide a mutex to v4l2 core. It will be used to protect
1170                 * all fops and v4l2 ioctls.
1171                 */
1172                vfd->lock = &dev->mutex;
1173                video_set_drvdata(vfd, dev);
1174
1175                ret = video_register_device(vfd, VFL_TYPE_GRABBER, vid_out_nr[inst]);
1176                if (ret < 0)
1177                        goto unreg_dev;
1178                v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s\n",
1179                                          video_device_node_name(vfd));
1180        }
1181
1182        if (dev->has_vbi_cap) {
1183                vfd = &dev->vbi_cap_dev;
1184                strlcpy(vfd->name, "vivid-vbi-cap", sizeof(vfd->name));
1185                vfd->fops = &vivid_fops;
1186                vfd->ioctl_ops = &vivid_ioctl_ops;
1187                vfd->release = video_device_release_empty;
1188                vfd->v4l2_dev = &dev->v4l2_dev;
1189                vfd->queue = &dev->vb_vbi_cap_q;
1190                vfd->lock = &dev->mutex;
1191                vfd->tvnorms = tvnorms_cap;
1192                video_set_drvdata(vfd, dev);
1193
1194                ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_cap_nr[inst]);
1195                if (ret < 0)
1196                        goto unreg_dev;
1197                v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s, supports %s VBI\n",
1198                                          video_device_node_name(vfd),
1199                                          (dev->has_raw_vbi_cap && dev->has_sliced_vbi_cap) ?
1200                                          "raw and sliced" :
1201                                          (dev->has_raw_vbi_cap ? "raw" : "sliced"));
1202        }
1203
1204        if (dev->has_vbi_out) {
1205                vfd = &dev->vbi_out_dev;
1206                strlcpy(vfd->name, "vivid-vbi-out", sizeof(vfd->name));
1207                vfd->vfl_dir = VFL_DIR_TX;
1208                vfd->fops = &vivid_fops;
1209                vfd->ioctl_ops = &vivid_ioctl_ops;
1210                vfd->release = video_device_release_empty;
1211                vfd->v4l2_dev = &dev->v4l2_dev;
1212                vfd->queue = &dev->vb_vbi_out_q;
1213                vfd->lock = &dev->mutex;
1214                vfd->tvnorms = tvnorms_out;
1215                video_set_drvdata(vfd, dev);
1216
1217                ret = video_register_device(vfd, VFL_TYPE_VBI, vbi_out_nr[inst]);
1218                if (ret < 0)
1219                        goto unreg_dev;
1220                v4l2_info(&dev->v4l2_dev, "V4L2 output device registered as %s, supports %s VBI\n",
1221                                          video_device_node_name(vfd),
1222                                          (dev->has_raw_vbi_out && dev->has_sliced_vbi_out) ?
1223                                          "raw and sliced" :
1224                                          (dev->has_raw_vbi_out ? "raw" : "sliced"));
1225        }
1226
1227        if (dev->has_sdr_cap) {
1228                vfd = &dev->sdr_cap_dev;
1229                strlcpy(vfd->name, "vivid-sdr-cap", sizeof(vfd->name));
1230                vfd->fops = &vivid_fops;
1231                vfd->ioctl_ops = &vivid_ioctl_ops;
1232                vfd->release = video_device_release_empty;
1233                vfd->v4l2_dev = &dev->v4l2_dev;
1234                vfd->queue = &dev->vb_sdr_cap_q;
1235                vfd->lock = &dev->mutex;
1236                video_set_drvdata(vfd, dev);
1237
1238                ret = video_register_device(vfd, VFL_TYPE_SDR, sdr_cap_nr[inst]);
1239                if (ret < 0)
1240                        goto unreg_dev;
1241                v4l2_info(&dev->v4l2_dev, "V4L2 capture device registered as %s\n",
1242                                          video_device_node_name(vfd));
1243        }
1244
1245        if (dev->has_radio_rx) {
1246                vfd = &dev->radio_rx_dev;
1247                strlcpy(vfd->name, "vivid-rad-rx", sizeof(vfd->name));
1248                vfd->fops = &vivid_radio_fops;
1249                vfd->ioctl_ops = &vivid_ioctl_ops;
1250                vfd->release = video_device_release_empty;
1251                vfd->v4l2_dev = &dev->v4l2_dev;
1252                vfd->lock = &dev->mutex;
1253                video_set_drvdata(vfd, dev);
1254
1255                ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_rx_nr[inst]);
1256                if (ret < 0)
1257                        goto unreg_dev;
1258                v4l2_info(&dev->v4l2_dev, "V4L2 receiver device registered as %s\n",
1259                                          video_device_node_name(vfd));
1260        }
1261
1262        if (dev->has_radio_tx) {
1263                vfd = &dev->radio_tx_dev;
1264                strlcpy(vfd->name, "vivid-rad-tx", sizeof(vfd->name));
1265                vfd->vfl_dir = VFL_DIR_TX;
1266                vfd->fops = &vivid_radio_fops;
1267                vfd->ioctl_ops = &vivid_ioctl_ops;
1268                vfd->release = video_device_release_empty;
1269                vfd->v4l2_dev = &dev->v4l2_dev;
1270                vfd->lock = &dev->mutex;
1271                video_set_drvdata(vfd, dev);
1272
1273                ret = video_register_device(vfd, VFL_TYPE_RADIO, radio_tx_nr[inst]);
1274                if (ret < 0)
1275                        goto unreg_dev;
1276                v4l2_info(&dev->v4l2_dev, "V4L2 transmitter device registered as %s\n",
1277                                          video_device_node_name(vfd));
1278        }
1279
1280        /* Now that everything is fine, let's add it to device list */
1281        vivid_devs[inst] = dev;
1282
1283        return 0;
1284
1285unreg_dev:
1286        video_unregister_device(&dev->radio_tx_dev);
1287        video_unregister_device(&dev->radio_rx_dev);
1288        video_unregister_device(&dev->sdr_cap_dev);
1289        video_unregister_device(&dev->vbi_out_dev);
1290        video_unregister_device(&dev->vbi_cap_dev);
1291        video_unregister_device(&dev->vid_out_dev);
1292        video_unregister_device(&dev->vid_cap_dev);
1293free_dev:
1294        v4l2_device_put(&dev->v4l2_dev);
1295        return ret;
1296}
1297
1298/* This routine allocates from 1 to n_devs virtual drivers.
1299
1300   The real maximum number of virtual drivers will depend on how many drivers
1301   will succeed. This is limited to the maximum number of devices that
1302   videodev supports, which is equal to VIDEO_NUM_DEVICES.
1303 */
1304static int vivid_probe(struct platform_device *pdev)
1305{
1306        const struct font_desc *font = find_font("VGA8x16");
1307        int ret = 0, i;
1308
1309        if (font == NULL) {
1310                pr_err("vivid: could not find font\n");
1311                return -ENODEV;
1312        }
1313
1314        tpg_set_font(font->data);
1315
1316        n_devs = clamp_t(unsigned, n_devs, 1, VIVID_MAX_DEVS);
1317
1318        for (i = 0; i < n_devs; i++) {
1319                ret = vivid_create_instance(pdev, i);
1320                if (ret) {
1321                        /* If some instantiations succeeded, keep driver */
1322                        if (i)
1323                                ret = 0;
1324                        break;
1325                }
1326        }
1327
1328        if (ret < 0) {
1329                pr_err("vivid: error %d while loading driver\n", ret);
1330                return ret;
1331        }
1332
1333        /* n_devs will reflect the actual number of allocated devices */
1334        n_devs = i;
1335
1336        return ret;
1337}
1338
1339static int vivid_remove(struct platform_device *pdev)
1340{
1341        struct vivid_dev *dev;
1342        unsigned i;
1343
1344
1345        for (i = 0; i < n_devs; i++) {
1346                dev = vivid_devs[i];
1347                if (!dev)
1348                        continue;
1349
1350                if (dev->has_vid_cap) {
1351                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1352                                video_device_node_name(&dev->vid_cap_dev));
1353                        video_unregister_device(&dev->vid_cap_dev);
1354                }
1355                if (dev->has_vid_out) {
1356                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1357                                video_device_node_name(&dev->vid_out_dev));
1358                        video_unregister_device(&dev->vid_out_dev);
1359                }
1360                if (dev->has_vbi_cap) {
1361                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1362                                video_device_node_name(&dev->vbi_cap_dev));
1363                        video_unregister_device(&dev->vbi_cap_dev);
1364                }
1365                if (dev->has_vbi_out) {
1366                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1367                                video_device_node_name(&dev->vbi_out_dev));
1368                        video_unregister_device(&dev->vbi_out_dev);
1369                }
1370                if (dev->has_sdr_cap) {
1371                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1372                                video_device_node_name(&dev->sdr_cap_dev));
1373                        video_unregister_device(&dev->sdr_cap_dev);
1374                }
1375                if (dev->has_radio_rx) {
1376                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1377                                video_device_node_name(&dev->radio_rx_dev));
1378                        video_unregister_device(&dev->radio_rx_dev);
1379                }
1380                if (dev->has_radio_tx) {
1381                        v4l2_info(&dev->v4l2_dev, "unregistering %s\n",
1382                                video_device_node_name(&dev->radio_tx_dev));
1383                        video_unregister_device(&dev->radio_tx_dev);
1384                }
1385                if (dev->has_fb) {
1386                        v4l2_info(&dev->v4l2_dev, "unregistering fb%d\n",
1387                                dev->fb_info.node);
1388                        unregister_framebuffer(&dev->fb_info);
1389                        vivid_fb_release_buffers(dev);
1390                }
1391                v4l2_device_put(&dev->v4l2_dev);
1392                vivid_devs[i] = NULL;
1393        }
1394        return 0;
1395}
1396
1397static void vivid_pdev_release(struct device *dev)
1398{
1399}
1400
1401static struct platform_device vivid_pdev = {
1402        .name           = "vivid",
1403        .dev.release    = vivid_pdev_release,
1404};
1405
1406static struct platform_driver vivid_pdrv = {
1407        .probe          = vivid_probe,
1408        .remove         = vivid_remove,
1409        .driver         = {
1410                .name   = "vivid",
1411        },
1412};
1413
1414static int __init vivid_init(void)
1415{
1416        int ret;
1417
1418        ret = platform_device_register(&vivid_pdev);
1419        if (ret)
1420                return ret;
1421
1422        ret = platform_driver_register(&vivid_pdrv);
1423        if (ret)
1424                platform_device_unregister(&vivid_pdev);
1425
1426        return ret;
1427}
1428
1429static void __exit vivid_exit(void)
1430{
1431        platform_driver_unregister(&vivid_pdrv);
1432        platform_device_unregister(&vivid_pdev);
1433}
1434
1435module_init(vivid_init);
1436module_exit(vivid_exit);
1437