xref: /openbmc/linux/drivers/media/test-drivers/vivid/vivid-radio-tx.c (revision 4b4193256c8d3bc3a5397b5cd9494c2ad386317d)
1*dacca5f0SHans Verkuil // SPDX-License-Identifier: GPL-2.0-only
2*dacca5f0SHans Verkuil /*
3*dacca5f0SHans Verkuil  * vivid-radio-tx.c - radio transmitter support functions.
4*dacca5f0SHans Verkuil  *
5*dacca5f0SHans Verkuil  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6*dacca5f0SHans Verkuil  */
7*dacca5f0SHans Verkuil 
8*dacca5f0SHans Verkuil #include <linux/errno.h>
9*dacca5f0SHans Verkuil #include <linux/kernel.h>
10*dacca5f0SHans Verkuil #include <linux/sched/signal.h>
11*dacca5f0SHans Verkuil #include <linux/delay.h>
12*dacca5f0SHans Verkuil #include <linux/videodev2.h>
13*dacca5f0SHans Verkuil #include <linux/v4l2-dv-timings.h>
14*dacca5f0SHans Verkuil #include <media/v4l2-common.h>
15*dacca5f0SHans Verkuil #include <media/v4l2-event.h>
16*dacca5f0SHans Verkuil #include <media/v4l2-dv-timings.h>
17*dacca5f0SHans Verkuil 
18*dacca5f0SHans Verkuil #include "vivid-core.h"
19*dacca5f0SHans Verkuil #include "vivid-ctrls.h"
20*dacca5f0SHans Verkuil #include "vivid-radio-common.h"
21*dacca5f0SHans Verkuil #include "vivid-radio-tx.h"
22*dacca5f0SHans Verkuil 
vivid_radio_tx_write(struct file * file,const char __user * buf,size_t size,loff_t * offset)23*dacca5f0SHans Verkuil ssize_t vivid_radio_tx_write(struct file *file, const char __user *buf,
24*dacca5f0SHans Verkuil 			  size_t size, loff_t *offset)
25*dacca5f0SHans Verkuil {
26*dacca5f0SHans Verkuil 	struct vivid_dev *dev = video_drvdata(file);
27*dacca5f0SHans Verkuil 	struct v4l2_rds_data *data = dev->rds_gen.data;
28*dacca5f0SHans Verkuil 	ktime_t timestamp;
29*dacca5f0SHans Verkuil 	unsigned blk;
30*dacca5f0SHans Verkuil 	int i;
31*dacca5f0SHans Verkuil 
32*dacca5f0SHans Verkuil 	if (dev->radio_tx_rds_controls)
33*dacca5f0SHans Verkuil 		return -EINVAL;
34*dacca5f0SHans Verkuil 
35*dacca5f0SHans Verkuil 	if (size < sizeof(*data))
36*dacca5f0SHans Verkuil 		return -EINVAL;
37*dacca5f0SHans Verkuil 	size = sizeof(*data) * (size / sizeof(*data));
38*dacca5f0SHans Verkuil 
39*dacca5f0SHans Verkuil 	if (mutex_lock_interruptible(&dev->mutex))
40*dacca5f0SHans Verkuil 		return -ERESTARTSYS;
41*dacca5f0SHans Verkuil 	if (dev->radio_tx_rds_owner &&
42*dacca5f0SHans Verkuil 	    file->private_data != dev->radio_tx_rds_owner) {
43*dacca5f0SHans Verkuil 		mutex_unlock(&dev->mutex);
44*dacca5f0SHans Verkuil 		return -EBUSY;
45*dacca5f0SHans Verkuil 	}
46*dacca5f0SHans Verkuil 	dev->radio_tx_rds_owner = file->private_data;
47*dacca5f0SHans Verkuil 
48*dacca5f0SHans Verkuil retry:
49*dacca5f0SHans Verkuil 	timestamp = ktime_sub(ktime_get(), dev->radio_rds_init_time);
50*dacca5f0SHans Verkuil 	blk = ktime_divns(timestamp, VIVID_RDS_NSEC_PER_BLK);
51*dacca5f0SHans Verkuil 	if (blk - VIVID_RDS_GEN_BLOCKS >= dev->radio_tx_rds_last_block)
52*dacca5f0SHans Verkuil 		dev->radio_tx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1;
53*dacca5f0SHans Verkuil 
54*dacca5f0SHans Verkuil 	/*
55*dacca5f0SHans Verkuil 	 * No data is available if there hasn't been time to get new data,
56*dacca5f0SHans Verkuil 	 * or if the RDS receiver has been disabled, or if we use the data
57*dacca5f0SHans Verkuil 	 * from the RDS transmitter and that RDS transmitter has been disabled,
58*dacca5f0SHans Verkuil 	 * or if the signal quality is too weak.
59*dacca5f0SHans Verkuil 	 */
60*dacca5f0SHans Verkuil 	if (blk == dev->radio_tx_rds_last_block ||
61*dacca5f0SHans Verkuil 	    !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) {
62*dacca5f0SHans Verkuil 		mutex_unlock(&dev->mutex);
63*dacca5f0SHans Verkuil 		if (file->f_flags & O_NONBLOCK)
64*dacca5f0SHans Verkuil 			return -EWOULDBLOCK;
65*dacca5f0SHans Verkuil 		if (msleep_interruptible(20) && signal_pending(current))
66*dacca5f0SHans Verkuil 			return -EINTR;
67*dacca5f0SHans Verkuil 		if (mutex_lock_interruptible(&dev->mutex))
68*dacca5f0SHans Verkuil 			return -ERESTARTSYS;
69*dacca5f0SHans Verkuil 		goto retry;
70*dacca5f0SHans Verkuil 	}
71*dacca5f0SHans Verkuil 
72*dacca5f0SHans Verkuil 	for (i = 0; i < size && blk > dev->radio_tx_rds_last_block;
73*dacca5f0SHans Verkuil 			dev->radio_tx_rds_last_block++) {
74*dacca5f0SHans Verkuil 		unsigned data_blk = dev->radio_tx_rds_last_block % VIVID_RDS_GEN_BLOCKS;
75*dacca5f0SHans Verkuil 		struct v4l2_rds_data rds;
76*dacca5f0SHans Verkuil 
77*dacca5f0SHans Verkuil 		if (copy_from_user(&rds, buf + i, sizeof(rds))) {
78*dacca5f0SHans Verkuil 			i = -EFAULT;
79*dacca5f0SHans Verkuil 			break;
80*dacca5f0SHans Verkuil 		}
81*dacca5f0SHans Verkuil 		i += sizeof(rds);
82*dacca5f0SHans Verkuil 		if (!dev->radio_rds_loop)
83*dacca5f0SHans Verkuil 			continue;
84*dacca5f0SHans Verkuil 		if ((rds.block & V4L2_RDS_BLOCK_MSK) == V4L2_RDS_BLOCK_INVALID ||
85*dacca5f0SHans Verkuil 		    (rds.block & V4L2_RDS_BLOCK_ERROR))
86*dacca5f0SHans Verkuil 			continue;
87*dacca5f0SHans Verkuil 		rds.block &= V4L2_RDS_BLOCK_MSK;
88*dacca5f0SHans Verkuil 		data[data_blk] = rds;
89*dacca5f0SHans Verkuil 	}
90*dacca5f0SHans Verkuil 	mutex_unlock(&dev->mutex);
91*dacca5f0SHans Verkuil 	return i;
92*dacca5f0SHans Verkuil }
93*dacca5f0SHans Verkuil 
vivid_radio_tx_poll(struct file * file,struct poll_table_struct * wait)94*dacca5f0SHans Verkuil __poll_t vivid_radio_tx_poll(struct file *file, struct poll_table_struct *wait)
95*dacca5f0SHans Verkuil {
96*dacca5f0SHans Verkuil 	return EPOLLOUT | EPOLLWRNORM | v4l2_ctrl_poll(file, wait);
97*dacca5f0SHans Verkuil }
98*dacca5f0SHans Verkuil 
vidioc_g_modulator(struct file * file,void * fh,struct v4l2_modulator * a)99*dacca5f0SHans Verkuil int vidioc_g_modulator(struct file *file, void *fh, struct v4l2_modulator *a)
100*dacca5f0SHans Verkuil {
101*dacca5f0SHans Verkuil 	struct vivid_dev *dev = video_drvdata(file);
102*dacca5f0SHans Verkuil 
103*dacca5f0SHans Verkuil 	if (a->index > 0)
104*dacca5f0SHans Verkuil 		return -EINVAL;
105*dacca5f0SHans Verkuil 
106*dacca5f0SHans Verkuil 	strscpy(a->name, "AM/FM/SW Transmitter", sizeof(a->name));
107*dacca5f0SHans Verkuil 	a->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
108*dacca5f0SHans Verkuil 			V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS |
109*dacca5f0SHans Verkuil 			(dev->radio_tx_rds_controls ?
110*dacca5f0SHans Verkuil 				V4L2_TUNER_CAP_RDS_CONTROLS :
111*dacca5f0SHans Verkuil 				V4L2_TUNER_CAP_RDS_BLOCK_IO);
112*dacca5f0SHans Verkuil 	a->rangelow = AM_FREQ_RANGE_LOW;
113*dacca5f0SHans Verkuil 	a->rangehigh = FM_FREQ_RANGE_HIGH;
114*dacca5f0SHans Verkuil 	a->txsubchans = dev->radio_tx_subchans;
115*dacca5f0SHans Verkuil 	return 0;
116*dacca5f0SHans Verkuil }
117*dacca5f0SHans Verkuil 
vidioc_s_modulator(struct file * file,void * fh,const struct v4l2_modulator * a)118*dacca5f0SHans Verkuil int vidioc_s_modulator(struct file *file, void *fh, const struct v4l2_modulator *a)
119*dacca5f0SHans Verkuil {
120*dacca5f0SHans Verkuil 	struct vivid_dev *dev = video_drvdata(file);
121*dacca5f0SHans Verkuil 
122*dacca5f0SHans Verkuil 	if (a->index)
123*dacca5f0SHans Verkuil 		return -EINVAL;
124*dacca5f0SHans Verkuil 	if (a->txsubchans & ~0x13)
125*dacca5f0SHans Verkuil 		return -EINVAL;
126*dacca5f0SHans Verkuil 	dev->radio_tx_subchans = a->txsubchans;
127*dacca5f0SHans Verkuil 	return 0;
128*dacca5f0SHans Verkuil }
129