blob: 791a5161809be07c4d8c90719ba975db8c9786cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
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
Mauro Carvalho Chehab9a12ccf2015-04-30 06:44:56 -030020#include "saa7134.h"
21#include "saa7134-reg.h"
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
28
Michael Krufky5e453dc2006-01-09 15:32:31 -020029#include <media/v4l2-common.h>
Hans Verkuila2004502013-12-14 08:28:28 -030030#include <media/v4l2-event.h>
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/* ------------------------------------------------------------------ */
33
34MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35MODULE_LICENSE("GPL");
36
37static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
Mauro Carvalho Chehab674434c2005-12-12 00:37:28 -080038
Linus Torvalds1da177e2005-04-16 15:20:36 -070039module_param_array(empress_nr, int, NULL, 0444);
40MODULE_PARM_DESC(empress_nr,"ts device number");
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* ------------------------------------------------------------------ */
43
Hans Verkuil2ada8152014-04-17 07:30:53 -030044static int start_streaming(struct vb2_queue *vq, unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Hans Verkuil2ada8152014-04-17 07:30:53 -030046 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
47 struct saa7134_dev *dev = dmaq->dev;
Dmitry Belimovf03813e2008-08-26 13:44:40 -030048 u32 leading_null_bytes = 0;
Hans Verkuil2ada8152014-04-17 07:30:53 -030049 int err;
50
51 err = saa7134_ts_start_streaming(vq, count);
52 if (err)
53 return err;
Hans Verkuil86b79d662006-06-18 16:40:10 -030054
Dmitry Belimovf03813e2008-08-26 13:44:40 -030055 /* 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 }
Hans Verkuilfac69862009-01-17 12:17:14 -030064 saa_call_all(dev, core, init, leading_null_bytes);
Hans Verkuil2ada8152014-04-17 07:30:53 -030065 /* Unmute audio */
66 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
67 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dev->empress_started = 1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -080069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71
Hans Verkuil2ada8152014-04-17 07:30:53 -030072static void stop_streaming(struct vb2_queue *vq)
Hans Verkuil51aefd72014-04-17 04:23:07 -030073{
Hans Verkuil2ada8152014-04-17 07:30:53 -030074 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
75 struct saa7134_dev *dev = dmaq->dev;
Hans Verkuil51aefd72014-04-17 04:23:07 -030076
Hans Verkuil2ada8152014-04-17 07:30:53 -030077 saa7134_ts_stop_streaming(vq);
Hans Verkuil51aefd72014-04-17 04:23:07 -030078 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
Hans Verkuil2ada8152014-04-17 07:30:53 -030079 msleep(20);
Hans Verkuil51aefd72014-04-17 04:23:07 -030080 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
81 msleep(100);
Hans Verkuil2ada8152014-04-17 07:30:53 -030082 /* Mute audio */
83 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
84 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
Hans Verkuil51aefd72014-04-17 04:23:07 -030085 dev->empress_started = 0;
86}
87
Hans Verkuil2ada8152014-04-17 07:30:53 -030088static 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,
Hans Verkuil2ada8152014-04-17 07:30:53 -030092 .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
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* ------------------------------------------------------------------ */
100
Hans Verkuil78b526a2008-05-28 12:16:41 -0300101static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300102 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;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300109 f->flags = V4L2_FMT_FLAG_COMPRESSED;
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300110 return 0;
111}
112
Hans Verkuil78b526a2008-05-28 12:16:41 -0300113static int empress_g_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300114 struct v4l2_format *f)
115{
Hans Verkuilb9f63b22013-12-14 08:28:26 -0300116 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilda298c62015-04-09 04:02:34 -0300117 struct v4l2_subdev_format fmt = {
118 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
119 };
120 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300121
Hans Verkuilda298c62015-04-09 04:02:34 -0300122 saa_call_all(dev, pad, get_fmt, NULL, &fmt);
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300123
Hans Verkuilda298c62015-04-09 04:02:34 -0300124 v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt);
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300125 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
126 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300127 f->fmt.pix.bytesperline = 0;
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300128
129 return 0;
130}
131
Hans Verkuil78b526a2008-05-28 12:16:41 -0300132static int empress_s_fmt_vid_cap(struct file *file, void *priv,
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300133 struct v4l2_format *f)
134{
Hans Verkuilb9f63b22013-12-14 08:28:26 -0300135 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuilebf984b2015-04-09 04:05:59 -0300136 struct v4l2_subdev_format format = {
137 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
138 };
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300139
Hans Verkuilebf984b2015-04-09 04:05:59 -0300140 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);
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300143
144 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
145 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300146 f->fmt.pix.bytesperline = 0;
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300147
148 return 0;
149}
150
Dmitri Belimov6b6b7542009-05-28 01:58:57 -0300151static int empress_try_fmt_vid_cap(struct file *file, void *priv,
152 struct v4l2_format *f)
153{
Hans Verkuilb9f63b22013-12-14 08:28:26 -0300154 struct saa7134_dev *dev = video_drvdata(file);
Hans Verkuil5eab4982015-04-09 04:05:35 -0300155 struct v4l2_subdev_pad_config pad_cfg;
156 struct v4l2_subdev_format format = {
157 .which = V4L2_SUBDEV_FORMAT_TRY,
158 };
Hans Verkuilcabc6502013-06-01 10:02:38 -0300159
Hans Verkuil5eab4982015-04-09 04:05:35 -0300160 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);
Dmitri Belimov6b6b7542009-05-28 01:58:57 -0300163
164 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
165 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
Hans Verkuilcabc6502013-06-01 10:02:38 -0300166 f->fmt.pix.bytesperline = 0;
Dmitri Belimov6b6b7542009-05-28 01:58:57 -0300167
168 return 0;
169}
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300170
Hans Verkuilbec43662008-12-30 06:58:20 -0300171static const struct v4l2_file_operations ts_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 .owner = THIS_MODULE,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300174 .open = v4l2_fh_open,
175 .release = vb2_fop_release,
176 .read = vb2_fop_read,
177 .poll = vb2_fop_poll,
178 .mmap = vb2_fop_mmap,
Hans Verkuil13936af2014-06-16 04:32:37 -0300179 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180};
181
Hans Verkuila3998102008-07-21 02:57:38 -0300182static const struct v4l2_ioctl_ops ts_ioctl_ops = {
Hans Verkuilb93a18d2013-12-14 08:28:25 -0300183 .vidioc_querycap = saa7134_querycap,
Hans Verkuil78b526a2008-05-28 12:16:41 -0300184 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
Dmitri Belimov6b6b7542009-05-28 01:58:57 -0300185 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
Hans Verkuil78b526a2008-05-28 12:16:41 -0300186 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
187 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300188 .vidioc_reqbufs = vb2_ioctl_reqbufs,
189 .vidioc_querybuf = vb2_ioctl_querybuf,
190 .vidioc_qbuf = vb2_ioctl_qbuf,
191 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuil9f183022015-12-14 08:17:00 -0200192 .vidioc_expbuf = vb2_ioctl_expbuf,
Hans Verkuil2ada8152014-04-17 07:30:53 -0300193 .vidioc_streamon = vb2_ioctl_streamon,
194 .vidioc_streamoff = vb2_ioctl_streamoff,
Hans Verkuilb93a18d2013-12-14 08:28:25 -0300195 .vidioc_g_frequency = saa7134_g_frequency,
196 .vidioc_s_frequency = saa7134_s_frequency,
197 .vidioc_g_tuner = saa7134_g_tuner,
198 .vidioc_s_tuner = saa7134_s_tuner,
199 .vidioc_enum_input = saa7134_enum_input,
200 .vidioc_g_input = saa7134_g_input,
201 .vidioc_s_input = saa7134_s_input,
202 .vidioc_s_std = saa7134_s_std,
203 .vidioc_g_std = saa7134_g_std,
Mikhail Domrachev707b7f82014-04-01 09:28:17 -0300204 .vidioc_querystd = saa7134_querystd,
Hans Verkuila2004502013-12-14 08:28:28 -0300205 .vidioc_log_status = v4l2_ctrl_log_status,
206 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
207 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuila3998102008-07-21 02:57:38 -0300208};
209
210/* ----------------------------------------------------------- */
211
212static struct video_device saa7134_empress_template = {
213 .name = "saa7134-empress",
Hans Verkuila3998102008-07-21 02:57:38 -0300214 .fops = &ts_fops,
Hans Verkuila3998102008-07-21 02:57:38 -0300215 .ioctl_ops = &ts_ioctl_ops,
Mauro Carvalho Chehab2c10e8a2007-12-11 12:56:23 -0300216
217 .tvnorms = SAA7134_NORMS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
David Howellsc4028952006-11-22 14:57:56 +0000220static void empress_signal_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
David Howellsc4028952006-11-22 14:57:56 +0000222 struct saa7134_dev* dev =
223 container_of(work, struct saa7134_dev, empress_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (dev->nosignal) {
Mauro Carvalho Chehabe43c1732015-04-30 07:12:19 -0300226 pr_debug("no video signal\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } else {
Mauro Carvalho Chehabe43c1732015-04-30 07:12:19 -0300228 pr_debug("video signal acquired\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230}
231
232static void empress_signal_change(struct saa7134_dev *dev)
233{
234 schedule_work(&dev->empress_workqueue);
235}
236
Hans Verkuil718bde12013-12-14 08:28:24 -0300237static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
238{
239 switch (ctrl->id) {
240 case V4L2_CID_BRIGHTNESS:
241 case V4L2_CID_HUE:
242 case V4L2_CID_CONTRAST:
243 case V4L2_CID_SATURATION:
244 case V4L2_CID_AUDIO_MUTE:
245 case V4L2_CID_AUDIO_VOLUME:
246 case V4L2_CID_PRIVATE_INVERT:
247 case V4L2_CID_PRIVATE_AUTOMUTE:
248 return true;
249 default:
250 return false;
251 }
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254static int empress_init(struct saa7134_dev *dev)
255{
Hans Verkuil718bde12013-12-14 08:28:24 -0300256 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300257 struct vb2_queue *q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 int err;
259
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300260 pr_debug("%s: %s\n", dev->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 dev->empress_dev = video_device_alloc();
262 if (NULL == dev->empress_dev)
263 return -ENOMEM;
264 *(dev->empress_dev) = saa7134_empress_template;
Hans Verkuild481c582013-06-20 10:57:10 -0300265 dev->empress_dev->v4l2_dev = &dev->v4l2_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 dev->empress_dev->release = video_device_release;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300267 dev->empress_dev->lock = &dev->lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
269 "%s empress (%s)", dev->name,
270 saa7134_boards[dev->board].name);
Hans Verkuil718bde12013-12-14 08:28:24 -0300271 v4l2_ctrl_handler_init(hdl, 21);
272 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
273 if (dev->empress_sd)
274 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
275 if (hdl->error) {
276 video_device_release(dev->empress_dev);
277 return hdl->error;
278 }
279 dev->empress_dev->ctrl_handler = hdl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
David Howellsc4028952006-11-22 14:57:56 +0000281 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Hans Verkuil2ada8152014-04-17 07:30:53 -0300283 q = &dev->empress_vbq;
284 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
285 /*
286 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
287 * transfers that do not start at the beginning of a page. A USERPTR
288 * can start anywhere in a page, so USERPTR support is a no-go.
289 */
Hans Verkuil9f183022015-12-14 08:17:00 -0200290 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300291 q->drv_priv = &dev->ts_q;
292 q->ops = &saa7134_empress_qops;
293 q->gfp_flags = GFP_DMA32;
294 q->mem_ops = &vb2_dma_sg_memops;
295 q->buf_struct_size = sizeof(struct saa7134_buf);
296 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
297 q->lock = &dev->lock;
Hans Verkuild8feef92016-09-18 11:24:50 -0300298 q->dev = &dev->pci->dev;
Hans Verkuil2ada8152014-04-17 07:30:53 -0300299 err = vb2_queue_init(q);
300 if (err)
301 return err;
302 dev->empress_dev->queue = q;
303
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200304 video_set_drvdata(dev->empress_dev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
306 empress_nr[dev->nr]);
307 if (err < 0) {
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -0300308 pr_info("%s: can't register video device\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 dev->name);
310 video_device_release(dev->empress_dev);
311 dev->empress_dev = NULL;
312 return err;
313 }
Mauro Carvalho Chehab83582002015-04-30 06:46:34 -0300314 pr_info("%s: registered device %s [mpeg]\n",
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300315 dev->name, video_device_node_name(dev->empress_dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
David Howellsc4028952006-11-22 14:57:56 +0000317 empress_signal_update(&dev->empress_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return 0;
319}
320
321static int empress_fini(struct saa7134_dev *dev)
322{
Mauro Carvalho Chehab6139ebc2015-05-13 14:09:42 -0300323 pr_debug("%s: %s\n", dev->name, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (NULL == dev->empress_dev)
326 return 0;
Tejun Heo43829732012-08-20 14:51:24 -0700327 flush_work(&dev->empress_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 video_unregister_device(dev->empress_dev);
Hans Verkuil2ada8152014-04-17 07:30:53 -0300329 vb2_queue_release(&dev->empress_vbq);
Hans Verkuil718bde12013-12-14 08:28:24 -0300330 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 dev->empress_dev = NULL;
332 return 0;
333}
334
335static struct saa7134_mpeg_ops empress_ops = {
336 .type = SAA7134_MPEG_EMPRESS,
337 .init = empress_init,
338 .fini = empress_fini,
339 .signal_change = empress_signal_change,
340};
341
342static int __init empress_register(void)
343{
344 return saa7134_ts_register(&empress_ops);
345}
346
347static void __exit empress_unregister(void)
348{
349 saa7134_ts_unregister(&empress_ops);
350}
351
352module_init(empress_register);
353module_exit(empress_unregister);