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/math64.h>
26 #include <linux/videodev2.h>
27 #include <linux/v4l2-dv-timings.h>
28 #include <media/v4l2-common.h>
29 #include <media/v4l2-event.h>
30 #include <media/v4l2-dv-timings.h>
31 #include <linux/fixp-arith.h>
33 #include "vivid-core.h"
34 #include "vivid-ctrls.h"
35 #include "vivid-sdr-cap.h"
43 /* format descriptions for capture and preview */
44 static const struct vivid_format formats[] = {
46 .pixelformat = V4L2_SDR_FMT_CU8,
47 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
49 .pixelformat = V4L2_SDR_FMT_CS8,
50 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
54 static const struct v4l2_frequency_band bands_adc[] = {
57 .type = V4L2_TUNER_ADC,
59 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
65 .type = V4L2_TUNER_ADC,
67 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
73 .type = V4L2_TUNER_ADC,
75 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
81 /* ADC band midpoints */
82 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
83 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
85 static const struct v4l2_frequency_band bands_fm[] = {
88 .type = V4L2_TUNER_RF,
90 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
92 .rangehigh = 2000000000,
96 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev)
98 struct vivid_buffer *sdr_cap_buf = NULL;
100 dprintk(dev, 1, "SDR Capture Thread Tick\n");
102 /* Drop a certain percentage of buffers. */
103 if (dev->perc_dropped_buffers &&
104 prandom_u32_max(100) < dev->perc_dropped_buffers)
107 spin_lock(&dev->slock);
108 if (!list_empty(&dev->sdr_cap_active)) {
109 sdr_cap_buf = list_entry(dev->sdr_cap_active.next,
110 struct vivid_buffer, list);
111 list_del(&sdr_cap_buf->list);
113 spin_unlock(&dev->slock);
116 sdr_cap_buf->vb.sequence = dev->sdr_cap_seq_count;
117 vivid_sdr_cap_process(dev, sdr_cap_buf);
118 sdr_cap_buf->vb.vb2_buf.timestamp =
119 ktime_get_ns() + dev->time_wrap_offset;
120 vb2_buffer_done(&sdr_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
121 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
122 dev->dqbuf_error = false;
126 static int vivid_thread_sdr_cap(void *data)
128 struct vivid_dev *dev = data;
129 u64 samples_since_start;
130 u64 buffers_since_start;
131 u64 next_jiffies_since_start;
132 unsigned long jiffies_since_start;
133 unsigned long cur_jiffies;
134 unsigned wait_jiffies;
136 dprintk(dev, 1, "SDR Capture Thread Start\n");
140 /* Resets frame counters */
141 dev->sdr_cap_seq_offset = 0;
143 dev->sdr_cap_seq_offset = 0xffffff80U;
144 dev->jiffies_sdr_cap = jiffies;
145 dev->sdr_cap_seq_resync = false;
149 if (kthread_should_stop())
152 if (!mutex_trylock(&dev->mutex)) {
153 schedule_timeout_uninterruptible(1);
157 cur_jiffies = jiffies;
158 if (dev->sdr_cap_seq_resync) {
159 dev->jiffies_sdr_cap = cur_jiffies;
160 dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1;
161 dev->sdr_cap_seq_count = 0;
162 dev->sdr_cap_seq_resync = false;
164 /* Calculate the number of jiffies since we started streaming */
165 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap;
166 /* Get the number of buffers streamed since the start */
167 buffers_since_start =
168 (u64)jiffies_since_start * dev->sdr_adc_freq +
169 (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2;
170 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF);
173 * After more than 0xf0000000 (rounded down to a multiple of
174 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
175 * jiffies have passed since we started streaming reset the
176 * counters and keep track of the sequence offset.
178 if (jiffies_since_start > JIFFIES_RESYNC) {
179 dev->jiffies_sdr_cap = cur_jiffies;
180 dev->sdr_cap_seq_offset = buffers_since_start;
181 buffers_since_start = 0;
183 dev->sdr_cap_seq_count =
184 buffers_since_start + dev->sdr_cap_seq_offset;
186 vivid_thread_sdr_cap_tick(dev);
187 mutex_unlock(&dev->mutex);
190 * Calculate the number of samples streamed since we started,
191 * not including the current buffer.
193 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF;
195 /* And the number of jiffies since we started */
196 jiffies_since_start = jiffies - dev->jiffies_sdr_cap;
198 /* Increase by the number of samples in one buffer */
199 samples_since_start += SDR_CAP_SAMPLES_PER_BUF;
201 * Calculate when that next buffer is supposed to start
202 * in jiffies since we started streaming.
204 next_jiffies_since_start = samples_since_start * HZ +
205 dev->sdr_adc_freq / 2;
206 do_div(next_jiffies_since_start, dev->sdr_adc_freq);
207 /* If it is in the past, then just schedule asap */
208 if (next_jiffies_since_start < jiffies_since_start)
209 next_jiffies_since_start = jiffies_since_start;
211 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
212 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
214 dprintk(dev, 1, "SDR Capture Thread End\n");
218 static int sdr_cap_queue_setup(struct vb2_queue *vq,
219 unsigned *nbuffers, unsigned *nplanes,
220 unsigned sizes[], struct device *alloc_devs[])
222 /* 2 = max 16-bit sample returned */
223 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
228 static int sdr_cap_buf_prepare(struct vb2_buffer *vb)
230 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
231 unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2;
233 dprintk(dev, 1, "%s\n", __func__);
235 if (dev->buf_prepare_error) {
237 * Error injection: test what happens if buf_prepare() returns
240 dev->buf_prepare_error = false;
243 if (vb2_plane_size(vb, 0) < size) {
244 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
245 __func__, vb2_plane_size(vb, 0), size);
248 vb2_set_plane_payload(vb, 0, size);
253 static void sdr_cap_buf_queue(struct vb2_buffer *vb)
255 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
256 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
257 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
259 dprintk(dev, 1, "%s\n", __func__);
261 spin_lock(&dev->slock);
262 list_add_tail(&buf->list, &dev->sdr_cap_active);
263 spin_unlock(&dev->slock);
266 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count)
268 struct vivid_dev *dev = vb2_get_drv_priv(vq);
271 dprintk(dev, 1, "%s\n", __func__);
272 dev->sdr_cap_seq_count = 0;
273 if (dev->start_streaming_error) {
274 dev->start_streaming_error = false;
276 } else if (dev->kthread_sdr_cap == NULL) {
277 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev,
278 "%s-sdr-cap", dev->v4l2_dev.name);
280 if (IS_ERR(dev->kthread_sdr_cap)) {
281 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
282 err = PTR_ERR(dev->kthread_sdr_cap);
283 dev->kthread_sdr_cap = NULL;
287 struct vivid_buffer *buf, *tmp;
289 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) {
290 list_del(&buf->list);
291 vb2_buffer_done(&buf->vb.vb2_buf,
292 VB2_BUF_STATE_QUEUED);
298 /* abort streaming and wait for last buffer */
299 static void sdr_cap_stop_streaming(struct vb2_queue *vq)
301 struct vivid_dev *dev = vb2_get_drv_priv(vq);
303 if (dev->kthread_sdr_cap == NULL)
306 while (!list_empty(&dev->sdr_cap_active)) {
307 struct vivid_buffer *buf;
309 buf = list_entry(dev->sdr_cap_active.next,
310 struct vivid_buffer, list);
311 list_del(&buf->list);
312 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
315 /* shutdown control thread */
316 kthread_stop(dev->kthread_sdr_cap);
317 dev->kthread_sdr_cap = NULL;
320 const struct vb2_ops vivid_sdr_cap_qops = {
321 .queue_setup = sdr_cap_queue_setup,
322 .buf_prepare = sdr_cap_buf_prepare,
323 .buf_queue = sdr_cap_buf_queue,
324 .start_streaming = sdr_cap_start_streaming,
325 .stop_streaming = sdr_cap_stop_streaming,
326 .wait_prepare = vb2_ops_wait_prepare,
327 .wait_finish = vb2_ops_wait_finish,
330 int vivid_sdr_enum_freq_bands(struct file *file, void *fh,
331 struct v4l2_frequency_band *band)
333 switch (band->tuner) {
335 if (band->index >= ARRAY_SIZE(bands_adc))
337 *band = bands_adc[band->index];
340 if (band->index >= ARRAY_SIZE(bands_fm))
342 *band = bands_fm[band->index];
349 int vivid_sdr_g_frequency(struct file *file, void *fh,
350 struct v4l2_frequency *vf)
352 struct vivid_dev *dev = video_drvdata(file);
356 vf->frequency = dev->sdr_adc_freq;
357 vf->type = V4L2_TUNER_ADC;
360 vf->frequency = dev->sdr_fm_freq;
361 vf->type = V4L2_TUNER_RF;
368 int vivid_sdr_s_frequency(struct file *file, void *fh,
369 const struct v4l2_frequency *vf)
371 struct vivid_dev *dev = video_drvdata(file);
372 unsigned freq = vf->frequency;
377 if (vf->type != V4L2_TUNER_ADC)
379 if (freq < BAND_ADC_0)
381 else if (freq < BAND_ADC_1)
386 freq = clamp_t(unsigned, freq,
387 bands_adc[band].rangelow,
388 bands_adc[band].rangehigh);
390 if (vb2_is_streaming(&dev->vb_sdr_cap_q) &&
391 freq != dev->sdr_adc_freq) {
392 /* resync the thread's timings */
393 dev->sdr_cap_seq_resync = true;
395 dev->sdr_adc_freq = freq;
398 if (vf->type != V4L2_TUNER_RF)
400 dev->sdr_fm_freq = clamp_t(unsigned, freq,
401 bands_fm[0].rangelow,
402 bands_fm[0].rangehigh);
409 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
413 strlcpy(vt->name, "ADC", sizeof(vt->name));
414 vt->type = V4L2_TUNER_ADC;
416 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
417 vt->rangelow = bands_adc[0].rangelow;
418 vt->rangehigh = bands_adc[2].rangehigh;
421 strlcpy(vt->name, "RF", sizeof(vt->name));
422 vt->type = V4L2_TUNER_RF;
424 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
425 vt->rangelow = bands_fm[0].rangelow;
426 vt->rangehigh = bands_fm[0].rangehigh;
433 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
440 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
442 if (f->index >= ARRAY_SIZE(formats))
444 f->pixelformat = formats[f->index].pixelformat;
448 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
450 struct vivid_dev *dev = video_drvdata(file);
452 f->fmt.sdr.pixelformat = dev->sdr_pixelformat;
453 f->fmt.sdr.buffersize = dev->sdr_buffersize;
454 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
458 int vidioc_s_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
460 struct vivid_dev *dev = video_drvdata(file);
461 struct vb2_queue *q = &dev->vb_sdr_cap_q;
467 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
468 for (i = 0; i < ARRAY_SIZE(formats); i++) {
469 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
470 dev->sdr_pixelformat = formats[i].pixelformat;
471 dev->sdr_buffersize = formats[i].buffersize;
472 f->fmt.sdr.buffersize = formats[i].buffersize;
476 dev->sdr_pixelformat = formats[0].pixelformat;
477 dev->sdr_buffersize = formats[0].buffersize;
478 f->fmt.sdr.pixelformat = formats[0].pixelformat;
479 f->fmt.sdr.buffersize = formats[0].buffersize;
483 int vidioc_try_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
487 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
488 for (i = 0; i < ARRAY_SIZE(formats); i++) {
489 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
490 f->fmt.sdr.buffersize = formats[i].buffersize;
494 f->fmt.sdr.pixelformat = formats[0].pixelformat;
495 f->fmt.sdr.buffersize = formats[0].buffersize;
500 #define FIXP_FRAC (1 << FIXP_N)
501 #define FIXP_2PI ((int)(2 * 3.141592653589 * FIXP_FRAC))
502 #define M_100000PI (3.14159 * 100000)
504 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
506 u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
508 unsigned long plane_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
515 /* calculate phase step */
516 #define BEEP_FREQ 1000 /* 1kHz beep */
517 src_phase_step = DIV_ROUND_CLOSEST(FIXP_2PI * BEEP_FREQ,
520 for (i = 0; i < plane_size; i += 2) {
521 mod_phase_step = fixp_cos32_rad(dev->sdr_fixp_src_phase,
522 FIXP_2PI) >> (31 - FIXP_N);
524 dev->sdr_fixp_src_phase += src_phase_step;
525 s64tmp = (s64) mod_phase_step * dev->sdr_fm_deviation;
526 dev->sdr_fixp_mod_phase += div_s64(s64tmp, M_100000PI);
529 * Transfer phase angle to [0, 2xPI] in order to avoid variable
530 * overflow and make it suitable for cosine implementation
531 * used, which does not support negative angles.
533 dev->sdr_fixp_src_phase %= FIXP_2PI;
534 dev->sdr_fixp_mod_phase %= FIXP_2PI;
536 if (dev->sdr_fixp_mod_phase < 0)
537 dev->sdr_fixp_mod_phase += FIXP_2PI;
539 fixp_i = fixp_cos32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
540 fixp_q = fixp_sin32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
542 /* Normalize fraction values represented with 32 bit precision
543 * to fixed point representation with FIXP_N bits */
544 fixp_i >>= (31 - FIXP_N);
545 fixp_q >>= (31 - FIXP_N);
547 switch (dev->sdr_pixelformat) {
548 case V4L2_SDR_FMT_CU8:
549 /* convert 'fixp float' to u8 [0, +255] */
550 /* u8 = X * 127.5 + 127.5; X is float [-1.0, +1.0] */
551 fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275;
552 fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275;
553 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
554 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
556 case V4L2_SDR_FMT_CS8:
557 /* convert 'fixp float' to s8 [-128, +127] */
558 /* s8 = X * 127.5 - 0.5; X is float [-1.0, +1.0] */
559 fixp_i = fixp_i * 1275 - FIXP_FRAC * 5;
560 fixp_q = fixp_q * 1275 - FIXP_FRAC * 5;
561 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
562 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);