/home/lenb/src/to-linus branch 'acpi-2.6.12'
[linux-2.6] / drivers / media / video / saa7134 / saa7134-empress.c
1 /*
2  * $Id: saa7134-empress.c,v 1.11 2005/05/22 19:23:39 nsh Exp $
3  *
4  * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
5  *
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.
10  *
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.
15  *
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.
19  */
20
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>
28
29 #include "saa7134-reg.h"
30 #include "saa7134.h"
31
32 #include <media/saa6752hs.h>
33
34 /* ------------------------------------------------------------------ */
35
36 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
37 MODULE_LICENSE("GPL");
38
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");
42
43 static unsigned int debug = 0;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug,"enable debug messages");
46
47 #define dprintk(fmt, arg...)    if (debug)                      \
48         printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
49
50 /* ------------------------------------------------------------------ */
51
52 static void ts_reset_encoder(struct saa7134_dev* dev)
53 {
54         if (!dev->empress_started)
55                 return;
56
57         saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
58         msleep(10);
59         saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
60         msleep(100);
61         dev->empress_started = 0;
62 }
63
64 static int ts_init_encoder(struct saa7134_dev* dev)
65 {
66         ts_reset_encoder(dev);
67         saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, NULL);
68         dev->empress_started = 1;
69         return 0;
70 }
71
72 /* ------------------------------------------------------------------ */
73
74 static int ts_open(struct inode *inode, struct file *file)
75 {
76         int minor = iminor(inode);
77         struct saa7134_dev *h,*dev = NULL;
78         struct list_head *list;
79         int err;
80
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)
84                         dev = h;
85         }
86         if (NULL == dev)
87                 return -ENODEV;
88
89         dprintk("open minor=%d\n",minor);
90         err = -EBUSY;
91         if (down_trylock(&dev->empress_tsq.lock))
92                 goto done;
93         if (dev->empress_users)
94                 goto done_up;
95
96         dev->empress_users++;
97         file->private_data = dev;
98         err = 0;
99
100 done_up:
101         up(&dev->empress_tsq.lock);
102 done:
103         return err;
104 }
105
106 static int ts_release(struct inode *inode, struct file *file)
107 {
108         struct saa7134_dev *dev = file->private_data;
109
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--;
117
118         /* stop the encoder */
119         ts_reset_encoder(dev);
120
121         up(&dev->empress_tsq.lock);
122         return 0;
123 }
124
125 static ssize_t
126 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
127 {
128         struct saa7134_dev *dev = file->private_data;
129
130         if (!dev->empress_started)
131                 ts_init_encoder(dev);
132
133         return videobuf_read_stream(&dev->empress_tsq,
134                                     data, count, ppos, 0,
135                                     file->f_flags & O_NONBLOCK);
136 }
137
138 static unsigned int
139 ts_poll(struct file *file, struct poll_table_struct *wait)
140 {
141         struct saa7134_dev *dev = file->private_data;
142
143         return videobuf_poll_stream(file, &dev->empress_tsq, wait);
144 }
145
146
147 static int
148 ts_mmap(struct file *file, struct vm_area_struct * vma)
149 {
150         struct saa7134_dev *dev = file->private_data;
151
152         return videobuf_mmap_mapper(&dev->empress_tsq, vma);
153 }
154
155 /*
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.
159  */
160 static int ts_do_ioctl(struct inode *inode, struct file *file,
161                        unsigned int cmd, void *arg)
162 {
163         struct saa7134_dev *dev = file->private_data;
164
165         if (debug > 1)
166                 saa7134_print_ioctl(dev->name,cmd);
167         switch (cmd) {
168         case VIDIOC_QUERYCAP:
169         {
170                 struct v4l2_capability *cap = arg;
171
172                 memset(cap,0,sizeof(*cap));
173                 strcpy(cap->driver, "saa7134");
174                 strlcpy(cap->card, saa7134_boards[dev->board].name,
175                         sizeof(cap->card));
176                 sprintf(cap->bus_info,"PCI:%s",pci_name(dev->pci));
177                 cap->version = SAA7134_VERSION_CODE;
178                 cap->capabilities =
179                         V4L2_CAP_VIDEO_CAPTURE |
180                         V4L2_CAP_READWRITE |
181                         V4L2_CAP_STREAMING;
182                 return 0;
183         }
184
185         /* --- input switching --------------------------------------- */
186         case VIDIOC_ENUMINPUT:
187         {
188                 struct v4l2_input *i = arg;
189
190                 if (i->index != 0)
191                         return -EINVAL;
192                 i->type = V4L2_INPUT_TYPE_CAMERA;
193                 strcpy(i->name,"CCIR656");
194                 return 0;
195         }
196         case VIDIOC_G_INPUT:
197         {
198                 int *i = arg;
199                 *i = 0;
200                 return 0;
201         }
202         case VIDIOC_S_INPUT:
203         {
204                 int *i = arg;
205
206                 if (*i != 0)
207                         return -EINVAL;
208                 return 0;
209         }
210         /* --- capture ioctls ---------------------------------------- */
211
212         case VIDIOC_ENUM_FMT:
213         {
214                 struct v4l2_fmtdesc *f = arg;
215                 int index;
216
217                 index = f->index;
218                 if (index != 0)
219                         return -EINVAL;
220
221                 memset(f,0,sizeof(*f));
222                 f->index = index;
223                 strlcpy(f->description, "MPEG TS", sizeof(f->description));
224                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
225                 f->pixelformat = V4L2_PIX_FMT_MPEG;
226                 return 0;
227         }
228
229         case VIDIOC_G_FMT:
230         {
231                 struct v4l2_format *f = arg;
232
233                 memset(f,0,sizeof(*f));
234                 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
235
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;
239                 return 0;
240         }
241
242         case VIDIOC_S_FMT:
243         {
244                 struct v4l2_format *f = arg;
245
246                 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
247                     return -EINVAL;
248
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;
252                 return 0;
253         }
254
255         case VIDIOC_REQBUFS:
256                 return videobuf_reqbufs(&dev->empress_tsq,arg);
257
258         case VIDIOC_QUERYBUF:
259                 return videobuf_querybuf(&dev->empress_tsq,arg);
260
261         case VIDIOC_QBUF:
262                 return videobuf_qbuf(&dev->empress_tsq,arg);
263
264         case VIDIOC_DQBUF:
265                 return videobuf_dqbuf(&dev->empress_tsq,arg,
266                                       file->f_flags & O_NONBLOCK);
267
268         case VIDIOC_STREAMON:
269                 return videobuf_streamon(&dev->empress_tsq);
270
271         case VIDIOC_STREAMOFF:
272                 return videobuf_streamoff(&dev->empress_tsq);
273
274         case VIDIOC_QUERYCTRL:
275         case VIDIOC_G_CTRL:
276         case VIDIOC_S_CTRL:
277                 return saa7134_common_ioctl(dev, cmd, arg);
278
279         case VIDIOC_S_MPEGCOMP:
280                 saa7134_i2c_call_clients(dev, VIDIOC_S_MPEGCOMP, arg);
281                 ts_init_encoder(dev);
282                 return 0;
283         case VIDIOC_G_MPEGCOMP:
284                 saa7134_i2c_call_clients(dev, VIDIOC_G_MPEGCOMP, arg);
285                 return 0;
286
287         default:
288                 return -ENOIOCTLCMD;
289         }
290         return 0;
291 }
292
293 static int ts_ioctl(struct inode *inode, struct file *file,
294                      unsigned int cmd, unsigned long arg)
295 {
296         return video_usercopy(inode, file, cmd, arg, ts_do_ioctl);
297 }
298
299 static struct file_operations ts_fops =
300 {
301         .owner    = THIS_MODULE,
302         .open     = ts_open,
303         .release  = ts_release,
304         .read     = ts_read,
305         .poll     = ts_poll,
306         .mmap     = ts_mmap,
307         .ioctl    = ts_ioctl,
308         .llseek   = no_llseek,
309 };
310
311 /* ----------------------------------------------------------- */
312
313 static struct video_device saa7134_empress_template =
314 {
315         .name          = "saa7134-empress",
316         .type          = 0 /* FIXME */,
317         .type2         = 0 /* FIXME */,
318         .hardware      = 0,
319         .fops          = &ts_fops,
320         .minor         = -1,
321 };
322
323 static void empress_signal_update(void* data)
324 {
325         struct saa7134_dev* dev = (struct saa7134_dev*) data;
326
327         if (dev->nosignal) {
328                 dprintk("no video signal\n");
329                 ts_reset_encoder(dev);
330         } else {
331                 dprintk("video signal acquired\n");
332                 if (dev->empress_users)
333                         ts_init_encoder(dev);
334         }
335 }
336
337 static void empress_signal_change(struct saa7134_dev *dev)
338 {
339         schedule_work(&dev->empress_workqueue);
340 }
341
342
343 static int empress_init(struct saa7134_dev *dev)
344 {
345         int err;
346
347         dprintk("%s: %s\n",dev->name,__FUNCTION__);
348         dev->empress_dev = video_device_alloc();
349         if (NULL == dev->empress_dev)
350                 return -ENOMEM;
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);
357
358         INIT_WORK(&dev->empress_workqueue, empress_signal_update, (void*) dev);
359
360         err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
361                                     empress_nr[dev->nr]);
362         if (err < 0) {
363                 printk(KERN_INFO "%s: can't register video device\n",
364                        dev->name);
365                 video_device_release(dev->empress_dev);
366                 dev->empress_dev = NULL;
367                 return err;
368         }
369         printk(KERN_INFO "%s: registered device video%d [mpeg]\n",
370                dev->name,dev->empress_dev->minor & 0x1f);
371
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),
377                             dev);
378
379         empress_signal_update(dev);
380         return 0;
381 }
382
383 static int empress_fini(struct saa7134_dev *dev)
384 {
385         dprintk("%s: %s\n",dev->name,__FUNCTION__);
386
387         if (NULL == dev->empress_dev)
388                 return 0;
389         flush_scheduled_work();
390         video_unregister_device(dev->empress_dev);
391         dev->empress_dev = NULL;
392         return 0;
393 }
394
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,
400 };
401
402 static int __init empress_register(void)
403 {
404         return saa7134_ts_register(&empress_ops);
405 }
406
407 static void __exit empress_unregister(void)
408 {
409         saa7134_ts_unregister(&empress_ops);
410 }
411
412 module_init(empress_register);
413 module_exit(empress_unregister);
414
415 /* ----------------------------------------------------------- */
416 /*
417  * Local variables:
418  * c-basic-offset: 8
419  * End:
420  */