^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * vivid-radio-rx.c - radio receiver support functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/videodev2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/v4l2-dv-timings.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <media/v4l2-common.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <media/v4l2-event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <media/v4l2-dv-timings.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include "vivid-core.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "vivid-ctrls.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "vivid-radio-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "vivid-rds-gen.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include "vivid-radio-rx.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ssize_t vivid_radio_rx_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) size_t size, loff_t *offset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct vivid_dev *dev = video_drvdata(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct v4l2_rds_data *data = dev->rds_gen.data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) bool use_alternates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) ktime_t timestamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned blk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int perc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (dev->radio_rx_rds_controls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (size < sizeof(*data))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) size = sizeof(*data) * (size / sizeof(*data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (mutex_lock_interruptible(&dev->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) if (dev->radio_rx_rds_owner &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) file->private_data != dev->radio_rx_rds_owner) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (dev->radio_rx_rds_owner == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) vivid_radio_rds_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) dev->radio_rx_rds_owner = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) timestamp = ktime_sub(ktime_get(), dev->radio_rds_init_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) blk = ktime_divns(timestamp, VIVID_RDS_NSEC_PER_BLK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) use_alternates = (blk % VIVID_RDS_GEN_BLOCKS) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (dev->radio_rx_rds_last_block == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) dev->radio_rx_rds_use_alternates != use_alternates) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dev->radio_rx_rds_use_alternates = use_alternates;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* Re-init the RDS generator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) vivid_radio_rds_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (blk >= dev->radio_rx_rds_last_block + VIVID_RDS_GEN_BLOCKS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dev->radio_rx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * No data is available if there hasn't been time to get new data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * or if the RDS receiver has been disabled, or if we use the data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * from the RDS transmitter and that RDS transmitter has been disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * or if the signal quality is too weak.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (blk == dev->radio_rx_rds_last_block || !dev->radio_rx_rds_enabled ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (dev->radio_rds_loop && !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) abs(dev->radio_rx_sig_qual) > 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (msleep_interruptible(20) && signal_pending(current))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (mutex_lock_interruptible(&dev->mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* abs(dev->radio_rx_sig_qual) <= 200, map that to a 0-50% range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) perc = abs(dev->radio_rx_sig_qual) / 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (i = 0; i < size && blk > dev->radio_rx_rds_last_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) dev->radio_rx_rds_last_block++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) unsigned data_blk = dev->radio_rx_rds_last_block % VIVID_RDS_GEN_BLOCKS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct v4l2_rds_data rds = data[data_blk];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (data_blk == 0 && dev->radio_rds_loop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) vivid_radio_rds_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (perc && prandom_u32_max(100) < perc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) switch (prandom_u32_max(4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) rds.block |= V4L2_RDS_BLOCK_CORRECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) rds.block |= V4L2_RDS_BLOCK_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) rds.block |= V4L2_RDS_BLOCK_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) rds.lsb = prandom_u32_max(256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) rds.msb = prandom_u32_max(256);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) case 3: /* Skip block altogether */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Must make sure at least one block is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * returned, otherwise the application
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * might think that end-of-file occurred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (copy_to_user(buf + i, &rds, sizeof(rds))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) i = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) i += sizeof(rds);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) mutex_unlock(&dev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) __poll_t vivid_radio_rx_poll(struct file *file, struct poll_table_struct *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return EPOLLIN | EPOLLRDNORM | v4l2_ctrl_poll(file, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) int vivid_radio_rx_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (band->tuner != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (band->index >= TOT_BANDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *band = vivid_radio_bands[band->index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int vivid_radio_rx_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct vivid_dev *dev = video_drvdata(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned low, high;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned spacing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned band;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (a->tuner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (a->wrap_around && dev->radio_rx_hw_seek_mode == VIVID_HW_SEEK_BOUNDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!a->wrap_around && dev->radio_rx_hw_seek_mode == VIVID_HW_SEEK_WRAP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) if (!a->rangelow ^ !a->rangehigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (file->f_flags & O_NONBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return -EWOULDBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (a->rangelow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) for (band = 0; band < TOT_BANDS; band++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (a->rangelow >= vivid_radio_bands[band].rangelow &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) a->rangehigh <= vivid_radio_bands[band].rangehigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (band == TOT_BANDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (!dev->radio_rx_hw_seek_prog_lim &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) (a->rangelow != vivid_radio_bands[band].rangelow ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) a->rangehigh != vivid_radio_bands[band].rangehigh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) low = a->rangelow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) high = a->rangehigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) for (band = 0; band < TOT_BANDS; band++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (dev->radio_rx_freq >= vivid_radio_bands[band].rangelow &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) dev->radio_rx_freq <= vivid_radio_bands[band].rangehigh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (band == TOT_BANDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) low = vivid_radio_bands[band].rangelow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) high = vivid_radio_bands[band].rangehigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) spacing = band == BAND_AM ? 1600 : 16000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) freq = clamp(dev->radio_rx_freq, low, high);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (a->seek_upward) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) freq = spacing * (freq / spacing) + spacing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (freq > high) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (!a->wrap_around)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) freq = spacing * (low / spacing) + spacing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (freq >= dev->radio_rx_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) freq = spacing * ((freq + spacing - 1) / spacing) - spacing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (freq < low) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!a->wrap_around)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) freq = spacing * ((high + spacing - 1) / spacing) - spacing;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (freq <= dev->radio_rx_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) int vivid_radio_rx_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) struct vivid_dev *dev = video_drvdata(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int delta = 800;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) int sig_qual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (vt->index > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) strscpy(vt->name, "AM/FM/SW Receiver", sizeof(vt->name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) vt->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) (dev->radio_rx_rds_controls ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) V4L2_TUNER_CAP_RDS_CONTROLS :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) V4L2_TUNER_CAP_RDS_BLOCK_IO) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) (dev->radio_rx_hw_seek_prog_lim ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) V4L2_TUNER_CAP_HWSEEK_PROG_LIM : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) switch (dev->radio_rx_hw_seek_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) case VIVID_HW_SEEK_BOUNDED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) vt->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) case VIVID_HW_SEEK_WRAP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) vt->capability |= V4L2_TUNER_CAP_HWSEEK_WRAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) case VIVID_HW_SEEK_BOTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) vt->capability |= V4L2_TUNER_CAP_HWSEEK_WRAP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) V4L2_TUNER_CAP_HWSEEK_BOUNDED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) vt->rangelow = AM_FREQ_RANGE_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) vt->rangehigh = FM_FREQ_RANGE_HIGH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) sig_qual = dev->radio_rx_sig_qual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) vt->signal = abs(sig_qual) > delta ? 0 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 0xffff - ((unsigned)abs(sig_qual) * 0xffff) / delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) vt->afc = sig_qual > delta ? 0 : sig_qual;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (abs(sig_qual) > delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) vt->rxsubchans = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) else if (dev->radio_rx_freq < FM_FREQ_RANGE_LOW || vt->signal < 0x8000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) vt->rxsubchans = V4L2_TUNER_SUB_MONO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) else if (dev->radio_rds_loop && !(dev->radio_tx_subchans & V4L2_TUNER_SUB_STEREO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) vt->rxsubchans = V4L2_TUNER_SUB_MONO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (dev->radio_rx_rds_enabled &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) (!dev->radio_rds_loop || (dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) dev->radio_rx_freq >= FM_FREQ_RANGE_LOW && vt->signal >= 0xc000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (dev->radio_rx_rds_controls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) vivid_radio_rds_init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) vt->audmode = dev->radio_rx_audmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int vivid_radio_rx_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) struct vivid_dev *dev = video_drvdata(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (vt->index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) dev->radio_rx_audmode = vt->audmode >= V4L2_TUNER_MODE_STEREO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }