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