Merge tag 'master-2014-07-23' of git://git.kernel.org/pub/scm/linux/kernel/git/linvil...
[linux-2.6-block.git] / drivers / media / pci / saa7134 / saa7134-empress.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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 <linux/init.h>
21#include <linux/list.h>
22#include <linux/module.h>
1da177e4 23#include <linux/kernel.h>
1da177e4
LT
24#include <linux/delay.h>
25
5e453dc7 26#include <media/v4l2-common.h>
a2004502
HV
27#include <media/v4l2-event.h>
28
29#include "saa7134-reg.h"
30#include "saa7134.h"
1da177e4
LT
31
32/* ------------------------------------------------------------------ */
33
34MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35MODULE_LICENSE("GPL");
36
37static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
674434c6 38
1da177e4
LT
39module_param_array(empress_nr, int, NULL, 0444);
40MODULE_PARM_DESC(empress_nr,"ts device number");
41
ff699e6b 42static unsigned int debug;
1da177e4
LT
43module_param(debug, int, 0644);
44MODULE_PARM_DESC(debug,"enable debug messages");
45
46#define dprintk(fmt, arg...) if (debug) \
47 printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
48
49/* ------------------------------------------------------------------ */
50
2ada815f 51static int start_streaming(struct vb2_queue *vq, unsigned int count)
1da177e4 52{
2ada815f
HV
53 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
54 struct saa7134_dev *dev = dmaq->dev;
f03813e4 55 u32 leading_null_bytes = 0;
2ada815f
HV
56 int err;
57
58 err = saa7134_ts_start_streaming(vq, count);
59 if (err)
60 return err;
86b79d66 61
f03813e4
DB
62 /* If more cards start to need this, then this
63 should probably be added to the card definitions. */
64 switch (dev->board) {
65 case SAA7134_BOARD_BEHOLD_M6:
66 case SAA7134_BOARD_BEHOLD_M63:
67 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
68 leading_null_bytes = 1;
69 break;
70 }
fac6986c 71 saa_call_all(dev, core, init, leading_null_bytes);
2ada815f
HV
72 /* Unmute audio */
73 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
74 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
1da177e4 75 dev->empress_started = 1;
4ac97914 76 return 0;
1da177e4
LT
77}
78
2ada815f 79static void stop_streaming(struct vb2_queue *vq)
51aefd73 80{
2ada815f
HV
81 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
82 struct saa7134_dev *dev = dmaq->dev;
51aefd73 83
2ada815f 84 saa7134_ts_stop_streaming(vq);
51aefd73 85 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
2ada815f 86 msleep(20);
51aefd73
HV
87 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
88 msleep(100);
2ada815f 89 /* Mute audio */
b784e526 90 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
2ada815f
HV
91 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
92 dev->empress_started = 0;
1da177e4
LT
93}
94
2ada815f
HV
95static struct vb2_ops saa7134_empress_qops = {
96 .queue_setup = saa7134_ts_queue_setup,
97 .buf_init = saa7134_ts_buffer_init,
98 .buf_prepare = saa7134_ts_buffer_prepare,
99 .buf_finish = saa7134_ts_buffer_finish,
100 .buf_queue = saa7134_vb2_buffer_queue,
101 .wait_prepare = vb2_ops_wait_prepare,
102 .wait_finish = vb2_ops_wait_finish,
103 .start_streaming = start_streaming,
104 .stop_streaming = stop_streaming,
105};
1da177e4 106
2ada815f 107/* ------------------------------------------------------------------ */
1da177e4 108
78b526a4 109static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
2c10e8a8
MCC
110 struct v4l2_fmtdesc *f)
111{
112 if (f->index != 0)
113 return -EINVAL;
1da177e4 114
2c10e8a8
MCC
115 strlcpy(f->description, "MPEG TS", sizeof(f->description));
116 f->pixelformat = V4L2_PIX_FMT_MPEG;
cabc6508 117 f->flags = V4L2_FMT_FLAG_COMPRESSED;
2c10e8a8
MCC
118 return 0;
119}
1da177e4 120
78b526a4 121static int empress_g_fmt_vid_cap(struct file *file, void *priv,
2c10e8a8
MCC
122 struct v4l2_format *f)
123{
b9f63b25 124 struct saa7134_dev *dev = video_drvdata(file);
31bf95fb 125 struct v4l2_mbus_framefmt mbus_fmt;
1da177e4 126
31bf95fb 127 saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
1da177e4 128
31bf95fb 129 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
2c10e8a8
MCC
130 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
131 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
cabc6508
HV
132 f->fmt.pix.bytesperline = 0;
133 f->fmt.pix.priv = 0;
1da177e4 134
2c10e8a8
MCC
135 return 0;
136}
1da177e4 137
78b526a4 138static int empress_s_fmt_vid_cap(struct file *file, void *priv,
2c10e8a8
MCC
139 struct v4l2_format *f)
140{
b9f63b25 141 struct saa7134_dev *dev = video_drvdata(file);
31bf95fb 142 struct v4l2_mbus_framefmt mbus_fmt;
1da177e4 143
31bf95fb
HV
144 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
145 saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
146 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
1da177e4 147
2c10e8a8
MCC
148 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
149 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
cabc6508
HV
150 f->fmt.pix.bytesperline = 0;
151 f->fmt.pix.priv = 0;
1da177e4 152
2c10e8a8
MCC
153 return 0;
154}
155
6b6b7543
DB
156static int empress_try_fmt_vid_cap(struct file *file, void *priv,
157 struct v4l2_format *f)
158{
b9f63b25 159 struct saa7134_dev *dev = video_drvdata(file);
cabc6508
HV
160 struct v4l2_mbus_framefmt mbus_fmt;
161
162 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
163 saa_call_all(dev, video, try_mbus_fmt, &mbus_fmt);
164 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
6b6b7543
DB
165
166 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
167 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
cabc6508
HV
168 f->fmt.pix.bytesperline = 0;
169 f->fmt.pix.priv = 0;
6b6b7543
DB
170
171 return 0;
172}
2c10e8a8 173
bec43661 174static const struct v4l2_file_operations ts_fops =
1da177e4
LT
175{
176 .owner = THIS_MODULE,
2ada815f
HV
177 .open = v4l2_fh_open,
178 .release = vb2_fop_release,
179 .read = vb2_fop_read,
180 .poll = vb2_fop_poll,
181 .mmap = vb2_fop_mmap,
13936af3 182 .unlocked_ioctl = video_ioctl2,
1da177e4
LT
183};
184
a399810c 185static const struct v4l2_ioctl_ops ts_ioctl_ops = {
b93a18d5 186 .vidioc_querycap = saa7134_querycap,
78b526a4 187 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
6b6b7543 188 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
78b526a4
HV
189 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
190 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
2ada815f
HV
191 .vidioc_reqbufs = vb2_ioctl_reqbufs,
192 .vidioc_querybuf = vb2_ioctl_querybuf,
193 .vidioc_qbuf = vb2_ioctl_qbuf,
194 .vidioc_dqbuf = vb2_ioctl_dqbuf,
195 .vidioc_streamon = vb2_ioctl_streamon,
196 .vidioc_streamoff = vb2_ioctl_streamoff,
b93a18d5
HV
197 .vidioc_g_frequency = saa7134_g_frequency,
198 .vidioc_s_frequency = saa7134_s_frequency,
199 .vidioc_g_tuner = saa7134_g_tuner,
200 .vidioc_s_tuner = saa7134_s_tuner,
201 .vidioc_enum_input = saa7134_enum_input,
202 .vidioc_g_input = saa7134_g_input,
203 .vidioc_s_input = saa7134_s_input,
204 .vidioc_s_std = saa7134_s_std,
205 .vidioc_g_std = saa7134_g_std,
707b7f80 206 .vidioc_querystd = saa7134_querystd,
a2004502
HV
207 .vidioc_log_status = v4l2_ctrl_log_status,
208 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
209 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
a399810c
HV
210};
211
212/* ----------------------------------------------------------- */
213
214static struct video_device saa7134_empress_template = {
215 .name = "saa7134-empress",
a399810c 216 .fops = &ts_fops,
a399810c 217 .ioctl_ops = &ts_ioctl_ops,
2c10e8a8
MCC
218
219 .tvnorms = SAA7134_NORMS,
1da177e4
LT
220};
221
c4028958 222static void empress_signal_update(struct work_struct *work)
1da177e4 223{
c4028958
DH
224 struct saa7134_dev* dev =
225 container_of(work, struct saa7134_dev, empress_workqueue);
1da177e4
LT
226
227 if (dev->nosignal) {
228 dprintk("no video signal\n");
1da177e4
LT
229 } else {
230 dprintk("video signal acquired\n");
1da177e4
LT
231 }
232}
233
234static void empress_signal_change(struct saa7134_dev *dev)
235{
236 schedule_work(&dev->empress_workqueue);
237}
238
718bde1a
HV
239static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
240{
241 switch (ctrl->id) {
242 case V4L2_CID_BRIGHTNESS:
243 case V4L2_CID_HUE:
244 case V4L2_CID_CONTRAST:
245 case V4L2_CID_SATURATION:
246 case V4L2_CID_AUDIO_MUTE:
247 case V4L2_CID_AUDIO_VOLUME:
248 case V4L2_CID_PRIVATE_INVERT:
249 case V4L2_CID_PRIVATE_AUTOMUTE:
250 return true;
251 default:
252 return false;
253 }
254}
1da177e4
LT
255
256static int empress_init(struct saa7134_dev *dev)
257{
718bde1a 258 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
2ada815f 259 struct vb2_queue *q;
1da177e4
LT
260 int err;
261
5016381c 262 dprintk("%s: %s\n",dev->name,__func__);
1da177e4
LT
263 dev->empress_dev = video_device_alloc();
264 if (NULL == dev->empress_dev)
265 return -ENOMEM;
266 *(dev->empress_dev) = saa7134_empress_template;
d481c581 267 dev->empress_dev->v4l2_dev = &dev->v4l2_dev;
1da177e4 268 dev->empress_dev->release = video_device_release;
2ada815f 269 dev->empress_dev->lock = &dev->lock;
1da177e4
LT
270 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
271 "%s empress (%s)", dev->name,
272 saa7134_boards[dev->board].name);
ce791139 273 set_bit(V4L2_FL_USE_FH_PRIO, &dev->empress_dev->flags);
718bde1a
HV
274 v4l2_ctrl_handler_init(hdl, 21);
275 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
276 if (dev->empress_sd)
277 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
278 if (hdl->error) {
279 video_device_release(dev->empress_dev);
280 return hdl->error;
281 }
282 dev->empress_dev->ctrl_handler = hdl;
1da177e4 283
c4028958 284 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
1da177e4 285
2ada815f
HV
286 q = &dev->empress_vbq;
287 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288 /*
289 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
290 * transfers that do not start at the beginning of a page. A USERPTR
291 * can start anywhere in a page, so USERPTR support is a no-go.
292 */
293 q->io_modes = VB2_MMAP | VB2_READ;
294 q->drv_priv = &dev->ts_q;
295 q->ops = &saa7134_empress_qops;
296 q->gfp_flags = GFP_DMA32;
297 q->mem_ops = &vb2_dma_sg_memops;
298 q->buf_struct_size = sizeof(struct saa7134_buf);
299 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
300 q->lock = &dev->lock;
301 err = vb2_queue_init(q);
302 if (err)
303 return err;
304 dev->empress_dev->queue = q;
305
63b0d5ad 306 video_set_drvdata(dev->empress_dev, dev);
1da177e4
LT
307 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
308 empress_nr[dev->nr]);
309 if (err < 0) {
310 printk(KERN_INFO "%s: can't register video device\n",
311 dev->name);
312 video_device_release(dev->empress_dev);
313 dev->empress_dev = NULL;
314 return err;
315 }
38c7c036
LP
316 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
317 dev->name, video_device_node_name(dev->empress_dev));
1da177e4 318
c4028958 319 empress_signal_update(&dev->empress_workqueue);
1da177e4
LT
320 return 0;
321}
322
323static int empress_fini(struct saa7134_dev *dev)
324{
5016381c 325 dprintk("%s: %s\n",dev->name,__func__);
1da177e4
LT
326
327 if (NULL == dev->empress_dev)
328 return 0;
43829731 329 flush_work(&dev->empress_workqueue);
1da177e4 330 video_unregister_device(dev->empress_dev);
2ada815f 331 vb2_queue_release(&dev->empress_vbq);
718bde1a 332 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
1da177e4
LT
333 dev->empress_dev = NULL;
334 return 0;
335}
336
337static struct saa7134_mpeg_ops empress_ops = {
338 .type = SAA7134_MPEG_EMPRESS,
339 .init = empress_init,
340 .fini = empress_fini,
341 .signal_change = empress_signal_change,
342};
343
344static int __init empress_register(void)
345{
346 return saa7134_ts_register(&empress_ops);
347}
348
349static void __exit empress_unregister(void)
350{
351 saa7134_ts_unregister(&empress_ops);
352}
353
354module_init(empress_register);
355module_exit(empress_unregister);