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