vivid-radio-tx.c 4.2 KB

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