vivid-sdr-cap.c 14 KB

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