vivid-sdr-cap.c 15 KB

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