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 <media/v4l2-common.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-dv-timings.h>
29 #include "vivid-core.h"
30 #include "vivid-ctrls.h"
31 #include "vivid-radio-common.h"
32 #include "vivid-rds-gen.h"
33 #include "vivid-radio-rx.h"
35 ssize_t
vivid_radio_rx_read(struct file
*file
, char __user
*buf
,
36 size_t size
, loff_t
*offset
)
38 struct vivid_dev
*dev
= video_drvdata(file
);
40 struct v4l2_rds_data
*data
= dev
->rds_gen
.data
;
46 if (dev
->radio_rx_rds_controls
)
48 if (size
< sizeof(*data
))
50 size
= sizeof(*data
) * (size
/ sizeof(*data
));
52 if (mutex_lock_interruptible(&dev
->mutex
))
54 if (dev
->radio_rx_rds_owner
&&
55 file
->private_data
!= dev
->radio_rx_rds_owner
) {
56 mutex_unlock(&dev
->mutex
);
59 if (dev
->radio_rx_rds_owner
== NULL
) {
60 vivid_radio_rds_init(dev
);
61 dev
->radio_rx_rds_owner
= file
->private_data
;
66 use_alternates
= ts
.tv_sec
% 10 >= 5;
67 if (dev
->radio_rx_rds_last_block
== 0 ||
68 dev
->radio_rx_rds_use_alternates
!= use_alternates
) {
69 dev
->radio_rx_rds_use_alternates
= use_alternates
;
70 /* Re-init the RDS generator */
71 vivid_radio_rds_init(dev
);
73 ts
= timespec_sub(ts
, dev
->radio_rds_init_ts
);
74 blk
= ts
.tv_sec
* 100 + ts
.tv_nsec
/ 10000000;
75 blk
= (blk
* VIVID_RDS_GEN_BLOCKS
) / 500;
76 if (blk
>= dev
->radio_rx_rds_last_block
+ VIVID_RDS_GEN_BLOCKS
)
77 dev
->radio_rx_rds_last_block
= blk
- VIVID_RDS_GEN_BLOCKS
+ 1;
80 * No data is available if there hasn't been time to get new data,
81 * or if the RDS receiver has been disabled, or if we use the data
82 * from the RDS transmitter and that RDS transmitter has been disabled,
83 * or if the signal quality is too weak.
85 if (blk
== dev
->radio_rx_rds_last_block
|| !dev
->radio_rx_rds_enabled
||
86 (dev
->radio_rds_loop
&& !(dev
->radio_tx_subchans
& V4L2_TUNER_SUB_RDS
)) ||
87 abs(dev
->radio_rx_sig_qual
) > 200) {
88 mutex_unlock(&dev
->mutex
);
89 if (file
->f_flags
& O_NONBLOCK
)
91 if (msleep_interruptible(20) && signal_pending(current
))
93 if (mutex_lock_interruptible(&dev
->mutex
))
98 /* abs(dev->radio_rx_sig_qual) <= 200, map that to a 0-50% range */
99 perc
= abs(dev
->radio_rx_sig_qual
) / 4;
101 for (i
= 0; i
< size
&& blk
> dev
->radio_rx_rds_last_block
;
102 dev
->radio_rx_rds_last_block
++) {
103 unsigned data_blk
= dev
->radio_rx_rds_last_block
% VIVID_RDS_GEN_BLOCKS
;
104 struct v4l2_rds_data rds
= data
[data_blk
];
106 if (data_blk
== 0 && dev
->radio_rds_loop
)
107 vivid_radio_rds_init(dev
);
108 if (perc
&& prandom_u32_max(100) < perc
) {
109 switch (prandom_u32_max(4)) {
111 rds
.block
|= V4L2_RDS_BLOCK_CORRECTED
;
114 rds
.block
|= V4L2_RDS_BLOCK_INVALID
;
117 rds
.block
|= V4L2_RDS_BLOCK_ERROR
;
118 rds
.lsb
= prandom_u32_max(256);
119 rds
.msb
= prandom_u32_max(256);
121 case 3: /* Skip block altogether */
125 * Must make sure at least one block is
126 * returned, otherwise the application
127 * might think that end-of-file occurred.
132 if (copy_to_user(buf
+ i
, &rds
, sizeof(rds
))) {
138 mutex_unlock(&dev
->mutex
);
142 unsigned int vivid_radio_rx_poll(struct file
*file
, struct poll_table_struct
*wait
)
144 return POLLIN
| POLLRDNORM
| v4l2_ctrl_poll(file
, wait
);
147 int vivid_radio_rx_enum_freq_bands(struct file
*file
, void *fh
, struct v4l2_frequency_band
*band
)
149 if (band
->tuner
!= 0)
152 if (band
->index
>= TOT_BANDS
)
155 *band
= vivid_radio_bands
[band
->index
];
159 int vivid_radio_rx_s_hw_freq_seek(struct file
*file
, void *fh
, const struct v4l2_hw_freq_seek
*a
)
161 struct vivid_dev
*dev
= video_drvdata(file
);
169 if (a
->wrap_around
&& dev
->radio_rx_hw_seek_mode
== VIVID_HW_SEEK_BOUNDED
)
172 if (!a
->wrap_around
&& dev
->radio_rx_hw_seek_mode
== VIVID_HW_SEEK_WRAP
)
174 if (!a
->rangelow
^ !a
->rangehigh
)
177 if (file
->f_flags
& O_NONBLOCK
)
181 for (band
= 0; band
< TOT_BANDS
; band
++)
182 if (a
->rangelow
>= vivid_radio_bands
[band
].rangelow
&&
183 a
->rangehigh
<= vivid_radio_bands
[band
].rangehigh
)
185 if (band
== TOT_BANDS
)
187 if (!dev
->radio_rx_hw_seek_prog_lim
&&
188 (a
->rangelow
!= vivid_radio_bands
[band
].rangelow
||
189 a
->rangehigh
!= vivid_radio_bands
[band
].rangehigh
))
194 for (band
= 0; band
< TOT_BANDS
; band
++)
195 if (dev
->radio_rx_freq
>= vivid_radio_bands
[band
].rangelow
&&
196 dev
->radio_rx_freq
<= vivid_radio_bands
[band
].rangehigh
)
198 if (band
== TOT_BANDS
)
200 low
= vivid_radio_bands
[band
].rangelow
;
201 high
= vivid_radio_bands
[band
].rangehigh
;
203 spacing
= band
== BAND_AM
? 1600 : 16000;
204 freq
= clamp(dev
->radio_rx_freq
, low
, high
);
206 if (a
->seek_upward
) {
207 freq
= spacing
* (freq
/ spacing
) + spacing
;
211 freq
= spacing
* (low
/ spacing
) + spacing
;
212 if (freq
>= dev
->radio_rx_freq
)
216 freq
= spacing
* ((freq
+ spacing
- 1) / spacing
) - spacing
;
220 freq
= spacing
* ((high
+ spacing
- 1) / spacing
) - spacing
;
221 if (freq
<= dev
->radio_rx_freq
)
228 int vivid_radio_rx_g_tuner(struct file
*file
, void *fh
, struct v4l2_tuner
*vt
)
230 struct vivid_dev
*dev
= video_drvdata(file
);
237 strlcpy(vt
->name
, "AM/FM/SW Receiver", sizeof(vt
->name
));
238 vt
->capability
= V4L2_TUNER_CAP_LOW
| V4L2_TUNER_CAP_STEREO
|
239 V4L2_TUNER_CAP_FREQ_BANDS
| V4L2_TUNER_CAP_RDS
|
240 (dev
->radio_rx_rds_controls
?
241 V4L2_TUNER_CAP_RDS_CONTROLS
:
242 V4L2_TUNER_CAP_RDS_BLOCK_IO
) |
243 (dev
->radio_rx_hw_seek_prog_lim
?
244 V4L2_TUNER_CAP_HWSEEK_PROG_LIM
: 0);
245 switch (dev
->radio_rx_hw_seek_mode
) {
246 case VIVID_HW_SEEK_BOUNDED
:
247 vt
->capability
|= V4L2_TUNER_CAP_HWSEEK_BOUNDED
;
249 case VIVID_HW_SEEK_WRAP
:
250 vt
->capability
|= V4L2_TUNER_CAP_HWSEEK_WRAP
;
252 case VIVID_HW_SEEK_BOTH
:
253 vt
->capability
|= V4L2_TUNER_CAP_HWSEEK_WRAP
|
254 V4L2_TUNER_CAP_HWSEEK_BOUNDED
;
257 vt
->rangelow
= AM_FREQ_RANGE_LOW
;
258 vt
->rangehigh
= FM_FREQ_RANGE_HIGH
;
259 sig_qual
= dev
->radio_rx_sig_qual
;
260 vt
->signal
= abs(sig_qual
) > delta
? 0 :
261 0xffff - (abs(sig_qual
) * 0xffff) / delta
;
262 vt
->afc
= sig_qual
> delta
? 0 : sig_qual
;
263 if (abs(sig_qual
) > delta
)
265 else if (dev
->radio_rx_freq
< FM_FREQ_RANGE_LOW
|| vt
->signal
< 0x8000)
266 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
267 else if (dev
->radio_rds_loop
&& !(dev
->radio_tx_subchans
& V4L2_TUNER_SUB_STEREO
))
268 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
270 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
271 if (dev
->radio_rx_rds_enabled
&&
272 (!dev
->radio_rds_loop
|| (dev
->radio_tx_subchans
& V4L2_TUNER_SUB_RDS
)) &&
273 dev
->radio_rx_freq
>= FM_FREQ_RANGE_LOW
&& vt
->signal
>= 0xc000)
274 vt
->rxsubchans
|= V4L2_TUNER_SUB_RDS
;
275 if (dev
->radio_rx_rds_controls
)
276 vivid_radio_rds_init(dev
);
277 vt
->audmode
= dev
->radio_rx_audmode
;
281 int vivid_radio_rx_s_tuner(struct file
*file
, void *fh
, const struct v4l2_tuner
*vt
)
283 struct vivid_dev
*dev
= video_drvdata(file
);
287 dev
->radio_rx_audmode
= vt
->audmode
>= V4L2_TUNER_MODE_STEREO
;