• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * vivid-radio-tx.c - radio transmitter 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/sched/signal.h>
23 #include <linux/delay.h>
24 #include <linux/videodev2.h>
25 #include <linux/v4l2-dv-timings.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-dv-timings.h>
29 
30 #include "vivid-core.h"
31 #include "vivid-ctrls.h"
32 #include "vivid-radio-common.h"
33 #include "vivid-radio-tx.h"
34 
vivid_radio_tx_write(struct file * file,const char __user * buf,size_t size,loff_t * offset)35 ssize_t vivid_radio_tx_write(struct file *file, const char __user *buf,
36 			  size_t size, loff_t *offset)
37 {
38 	struct vivid_dev *dev = video_drvdata(file);
39 	struct v4l2_rds_data *data = dev->rds_gen.data;
40 	struct timespec ts;
41 	unsigned blk;
42 	int i;
43 
44 	if (dev->radio_tx_rds_controls)
45 		return -EINVAL;
46 
47 	if (size < sizeof(*data))
48 		return -EINVAL;
49 	size = sizeof(*data) * (size / sizeof(*data));
50 
51 	if (mutex_lock_interruptible(&dev->mutex))
52 		return -ERESTARTSYS;
53 	if (dev->radio_tx_rds_owner &&
54 	    file->private_data != dev->radio_tx_rds_owner) {
55 		mutex_unlock(&dev->mutex);
56 		return -EBUSY;
57 	}
58 	dev->radio_tx_rds_owner = file->private_data;
59 
60 retry:
61 	ktime_get_ts(&ts);
62 	ts = timespec_sub(ts, dev->radio_rds_init_ts);
63 	blk = ts.tv_sec * 100 + ts.tv_nsec / 10000000;
64 	blk = (blk * VIVID_RDS_GEN_BLOCKS) / 500;
65 	if (blk - VIVID_RDS_GEN_BLOCKS >= dev->radio_tx_rds_last_block)
66 		dev->radio_tx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1;
67 
68 	/*
69 	 * No data is available if there hasn't been time to get new data,
70 	 * or if the RDS receiver has been disabled, or if we use the data
71 	 * from the RDS transmitter and that RDS transmitter has been disabled,
72 	 * or if the signal quality is too weak.
73 	 */
74 	if (blk == dev->radio_tx_rds_last_block ||
75 	    !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) {
76 		mutex_unlock(&dev->mutex);
77 		if (file->f_flags & O_NONBLOCK)
78 			return -EWOULDBLOCK;
79 		if (msleep_interruptible(20) && signal_pending(current))
80 			return -EINTR;
81 		if (mutex_lock_interruptible(&dev->mutex))
82 			return -ERESTARTSYS;
83 		goto retry;
84 	}
85 
86 	for (i = 0; i < size && blk > dev->radio_tx_rds_last_block;
87 			dev->radio_tx_rds_last_block++) {
88 		unsigned data_blk = dev->radio_tx_rds_last_block % VIVID_RDS_GEN_BLOCKS;
89 		struct v4l2_rds_data rds;
90 
91 		if (copy_from_user(&rds, buf + i, sizeof(rds))) {
92 			i = -EFAULT;
93 			break;
94 		}
95 		i += sizeof(rds);
96 		if (!dev->radio_rds_loop)
97 			continue;
98 		if ((rds.block & V4L2_RDS_BLOCK_MSK) == V4L2_RDS_BLOCK_INVALID ||
99 		    (rds.block & V4L2_RDS_BLOCK_ERROR))
100 			continue;
101 		rds.block &= V4L2_RDS_BLOCK_MSK;
102 		data[data_blk] = rds;
103 	}
104 	mutex_unlock(&dev->mutex);
105 	return i;
106 }
107 
vivid_radio_tx_poll(struct file * file,struct poll_table_struct * wait)108 unsigned int vivid_radio_tx_poll(struct file *file, struct poll_table_struct *wait)
109 {
110 	return POLLOUT | POLLWRNORM | v4l2_ctrl_poll(file, wait);
111 }
112 
vidioc_g_modulator(struct file * file,void * fh,struct v4l2_modulator * a)113 int vidioc_g_modulator(struct file *file, void *fh, struct v4l2_modulator *a)
114 {
115 	struct vivid_dev *dev = video_drvdata(file);
116 
117 	if (a->index > 0)
118 		return -EINVAL;
119 
120 	strlcpy(a->name, "AM/FM/SW Transmitter", sizeof(a->name));
121 	a->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
122 			V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS |
123 			(dev->radio_tx_rds_controls ?
124 				V4L2_TUNER_CAP_RDS_CONTROLS :
125 				V4L2_TUNER_CAP_RDS_BLOCK_IO);
126 	a->rangelow = AM_FREQ_RANGE_LOW;
127 	a->rangehigh = FM_FREQ_RANGE_HIGH;
128 	a->txsubchans = dev->radio_tx_subchans;
129 	return 0;
130 }
131 
vidioc_s_modulator(struct file * file,void * fh,const struct v4l2_modulator * a)132 int vidioc_s_modulator(struct file *file, void *fh, const struct v4l2_modulator *a)
133 {
134 	struct vivid_dev *dev = video_drvdata(file);
135 
136 	if (a->index)
137 		return -EINVAL;
138 	if (a->txsubchans & ~0x13)
139 		return -EINVAL;
140 	dev->radio_tx_subchans = a->txsubchans;
141 	return 0;
142 }
143