OSDN Git Service

ixgbe: use mii_bus to handle MII related ioctls
[uclinux-h8/linux.git] / drivers / media / platform / vivid / vivid-vbi-out.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vbi-out.c - vbi output support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/videodev2.h>
11 #include <media/v4l2-common.h>
12
13 #include "vivid-core.h"
14 #include "vivid-kthread-out.h"
15 #include "vivid-vbi-out.h"
16 #include "vivid-vbi-cap.h"
17
18 static int vbi_out_queue_setup(struct vb2_queue *vq,
19                        unsigned *nbuffers, unsigned *nplanes,
20                        unsigned sizes[], struct device *alloc_devs[])
21 {
22         struct vivid_dev *dev = vb2_get_drv_priv(vq);
23         bool is_60hz = dev->std_out & V4L2_STD_525_60;
24         unsigned size = vq->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
25                 36 * sizeof(struct v4l2_sliced_vbi_data) :
26                 1440 * 2 * (is_60hz ? 12 : 18);
27
28         if (!vivid_is_svid_out(dev))
29                 return -EINVAL;
30
31         sizes[0] = size;
32
33         if (vq->num_buffers + *nbuffers < 2)
34                 *nbuffers = 2 - vq->num_buffers;
35
36         *nplanes = 1;
37         return 0;
38 }
39
40 static int vbi_out_buf_prepare(struct vb2_buffer *vb)
41 {
42         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
43         bool is_60hz = dev->std_out & V4L2_STD_525_60;
44         unsigned size = vb->vb2_queue->type == V4L2_BUF_TYPE_SLICED_VBI_OUTPUT ?
45                 36 * sizeof(struct v4l2_sliced_vbi_data) :
46                 1440 * 2 * (is_60hz ? 12 : 18);
47
48         dprintk(dev, 1, "%s\n", __func__);
49
50         if (dev->buf_prepare_error) {
51                 /*
52                  * Error injection: test what happens if buf_prepare() returns
53                  * an error.
54                  */
55                 dev->buf_prepare_error = false;
56                 return -EINVAL;
57         }
58         if (vb2_plane_size(vb, 0) < size) {
59                 dprintk(dev, 1, "%s data will not fit into plane (%lu < %u)\n",
60                                 __func__, vb2_plane_size(vb, 0), size);
61                 return -EINVAL;
62         }
63         vb2_set_plane_payload(vb, 0, size);
64
65         return 0;
66 }
67
68 static void vbi_out_buf_queue(struct vb2_buffer *vb)
69 {
70         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
71         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
72         struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
73
74         dprintk(dev, 1, "%s\n", __func__);
75
76         spin_lock(&dev->slock);
77         list_add_tail(&buf->list, &dev->vbi_out_active);
78         spin_unlock(&dev->slock);
79 }
80
81 static int vbi_out_start_streaming(struct vb2_queue *vq, unsigned count)
82 {
83         struct vivid_dev *dev = vb2_get_drv_priv(vq);
84         int err;
85
86         dprintk(dev, 1, "%s\n", __func__);
87         dev->vbi_out_seq_count = 0;
88         if (dev->start_streaming_error) {
89                 dev->start_streaming_error = false;
90                 err = -EINVAL;
91         } else {
92                 err = vivid_start_generating_vid_out(dev, &dev->vbi_out_streaming);
93         }
94         if (err) {
95                 struct vivid_buffer *buf, *tmp;
96
97                 list_for_each_entry_safe(buf, tmp, &dev->vbi_out_active, list) {
98                         list_del(&buf->list);
99                         v4l2_ctrl_request_complete(buf->vb.vb2_buf.req_obj.req,
100                                                    &dev->ctrl_hdl_vbi_out);
101                         vb2_buffer_done(&buf->vb.vb2_buf,
102                                         VB2_BUF_STATE_QUEUED);
103                 }
104         }
105         return err;
106 }
107
108 /* abort streaming and wait for last buffer */
109 static void vbi_out_stop_streaming(struct vb2_queue *vq)
110 {
111         struct vivid_dev *dev = vb2_get_drv_priv(vq);
112
113         dprintk(dev, 1, "%s\n", __func__);
114         vivid_stop_generating_vid_out(dev, &dev->vbi_out_streaming);
115         dev->vbi_out_have_wss = false;
116         dev->vbi_out_have_cc[0] = false;
117         dev->vbi_out_have_cc[1] = false;
118 }
119
120 static void vbi_out_buf_request_complete(struct vb2_buffer *vb)
121 {
122         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
123
124         v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vbi_out);
125 }
126
127 const struct vb2_ops vivid_vbi_out_qops = {
128         .queue_setup            = vbi_out_queue_setup,
129         .buf_prepare            = vbi_out_buf_prepare,
130         .buf_queue              = vbi_out_buf_queue,
131         .start_streaming        = vbi_out_start_streaming,
132         .stop_streaming         = vbi_out_stop_streaming,
133         .buf_request_complete   = vbi_out_buf_request_complete,
134         .wait_prepare           = vb2_ops_wait_prepare,
135         .wait_finish            = vb2_ops_wait_finish,
136 };
137
138 int vidioc_g_fmt_vbi_out(struct file *file, void *priv,
139                                         struct v4l2_format *f)
140 {
141         struct vivid_dev *dev = video_drvdata(file);
142         struct v4l2_vbi_format *vbi = &f->fmt.vbi;
143         bool is_60hz = dev->std_out & V4L2_STD_525_60;
144
145         if (!vivid_is_svid_out(dev) || !dev->has_raw_vbi_out)
146                 return -EINVAL;
147
148         vbi->sampling_rate = 25000000;
149         vbi->offset = 24;
150         vbi->samples_per_line = 1440;
151         vbi->sample_format = V4L2_PIX_FMT_GREY;
152         vbi->start[0] = is_60hz ? V4L2_VBI_ITU_525_F1_START + 9 : V4L2_VBI_ITU_625_F1_START + 5;
153         vbi->start[1] = is_60hz ? V4L2_VBI_ITU_525_F2_START + 9 : V4L2_VBI_ITU_625_F2_START + 5;
154         vbi->count[0] = vbi->count[1] = is_60hz ? 12 : 18;
155         vbi->flags = dev->vbi_cap_interlaced ? V4L2_VBI_INTERLACED : 0;
156         vbi->reserved[0] = 0;
157         vbi->reserved[1] = 0;
158         return 0;
159 }
160
161 int vidioc_s_fmt_vbi_out(struct file *file, void *priv,
162                                         struct v4l2_format *f)
163 {
164         struct vivid_dev *dev = video_drvdata(file);
165         int ret = vidioc_g_fmt_vbi_out(file, priv, f);
166
167         if (ret)
168                 return ret;
169         if (vb2_is_busy(&dev->vb_vbi_out_q))
170                 return -EBUSY;
171         dev->stream_sliced_vbi_out = false;
172         dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_VBI_OUTPUT;
173         return 0;
174 }
175
176 int vidioc_g_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
177 {
178         struct vivid_dev *dev = video_drvdata(file);
179         struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
180
181         if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
182                 return -EINVAL;
183
184         vivid_fill_service_lines(vbi, dev->service_set_out);
185         return 0;
186 }
187
188 int vidioc_try_fmt_sliced_vbi_out(struct file *file, void *fh, struct v4l2_format *fmt)
189 {
190         struct vivid_dev *dev = video_drvdata(file);
191         struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
192         bool is_60hz = dev->std_out & V4L2_STD_525_60;
193         u32 service_set = vbi->service_set;
194
195         if (!vivid_is_svid_out(dev) || !dev->has_sliced_vbi_out)
196                 return -EINVAL;
197
198         service_set &= is_60hz ? V4L2_SLICED_CAPTION_525 :
199                                  V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
200         vivid_fill_service_lines(vbi, service_set);
201         return 0;
202 }
203
204 int vidioc_s_fmt_sliced_vbi_out(struct file *file, void *fh,
205                 struct v4l2_format *fmt)
206 {
207         struct vivid_dev *dev = video_drvdata(file);
208         struct v4l2_sliced_vbi_format *vbi = &fmt->fmt.sliced;
209         int ret = vidioc_try_fmt_sliced_vbi_out(file, fh, fmt);
210
211         if (ret)
212                 return ret;
213         if (vb2_is_busy(&dev->vb_vbi_out_q))
214                 return -EBUSY;
215         dev->service_set_out = vbi->service_set;
216         dev->stream_sliced_vbi_out = true;
217         dev->vbi_out_dev.queue->type = V4L2_BUF_TYPE_SLICED_VBI_OUTPUT;
218         return 0;
219 }
220
221 void vivid_sliced_vbi_out_process(struct vivid_dev *dev,
222                 struct vivid_buffer *buf)
223 {
224         struct v4l2_sliced_vbi_data *vbi =
225                 vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
226         unsigned elems =
227                 vb2_get_plane_payload(&buf->vb.vb2_buf, 0) / sizeof(*vbi);
228
229         dev->vbi_out_have_cc[0] = false;
230         dev->vbi_out_have_cc[1] = false;
231         dev->vbi_out_have_wss = false;
232         while (elems--) {
233                 switch (vbi->id) {
234                 case V4L2_SLICED_CAPTION_525:
235                         if ((dev->std_out & V4L2_STD_525_60) && vbi->line == 21) {
236                                 dev->vbi_out_have_cc[!!vbi->field] = true;
237                                 dev->vbi_out_cc[!!vbi->field][0] = vbi->data[0];
238                                 dev->vbi_out_cc[!!vbi->field][1] = vbi->data[1];
239                         }
240                         break;
241                 case V4L2_SLICED_WSS_625:
242                         if ((dev->std_out & V4L2_STD_625_50) &&
243                             vbi->field == 0 && vbi->line == 23) {
244                                 dev->vbi_out_have_wss = true;
245                                 dev->vbi_out_wss[0] = vbi->data[0];
246                                 dev->vbi_out_wss[1] = vbi->data[1];
247                         }
248                         break;
249                 }
250                 vbi++;
251         }
252 }