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