vivid-radio-tx.c 4.2 KB

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