1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * vivid-sdr-cap.c - software defined radio support functions. 4 * 5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved. 6 */ 7 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> 20 21 #include "vivid-core.h" 22 #include "vivid-ctrls.h" 23 #include "vivid-sdr-cap.h" 24 25 /* stream formats */ 26 struct vivid_format { 27 u32 pixelformat; 28 u32 buffersize; 29 }; 30 31 /* format descriptions for capture and preview */ 32 static const struct vivid_format formats[] = { 33 { 34 .pixelformat = V4L2_SDR_FMT_CU8, 35 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2, 36 }, { 37 .pixelformat = V4L2_SDR_FMT_CS8, 38 .buffersize = SDR_CAP_SAMPLES_PER_BUF * 2, 39 }, 40 }; 41 42 static const struct v4l2_frequency_band bands_adc[] = { 43 { 44 .tuner = 0, 45 .type = V4L2_TUNER_ADC, 46 .index = 0, 47 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 48 .rangelow = 300000, 49 .rangehigh = 300000, 50 }, 51 { 52 .tuner = 0, 53 .type = V4L2_TUNER_ADC, 54 .index = 1, 55 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 56 .rangelow = 900001, 57 .rangehigh = 2800000, 58 }, 59 { 60 .tuner = 0, 61 .type = V4L2_TUNER_ADC, 62 .index = 2, 63 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 64 .rangelow = 3200000, 65 .rangehigh = 3200000, 66 }, 67 }; 68 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) 72 73 static const struct v4l2_frequency_band bands_fm[] = { 74 { 75 .tuner = 1, 76 .type = V4L2_TUNER_RF, 77 .index = 0, 78 .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS, 79 .rangelow = 50000000, 80 .rangehigh = 2000000000, 81 }, 82 }; 83 84 static void vivid_thread_sdr_cap_tick(struct vivid_dev *dev) 85 { 86 struct vivid_buffer *sdr_cap_buf = NULL; 87 88 dprintk(dev, 1, "SDR Capture Thread Tick\n"); 89 90 /* Drop a certain percentage of buffers. */ 91 if (dev->perc_dropped_buffers && 92 prandom_u32_max(100) < dev->perc_dropped_buffers) 93 return; 94 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); 100 } 101 spin_unlock(&dev->slock); 102 103 if (sdr_cap_buf) { 104 sdr_cap_buf->vb.sequence = dev->sdr_cap_with_seq_wrap_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; 115 } 116 } 117 118 static int vivid_thread_sdr_cap(void *data) 119 { 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; 127 128 dprintk(dev, 1, "SDR Capture Thread Start\n"); 129 130 set_freezable(); 131 132 /* Resets frame counters */ 133 dev->sdr_cap_seq_offset = 0; 134 dev->sdr_cap_seq_count = 0; 135 dev->jiffies_sdr_cap = jiffies; 136 dev->sdr_cap_seq_resync = false; 137 if (dev->time_wrap) 138 dev->time_wrap_offset = dev->time_wrap - ktime_get_ns(); 139 else 140 dev->time_wrap_offset = 0; 141 142 for (;;) { 143 try_to_freeze(); 144 if (kthread_should_stop()) 145 break; 146 147 if (!mutex_trylock(&dev->mutex)) { 148 schedule(); 149 continue; 150 } 151 152 cur_jiffies = jiffies; 153 if (dev->sdr_cap_seq_resync) { 154 dev->jiffies_sdr_cap = cur_jiffies; 155 dev->sdr_cap_seq_offset = dev->sdr_cap_seq_count + 1; 156 dev->sdr_cap_seq_count = 0; 157 dev->sdr_cap_seq_resync = false; 158 } 159 /* Calculate the number of jiffies since we started streaming */ 160 jiffies_since_start = cur_jiffies - dev->jiffies_sdr_cap; 161 /* Get the number of buffers streamed since the start */ 162 buffers_since_start = 163 (u64)jiffies_since_start * dev->sdr_adc_freq + 164 (HZ * SDR_CAP_SAMPLES_PER_BUF) / 2; 165 do_div(buffers_since_start, HZ * SDR_CAP_SAMPLES_PER_BUF); 166 167 /* 168 * After more than 0xf0000000 (rounded down to a multiple of 169 * 'jiffies-per-day' to ease jiffies_to_msecs calculation) 170 * jiffies have passed since we started streaming reset the 171 * counters and keep track of the sequence offset. 172 */ 173 if (jiffies_since_start > JIFFIES_RESYNC) { 174 dev->jiffies_sdr_cap = cur_jiffies; 175 dev->sdr_cap_seq_offset = buffers_since_start; 176 buffers_since_start = 0; 177 } 178 dev->sdr_cap_seq_count = 179 buffers_since_start + dev->sdr_cap_seq_offset; 180 dev->sdr_cap_with_seq_wrap_count = dev->sdr_cap_seq_count - dev->sdr_cap_seq_start; 181 182 vivid_thread_sdr_cap_tick(dev); 183 mutex_unlock(&dev->mutex); 184 185 /* 186 * Calculate the number of samples streamed since we started, 187 * not including the current buffer. 188 */ 189 samples_since_start = buffers_since_start * SDR_CAP_SAMPLES_PER_BUF; 190 191 /* And the number of jiffies since we started */ 192 jiffies_since_start = jiffies - dev->jiffies_sdr_cap; 193 194 /* Increase by the number of samples in one buffer */ 195 samples_since_start += SDR_CAP_SAMPLES_PER_BUF; 196 /* 197 * Calculate when that next buffer is supposed to start 198 * in jiffies since we started streaming. 199 */ 200 next_jiffies_since_start = samples_since_start * HZ + 201 dev->sdr_adc_freq / 2; 202 do_div(next_jiffies_since_start, dev->sdr_adc_freq); 203 /* If it is in the past, then just schedule asap */ 204 if (next_jiffies_since_start < jiffies_since_start) 205 next_jiffies_since_start = jiffies_since_start; 206 207 wait_jiffies = next_jiffies_since_start - jiffies_since_start; 208 while (jiffies - cur_jiffies < wait_jiffies && 209 !kthread_should_stop()) 210 schedule(); 211 } 212 dprintk(dev, 1, "SDR Capture Thread End\n"); 213 return 0; 214 } 215 216 static int sdr_cap_queue_setup(struct vb2_queue *vq, 217 unsigned *nbuffers, unsigned *nplanes, 218 unsigned sizes[], struct device *alloc_devs[]) 219 { 220 /* 2 = max 16-bit sample returned */ 221 sizes[0] = SDR_CAP_SAMPLES_PER_BUF * 2; 222 *nplanes = 1; 223 return 0; 224 } 225 226 static int sdr_cap_buf_prepare(struct vb2_buffer *vb) 227 { 228 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 229 unsigned size = SDR_CAP_SAMPLES_PER_BUF * 2; 230 231 dprintk(dev, 1, "%s\n", __func__); 232 233 if (dev->buf_prepare_error) { 234 /* 235 * Error injection: test what happens if buf_prepare() returns 236 * an error. 237 */ 238 dev->buf_prepare_error = false; 239 return -EINVAL; 240 } 241 if (vb2_plane_size(vb, 0) < size) { 242 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n", 243 __func__, vb2_plane_size(vb, 0), size); 244 return -EINVAL; 245 } 246 vb2_set_plane_payload(vb, 0, size); 247 248 return 0; 249 } 250 251 static void sdr_cap_buf_queue(struct vb2_buffer *vb) 252 { 253 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 254 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 255 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb); 256 257 dprintk(dev, 1, "%s\n", __func__); 258 259 spin_lock(&dev->slock); 260 list_add_tail(&buf->list, &dev->sdr_cap_active); 261 spin_unlock(&dev->slock); 262 } 263 264 static int sdr_cap_start_streaming(struct vb2_queue *vq, unsigned count) 265 { 266 struct vivid_dev *dev = vb2_get_drv_priv(vq); 267 int err = 0; 268 269 dprintk(dev, 1, "%s\n", __func__); 270 dev->sdr_cap_seq_start = dev->seq_wrap * 128; 271 if (dev->start_streaming_error) { 272 dev->start_streaming_error = false; 273 err = -EINVAL; 274 } else if (dev->kthread_sdr_cap == NULL) { 275 dev->kthread_sdr_cap = kthread_run(vivid_thread_sdr_cap, dev, 276 "%s-sdr-cap", dev->v4l2_dev.name); 277 278 if (IS_ERR(dev->kthread_sdr_cap)) { 279 v4l2_err(&dev->v4l2_dev, "kernel_thread() failed\n"); 280 err = PTR_ERR(dev->kthread_sdr_cap); 281 dev->kthread_sdr_cap = NULL; 282 } 283 } 284 if (err) { 285 struct vivid_buffer *buf, *tmp; 286 287 list_for_each_entry_safe(buf, tmp, &dev->sdr_cap_active, list) { 288 list_del(&buf->list); 289 vb2_buffer_done(&buf->vb.vb2_buf, 290 VB2_BUF_STATE_QUEUED); 291 } 292 } 293 return err; 294 } 295 296 /* abort streaming and wait for last buffer */ 297 static void sdr_cap_stop_streaming(struct vb2_queue *vq) 298 { 299 struct vivid_dev *dev = vb2_get_drv_priv(vq); 300 301 if (dev->kthread_sdr_cap == NULL) 302 return; 303 304 while (!list_empty(&dev->sdr_cap_active)) { 305 struct vivid_buffer *buf; 306 307 buf = list_entry(dev->sdr_cap_active.next, 308 struct vivid_buffer, list); 309 list_del(&buf->list); 310 v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req, 311 &dev->ctrl_hdl_sdr_cap); 312 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); 313 } 314 315 /* shutdown control thread */ 316 kthread_stop(dev->kthread_sdr_cap); 317 dev->kthread_sdr_cap = NULL; 318 } 319 320 static void sdr_cap_buf_request_complete(struct vb2_buffer *vb) 321 { 322 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue); 323 324 v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_sdr_cap); 325 } 326 327 const struct vb2_ops vivid_sdr_cap_qops = { 328 .queue_setup = sdr_cap_queue_setup, 329 .buf_prepare = sdr_cap_buf_prepare, 330 .buf_queue = sdr_cap_buf_queue, 331 .start_streaming = sdr_cap_start_streaming, 332 .stop_streaming = sdr_cap_stop_streaming, 333 .buf_request_complete = sdr_cap_buf_request_complete, 334 .wait_prepare = vb2_ops_wait_prepare, 335 .wait_finish = vb2_ops_wait_finish, 336 }; 337 338 int vivid_sdr_enum_freq_bands(struct file *file, void *fh, 339 struct v4l2_frequency_band *band) 340 { 341 switch (band->tuner) { 342 case 0: 343 if (band->index >= ARRAY_SIZE(bands_adc)) 344 return -EINVAL; 345 *band = bands_adc[band->index]; 346 return 0; 347 case 1: 348 if (band->index >= ARRAY_SIZE(bands_fm)) 349 return -EINVAL; 350 *band = bands_fm[band->index]; 351 return 0; 352 default: 353 return -EINVAL; 354 } 355 } 356 357 int vivid_sdr_g_frequency(struct file *file, void *fh, 358 struct v4l2_frequency *vf) 359 { 360 struct vivid_dev *dev = video_drvdata(file); 361 362 switch (vf->tuner) { 363 case 0: 364 vf->frequency = dev->sdr_adc_freq; 365 vf->type = V4L2_TUNER_ADC; 366 return 0; 367 case 1: 368 vf->frequency = dev->sdr_fm_freq; 369 vf->type = V4L2_TUNER_RF; 370 return 0; 371 default: 372 return -EINVAL; 373 } 374 } 375 376 int vivid_sdr_s_frequency(struct file *file, void *fh, 377 const struct v4l2_frequency *vf) 378 { 379 struct vivid_dev *dev = video_drvdata(file); 380 unsigned freq = vf->frequency; 381 unsigned band; 382 383 switch (vf->tuner) { 384 case 0: 385 if (vf->type != V4L2_TUNER_ADC) 386 return -EINVAL; 387 if (freq < BAND_ADC_0) 388 band = 0; 389 else if (freq < BAND_ADC_1) 390 band = 1; 391 else 392 band = 2; 393 394 freq = clamp_t(unsigned, freq, 395 bands_adc[band].rangelow, 396 bands_adc[band].rangehigh); 397 398 if (vb2_is_streaming(&dev->vb_sdr_cap_q) && 399 freq != dev->sdr_adc_freq) { 400 /* resync the thread's timings */ 401 dev->sdr_cap_seq_resync = true; 402 } 403 dev->sdr_adc_freq = freq; 404 return 0; 405 case 1: 406 if (vf->type != V4L2_TUNER_RF) 407 return -EINVAL; 408 dev->sdr_fm_freq = clamp_t(unsigned, freq, 409 bands_fm[0].rangelow, 410 bands_fm[0].rangehigh); 411 return 0; 412 default: 413 return -EINVAL; 414 } 415 } 416 417 int vivid_sdr_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt) 418 { 419 switch (vt->index) { 420 case 0: 421 strscpy(vt->name, "ADC", sizeof(vt->name)); 422 vt->type = V4L2_TUNER_ADC; 423 vt->capability = 424 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 425 vt->rangelow = bands_adc[0].rangelow; 426 vt->rangehigh = bands_adc[2].rangehigh; 427 return 0; 428 case 1: 429 strscpy(vt->name, "RF", sizeof(vt->name)); 430 vt->type = V4L2_TUNER_RF; 431 vt->capability = 432 V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS; 433 vt->rangelow = bands_fm[0].rangelow; 434 vt->rangehigh = bands_fm[0].rangehigh; 435 return 0; 436 default: 437 return -EINVAL; 438 } 439 } 440 441 int vivid_sdr_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt) 442 { 443 if (vt->index > 1) 444 return -EINVAL; 445 return 0; 446 } 447 448 int vidioc_enum_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_fmtdesc *f) 449 { 450 if (f->index >= ARRAY_SIZE(formats)) 451 return -EINVAL; 452 f->pixelformat = formats[f->index].pixelformat; 453 return 0; 454 } 455 456 int vidioc_g_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f) 457 { 458 struct vivid_dev *dev = video_drvdata(file); 459 460 f->fmt.sdr.pixelformat = dev->sdr_pixelformat; 461 f->fmt.sdr.buffersize = dev->sdr_buffersize; 462 return 0; 463 } 464 465 int vidioc_s_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f) 466 { 467 struct vivid_dev *dev = video_drvdata(file); 468 struct vb2_queue *q = &dev->vb_sdr_cap_q; 469 int i; 470 471 if (vb2_is_busy(q)) 472 return -EBUSY; 473 474 for (i = 0; i < ARRAY_SIZE(formats); i++) { 475 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 476 dev->sdr_pixelformat = formats[i].pixelformat; 477 dev->sdr_buffersize = formats[i].buffersize; 478 f->fmt.sdr.buffersize = formats[i].buffersize; 479 return 0; 480 } 481 } 482 dev->sdr_pixelformat = formats[0].pixelformat; 483 dev->sdr_buffersize = formats[0].buffersize; 484 f->fmt.sdr.pixelformat = formats[0].pixelformat; 485 f->fmt.sdr.buffersize = formats[0].buffersize; 486 return 0; 487 } 488 489 int vidioc_try_fmt_sdr_cap(struct file *file, void *fh, struct v4l2_format *f) 490 { 491 int i; 492 493 for (i = 0; i < ARRAY_SIZE(formats); i++) { 494 if (formats[i].pixelformat == f->fmt.sdr.pixelformat) { 495 f->fmt.sdr.buffersize = formats[i].buffersize; 496 return 0; 497 } 498 } 499 f->fmt.sdr.pixelformat = formats[0].pixelformat; 500 f->fmt.sdr.buffersize = formats[0].buffersize; 501 return 0; 502 } 503 504 #define FIXP_N (15) 505 #define FIXP_FRAC (1 << FIXP_N) 506 #define FIXP_2PI ((int)(2 * 3.141592653589 * FIXP_FRAC)) 507 #define M_100000PI (3.14159 * 100000) 508 509 void vivid_sdr_cap_process(struct vivid_dev *dev, struct vivid_buffer *buf) 510 { 511 u8 *vbuf = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); 512 unsigned long i; 513 unsigned long plane_size = vb2_plane_size(&buf->vb.vb2_buf, 0); 514 s64 s64tmp; 515 s32 src_phase_step; 516 s32 mod_phase_step; 517 s32 fixp_i; 518 s32 fixp_q; 519 520 /* calculate phase step */ 521 #define BEEP_FREQ 1000 /* 1kHz beep */ 522 src_phase_step = DIV_ROUND_CLOSEST(FIXP_2PI * BEEP_FREQ, 523 dev->sdr_adc_freq); 524 525 for (i = 0; i < plane_size; i += 2) { 526 mod_phase_step = fixp_cos32_rad(dev->sdr_fixp_src_phase, 527 FIXP_2PI) >> (31 - FIXP_N); 528 529 dev->sdr_fixp_src_phase += src_phase_step; 530 s64tmp = (s64) mod_phase_step * dev->sdr_fm_deviation; 531 dev->sdr_fixp_mod_phase += div_s64(s64tmp, M_100000PI); 532 533 /* 534 * Transfer phase angle to [0, 2xPI] in order to avoid variable 535 * overflow and make it suitable for cosine implementation 536 * used, which does not support negative angles. 537 */ 538 dev->sdr_fixp_src_phase %= FIXP_2PI; 539 dev->sdr_fixp_mod_phase %= FIXP_2PI; 540 541 if (dev->sdr_fixp_mod_phase < 0) 542 dev->sdr_fixp_mod_phase += FIXP_2PI; 543 544 fixp_i = fixp_cos32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI); 545 fixp_q = fixp_sin32_rad(dev->sdr_fixp_mod_phase, FIXP_2PI); 546 547 /* Normalize fraction values represented with 32 bit precision 548 * to fixed point representation with FIXP_N bits */ 549 fixp_i >>= (31 - FIXP_N); 550 fixp_q >>= (31 - FIXP_N); 551 552 switch (dev->sdr_pixelformat) { 553 case V4L2_SDR_FMT_CU8: 554 /* convert 'fixp float' to u8 [0, +255] */ 555 /* u8 = X * 127.5 + 127.5; X is float [-1.0, +1.0] */ 556 fixp_i = fixp_i * 1275 + FIXP_FRAC * 1275; 557 fixp_q = fixp_q * 1275 + FIXP_FRAC * 1275; 558 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10); 559 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10); 560 break; 561 case V4L2_SDR_FMT_CS8: 562 /* convert 'fixp float' to s8 [-128, +127] */ 563 /* s8 = X * 127.5 - 0.5; X is float [-1.0, +1.0] */ 564 fixp_i = fixp_i * 1275 - FIXP_FRAC * 5; 565 fixp_q = fixp_q * 1275 - FIXP_FRAC * 5; 566 *vbuf++ = DIV_ROUND_CLOSEST(fixp_i, FIXP_FRAC * 10); 567 *vbuf++ = DIV_ROUND_CLOSEST(fixp_q, FIXP_FRAC * 10); 568 break; 569 default: 570 break; 571 } 572 } 573 } 574