These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / media / pci / saa7134 / saa7134-empress.c
1 /*
2  *
3  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19
20 #include "saa7134.h"
21 #include "saa7134-reg.h"
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/delay.h>
28
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-event.h>
31
32 /* ------------------------------------------------------------------ */
33
34 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35 MODULE_LICENSE("GPL");
36
37 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
38
39 module_param_array(empress_nr, int, NULL, 0444);
40 MODULE_PARM_DESC(empress_nr,"ts device number");
41
42 /* ------------------------------------------------------------------ */
43
44 static int start_streaming(struct vb2_queue *vq, unsigned int count)
45 {
46         struct saa7134_dmaqueue *dmaq = vq->drv_priv;
47         struct saa7134_dev *dev = dmaq->dev;
48         u32 leading_null_bytes = 0;
49         int err;
50
51         err = saa7134_ts_start_streaming(vq, count);
52         if (err)
53                 return err;
54
55         /* If more cards start to need this, then this
56            should probably be added to the card definitions. */
57         switch (dev->board) {
58         case SAA7134_BOARD_BEHOLD_M6:
59         case SAA7134_BOARD_BEHOLD_M63:
60         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
61                 leading_null_bytes = 1;
62                 break;
63         }
64         saa_call_all(dev, core, init, leading_null_bytes);
65         /* Unmute audio */
66         saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
67                         saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
68         dev->empress_started = 1;
69         return 0;
70 }
71
72 static void stop_streaming(struct vb2_queue *vq)
73 {
74         struct saa7134_dmaqueue *dmaq = vq->drv_priv;
75         struct saa7134_dev *dev = dmaq->dev;
76
77         saa7134_ts_stop_streaming(vq);
78         saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
79         msleep(20);
80         saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
81         msleep(100);
82         /* Mute audio */
83         saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
84                         saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
85         dev->empress_started = 0;
86 }
87
88 static struct vb2_ops saa7134_empress_qops = {
89         .queue_setup    = saa7134_ts_queue_setup,
90         .buf_init       = saa7134_ts_buffer_init,
91         .buf_prepare    = saa7134_ts_buffer_prepare,
92         .buf_queue      = saa7134_vb2_buffer_queue,
93         .wait_prepare   = vb2_ops_wait_prepare,
94         .wait_finish    = vb2_ops_wait_finish,
95         .start_streaming = start_streaming,
96         .stop_streaming = stop_streaming,
97 };
98
99 /* ------------------------------------------------------------------ */
100
101 static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
102                                         struct v4l2_fmtdesc *f)
103 {
104         if (f->index != 0)
105                 return -EINVAL;
106
107         strlcpy(f->description, "MPEG TS", sizeof(f->description));
108         f->pixelformat = V4L2_PIX_FMT_MPEG;
109         f->flags = V4L2_FMT_FLAG_COMPRESSED;
110         return 0;
111 }
112
113 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
114                                 struct v4l2_format *f)
115 {
116         struct saa7134_dev *dev = video_drvdata(file);
117         struct v4l2_subdev_format fmt = {
118                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
119         };
120         struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
121
122         saa_call_all(dev, pad, get_fmt, NULL, &fmt);
123
124         v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt);
125         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
126         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
127         f->fmt.pix.bytesperline = 0;
128
129         return 0;
130 }
131
132 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
133                                 struct v4l2_format *f)
134 {
135         struct saa7134_dev *dev = video_drvdata(file);
136         struct v4l2_subdev_format format = {
137                 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
138         };
139
140         v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
141         saa_call_all(dev, pad, set_fmt, NULL, &format);
142         v4l2_fill_pix_format(&f->fmt.pix, &format.format);
143
144         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
145         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
146         f->fmt.pix.bytesperline = 0;
147
148         return 0;
149 }
150
151 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
152                                 struct v4l2_format *f)
153 {
154         struct saa7134_dev *dev = video_drvdata(file);
155         struct v4l2_subdev_pad_config pad_cfg;
156         struct v4l2_subdev_format format = {
157                 .which = V4L2_SUBDEV_FORMAT_TRY,
158         };
159
160         v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
161         saa_call_all(dev, pad, set_fmt, &pad_cfg, &format);
162         v4l2_fill_pix_format(&f->fmt.pix, &format.format);
163
164         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
165         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
166         f->fmt.pix.bytesperline = 0;
167
168         return 0;
169 }
170
171 static const struct v4l2_file_operations ts_fops =
172 {
173         .owner    = THIS_MODULE,
174         .open     = v4l2_fh_open,
175         .release  = vb2_fop_release,
176         .read     = vb2_fop_read,
177         .poll     = vb2_fop_poll,
178         .mmap     = vb2_fop_mmap,
179         .unlocked_ioctl = video_ioctl2,
180 };
181
182 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
183         .vidioc_querycap                = saa7134_querycap,
184         .vidioc_enum_fmt_vid_cap        = empress_enum_fmt_vid_cap,
185         .vidioc_try_fmt_vid_cap         = empress_try_fmt_vid_cap,
186         .vidioc_s_fmt_vid_cap           = empress_s_fmt_vid_cap,
187         .vidioc_g_fmt_vid_cap           = empress_g_fmt_vid_cap,
188         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
189         .vidioc_querybuf                = vb2_ioctl_querybuf,
190         .vidioc_qbuf                    = vb2_ioctl_qbuf,
191         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
192         .vidioc_streamon                = vb2_ioctl_streamon,
193         .vidioc_streamoff               = vb2_ioctl_streamoff,
194         .vidioc_g_frequency             = saa7134_g_frequency,
195         .vidioc_s_frequency             = saa7134_s_frequency,
196         .vidioc_g_tuner                 = saa7134_g_tuner,
197         .vidioc_s_tuner                 = saa7134_s_tuner,
198         .vidioc_enum_input              = saa7134_enum_input,
199         .vidioc_g_input                 = saa7134_g_input,
200         .vidioc_s_input                 = saa7134_s_input,
201         .vidioc_s_std                   = saa7134_s_std,
202         .vidioc_g_std                   = saa7134_g_std,
203         .vidioc_querystd                = saa7134_querystd,
204         .vidioc_log_status              = v4l2_ctrl_log_status,
205         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
206         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
207 };
208
209 /* ----------------------------------------------------------- */
210
211 static struct video_device saa7134_empress_template = {
212         .name          = "saa7134-empress",
213         .fops          = &ts_fops,
214         .ioctl_ops     = &ts_ioctl_ops,
215
216         .tvnorms                        = SAA7134_NORMS,
217 };
218
219 static void empress_signal_update(struct work_struct *work)
220 {
221         struct saa7134_dev* dev =
222                 container_of(work, struct saa7134_dev, empress_workqueue);
223
224         if (dev->nosignal) {
225                 pr_debug("no video signal\n");
226         } else {
227                 pr_debug("video signal acquired\n");
228         }
229 }
230
231 static void empress_signal_change(struct saa7134_dev *dev)
232 {
233         schedule_work(&dev->empress_workqueue);
234 }
235
236 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
237 {
238         switch (ctrl->id) {
239         case V4L2_CID_BRIGHTNESS:
240         case V4L2_CID_HUE:
241         case V4L2_CID_CONTRAST:
242         case V4L2_CID_SATURATION:
243         case V4L2_CID_AUDIO_MUTE:
244         case V4L2_CID_AUDIO_VOLUME:
245         case V4L2_CID_PRIVATE_INVERT:
246         case V4L2_CID_PRIVATE_AUTOMUTE:
247                 return true;
248         default:
249                 return false;
250         }
251 }
252
253 static int empress_init(struct saa7134_dev *dev)
254 {
255         struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
256         struct vb2_queue *q;
257         int err;
258
259         pr_debug("%s: %s\n", dev->name, __func__);
260         dev->empress_dev = video_device_alloc();
261         if (NULL == dev->empress_dev)
262                 return -ENOMEM;
263         *(dev->empress_dev) = saa7134_empress_template;
264         dev->empress_dev->v4l2_dev  = &dev->v4l2_dev;
265         dev->empress_dev->release = video_device_release;
266         dev->empress_dev->lock = &dev->lock;
267         snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
268                  "%s empress (%s)", dev->name,
269                  saa7134_boards[dev->board].name);
270         v4l2_ctrl_handler_init(hdl, 21);
271         v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
272         if (dev->empress_sd)
273                 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
274         if (hdl->error) {
275                 video_device_release(dev->empress_dev);
276                 return hdl->error;
277         }
278         dev->empress_dev->ctrl_handler = hdl;
279
280         INIT_WORK(&dev->empress_workqueue, empress_signal_update);
281
282         q = &dev->empress_vbq;
283         q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
284         /*
285          * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
286          * transfers that do not start at the beginning of a page. A USERPTR
287          * can start anywhere in a page, so USERPTR support is a no-go.
288          */
289         q->io_modes = VB2_MMAP | VB2_READ;
290         q->drv_priv = &dev->ts_q;
291         q->ops = &saa7134_empress_qops;
292         q->gfp_flags = GFP_DMA32;
293         q->mem_ops = &vb2_dma_sg_memops;
294         q->buf_struct_size = sizeof(struct saa7134_buf);
295         q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
296         q->lock = &dev->lock;
297         err = vb2_queue_init(q);
298         if (err)
299                 return err;
300         dev->empress_dev->queue = q;
301
302         video_set_drvdata(dev->empress_dev, dev);
303         err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
304                                     empress_nr[dev->nr]);
305         if (err < 0) {
306                 pr_info("%s: can't register video device\n",
307                        dev->name);
308                 video_device_release(dev->empress_dev);
309                 dev->empress_dev = NULL;
310                 return err;
311         }
312         pr_info("%s: registered device %s [mpeg]\n",
313                dev->name, video_device_node_name(dev->empress_dev));
314
315         empress_signal_update(&dev->empress_workqueue);
316         return 0;
317 }
318
319 static int empress_fini(struct saa7134_dev *dev)
320 {
321         pr_debug("%s: %s\n", dev->name, __func__);
322
323         if (NULL == dev->empress_dev)
324                 return 0;
325         flush_work(&dev->empress_workqueue);
326         video_unregister_device(dev->empress_dev);
327         vb2_queue_release(&dev->empress_vbq);
328         v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
329         dev->empress_dev = NULL;
330         return 0;
331 }
332
333 static struct saa7134_mpeg_ops empress_ops = {
334         .type          = SAA7134_MPEG_EMPRESS,
335         .init          = empress_init,
336         .fini          = empress_fini,
337         .signal_change = empress_signal_change,
338 };
339
340 static int __init empress_register(void)
341 {
342         return saa7134_ts_register(&empress_ops);
343 }
344
345 static void __exit empress_unregister(void)
346 {
347         saa7134_ts_unregister(&empress_ops);
348 }
349
350 module_init(empress_register);
351 module_exit(empress_unregister);