2 * vivid-sdr-cap.c - software defined radio 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/kthread.h>
24 #include <linux/freezer.h>
25 #include <linux/videodev2.h>
26 #include <linux/v4l2-dv-timings.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-sdr-cap.h"
35 static const struct v4l2_frequency_band bands_adc[] = {
38 .type = V4L2_TUNER_ADC,
40 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
46 .type = V4L2_TUNER_ADC,
48 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
54 .type = V4L2_TUNER_ADC,
56 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
62 /* ADC band midpoints */
63 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
64 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
66 static const struct v4l2_frequency_band bands_fm[] = {
69 .type = V4L2_TUNER_RF,
71 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
73 .rangehigh = 2000000000,
77 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev)
79 struct vivid_buffer *sdr_cap_buf = NULL;
81 dprintk(dev, 1, "SDR Capture Thread Tick\n");
83 /* Drop a certain percentage of buffers. */
84 if (dev->perc_dropped_buffers &&
85 prandom_u32_max(100) < dev->perc_dropped_buffers)
88 spin_lock(&dev->slock);
89 if (!list_empty(&dev->sdr_cap_active)) {
90 sdr_cap_buf = list_entry(dev->sdr_cap_active.next,
91 struct vivid_buffer, list);
92 list_del(&sdr_cap_buf->list);
94 spin_unlock(&dev->slock);
97 sdr_cap_buf->vb.v4l2_buf.sequence = dev->sdr_cap_seq_count;
98 vivid_sdr_cap_process(dev, sdr_cap_buf);
99 v4l2_get_timestamp(&sdr_cap_buf->vb.v4l2_buf.timestamp);
100 sdr_cap_buf->vb.v4l2_buf.timestamp.tv_sec += dev->time_wrap_offset;
101 vb2_buffer_done(&sdr_cap_buf->vb, dev->dqbuf_error ?
102 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
103 dev->dqbuf_error = false;
107 static int vivid_thread_sdr_cap(void *data)
109 struct vivid_dev *dev = data;
110 u64 samples_since_start;
111 u64 buffers_since_start;
112 u64 next_jiffies_since_start;
113 unsigned long jiffies_since_start;
114 unsigned long cur_jiffies;
115 unsigned wait_jiffies;
117 dprintk(dev, 1, "SDR Capture Thread Start\n");
121 /* Resets frame counters */
122 dev->sdr_cap_seq_offset = 0;
124 dev->sdr_cap_seq_offset = 0xffffff80U;
125 dev->jiffies_sdr_cap = jiffies;
126 dev->sdr_cap_seq_resync = false;
130 if (kthread_should_stop())
133 mutex_lock(&dev->mutex);
134 cur_jiffies = jiffies;
135 if (dev->sdr_cap_seq_resync) {
136 dev->jiffies_sdr_cap = cur_jiffies;
137 dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1;
138 dev->sdr_cap_seq_count = 0;
139 dev->sdr_cap_seq_resync = false;
141 /* Calculate the number of jiffies since we started streaming */
142 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap;
143 /* Get the number of buffers streamed since the start */
144 buffers_since_start = (u64)jiffies_since_start * dev->sdr_adc_freq +
145 (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2;
146 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF);
149 * After more than 0xf0000000 (rounded down to a multiple of
150 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
151 * jiffies have passed since we started streaming reset the
152 * counters and keep track of the sequence offset.
154 if (jiffies_since_start > JIFFIES_RESYNC) {
155 dev->jiffies_sdr_cap = cur_jiffies;
156 dev->sdr_cap_seq_offset = buffers_since_start;
157 buffers_since_start = 0;
159 dev->sdr_cap_seq_count = buffers_since_start + dev->sdr_cap_seq_offset;
161 vivid_thread_sdr_cap_tick(dev);
162 mutex_unlock(&dev->mutex);
165 * Calculate the number of samples streamed since we started,
166 * not including the current buffer.
168 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF;
170 /* And the number of jiffies since we started */
171 jiffies_since_start = jiffies - dev->jiffies_sdr_cap;
173 /* Increase by the number of samples in one buffer */
174 samples_since_start += SDR_CAP_SAMPLES_PER_BUF;
176 * Calculate when that next buffer is supposed to start
177 * in jiffies since we started streaming.
179 next_jiffies_since_start = samples_since_start * HZ +
180 dev->sdr_adc_freq / 2;
181 do_div(next_jiffies_since_start, dev->sdr_adc_freq);
182 /* If it is in the past, then just schedule asap */
183 if (next_jiffies_since_start < jiffies_since_start)
184 next_jiffies_since_start = jiffies_since_start;
186 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
187 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
189 dprintk(dev, 1, "SDR Capture Thread End\n");
193 static int sdr_cap_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
194 unsigned *nbuffers, unsigned *nplanes,
195 unsigned sizes[], void *alloc_ctxs[])
197 /* 2 = max 16-bit sample returned */
198 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
203 static int sdr_cap_buf_prepare(struct vb2_buffer *vb)
205 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
206 unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2;
208 dprintk(dev, 1, "%s\n", __func__);
210 if (dev->buf_prepare_error) {
212 * Error injection: test what happens if buf_prepare() returns
215 dev->buf_prepare_error = false;
218 if (vb2_plane_size(vb, 0) < size) {
219 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
220 __func__, vb2_plane_size(vb, 0), size);
223 vb2_set_plane_payload(vb, 0, size);
228 static void sdr_cap_buf_queue(struct vb2_buffer *vb)
230 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
231 struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
233 dprintk(dev, 1, "%s\n", __func__);
235 spin_lock(&dev->slock);
236 list_add_tail(&buf->list, &dev->sdr_cap_active);
237 spin_unlock(&dev->slock);
240 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count)
242 struct vivid_dev *dev = vb2_get_drv_priv(vq);
245 dprintk(dev, 1, "%s\n", __func__);
246 dev->sdr_cap_seq_count = 0;
247 if (dev->start_streaming_error) {
248 dev->start_streaming_error = false;
250 } else if (dev->kthread_sdr_cap == NULL) {
251 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev,
252 "%s-sdr-cap", dev->v4l2_dev.name);
254 if (IS_ERR(dev->kthread_sdr_cap)) {
255 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
256 err = PTR_ERR(dev->kthread_sdr_cap);
257 dev->kthread_sdr_cap = NULL;
261 struct vivid_buffer *buf, *tmp;
263 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) {
264 list_del(&buf->list);
265 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
271 /* abort streaming and wait for last buffer */
272 static void sdr_cap_stop_streaming(struct vb2_queue *vq)
274 struct vivid_dev *dev = vb2_get_drv_priv(vq);
276 if (dev->kthread_sdr_cap == NULL)
279 while (!list_empty(&dev->sdr_cap_active)) {
280 struct vivid_buffer *buf;
282 buf = list_entry(dev->sdr_cap_active.next, struct vivid_buffer, list);
283 list_del(&buf->list);
284 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
287 /* shutdown control thread */
288 mutex_unlock(&dev->mutex);
289 kthread_stop(dev->kthread_sdr_cap);
290 dev->kthread_sdr_cap = NULL;
291 mutex_lock(&dev->mutex);
294 const struct vb2_ops vivid_sdr_cap_qops = {
295 .queue_setup = sdr_cap_queue_setup,
296 .buf_prepare = sdr_cap_buf_prepare,
297 .buf_queue = sdr_cap_buf_queue,
298 .start_streaming = sdr_cap_start_streaming,
299 .stop_streaming = sdr_cap_stop_streaming,
300 .wait_prepare = vivid_unlock,
301 .wait_finish = vivid_lock,
304 int vivid_sdr_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
306 switch (band->tuner) {
308 if (band->index >= ARRAY_SIZE(bands_adc))
310 *band = bands_adc[band->index];
313 if (band->index >= ARRAY_SIZE(bands_fm))
315 *band = bands_fm[band->index];
322 int vivid_sdr_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
324 struct vivid_dev *dev = video_drvdata(file);
328 vf->frequency = dev->sdr_adc_freq;
329 vf->type = V4L2_TUNER_ADC;
332 vf->frequency = dev->sdr_fm_freq;
333 vf->type = V4L2_TUNER_RF;
340 int vivid_sdr_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
342 struct vivid_dev *dev = video_drvdata(file);
343 unsigned freq = vf->frequency;
348 if (vf->type != V4L2_TUNER_ADC)
350 if (freq < BAND_ADC_0)
352 else if (freq < BAND_ADC_1)
357 freq = clamp_t(unsigned, freq,
358 bands_adc[band].rangelow,
359 bands_adc[band].rangehigh);
361 if (vb2_is_streaming(&dev->vb_sdr_cap_q) &&
362 freq != dev->sdr_adc_freq) {
363 /* resync the thread's timings */
364 dev->sdr_cap_seq_resync = true;
366 dev->sdr_adc_freq = freq;
369 if (vf->type != V4L2_TUNER_RF)
371 dev->sdr_fm_freq = clamp_t(unsigned, freq,
372 bands_fm[0].rangelow,
373 bands_fm[0].rangehigh);
380 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
384 strlcpy(vt->name, "ADC", sizeof(vt->name));
385 vt->type = V4L2_TUNER_ADC;
386 vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
387 vt->rangelow = bands_adc[0].rangelow;
388 vt->rangehigh = bands_adc[2].rangehigh;
391 strlcpy(vt->name, "RF", sizeof(vt->name));
392 vt->type = V4L2_TUNER_RF;
393 vt->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
394 vt->rangelow = bands_fm[0].rangelow;
395 vt->rangehigh = bands_fm[0].rangehigh;
402 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
409 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
413 f->pixelformat = V4L2_SDR_FMT_CU8;
414 strlcpy(f->description, "IQ U8", sizeof(f->description));
418 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
420 f->fmt.sdr.pixelformat = V4L2_SDR_FMT_CU8;
421 f->fmt.sdr.buffersize = SDR_CAP_SAMPLES_PER_BUF * 2;
422 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
426 #define FIXP_FRAC (1 << 15)
427 #define FIXP_PI ((int)(FIXP_FRAC * 3.141592653589))
429 /* cos() from cx88 driver: cx88-dsp.c */
430 static s32 fixp_cos(unsigned int x)
433 u16 period = x / FIXP_PI;
436 return -fixp_cos(x - FIXP_PI);
439 return -fixp_cos(FIXP_PI/2 - (x % (FIXP_PI/2)));
440 /* Now x is between 0 and FIXP_PI/2.
441 * To calculate cos(x) we use it's Taylor polinom. */
442 t2 = x*x/FIXP_FRAC/2;
443 t4 = t2*x/FIXP_FRAC*x/FIXP_FRAC/3/4;
444 t6 = t4*x/FIXP_FRAC*x/FIXP_FRAC/5/6;
445 t8 = t6*x/FIXP_FRAC*x/FIXP_FRAC/7/8;
446 return FIXP_FRAC-t2+t4-t6+t8;
449 static inline s32 fixp_sin(unsigned int x)
451 return -fixp_cos(x + (FIXP_PI / 2));
454 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
456 u8 *vbuf = vb2_plane_vaddr(&buf->vb, 0);
458 unsigned long plane_size = vb2_plane_size(&buf->vb, 0);
459 int fixp_src_phase_step, fixp_i, fixp_q;
462 * TODO: Generated beep tone goes very crackly when sample rate is
463 * increased to ~1Msps or more. That is because of huge rounding error
464 * of phase angle caused by used cosine implementation.
467 /* calculate phase step */
468 #define BEEP_FREQ 1000 /* 1kHz beep */
469 fixp_src_phase_step = DIV_ROUND_CLOSEST(2 * FIXP_PI * BEEP_FREQ,
472 for (i = 0; i < plane_size; i += 2) {
473 dev->sdr_fixp_mod_phase += fixp_cos(dev->sdr_fixp_src_phase);
474 dev->sdr_fixp_src_phase += fixp_src_phase_step;
477 * Transfer phases to [0 / 2xPI] in order to avoid variable
478 * overflow and make it suitable for cosine implementation
479 * used, which does not support negative angles.
481 while (dev->sdr_fixp_mod_phase < (0 * FIXP_PI))
482 dev->sdr_fixp_mod_phase += (2 * FIXP_PI);
483 while (dev->sdr_fixp_mod_phase > (2 * FIXP_PI))
484 dev->sdr_fixp_mod_phase -= (2 * FIXP_PI);
486 while (dev->sdr_fixp_src_phase > (2 * FIXP_PI))
487 dev->sdr_fixp_src_phase -= (2 * FIXP_PI);
489 fixp_i = fixp_cos(dev->sdr_fixp_mod_phase);
490 fixp_q = fixp_sin(dev->sdr_fixp_mod_phase);
492 /* convert 'fixp float' to u8 */
493 /* u8 = X * 127.5f + 127.5f; where X is float [-1.0 / +1.0] */
494 fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275;
495 fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275;
496 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
497 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);