3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
26 #include "saa7134-reg.h"
29 #include <media/saa6752hs.h>
30 #include <media/v4l2-common.h>
32 /* ------------------------------------------------------------------ */
34 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35 MODULE_LICENSE("GPL");
37 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
39 module_param_array(empress_nr, int, NULL, 0444);
40 MODULE_PARM_DESC(empress_nr,"ts device number");
42 static unsigned int debug;
43 module_param(debug, int, 0644);
44 MODULE_PARM_DESC(debug,"enable debug messages");
46 #define dprintk(fmt, arg...) if (debug) \
47 printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
49 /* ------------------------------------------------------------------ */
51 static void ts_reset_encoder(struct saa7134_dev* dev)
53 if (!dev->empress_started)
56 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
58 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
60 dev->empress_started = 0;
63 static int ts_init_encoder(struct saa7134_dev* dev)
65 u32 leading_null_bytes = 0;
67 /* If more cards start to need this, then this
68 should probably be added to the card definitions. */
70 case SAA7134_BOARD_BEHOLD_M6:
71 case SAA7134_BOARD_BEHOLD_M63:
72 case SAA7134_BOARD_BEHOLD_M6_EXTRA:
73 leading_null_bytes = 1;
76 ts_reset_encoder(dev);
77 saa_call_all(dev, core, init, leading_null_bytes);
78 dev->empress_started = 1;
82 /* ------------------------------------------------------------------ */
84 static int ts_open(struct file *file)
86 struct video_device *vdev = video_devdata(file);
87 struct saa7134_dev *dev = video_drvdata(file);
90 dprintk("open dev=%s\n", video_device_node_name(vdev));
92 if (!mutex_trylock(&dev->empress_tsq.vb_lock))
94 if (atomic_read(&dev->empress_users))
98 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
99 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
101 atomic_inc(&dev->empress_users);
102 file->private_data = dev;
106 mutex_unlock(&dev->empress_tsq.vb_lock);
110 static int ts_release(struct file *file)
112 struct saa7134_dev *dev = file->private_data;
114 videobuf_stop(&dev->empress_tsq);
115 videobuf_mmap_free(&dev->empress_tsq);
117 /* stop the encoder */
118 ts_reset_encoder(dev);
121 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
122 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
124 atomic_dec(&dev->empress_users);
130 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
132 struct saa7134_dev *dev = file->private_data;
134 if (!dev->empress_started)
135 ts_init_encoder(dev);
137 return videobuf_read_stream(&dev->empress_tsq,
138 data, count, ppos, 0,
139 file->f_flags & O_NONBLOCK);
143 ts_poll(struct file *file, struct poll_table_struct *wait)
145 struct saa7134_dev *dev = file->private_data;
147 return videobuf_poll_stream(file, &dev->empress_tsq, wait);
152 ts_mmap(struct file *file, struct vm_area_struct * vma)
154 struct saa7134_dev *dev = file->private_data;
156 return videobuf_mmap_mapper(&dev->empress_tsq, vma);
160 * This function is _not_ called directly, but from
161 * video_generic_ioctl (and maybe others). userspace
162 * copying is done already, arg is a kernel pointer.
165 static int empress_querycap(struct file *file, void *priv,
166 struct v4l2_capability *cap)
168 struct saa7134_dev *dev = file->private_data;
170 strcpy(cap->driver, "saa7134");
171 strlcpy(cap->card, saa7134_boards[dev->board].name,
173 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
175 V4L2_CAP_VIDEO_CAPTURE |
181 static int empress_enum_input(struct file *file, void *priv,
182 struct v4l2_input *i)
187 i->type = V4L2_INPUT_TYPE_CAMERA;
188 strcpy(i->name, "CCIR656");
193 static int empress_g_input(struct file *file, void *priv, unsigned int *i)
199 static int empress_s_input(struct file *file, void *priv, unsigned int i)
207 static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
208 struct v4l2_fmtdesc *f)
213 strlcpy(f->description, "MPEG TS", sizeof(f->description));
214 f->pixelformat = V4L2_PIX_FMT_MPEG;
219 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
220 struct v4l2_format *f)
222 struct saa7134_dev *dev = file->private_data;
223 struct v4l2_mbus_framefmt mbus_fmt;
225 saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
227 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
228 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
229 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
234 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
235 struct v4l2_format *f)
237 struct saa7134_dev *dev = file->private_data;
238 struct v4l2_mbus_framefmt mbus_fmt;
240 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
241 saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
242 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
244 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
245 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
250 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
251 struct v4l2_format *f)
253 struct saa7134_dev *dev = file->private_data;
255 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
256 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
261 static int empress_reqbufs(struct file *file, void *priv,
262 struct v4l2_requestbuffers *p)
264 struct saa7134_dev *dev = file->private_data;
266 return videobuf_reqbufs(&dev->empress_tsq, p);
269 static int empress_querybuf(struct file *file, void *priv,
270 struct v4l2_buffer *b)
272 struct saa7134_dev *dev = file->private_data;
274 return videobuf_querybuf(&dev->empress_tsq, b);
277 static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
279 struct saa7134_dev *dev = file->private_data;
281 return videobuf_qbuf(&dev->empress_tsq, b);
284 static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
286 struct saa7134_dev *dev = file->private_data;
288 return videobuf_dqbuf(&dev->empress_tsq, b,
289 file->f_flags & O_NONBLOCK);
292 static int empress_streamon(struct file *file, void *priv,
293 enum v4l2_buf_type type)
295 struct saa7134_dev *dev = file->private_data;
297 return videobuf_streamon(&dev->empress_tsq);
300 static int empress_streamoff(struct file *file, void *priv,
301 enum v4l2_buf_type type)
303 struct saa7134_dev *dev = file->private_data;
305 return videobuf_streamoff(&dev->empress_tsq);
308 static int empress_s_ext_ctrls(struct file *file, void *priv,
309 struct v4l2_ext_controls *ctrls)
311 struct saa7134_dev *dev = file->private_data;
314 /* count == 0 is abused in saa6752hs.c, so that special
315 case is handled here explicitly. */
316 if (ctrls->count == 0)
319 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
322 err = saa_call_empress(dev, core, s_ext_ctrls, ctrls);
323 ts_init_encoder(dev);
328 static int empress_g_ext_ctrls(struct file *file, void *priv,
329 struct v4l2_ext_controls *ctrls)
331 struct saa7134_dev *dev = file->private_data;
333 if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
335 return saa_call_empress(dev, core, g_ext_ctrls, ctrls);
338 static int empress_g_ctrl(struct file *file, void *priv,
339 struct v4l2_control *c)
341 struct saa7134_dev *dev = file->private_data;
343 return saa7134_g_ctrl_internal(dev, NULL, c);
346 static int empress_s_ctrl(struct file *file, void *priv,
347 struct v4l2_control *c)
349 struct saa7134_dev *dev = file->private_data;
351 return saa7134_s_ctrl_internal(dev, NULL, c);
354 static int empress_queryctrl(struct file *file, void *priv,
355 struct v4l2_queryctrl *c)
357 /* Must be sorted from low to high control ID! */
358 static const u32 user_ctrls[] = {
364 V4L2_CID_AUDIO_VOLUME,
370 /* Must be sorted from low to high control ID! */
371 static const u32 mpeg_ctrls[] = {
373 V4L2_CID_MPEG_STREAM_TYPE,
374 V4L2_CID_MPEG_STREAM_PID_PMT,
375 V4L2_CID_MPEG_STREAM_PID_AUDIO,
376 V4L2_CID_MPEG_STREAM_PID_VIDEO,
377 V4L2_CID_MPEG_STREAM_PID_PCR,
378 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
379 V4L2_CID_MPEG_AUDIO_ENCODING,
380 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
381 V4L2_CID_MPEG_VIDEO_ENCODING,
382 V4L2_CID_MPEG_VIDEO_ASPECT,
383 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
384 V4L2_CID_MPEG_VIDEO_BITRATE,
385 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
388 static const u32 *ctrl_classes[] = {
393 struct saa7134_dev *dev = file->private_data;
395 c->id = v4l2_ctrl_next(ctrl_classes, c->id);
398 if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
399 return v4l2_ctrl_query_fill(c, 0, 0, 0, 0);
400 if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
401 return saa7134_queryctrl(file, priv, c);
402 return saa_call_empress(dev, core, queryctrl, c);
405 static int empress_querymenu(struct file *file, void *priv,
406 struct v4l2_querymenu *c)
408 struct saa7134_dev *dev = file->private_data;
410 if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
412 return saa_call_empress(dev, core, querymenu, c);
415 static int empress_s_std(struct file *file, void *priv, v4l2_std_id id)
417 struct saa7134_dev *dev = file->private_data;
419 return saa7134_s_std_internal(dev, NULL, id);
422 static int empress_g_std(struct file *file, void *priv, v4l2_std_id *id)
424 struct saa7134_dev *dev = file->private_data;
426 *id = dev->tvnorm->id;
430 static const struct v4l2_file_operations ts_fops =
432 .owner = THIS_MODULE,
434 .release = ts_release,
438 .ioctl = video_ioctl2,
441 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
442 .vidioc_querycap = empress_querycap,
443 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
444 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
445 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
446 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
447 .vidioc_reqbufs = empress_reqbufs,
448 .vidioc_querybuf = empress_querybuf,
449 .vidioc_qbuf = empress_qbuf,
450 .vidioc_dqbuf = empress_dqbuf,
451 .vidioc_streamon = empress_streamon,
452 .vidioc_streamoff = empress_streamoff,
453 .vidioc_s_ext_ctrls = empress_s_ext_ctrls,
454 .vidioc_g_ext_ctrls = empress_g_ext_ctrls,
455 .vidioc_enum_input = empress_enum_input,
456 .vidioc_g_input = empress_g_input,
457 .vidioc_s_input = empress_s_input,
458 .vidioc_queryctrl = empress_queryctrl,
459 .vidioc_querymenu = empress_querymenu,
460 .vidioc_g_ctrl = empress_g_ctrl,
461 .vidioc_s_ctrl = empress_s_ctrl,
462 .vidioc_s_std = empress_s_std,
463 .vidioc_g_std = empress_g_std,
466 /* ----------------------------------------------------------- */
468 static struct video_device saa7134_empress_template = {
469 .name = "saa7134-empress",
471 .ioctl_ops = &ts_ioctl_ops,
473 .tvnorms = SAA7134_NORMS,
476 static void empress_signal_update(struct work_struct *work)
478 struct saa7134_dev* dev =
479 container_of(work, struct saa7134_dev, empress_workqueue);
482 dprintk("no video signal\n");
484 dprintk("video signal acquired\n");
488 static void empress_signal_change(struct saa7134_dev *dev)
490 schedule_work(&dev->empress_workqueue);
494 static int empress_init(struct saa7134_dev *dev)
498 dprintk("%s: %s\n",dev->name,__func__);
499 dev->empress_dev = video_device_alloc();
500 if (NULL == dev->empress_dev)
502 *(dev->empress_dev) = saa7134_empress_template;
503 dev->empress_dev->parent = &dev->pci->dev;
504 dev->empress_dev->release = video_device_release;
505 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
506 "%s empress (%s)", dev->name,
507 saa7134_boards[dev->board].name);
509 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
511 video_set_drvdata(dev->empress_dev, dev);
512 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
513 empress_nr[dev->nr]);
515 printk(KERN_INFO "%s: can't register video device\n",
517 video_device_release(dev->empress_dev);
518 dev->empress_dev = NULL;
521 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
522 dev->name, video_device_node_name(dev->empress_dev));
524 videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
525 &dev->pci->dev, &dev->slock,
526 V4L2_BUF_TYPE_VIDEO_CAPTURE,
527 V4L2_FIELD_ALTERNATE,
528 sizeof(struct saa7134_buf),
531 empress_signal_update(&dev->empress_workqueue);
535 static int empress_fini(struct saa7134_dev *dev)
537 dprintk("%s: %s\n",dev->name,__func__);
539 if (NULL == dev->empress_dev)
541 flush_work(&dev->empress_workqueue);
542 video_unregister_device(dev->empress_dev);
543 dev->empress_dev = NULL;
547 static struct saa7134_mpeg_ops empress_ops = {
548 .type = SAA7134_MPEG_EMPRESS,
549 .init = empress_init,
550 .fini = empress_fini,
551 .signal_change = empress_signal_change,
554 static int __init empress_register(void)
556 return saa7134_ts_register(&empress_ops);
559 static void __exit empress_unregister(void)
561 saa7134_ts_unregister(&empress_ops);
564 module_init(empress_register);
565 module_exit(empress_unregister);
567 /* ----------------------------------------------------------- */