vivid-radio-tx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * vivid-radio-tx.c - radio transmitter 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/sched/signal.h>
  10. #include <linux/delay.h>
  11. #include <linux/videodev2.h>
  12. #include <linux/v4l2-dv-timings.h>
  13. #include <media/v4l2-common.h>
  14. #include <media/v4l2-event.h>
  15. #include <media/v4l2-dv-timings.h>
  16. #include "vivid-core.h"
  17. #include "vivid-ctrls.h"
  18. #include "vivid-radio-common.h"
  19. #include "vivid-radio-tx.h"
  20. ssize_t vivid_radio_tx_write(struct file *file, const char __user *buf,
  21. size_t size, loff_t *offset)
  22. {
  23. struct vivid_dev *dev = video_drvdata(file);
  24. struct v4l2_rds_data *data = dev->rds_gen.data;
  25. ktime_t timestamp;
  26. unsigned blk;
  27. int i;
  28. if (dev->radio_tx_rds_controls)
  29. return -EINVAL;
  30. if (size < sizeof(*data))
  31. return -EINVAL;
  32. size = sizeof(*data) * (size / sizeof(*data));
  33. if (mutex_lock_interruptible(&dev->mutex))
  34. return -ERESTARTSYS;
  35. if (dev->radio_tx_rds_owner &&
  36. file->private_data != dev->radio_tx_rds_owner) {
  37. mutex_unlock(&dev->mutex);
  38. return -EBUSY;
  39. }
  40. dev->radio_tx_rds_owner = file->private_data;
  41. retry:
  42. timestamp = ktime_sub(ktime_get(), dev->radio_rds_init_time);
  43. blk = ktime_divns(timestamp, VIVID_RDS_NSEC_PER_BLK);
  44. if (blk - VIVID_RDS_GEN_BLOCKS >= dev->radio_tx_rds_last_block)
  45. dev->radio_tx_rds_last_block = blk - VIVID_RDS_GEN_BLOCKS + 1;
  46. /*
  47. * No data is available if there hasn't been time to get new data,
  48. * or if the RDS receiver has been disabled, or if we use the data
  49. * from the RDS transmitter and that RDS transmitter has been disabled,
  50. * or if the signal quality is too weak.
  51. */
  52. if (blk == dev->radio_tx_rds_last_block ||
  53. !(dev->radio_tx_subchans & V4L2_TUNER_SUB_RDS)) {
  54. mutex_unlock(&dev->mutex);
  55. if (file->f_flags & O_NONBLOCK)
  56. return -EWOULDBLOCK;
  57. if (msleep_interruptible(20) && signal_pending(current))
  58. return -EINTR;
  59. if (mutex_lock_interruptible(&dev->mutex))
  60. return -ERESTARTSYS;
  61. goto retry;
  62. }
  63. for (i = 0; i < size && blk > dev->radio_tx_rds_last_block;
  64. dev->radio_tx_rds_last_block++) {
  65. unsigned data_blk = dev->radio_tx_rds_last_block % VIVID_RDS_GEN_BLOCKS;
  66. struct v4l2_rds_data rds;
  67. if (copy_from_user(&rds, buf + i, sizeof(rds))) {
  68. i = -EFAULT;
  69. break;
  70. }
  71. i += sizeof(rds);
  72. if (!dev->radio_rds_loop)
  73. continue;
  74. if ((rds.block & V4L2_RDS_BLOCK_MSK) == V4L2_RDS_BLOCK_INVALID ||
  75. (rds.block & V4L2_RDS_BLOCK_ERROR))
  76. continue;
  77. rds.block &= V4L2_RDS_BLOCK_MSK;
  78. data[data_blk] = rds;
  79. }
  80. mutex_unlock(&dev->mutex);
  81. return i;
  82. }
  83. __poll_t vivid_radio_tx_poll(struct file *file, struct poll_table_struct *wait)
  84. {
  85. return EPOLLOUT | EPOLLWRNORM | v4l2_ctrl_poll(file, wait);
  86. }
  87. int vidioc_g_modulator(struct file *file, void *fh, struct v4l2_modulator *a)
  88. {
  89. struct vivid_dev *dev = video_drvdata(file);
  90. if (a->index > 0)
  91. return -EINVAL;
  92. strlcpy(a->name, "AM/FM/SW Transmitter", sizeof(a->name));
  93. a->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
  94. V4L2_TUNER_CAP_FREQ_BANDS | V4L2_TUNER_CAP_RDS |
  95. (dev->radio_tx_rds_controls ?
  96. V4L2_TUNER_CAP_RDS_CONTROLS :
  97. V4L2_TUNER_CAP_RDS_BLOCK_IO);
  98. a->rangelow = AM_FREQ_RANGE_LOW;
  99. a->rangehigh = FM_FREQ_RANGE_HIGH;
  100. a->txsubchans = dev->radio_tx_subchans;
  101. return 0;
  102. }
  103. int vidioc_s_modulator(struct file *file, void *fh, const struct v4l2_modulator *a)
  104. {
  105. struct vivid_dev *dev = video_drvdata(file);
  106. if (a->index)
  107. return -EINVAL;
  108. if (a->txsubchans & ~0x13)
  109. return -EINVAL;
  110. dev->radio_tx_subchans = a->txsubchans;
  111. return 0;
  112. }