vivid-sdr-cap.c 15 KB

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