vivid-sdr-cap.c 15 KB

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