1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-sdr-cap.c - software defined radio support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/delay.h>
11 #include <linux/kthread.h>
12 #include <linux/freezer.h>
13 #include <linux/math64.h>
14 #include <linux/videodev2.h>
15 #include <linux/v4l2-dv-timings.h>
16 #include <media/v4l2-common.h>
17 #include <media/v4l2-event.h>
18 #include <media/v4l2-dv-timings.h>
19 #include <linux/fixp-arith.h>
21 #include "vivid-core.h"
22 #include "vivid-ctrls.h"
23 #include "vivid-sdr-cap.h"
31 /* format descriptions for capture and preview */
32 static const struct vivid_format formats[] = {
34 .pixelformat = V4L2_SDR_FMT_CU8,
35 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
37 .pixelformat = V4L2_SDR_FMT_CS8,
38 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2,
42 static const struct v4l2_frequency_band bands_adc[] = {
45 .type = V4L2_TUNER_ADC,
47 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
53 .type = V4L2_TUNER_ADC,
55 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
61 .type = V4L2_TUNER_ADC,
63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
69 /* ADC band midpoints */
70 #define BAND_ADC_0 ((bands_adc[0].rangehigh + bands_adc[1].rangelow) / 2)
71 #define BAND_ADC_1 ((bands_adc[1].rangehigh + bands_adc[2].rangelow) / 2)
73 static const struct v4l2_frequency_band bands_fm[] = {
76 .type = V4L2_TUNER_RF,
78 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
80 .rangehigh = 2000000000,
84 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev)
86 struct vivid_buffer *sdr_cap_buf = NULL;
88 dprintk(dev, 1, "SDR Capture Thread Tick\n");
90 /* Drop a certain percentage of buffers. */
91 if (dev->perc_dropped_buffers &&
92 prandom_u32_max(100) < dev->perc_dropped_buffers)
95 spin_lock(&dev->slock);
96 if (!list_empty(&dev->sdr_cap_active)) {
97 sdr_cap_buf = list_entry(dev->sdr_cap_active.next,
98 struct vivid_buffer, list);
99 list_del(&sdr_cap_buf->list);
101 spin_unlock(&dev->slock);
104 sdr_cap_buf->vb.sequence = dev->sdr_cap_seq_count;
105 v4l2_ctrl_request_setup(sdr_cap_buf->vb.vb2_buf.req_obj.req,
106 &dev->ctrl_hdl_sdr_cap);
107 v4l2_ctrl_request_complete(sdr_cap_buf->vb.vb2_buf.req_obj.req,
108 &dev->ctrl_hdl_sdr_cap);
109 vivid_sdr_cap_process(dev, sdr_cap_buf);
110 sdr_cap_buf->vb.vb2_buf.timestamp =
111 ktime_get_ns() + dev->time_wrap_offset;
112 vb2_buffer_done(&sdr_cap_buf->vb.vb2_buf, dev->dqbuf_error ?
113 VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
114 dev->dqbuf_error = false;
118 static int vivid_thread_sdr_cap(void *data)
120 struct vivid_dev *dev = data;
121 u64 samples_since_start;
122 u64 buffers_since_start;
123 u64 next_jiffies_since_start;
124 unsigned long jiffies_since_start;
125 unsigned long cur_jiffies;
126 unsigned wait_jiffies;
128 dprintk(dev, 1, "SDR Capture Thread Start\n");
132 /* Resets frame counters */
133 dev->sdr_cap_seq_offset = 0;
135 dev->sdr_cap_seq_offset = 0xffffff80U;
136 dev->jiffies_sdr_cap = jiffies;
137 dev->sdr_cap_seq_resync = false;
141 if (kthread_should_stop())
144 if (!mutex_trylock(&dev->mutex)) {
145 schedule_timeout_uninterruptible(1);
149 cur_jiffies = jiffies;
150 if (dev->sdr_cap_seq_resync) {
151 dev->jiffies_sdr_cap = cur_jiffies;
152 dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1;
153 dev->sdr_cap_seq_count = 0;
154 dev->sdr_cap_seq_resync = false;
156 /* Calculate the number of jiffies since we started streaming */
157 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap;
158 /* Get the number of buffers streamed since the start */
159 buffers_since_start =
160 (u64)jiffies_since_start * dev->sdr_adc_freq +
161 (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2;
162 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF);
165 * After more than 0xf0000000 (rounded down to a multiple of
166 * 'jiffies-per-day' to ease jiffies_to_msecs calculation)
167 * jiffies have passed since we started streaming reset the
168 * counters and keep track of the sequence offset.
170 if (jiffies_since_start > JIFFIES_RESYNC) {
171 dev->jiffies_sdr_cap = cur_jiffies;
172 dev->sdr_cap_seq_offset = buffers_since_start;
173 buffers_since_start = 0;
175 dev->sdr_cap_seq_count =
176 buffers_since_start + dev->sdr_cap_seq_offset;
178 vivid_thread_sdr_cap_tick(dev);
179 mutex_unlock(&dev->mutex);
182 * Calculate the number of samples streamed since we started,
183 * not including the current buffer.
185 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF;
187 /* And the number of jiffies since we started */
188 jiffies_since_start = jiffies - dev->jiffies_sdr_cap;
190 /* Increase by the number of samples in one buffer */
191 samples_since_start += SDR_CAP_SAMPLES_PER_BUF;
193 * Calculate when that next buffer is supposed to start
194 * in jiffies since we started streaming.
196 next_jiffies_since_start = samples_since_start * HZ +
197 dev->sdr_adc_freq / 2;
198 do_div(next_jiffies_since_start, dev->sdr_adc_freq);
199 /* If it is in the past, then just schedule asap */
200 if (next_jiffies_since_start < jiffies_since_start)
201 next_jiffies_since_start = jiffies_since_start;
203 wait_jiffies = next_jiffies_since_start - jiffies_since_start;
204 schedule_timeout_interruptible(wait_jiffies ? wait_jiffies : 1);
206 dprintk(dev, 1, "SDR Capture Thread End\n");
210 static int sdr_cap_queue_setup(struct vb2_queue *vq,
211 unsigned *nbuffers, unsigned *nplanes,
212 unsigned sizes[], struct device *alloc_devs[])
214 /* 2 = max 16-bit sample returned */
215 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2;
220 static int sdr_cap_buf_prepare(struct vb2_buffer *vb)
222 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
223 unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2;
225 dprintk(dev, 1, "%s\n", __func__);
227 if (dev->buf_prepare_error) {
229 * Error injection: test what happens if buf_prepare() returns
232 dev->buf_prepare_error = false;
235 if (vb2_plane_size(vb, 0) < size) {
236 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
237 __func__, vb2_plane_size(vb, 0), size);
240 vb2_set_plane_payload(vb, 0, size);
245 static void sdr_cap_buf_queue(struct vb2_buffer *vb)
247 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
248 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
249 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
251 dprintk(dev, 1, "%s\n", __func__);
253 spin_lock(&dev->slock);
254 list_add_tail(&buf->list, &dev->sdr_cap_active);
255 spin_unlock(&dev->slock);
258 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count)
260 struct vivid_dev *dev = vb2_get_drv_priv(vq);
263 dprintk(dev, 1, "%s\n", __func__);
264 dev->sdr_cap_seq_count = 0;
265 if (dev->start_streaming_error) {
266 dev->start_streaming_error = false;
268 } else if (dev->kthread_sdr_cap == NULL) {
269 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev,
270 "%s-sdr-cap", dev->v4l2_dev.name);
272 if (IS_ERR(dev->kthread_sdr_cap)) {
273 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n");
274 err = PTR_ERR(dev->kthread_sdr_cap);
275 dev->kthread_sdr_cap = NULL;
279 struct vivid_buffer *buf, *tmp;
281 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) {
282 list_del(&buf->list);
283 vb2_buffer_done(&buf->vb.vb2_buf,
284 VB2_BUF_STATE_QUEUED);
290 /* abort streaming and wait for last buffer */
291 static void sdr_cap_stop_streaming(struct vb2_queue *vq)
293 struct vivid_dev *dev = vb2_get_drv_priv(vq);
295 if (dev->kthread_sdr_cap == NULL)
298 while (!list_empty(&dev->sdr_cap_active)) {
299 struct vivid_buffer *buf;
301 buf = list_entry(dev->sdr_cap_active.next,
302 struct vivid_buffer, list);
303 list_del(&buf->list);
304 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
305 &dev->ctrl_hdl_sdr_cap);
306 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
309 /* shutdown control thread */
310 kthread_stop(dev->kthread_sdr_cap);
311 dev->kthread_sdr_cap = NULL;
314 static void sdr_cap_buf_request_complete(struct vb2_buffer *vb)
316 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
318 v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_sdr_cap);
321 const struct vb2_ops vivid_sdr_cap_qops = {
322 .queue_setup = sdr_cap_queue_setup,
323 .buf_prepare = sdr_cap_buf_prepare,
324 .buf_queue = sdr_cap_buf_queue,
325 .start_streaming = sdr_cap_start_streaming,
326 .stop_streaming = sdr_cap_stop_streaming,
327 .buf_request_complete = sdr_cap_buf_request_complete,
328 .wait_prepare = vb2_ops_wait_prepare,
329 .wait_finish = vb2_ops_wait_finish,
332 int vivid_sdr_enum_freq_bands(struct file *file, void *fh,
333 struct v4l2_frequency_band *band)
335 switch (band->tuner) {
337 if (band->index >= ARRAY_SIZE(bands_adc))
339 *band = bands_adc[band->index];
342 if (band->index >= ARRAY_SIZE(bands_fm))
344 *band = bands_fm[band->index];
351 int vivid_sdr_g_frequency(struct file *file, void *fh,
352 struct v4l2_frequency *vf)
354 struct vivid_dev *dev = video_drvdata(file);
358 vf->frequency = dev->sdr_adc_freq;
359 vf->type = V4L2_TUNER_ADC;
362 vf->frequency = dev->sdr_fm_freq;
363 vf->type = V4L2_TUNER_RF;
370 int vivid_sdr_s_frequency(struct file *file, void *fh,
371 const struct v4l2_frequency *vf)
373 struct vivid_dev *dev = video_drvdata(file);
374 unsigned freq = vf->frequency;
379 if (vf->type != V4L2_TUNER_ADC)
381 if (freq < BAND_ADC_0)
383 else if (freq < BAND_ADC_1)
388 freq = clamp_t(unsigned, freq,
389 bands_adc[band].rangelow,
390 bands_adc[band].rangehigh);
392 if (vb2_is_streaming(&dev->vb_sdr_cap_q) &&
393 freq != dev->sdr_adc_freq) {
394 /* resync the thread's timings */
395 dev->sdr_cap_seq_resync = true;
397 dev->sdr_adc_freq = freq;
400 if (vf->type != V4L2_TUNER_RF)
402 dev->sdr_fm_freq = clamp_t(unsigned, freq,
403 bands_fm[0].rangelow,
404 bands_fm[0].rangehigh);
411 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
415 strscpy(vt->name, "ADC", sizeof(vt->name));
416 vt->type = V4L2_TUNER_ADC;
418 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
419 vt->rangelow = bands_adc[0].rangelow;
420 vt->rangehigh = bands_adc[2].rangehigh;
423 strscpy(vt->name, "RF", sizeof(vt->name));
424 vt->type = V4L2_TUNER_RF;
426 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
427 vt->rangelow = bands_fm[0].rangelow;
428 vt->rangehigh = bands_fm[0].rangehigh;
435 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
442 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f)
444 if (f->index >= ARRAY_SIZE(formats))
446 f->pixelformat = formats[f->index].pixelformat;
450 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
452 struct vivid_dev *dev = video_drvdata(file);
454 f->fmt.sdr.pixelformat = dev->sdr_pixelformat;
455 f->fmt.sdr.buffersize = dev->sdr_buffersize;
456 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
460 int vidioc_s_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
462 struct vivid_dev *dev = video_drvdata(file);
463 struct vb2_queue *q = &dev->vb_sdr_cap_q;
469 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
470 for (i = 0; i < ARRAY_SIZE(formats); i++) {
471 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
472 dev->sdr_pixelformat = formats[i].pixelformat;
473 dev->sdr_buffersize = formats[i].buffersize;
474 f->fmt.sdr.buffersize = formats[i].buffersize;
478 dev->sdr_pixelformat = formats[0].pixelformat;
479 dev->sdr_buffersize = formats[0].buffersize;
480 f->fmt.sdr.pixelformat = formats[0].pixelformat;
481 f->fmt.sdr.buffersize = formats[0].buffersize;
485 int vidioc_try_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f)
489 memset(f->fmt.sdr.reserved, 0, sizeof(f->fmt.sdr.reserved));
490 for (i = 0; i < ARRAY_SIZE(formats); i++) {
491 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) {
492 f->fmt.sdr.buffersize = formats[i].buffersize;
496 f->fmt.sdr.pixelformat = formats[0].pixelformat;
497 f->fmt.sdr.buffersize = formats[0].buffersize;
502 #define FIXP_FRAC (1 << FIXP_N)
503 #define FIXP_2PI ((int)(2 * 3.141592653589 * FIXP_FRAC))
504 #define M_100000PI (3.14159 * 100000)
506 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf)
508 u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
510 unsigned long plane_size = vb2_plane_size(&buf->vb.vb2_buf, 0);
517 /* calculate phase step */
518 #define BEEP_FREQ 1000 /* 1kHz beep */
519 src_phase_step = DIV_ROUND_CLOSEST(FIXP_2PI * BEEP_FREQ,
522 for (i = 0; i < plane_size; i += 2) {
523 mod_phase_step = fixp_cos32_rad(dev->sdr_fixp_src_phase,
524 FIXP_2PI) >> (31 - FIXP_N);
526 dev->sdr_fixp_src_phase += src_phase_step;
527 s64tmp = (s64) mod_phase_step * dev->sdr_fm_deviation;
528 dev->sdr_fixp_mod_phase += div_s64(s64tmp, M_100000PI);
531 * Transfer phase angle to [0, 2xPI] in order to avoid variable
532 * overflow and make it suitable for cosine implementation
533 * used, which does not support negative angles.
535 dev->sdr_fixp_src_phase %= FIXP_2PI;
536 dev->sdr_fixp_mod_phase %= FIXP_2PI;
538 if (dev->sdr_fixp_mod_phase < 0)
539 dev->sdr_fixp_mod_phase += FIXP_2PI;
541 fixp_i = fixp_cos32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
542 fixp_q = fixp_sin32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI);
544 /* Normalize fraction values represented with 32 bit precision
545 * to fixed point representation with FIXP_N bits */
546 fixp_i >>= (31 - FIXP_N);
547 fixp_q >>= (31 - FIXP_N);
549 switch (dev->sdr_pixelformat) {
550 case V4L2_SDR_FMT_CU8:
551 /* convert 'fixp float' to u8 [0, +255] */
552 /* u8 = X * 127.5 + 127.5; X is float [-1.0, +1.0] */
553 fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275;
554 fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275;
555 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
556 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);
558 case V4L2_SDR_FMT_CS8:
559 /* convert 'fixp float' to s8 [-128, +127] */
560 /* s8 = X * 127.5 - 0.5; X is float [-1.0, +1.0] */
561 fixp_i = fixp_i * 1275 - FIXP_FRAC * 5;
562 fixp_q = fixp_q * 1275 - FIXP_FRAC * 5;
563 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10);
564 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10);