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
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
;
48 if (dev
->radio_rx_rds_controls
)
50 if (size
< sizeof(*data
))
52 size
= sizeof(*data
) * (size
/ sizeof(*data
));
54 if (mutex_lock_interruptible(&dev
->mutex
))
56 if (dev
->radio_rx_rds_owner
&&
57 file
->private_data
!= dev
->radio_rx_rds_owner
) {
58 mutex_unlock(&dev
->mutex
);
61 if (dev
->radio_rx_rds_owner
== NULL
) {
62 vivid_radio_rds_init(dev
);
63 dev
->radio_rx_rds_owner
= file
->private_data
;
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
)
92 if (msleep_interruptible(20) && signal_pending(current
))
94 if (mutex_lock_interruptible(&dev
->mutex
))
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)) {
112 rds
.block
|= V4L2_RDS_BLOCK_CORRECTED
;
115 rds
.block
|= V4L2_RDS_BLOCK_INVALID
;
118 rds
.block
|= V4L2_RDS_BLOCK_ERROR
;
119 rds
.lsb
= prandom_u32_max(256);
120 rds
.msb
= prandom_u32_max(256);
122 case 3: /* Skip block altogether */
126 * Must make sure at least one block is
127 * returned, otherwise the application
128 * might think that end-of-file occurred.
133 if (copy_to_user(buf
+ i
, &rds
, sizeof(rds
))) {
139 mutex_unlock(&dev
->mutex
);
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)
153 if (band
->index
>= TOT_BANDS
)
156 *band
= vivid_radio_bands
[band
->index
];
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
);
170 if (a
->wrap_around
&& dev
->radio_rx_hw_seek_mode
== VIVID_HW_SEEK_BOUNDED
)
173 if (!a
->wrap_around
&& dev
->radio_rx_hw_seek_mode
== VIVID_HW_SEEK_WRAP
)
175 if (!a
->rangelow
^ !a
->rangehigh
)
178 if (file
->f_flags
& O_NONBLOCK
)
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
)
186 if (band
== TOT_BANDS
)
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
))
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
)
199 if (band
== TOT_BANDS
)
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
;
212 freq
= spacing
* (low
/ spacing
) + spacing
;
213 if (freq
>= dev
->radio_rx_freq
)
217 freq
= spacing
* ((freq
+ spacing
- 1) / spacing
) - spacing
;
221 freq
= spacing
* ((high
+ spacing
- 1) / spacing
) - spacing
;
222 if (freq
<= dev
->radio_rx_freq
)
229 int vivid_radio_rx_g_tuner(struct file
*file
, void *fh
, struct v4l2_tuner
*vt
)
231 struct vivid_dev
*dev
= video_drvdata(file
);
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
;
250 case VIVID_HW_SEEK_WRAP
:
251 vt
->capability
|= V4L2_TUNER_CAP_HWSEEK_WRAP
;
253 case VIVID_HW_SEEK_BOTH
:
254 vt
->capability
|= V4L2_TUNER_CAP_HWSEEK_WRAP
|
255 V4L2_TUNER_CAP_HWSEEK_BOUNDED
;
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
)
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
;
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
;
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
);
288 dev
->radio_rx_audmode
= vt
->audmode
>= V4L2_TUNER_MODE_STEREO
;