18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * vivid-radio-tx.c - radio transmitter support functions. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/errno.h> 98c2ecf20Sopenharmony_ci#include <linux/kernel.h> 108c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 118c2ecf20Sopenharmony_ci#include <linux/delay.h> 128c2ecf20Sopenharmony_ci#include <linux/videodev2.h> 138c2ecf20Sopenharmony_ci#include <linux/v4l2-dv-timings.h> 148c2ecf20Sopenharmony_ci#include <media/v4l2-common.h> 158c2ecf20Sopenharmony_ci#include <media/v4l2-event.h> 168c2ecf20Sopenharmony_ci#include <media/v4l2-dv-timings.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#include "vivid-core.h" 198c2ecf20Sopenharmony_ci#include "vivid-ctrls.h" 208c2ecf20Sopenharmony_ci#include "vivid-radio-common.h" 218c2ecf20Sopenharmony_ci#include "vivid-radio-tx.h" 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_cissize_t vivid_radio_tx_write(struct file *file, const char __user *buf, 248c2ecf20Sopenharmony_ci size_t size, loff_t *offset) 258c2ecf20Sopenharmony_ci{ 268c2ecf20Sopenharmony_ci struct vivid_dev *dev = video_drvdata(file); 278c2ecf20Sopenharmony_ci struct v4l2_rds_data *data = dev->rds_gen.data; 288c2ecf20Sopenharmony_ci ktime_t timestamp; 298c2ecf20Sopenharmony_ci unsigned blk; 308c2ecf20Sopenharmony_ci int i; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci if (dev->radio_tx_rds_controls) 338c2ecf20Sopenharmony_ci return -EINVAL; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci if (size < sizeof(*data)) 368c2ecf20Sopenharmony_ci return -EINVAL; 378c2ecf20Sopenharmony_ci size = sizeof(*data) * (size / sizeof(*data)); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci if (mutex_lock_interruptible(&dev->mutex)) 408c2ecf20Sopenharmony_ci return -ERESTARTSYS; 418c2ecf20Sopenharmony_ci if (dev->radio_tx_rds_owner && 428c2ecf20Sopenharmony_ci file->private_data != dev->radio_tx_rds_owner) { 438c2ecf20Sopenharmony_ci mutex_unlock(&dev->mutex); 448c2ecf20Sopenharmony_ci return -EBUSY; 458c2ecf20Sopenharmony_ci } 468c2ecf20Sopenharmony_ci dev->radio_tx_rds_owner = file->private_data; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ciretry: 498c2ecf20Sopenharmony_ci timestamp = ktime_sub(ktime_get(), dev->radio_rds_init_time); 508c2ecf20Sopenharmony_ci blk = ktime_divns(timestamp, VIVID_RDS_NSEC_PER_BLK); 518c2ecf20Sopenharmony_ci if (blk - VIVID_RDS_GEN_BLOCKS >= dev->radio_tx_rds_last_block) 528c2ecf20Sopenharmony_ci dev->radio_tx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci /* 558c2ecf20Sopenharmony_ci * No data is available if there hasn't been time to get new data, 568c2ecf20Sopenharmony_ci * or if the RDS receiver has been disabled, or if we use the data 578c2ecf20Sopenharmony_ci * from the RDS transmitter and that RDS transmitter has been disabled, 588c2ecf20Sopenharmony_ci * or if the signal quality is too weak. 598c2ecf20Sopenharmony_ci */ 608c2ecf20Sopenharmony_ci if (blk == dev->radio_tx_rds_last_block || 618c2ecf20Sopenharmony_ci !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) { 628c2ecf20Sopenharmony_ci mutex_unlock(&dev->mutex); 638c2ecf20Sopenharmony_ci if (file->f_flags & O_NONBLOCK) 648c2ecf20Sopenharmony_ci return -EWOULDBLOCK; 658c2ecf20Sopenharmony_ci if (msleep_interruptible(20) && signal_pending(current)) 668c2ecf20Sopenharmony_ci return -EINTR; 678c2ecf20Sopenharmony_ci if (mutex_lock_interruptible(&dev->mutex)) 688c2ecf20Sopenharmony_ci return -ERESTARTSYS; 698c2ecf20Sopenharmony_ci goto retry; 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci for (i = 0; i < size && blk > dev->radio_tx_rds_last_block; 738c2ecf20Sopenharmony_ci dev->radio_tx_rds_last_block++) { 748c2ecf20Sopenharmony_ci unsigned data_blk = dev->radio_tx_rds_last_block % VIVID_RDS_GEN_BLOCKS; 758c2ecf20Sopenharmony_ci struct v4l2_rds_data rds; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci if (copy_from_user(&rds, buf + i, sizeof(rds))) { 788c2ecf20Sopenharmony_ci i = -EFAULT; 798c2ecf20Sopenharmony_ci break; 808c2ecf20Sopenharmony_ci } 818c2ecf20Sopenharmony_ci i += sizeof(rds); 828c2ecf20Sopenharmony_ci if (!dev->radio_rds_loop) 838c2ecf20Sopenharmony_ci continue; 848c2ecf20Sopenharmony_ci if ((rds.block & V4L2_RDS_BLOCK_MSK) == V4L2_RDS_BLOCK_INVALID || 858c2ecf20Sopenharmony_ci (rds.block & V4L2_RDS_BLOCK_ERROR)) 868c2ecf20Sopenharmony_ci continue; 878c2ecf20Sopenharmony_ci rds.block &= V4L2_RDS_BLOCK_MSK; 888c2ecf20Sopenharmony_ci data[data_blk] = rds; 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci mutex_unlock(&dev->mutex); 918c2ecf20Sopenharmony_ci return i; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci__poll_t vivid_radio_tx_poll(struct file *file, struct poll_table_struct *wait) 958c2ecf20Sopenharmony_ci{ 968c2ecf20Sopenharmony_ci return EPOLLOUT | EPOLLWRNORM | v4l2_ctrl_poll(file, wait); 978c2ecf20Sopenharmony_ci} 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ciint vidioc_g_modulator(struct file *file, void *fh, struct v4l2_modulator *a) 1008c2ecf20Sopenharmony_ci{ 1018c2ecf20Sopenharmony_ci struct vivid_dev *dev = video_drvdata(file); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci if (a->index > 0) 1048c2ecf20Sopenharmony_ci return -EINVAL; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci strscpy(a->name, "AM/FM/SW Transmitter", sizeof(a->name)); 1078c2ecf20Sopenharmony_ci a->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO | 1088c2ecf20Sopenharmony_ci V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS | 1098c2ecf20Sopenharmony_ci (dev->radio_tx_rds_controls ? 1108c2ecf20Sopenharmony_ci V4L2_TUNER_CAP_RDS_CONTROLS : 1118c2ecf20Sopenharmony_ci V4L2_TUNER_CAP_RDS_BLOCK_IO); 1128c2ecf20Sopenharmony_ci a->rangelow = AM_FREQ_RANGE_LOW; 1138c2ecf20Sopenharmony_ci a->rangehigh = FM_FREQ_RANGE_HIGH; 1148c2ecf20Sopenharmony_ci a->txsubchans = dev->radio_tx_subchans; 1158c2ecf20Sopenharmony_ci return 0; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciint vidioc_s_modulator(struct file *file, void *fh, const struct v4l2_modulator *a) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct vivid_dev *dev = video_drvdata(file); 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci if (a->index) 1238c2ecf20Sopenharmony_ci return -EINVAL; 1248c2ecf20Sopenharmony_ci if (a->txsubchans & ~0x13) 1258c2ecf20Sopenharmony_ci return -EINVAL; 1268c2ecf20Sopenharmony_ci dev->radio_tx_subchans = a->txsubchans; 1278c2ecf20Sopenharmony_ci return 0; 1288c2ecf20Sopenharmony_ci} 129