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/moduleparam.h>
24 #include <linux/kernel.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
28 #include "saa7134-reg.h"
31 #include <media/saa6752hs.h>
33 /* ------------------------------------------------------------------ */
35 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
36 MODULE_LICENSE("GPL");
38 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 = 0;
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 ts_reset_encoder(dev);
66 saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, NULL);
67 dev->empress_started = 1;
71 /* ------------------------------------------------------------------ */
73 static int ts_open(struct inode *inode, struct file *file)
75 int minor = iminor(inode);
76 struct saa7134_dev *h,*dev = NULL;
77 struct list_head *list;
80 list_for_each(list,&saa7134_devlist) {
81 h = list_entry(list, struct saa7134_dev, devlist);
82 if (h->empress_dev && h->empress_dev->minor == minor)
88 dprintk("open minor=%d\n",minor);
90 if (down_trylock(&dev->empress_tsq.lock))
92 if (dev->empress_users)
96 file->private_data = dev;
100 up(&dev->empress_tsq.lock);
105 static int ts_release(struct inode *inode, struct file *file)
107 struct saa7134_dev *dev = file->private_data;
109 if (dev->empress_tsq.streaming)
110 videobuf_streamoff(&dev->empress_tsq);
111 down(&dev->empress_tsq.lock);
112 if (dev->empress_tsq.reading)
113 videobuf_read_stop(&dev->empress_tsq);
114 videobuf_mmap_free(&dev->empress_tsq);
115 dev->empress_users--;
117 /* stop the encoder */
118 ts_reset_encoder(dev);
120 up(&dev->empress_tsq.lock);
125 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
127 struct saa7134_dev *dev = file->private_data;
129 if (!dev->empress_started)
130 ts_init_encoder(dev);
132 return videobuf_read_stream(&dev->empress_tsq,
133 data, count, ppos, 0,
134 file->f_flags & O_NONBLOCK);
138 ts_poll(struct file *file, struct poll_table_struct *wait)
140 struct saa7134_dev *dev = file->private_data;
142 return videobuf_poll_stream(file, &dev->empress_tsq, wait);
147 ts_mmap(struct file *file, struct vm_area_struct * vma)
149 struct saa7134_dev *dev = file->private_data;
151 return videobuf_mmap_mapper(&dev->empress_tsq, vma);
155 * This function is _not_ called directly, but from
156 * video_generic_ioctl (and maybe others). userspace
157 * copying is done already, arg is a kernel pointer.
159 static int ts_do_ioctl(struct inode *inode, struct file *file,
160 unsigned int cmd, void *arg)
162 struct saa7134_dev *dev = file->private_data;
165 saa7134_print_ioctl(dev->name,cmd);
167 case VIDIOC_QUERYCAP:
169 struct v4l2_capability *cap = arg;
171 memset(cap,0,sizeof(*cap));
172 strcpy(cap->driver, "saa7134");
173 strlcpy(cap->card, saa7134_boards[dev->board].name,
175 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
176 cap->version = SAA7134_VERSION_CODE;
178 V4L2_CAP_VIDEO_CAPTURE |
184 /* --- input switching --------------------------------------- */
185 case VIDIOC_ENUMINPUT:
187 struct v4l2_input *i = arg;
191 i->type = V4L2_INPUT_TYPE_CAMERA;
192 strcpy(i->name,"CCIR656");
209 /* --- capture ioctls ---------------------------------------- */
211 case VIDIOC_ENUM_FMT:
213 struct v4l2_fmtdesc *f = arg;
220 memset(f,0,sizeof(*f));
222 strlcpy(f->description, "MPEG TS", sizeof(f->description));
223 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
224 f->pixelformat = V4L2_PIX_FMT_MPEG;
230 struct v4l2_format *f = arg;
232 memset(f,0,sizeof(*f));
233 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
235 saa7134_i2c_call_clients(dev, cmd, arg);
236 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
237 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
243 struct v4l2_format *f = arg;
245 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
248 saa7134_i2c_call_clients(dev, cmd, arg);
249 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
250 f->fmt.pix.sizeimage = TS_PACKET_SIZE* dev->ts.nr_packets;
255 return videobuf_reqbufs(&dev->empress_tsq,arg);
257 case VIDIOC_QUERYBUF:
258 return videobuf_querybuf(&dev->empress_tsq,arg);
261 return videobuf_qbuf(&dev->empress_tsq,arg);
264 return videobuf_dqbuf(&dev->empress_tsq,arg,
265 file->f_flags & O_NONBLOCK);
267 case VIDIOC_STREAMON:
268 return videobuf_streamon(&dev->empress_tsq);
270 case VIDIOC_STREAMOFF:
271 return videobuf_streamoff(&dev->empress_tsq);
273 case VIDIOC_QUERYCTRL:
276 return saa7134_common_ioctl(dev, cmd, arg);
278 case VIDIOC_S_MPEGCOMP:
279 saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, arg);
280 ts_init_encoder(dev);
282 case VIDIOC_G_MPEGCOMP:
283 saa7134_i2c_call_clients(dev, VIDIOC_G_MPEGCOMP, arg);
292 static int ts_ioctl(struct inode *inode, struct file *file,
293 unsigned int cmd, unsigned long arg)
295 return video_usercopy(inode, file, cmd, arg, ts_do_ioctl);
298 static struct file_operations ts_fops =
300 .owner = THIS_MODULE,
302 .release = ts_release,
310 /* ----------------------------------------------------------- */
312 static struct video_device saa7134_empress_template =
314 .name = "saa7134-empress",
315 .type = 0 /* FIXME */,
316 .type2 = 0 /* FIXME */,
322 static void empress_signal_update(void* data)
324 struct saa7134_dev* dev = (struct saa7134_dev*) data;
327 dprintk("no video signal\n");
328 ts_reset_encoder(dev);
330 dprintk("video signal acquired\n");
331 if (dev->empress_users)
332 ts_init_encoder(dev);
336 static void empress_signal_change(struct saa7134_dev *dev)
338 schedule_work(&dev->empress_workqueue);
342 static int empress_init(struct saa7134_dev *dev)
346 dprintk("%s: %s\n",dev->name,__FUNCTION__);
347 dev->empress_dev = video_device_alloc();
348 if (NULL == dev->empress_dev)
350 *(dev->empress_dev) = saa7134_empress_template;
351 dev->empress_dev->dev = &dev->pci->dev;
352 dev->empress_dev->release = video_device_release;
353 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
354 "%s empress (%s)", dev->name,
355 saa7134_boards[dev->board].name);
357 INIT_WORK(&dev->empress_workqueue, empress_signal_update, (void*) dev);
359 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
360 empress_nr[dev->nr]);
362 printk(KERN_INFO "%s: can't register video device\n",
364 video_device_release(dev->empress_dev);
365 dev->empress_dev = NULL;
368 printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
369 dev->name,dev->empress_dev->minor & 0x1f);
371 videobuf_queue_init(&dev->empress_tsq, &saa7134_ts_qops,
372 dev->pci, &dev->slock,
373 V4L2_BUF_TYPE_VIDEO_CAPTURE,
374 V4L2_FIELD_ALTERNATE,
375 sizeof(struct saa7134_buf),
378 empress_signal_update(dev);
382 static int empress_fini(struct saa7134_dev *dev)
384 dprintk("%s: %s\n",dev->name,__FUNCTION__);
386 if (NULL == dev->empress_dev)
388 flush_scheduled_work();
389 video_unregister_device(dev->empress_dev);
390 dev->empress_dev = NULL;
394 static struct saa7134_mpeg_ops empress_ops = {
395 .type = SAA7134_MPEG_EMPRESS,
396 .init = empress_init,
397 .fini = empress_fini,
398 .signal_change = empress_signal_change,
401 static int __init empress_register(void)
403 return saa7134_ts_register(&empress_ops);
406 static void __exit empress_unregister(void)
408 saa7134_ts_unregister(&empress_ops);
411 module_init(empress_register);
412 module_exit(empress_unregister);
414 /* ----------------------------------------------------------- */