vivid-radio-rx.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * vivid-radio-rx.c - radio receiver 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/videodev2.h>
  23. #include <linux/v4l2-dv-timings.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/v4l2-event.h>
  26. #include <media/v4l2-dv-timings.h>
  27. #include "vivid-core.h"
  28. #include "vivid-ctrls.h"
  29. #include "vivid-radio-common.h"
  30. #include "vivid-rds-gen.h"
  31. #include "vivid-radio-rx.h"
  32. ssize_t vivid_radio_rx_read(struct file *file, char __user *buf,
  33. size_t size, loff_t *offset)
  34. {
  35. struct vivid_dev *dev = video_drvdata(file);
  36. struct timespec ts;
  37. struct v4l2_rds_data *data = dev->rds_gen.data;
  38. bool use_alternates;
  39. unsigned blk;
  40. int perc;
  41. int i;
  42. if (dev->radio_rx_rds_controls)
  43. return -EINVAL;
  44. if (size < sizeof(*data))
  45. return 0;
  46. size = sizeof(*data) * (size / sizeof(*data));
  47. if (mutex_lock_interruptible(&dev->mutex))
  48. return -ERESTARTSYS;
  49. if (dev->radio_rx_rds_owner &&
  50. file->private_data != dev->radio_rx_rds_owner) {
  51. mutex_unlock(&dev->mutex);
  52. return -EBUSY;
  53. }
  54. if (dev->radio_rx_rds_owner == NULL) {
  55. vivid_radio_rds_init(dev);
  56. dev->radio_rx_rds_owner = file->private_data;
  57. }
  58. retry:
  59. ktime_get_ts(&ts);
  60. use_alternates = ts.tv_sec % 10 >= 5;
  61. if (dev->radio_rx_rds_last_block == 0 ||
  62. dev->radio_rx_rds_use_alternates != use_alternates) {
  63. dev->radio_rx_rds_use_alternates = use_alternates;
  64. /* Re-init the RDS generator */
  65. vivid_radio_rds_init(dev);
  66. }
  67. ts = timespec_sub(ts, dev->radio_rds_init_ts);
  68. blk = ts.tv_sec * 100 + ts.tv_nsec / 10000000;
  69. blk = (blk * VIVID_RDS_GEN_BLOCKS) / 500;
  70. if (blk >= dev->radio_rx_rds_last_block + VIVID_RDS_GEN_BLOCKS)
  71. dev->radio_rx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1;
  72. /*
  73. * No data is available if there hasn't been time to get new data,
  74. * or if the RDS receiver has been disabled, or if we use the data
  75. * from the RDS transmitter and that RDS transmitter has been disabled,
  76. * or if the signal quality is too weak.
  77. */
  78. if (blk == dev->radio_rx_rds_last_block || !dev->radio_rx_rds_enabled ||
  79. (dev->radio_rds_loop && !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) ||
  80. abs(dev->radio_rx_sig_qual) > 200) {
  81. mutex_unlock(&dev->mutex);
  82. if (file->f_flags & O_NONBLOCK)
  83. return -EWOULDBLOCK;
  84. if (msleep_interruptible(20) && signal_pending(current))
  85. return -EINTR;
  86. if (mutex_lock_interruptible(&dev->mutex))
  87. return -ERESTARTSYS;
  88. goto retry;
  89. }
  90. /* abs(dev->radio_rx_sig_qual) <= 200, map that to a 0-50% range */
  91. perc = abs(dev->radio_rx_sig_qual) / 4;
  92. for (i = 0; i < size && blk > dev->radio_rx_rds_last_block;
  93. dev->radio_rx_rds_last_block++) {
  94. unsigned data_blk = dev->radio_rx_rds_last_block % VIVID_RDS_GEN_BLOCKS;
  95. struct v4l2_rds_data rds = data[data_blk];
  96. if (data_blk == 0 && dev->radio_rds_loop)
  97. vivid_radio_rds_init(dev);
  98. if (perc && prandom_u32_max(100) < perc) {
  99. switch (prandom_u32_max(4)) {
  100. case 0:
  101. rds.block |= V4L2_RDS_BLOCK_CORRECTED;
  102. break;
  103. case 1:
  104. rds.block |= V4L2_RDS_BLOCK_INVALID;
  105. break;
  106. case 2:
  107. rds.block |= V4L2_RDS_BLOCK_ERROR;
  108. rds.lsb = prandom_u32_max(256);
  109. rds.msb = prandom_u32_max(256);
  110. break;
  111. case 3: /* Skip block altogether */
  112. if (i)
  113. continue;
  114. /*
  115. * Must make sure at least one block is
  116. * returned, otherwise the application
  117. * might think that end-of-file occurred.
  118. */
  119. break;
  120. }
  121. }
  122. if (copy_to_user(buf + i, &rds, sizeof(rds))) {
  123. i = -EFAULT;
  124. break;
  125. }
  126. i += sizeof(rds);
  127. }
  128. mutex_unlock(&dev->mutex);
  129. return i;
  130. }
  131. unsigned int vivid_radio_rx_poll(struct file *file, struct poll_table_struct *wait)
  132. {
  133. return POLLIN | POLLRDNORM | v4l2_ctrl_poll(file, wait);
  134. }
  135. int vivid_radio_rx_enum_freq_bands(struct file *file, void *fh, struct v4l2_frequency_band *band)
  136. {
  137. if (band->tuner != 0)
  138. return -EINVAL;
  139. if (band->index >= TOT_BANDS)
  140. return -EINVAL;
  141. *band = vivid_radio_bands[band->index];
  142. return 0;
  143. }
  144. int vivid_radio_rx_s_hw_freq_seek(struct file *file, void *fh, const struct v4l2_hw_freq_seek *a)
  145. {
  146. struct vivid_dev *dev = video_drvdata(file);
  147. unsigned low, high;
  148. unsigned freq;
  149. unsigned spacing;
  150. unsigned band;
  151. if (a->tuner)
  152. return -EINVAL;
  153. if (a->wrap_around && dev->radio_rx_hw_seek_mode == VIVID_HW_SEEK_BOUNDED)
  154. return -EINVAL;
  155. if (!a->wrap_around && dev->radio_rx_hw_seek_mode == VIVID_HW_SEEK_WRAP)
  156. return -EINVAL;
  157. if (!a->rangelow ^ !a->rangehigh)
  158. return -EINVAL;
  159. if (file->f_flags & O_NONBLOCK)
  160. return -EWOULDBLOCK;
  161. if (a->rangelow) {
  162. for (band = 0; band < TOT_BANDS; band++)
  163. if (a->rangelow >= vivid_radio_bands[band].rangelow &&
  164. a->rangehigh <= vivid_radio_bands[band].rangehigh)
  165. break;
  166. if (band == TOT_BANDS)
  167. return -EINVAL;
  168. if (!dev->radio_rx_hw_seek_prog_lim &&
  169. (a->rangelow != vivid_radio_bands[band].rangelow ||
  170. a->rangehigh != vivid_radio_bands[band].rangehigh))
  171. return -EINVAL;
  172. low = a->rangelow;
  173. high = a->rangehigh;
  174. } else {
  175. for (band = 0; band < TOT_BANDS; band++)
  176. if (dev->radio_rx_freq >= vivid_radio_bands[band].rangelow &&
  177. dev->radio_rx_freq <= vivid_radio_bands[band].rangehigh)
  178. break;
  179. if (band == TOT_BANDS)
  180. return -EINVAL;
  181. low = vivid_radio_bands[band].rangelow;
  182. high = vivid_radio_bands[band].rangehigh;
  183. }
  184. spacing = band == BAND_AM ? 1600 : 16000;
  185. freq = clamp(dev->radio_rx_freq, low, high);
  186. if (a->seek_upward) {
  187. freq = spacing * (freq / spacing) + spacing;
  188. if (freq > high) {
  189. if (!a->wrap_around)
  190. return -ENODATA;
  191. freq = spacing * (low / spacing) + spacing;
  192. if (freq >= dev->radio_rx_freq)
  193. return -ENODATA;
  194. }
  195. } else {
  196. freq = spacing * ((freq + spacing - 1) / spacing) - spacing;
  197. if (freq < low) {
  198. if (!a->wrap_around)
  199. return -ENODATA;
  200. freq = spacing * ((high + spacing - 1) / spacing) - spacing;
  201. if (freq <= dev->radio_rx_freq)
  202. return -ENODATA;
  203. }
  204. }
  205. return 0;
  206. }
  207. int vivid_radio_rx_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
  208. {
  209. struct vivid_dev *dev = video_drvdata(file);
  210. int delta = 800;
  211. int sig_qual;
  212. if (vt->index > 0)
  213. return -EINVAL;
  214. strlcpy(vt->name, "AM/FM/SW Receiver", sizeof(vt->name));
  215. vt->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  216. V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS |
  217. (dev->radio_rx_rds_controls ?
  218. V4L2_TUNER_CAP_RDS_CONTROLS :
  219. V4L2_TUNER_CAP_RDS_BLOCK_IO) |
  220. (dev->radio_rx_hw_seek_prog_lim ?
  221. V4L2_TUNER_CAP_HWSEEK_PROG_LIM : 0);
  222. switch (dev->radio_rx_hw_seek_mode) {
  223. case VIVID_HW_SEEK_BOUNDED:
  224. vt->capability |= V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  225. break;
  226. case VIVID_HW_SEEK_WRAP:
  227. vt->capability |= V4L2_TUNER_CAP_HWSEEK_WRAP;
  228. break;
  229. case VIVID_HW_SEEK_BOTH:
  230. vt->capability |= V4L2_TUNER_CAP_HWSEEK_WRAP |
  231. V4L2_TUNER_CAP_HWSEEK_BOUNDED;
  232. break;
  233. }
  234. vt->rangelow = AM_FREQ_RANGE_LOW;
  235. vt->rangehigh = FM_FREQ_RANGE_HIGH;
  236. sig_qual = dev->radio_rx_sig_qual;
  237. vt->signal = abs(sig_qual) > delta ? 0 :
  238. 0xffff - (abs(sig_qual) * 0xffff) / delta;
  239. vt->afc = sig_qual > delta ? 0 : sig_qual;
  240. if (abs(sig_qual) > delta)
  241. vt->rxsubchans = 0;
  242. else if (dev->radio_rx_freq < FM_FREQ_RANGE_LOW || vt->signal < 0x8000)
  243. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  244. else if (dev->radio_rds_loop && !(dev->radio_tx_subchans & V4L2_TUNER_SUB_STEREO))
  245. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  246. else
  247. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  248. if (dev->radio_rx_rds_enabled &&
  249. (!dev->radio_rds_loop || (dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) &&
  250. dev->radio_rx_freq >= FM_FREQ_RANGE_LOW && vt->signal >= 0xc000)
  251. vt->rxsubchans |= V4L2_TUNER_SUB_RDS;
  252. if (dev->radio_rx_rds_controls)
  253. vivid_radio_rds_init(dev);
  254. vt->audmode = dev->radio_rx_audmode;
  255. return 0;
  256. }
  257. int vivid_radio_rx_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
  258. {
  259. struct vivid_dev *dev = video_drvdata(file);
  260. if (vt->index)
  261. return -EINVAL;
  262. dev->radio_rx_audmode = vt->audmode >= V4L2_TUNER_MODE_STEREO;
  263. return 0;
  264. }