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