2 * $Id: saa7134-empress.c,v 1.10 2005/02/03 10:24:33 kraxel Exp $
4 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/init.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
29 #include "saa7134-reg.h"
32 #include <media/saa6752hs.h>
34 /* ------------------------------------------------------------------ */
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
39 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
40 module_param_array(empress_nr, int, NULL, 0444);
41 MODULE_PARM_DESC(empress_nr,"ts device number");
43 static unsigned int debug = 0;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug,"enable debug messages");
47 #define dprintk(fmt, arg...) if (debug) \
48 printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
50 /* ------------------------------------------------------------------ */
52 static void ts_reset_encoder(struct saa7134_dev* dev)
54 if (!dev->empress_started)
57 saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
59 saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
61 dev->empress_started = 0;
64 static int ts_init_encoder(struct saa7134_dev* dev)
66 ts_reset_encoder(dev);
67 saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, NULL);
68 dev->empress_started = 1;
72 /* ------------------------------------------------------------------ */
74 static int ts_open(struct inode *inode, struct file *file)
76 int minor = iminor(inode);
77 struct saa7134_dev *h,*dev = NULL;
78 struct list_head *list;
81 list_for_each(list,&saa7134_devlist) {
82 h = list_entry(list, struct saa7134_dev, devlist);
83 if (h->empress_dev && h->empress_dev->minor == minor)
89 dprintk("open minor=%d\n",minor);
91 if (down_trylock(&dev->empress_tsq.lock))
93 if (dev->empress_users)
97 file->private_data = dev;
101 up(&dev->empress_tsq.lock);
106 static int ts_release(struct inode *inode, struct file *file)
108 struct saa7134_dev *dev = file->private_data;
110 if (dev->empress_tsq.streaming)
111 videobuf_streamoff(&dev->empress_tsq);
112 down(&dev->empress_tsq.lock);
113 if (dev->empress_tsq.reading)
114 videobuf_read_stop(&dev->empress_tsq);
115 videobuf_mmap_free(&dev->empress_tsq);
116 dev->empress_users--;
118 /* stop the encoder */
119 ts_reset_encoder(dev);
121 up(&dev->empress_tsq.lock);
126 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
128 struct saa7134_dev *dev = file->private_data;
130 if (!dev->empress_started)
131 ts_init_encoder(dev);
133 return videobuf_read_stream(&dev->empress_tsq,
134 data, count, ppos, 0,
135 file->f_flags & O_NONBLOCK);
139 ts_poll(struct file *file, struct poll_table_struct *wait)
141 struct saa7134_dev *dev = file->private_data;
143 return videobuf_poll_stream(file, &dev->empress_tsq, wait);
148 ts_mmap(struct file *file, struct vm_area_struct * vma)
150 struct saa7134_dev *dev = file->private_data;
152 return videobuf_mmap_mapper(&dev->empress_tsq, vma);
156 * This function is _not_ called directly, but from
157 * video_generic_ioctl (and maybe others). userspace
158 * copying is done already, arg is a kernel pointer.
160 static int ts_do_ioctl(struct inode *inode, struct file *file,
161 unsigned int cmd, void *arg)
163 struct saa7134_dev *dev = file->private_data;
166 saa7134_print_ioctl(dev->name,cmd);
168 case VIDIOC_QUERYCAP:
170 struct v4l2_capability *cap = arg;
172 memset(cap,0,sizeof(*cap));
173 strcpy(cap->driver, "saa7134");
174 strlcpy(cap->card, saa7134_boards[dev->board].name,
176 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
177 cap->version = SAA7134_VERSION_CODE;
179 V4L2_CAP_VIDEO_CAPTURE |
185 /* --- input switching --------------------------------------- */
186 case VIDIOC_ENUMINPUT:
188 struct v4l2_input *i = arg;
192 i->type = V4L2_INPUT_TYPE_CAMERA;
193 strcpy(i->name,"CCIR656");
210 /* --- capture ioctls ---------------------------------------- */
212 case VIDIOC_ENUM_FMT:
214 struct v4l2_fmtdesc *f = arg;
221 memset(f,0,sizeof(*f));
223 strlcpy(f->description, "MPEG TS", sizeof(f->description));
224 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
225 f->pixelformat = V4L2_PIX_FMT_MPEG;
231 struct v4l2_format *f = arg;
233 memset(f,0,sizeof(*f));
234 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
236 saa7134_i2c_call_clients(dev, cmd, arg);
237 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
238 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
244 struct v4l2_format *f = arg;
246 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
249 saa7134_i2c_call_clients(dev, cmd, arg);
250 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
251 f->fmt.pix.sizeimage = TS_PACKET_SIZE* dev->ts.nr_packets;
256 return videobuf_reqbufs(&dev->empress_tsq,arg);
258 case VIDIOC_QUERYBUF:
259 return videobuf_querybuf(&dev->empress_tsq,arg);
262 return videobuf_qbuf(&dev->empress_tsq,arg);
265 return videobuf_dqbuf(&dev->empress_tsq,arg,
266 file->f_flags & O_NONBLOCK);
268 case VIDIOC_STREAMON:
269 return videobuf_streamon(&dev->empress_tsq);
271 case VIDIOC_STREAMOFF:
272 return videobuf_streamoff(&dev->empress_tsq);
274 case VIDIOC_QUERYCTRL:
277 return saa7134_common_ioctl(dev, cmd, arg);
279 case VIDIOC_S_MPEGCOMP:
280 saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, arg);
281 ts_init_encoder(dev);
283 case VIDIOC_G_MPEGCOMP:
284 saa7134_i2c_call_clients(dev, VIDIOC_G_MPEGCOMP, arg);
293 static int ts_ioctl(struct inode *inode, struct file *file,
294 unsigned int cmd, unsigned long arg)
296 return video_usercopy(inode, file, cmd, arg, ts_do_ioctl);
299 static struct file_operations ts_fops =
301 .owner = THIS_MODULE,
303 .release = ts_release,
311 /* ----------------------------------------------------------- */
313 static struct video_device saa7134_empress_template =
315 .name = "saa7134-empress",
316 .type = 0 /* FIXME */,
317 .type2 = 0 /* FIXME */,
323 static void empress_signal_update(void* data)
325 struct saa7134_dev* dev = (struct saa7134_dev*) data;
328 dprintk("no video signal\n");
329 ts_reset_encoder(dev);
331 dprintk("video signal acquired\n");
332 if (dev->empress_users)
333 ts_init_encoder(dev);
337 static void empress_signal_change(struct saa7134_dev *dev)
339 schedule_work(&dev->empress_workqueue);
343 static int empress_init(struct saa7134_dev *dev)
347 dprintk("%s: %s\n",dev->name,__FUNCTION__);
348 dev->empress_dev = video_device_alloc();
349 if (NULL == dev->empress_dev)
351 *(dev->empress_dev) = saa7134_empress_template;
352 dev->empress_dev->dev = &dev->pci->dev;
353 dev->empress_dev->release = video_device_release;
354 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
355 "%s empress (%s)", dev->name,
356 saa7134_boards[dev->board].name);
358 INIT_WORK(&dev->empress_workqueue, empress_signal_update, (void*) dev);
360 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
361 empress_nr[dev->nr]);
363 printk(KERN_INFO "%s: can't register video device\n",
365 video_device_release(dev->empress_dev);
366 dev->empress_dev = NULL;
369 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
370 dev->name,dev->empress_dev->minor & 0x1f);
372 videobuf_queue_init(&dev->empress_tsq, &saa7134_ts_qops,
373 dev->pci, &dev->slock,
374 V4L2_BUF_TYPE_VIDEO_CAPTURE,
375 V4L2_FIELD_ALTERNATE,
376 sizeof(struct saa7134_buf),
379 empress_signal_update(dev);
383 static int empress_fini(struct saa7134_dev *dev)
385 dprintk("%s: %s\n",dev->name,__FUNCTION__);
387 if (NULL == dev->empress_dev)
389 flush_scheduled_work();
390 video_unregister_device(dev->empress_dev);
391 dev->empress_dev = NULL;
395 static struct saa7134_mpeg_ops empress_ops = {
396 .type = SAA7134_MPEG_EMPRESS,
397 .init = empress_init,
398 .fini = empress_fini,
399 .signal_change = empress_signal_change,
402 static int __init empress_register(void)
404 return saa7134_ts_register(&empress_ops);
407 static void __exit empress_unregister(void)
409 saa7134_ts_unregister(&empress_ops);
412 module_init(empress_register);
413 module_exit(empress_unregister);
415 /* ----------------------------------------------------------- */