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);
88 struct saa7134_fh *fh;
90 /* allocate + initialize per filehandle data */
91 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
95 v4l2_fh_init(&fh->fh, vdev);
96 file->private_data = fh;
97 fh->is_empress = true;
101 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
102 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
107 static int ts_release(struct file *file)
109 struct saa7134_dev *dev = video_drvdata(file);
110 struct saa7134_fh *fh = file->private_data;
112 if (res_check(fh, RESOURCE_EMPRESS)) {
113 videobuf_stop(&dev->empress_tsq);
114 videobuf_mmap_free(&dev->empress_tsq);
116 /* stop the encoder */
117 ts_reset_encoder(dev);
120 saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
121 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
124 v4l2_fh_del(&fh->fh);
125 v4l2_fh_exit(&fh->fh);
130 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
132 struct saa7134_dev *dev = video_drvdata(file);
134 if (res_locked(dev, RESOURCE_EMPRESS))
136 if (!dev->empress_started)
137 ts_init_encoder(dev);
139 return videobuf_read_stream(&dev->empress_tsq,
140 data, count, ppos, 0,
141 file->f_flags & O_NONBLOCK);
145 ts_poll(struct file *file, struct poll_table_struct *wait)
147 struct saa7134_dev *dev = video_drvdata(file);
149 return videobuf_poll_stream(file, &dev->empress_tsq, wait);
154 ts_mmap(struct file *file, struct vm_area_struct * vma)
156 struct saa7134_dev *dev = video_drvdata(file);
158 return videobuf_mmap_mapper(&dev->empress_tsq, vma);
161 static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
162 struct v4l2_fmtdesc *f)
167 strlcpy(f->description, "MPEG TS", sizeof(f->description));
168 f->pixelformat = V4L2_PIX_FMT_MPEG;
169 f->flags = V4L2_FMT_FLAG_COMPRESSED;
173 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
174 struct v4l2_format *f)
176 struct saa7134_dev *dev = video_drvdata(file);
177 struct v4l2_mbus_framefmt mbus_fmt;
179 saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
181 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
182 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
183 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
184 f->fmt.pix.bytesperline = 0;
190 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
191 struct v4l2_format *f)
193 struct saa7134_dev *dev = video_drvdata(file);
194 struct v4l2_mbus_framefmt mbus_fmt;
196 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
197 saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
198 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
200 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
201 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
202 f->fmt.pix.bytesperline = 0;
208 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
209 struct v4l2_format *f)
211 struct saa7134_dev *dev = video_drvdata(file);
212 struct v4l2_mbus_framefmt mbus_fmt;
214 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
215 saa_call_all(dev, video, try_mbus_fmt, &mbus_fmt);
216 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
218 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
219 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
220 f->fmt.pix.bytesperline = 0;
226 static const struct v4l2_file_operations ts_fops =
228 .owner = THIS_MODULE,
230 .release = ts_release,
234 .ioctl = video_ioctl2,
237 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
238 .vidioc_querycap = saa7134_querycap,
239 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
240 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
241 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
242 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
243 .vidioc_reqbufs = saa7134_reqbufs,
244 .vidioc_querybuf = saa7134_querybuf,
245 .vidioc_qbuf = saa7134_qbuf,
246 .vidioc_dqbuf = saa7134_dqbuf,
247 .vidioc_streamon = saa7134_streamon,
248 .vidioc_streamoff = saa7134_streamoff,
249 .vidioc_g_frequency = saa7134_g_frequency,
250 .vidioc_s_frequency = saa7134_s_frequency,
251 .vidioc_g_tuner = saa7134_g_tuner,
252 .vidioc_s_tuner = saa7134_s_tuner,
253 .vidioc_enum_input = saa7134_enum_input,
254 .vidioc_g_input = saa7134_g_input,
255 .vidioc_s_input = saa7134_s_input,
256 .vidioc_s_std = saa7134_s_std,
257 .vidioc_g_std = saa7134_g_std,
260 /* ----------------------------------------------------------- */
262 static struct video_device saa7134_empress_template = {
263 .name = "saa7134-empress",
265 .ioctl_ops = &ts_ioctl_ops,
267 .tvnorms = SAA7134_NORMS,
270 static void empress_signal_update(struct work_struct *work)
272 struct saa7134_dev* dev =
273 container_of(work, struct saa7134_dev, empress_workqueue);
276 dprintk("no video signal\n");
278 dprintk("video signal acquired\n");
282 static void empress_signal_change(struct saa7134_dev *dev)
284 schedule_work(&dev->empress_workqueue);
287 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
290 case V4L2_CID_BRIGHTNESS:
292 case V4L2_CID_CONTRAST:
293 case V4L2_CID_SATURATION:
294 case V4L2_CID_AUDIO_MUTE:
295 case V4L2_CID_AUDIO_VOLUME:
296 case V4L2_CID_PRIVATE_INVERT:
297 case V4L2_CID_PRIVATE_AUTOMUTE:
304 static int empress_init(struct saa7134_dev *dev)
306 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
309 dprintk("%s: %s\n",dev->name,__func__);
310 dev->empress_dev = video_device_alloc();
311 if (NULL == dev->empress_dev)
313 *(dev->empress_dev) = saa7134_empress_template;
314 dev->empress_dev->v4l2_dev = &dev->v4l2_dev;
315 dev->empress_dev->release = video_device_release;
316 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
317 "%s empress (%s)", dev->name,
318 saa7134_boards[dev->board].name);
319 set_bit(V4L2_FL_USE_FH_PRIO, &dev->empress_dev->flags);
320 v4l2_ctrl_handler_init(hdl, 21);
321 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
323 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
325 video_device_release(dev->empress_dev);
328 dev->empress_dev->ctrl_handler = hdl;
330 INIT_WORK(&dev->empress_workqueue, empress_signal_update);
332 video_set_drvdata(dev->empress_dev, dev);
333 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
334 empress_nr[dev->nr]);
336 printk(KERN_INFO "%s: can't register video device\n",
338 video_device_release(dev->empress_dev);
339 dev->empress_dev = NULL;
342 printk(KERN_INFO "%s: registered device %s [mpeg]\n",
343 dev->name, video_device_node_name(dev->empress_dev));
345 videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
346 &dev->pci->dev, &dev->slock,
347 V4L2_BUF_TYPE_VIDEO_CAPTURE,
348 V4L2_FIELD_ALTERNATE,
349 sizeof(struct saa7134_buf),
352 empress_signal_update(&dev->empress_workqueue);
356 static int empress_fini(struct saa7134_dev *dev)
358 dprintk("%s: %s\n",dev->name,__func__);
360 if (NULL == dev->empress_dev)
362 flush_work(&dev->empress_workqueue);
363 video_unregister_device(dev->empress_dev);
364 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
365 dev->empress_dev = NULL;
369 static struct saa7134_mpeg_ops empress_ops = {
370 .type = SAA7134_MPEG_EMPRESS,
371 .init = empress_init,
372 .fini = empress_fini,
373 .signal_change = empress_signal_change,
376 static int __init empress_register(void)
378 return saa7134_ts_register(&empress_ops);
381 static void __exit empress_unregister(void)
383 saa7134_ts_unregister(&empress_ops);
386 module_init(empress_register);
387 module_exit(empress_unregister);
389 /* ----------------------------------------------------------- */