vivid-sdr-cap.c 15 KB

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