[media] saa7134: drop deprecated current_norm
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / media / pci / 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/kernel.h>
24 #include <linux/delay.h>
25
26 #include "saa7134-reg.h"
27 #include "saa7134.h"
28
29 #include <media/saa6752hs.h>
30 #include <media/v4l2-common.h>
31
32 /* ------------------------------------------------------------------ */
33
34 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35 MODULE_LICENSE("GPL");
36
37 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
38
39 module_param_array(empress_nr, int, NULL, 0444);
40 MODULE_PARM_DESC(empress_nr,"ts device number");
41
42 static unsigned int debug;
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         u32 leading_null_bytes = 0;
66
67         /* If more cards start to need this, then this
68            should probably be added to the card definitions. */
69         switch (dev->board) {
70         case SAA7134_BOARD_BEHOLD_M6:
71         case SAA7134_BOARD_BEHOLD_M63:
72         case SAA7134_BOARD_BEHOLD_M6_EXTRA:
73                 leading_null_bytes = 1;
74                 break;
75         }
76         ts_reset_encoder(dev);
77         saa_call_all(dev, core, init, leading_null_bytes);
78         dev->empress_started = 1;
79         return 0;
80 }
81
82 /* ------------------------------------------------------------------ */
83
84 static int ts_open(struct file *file)
85 {
86         struct video_device *vdev = video_devdata(file);
87         struct saa7134_dev *dev = video_drvdata(file);
88         int err;
89
90         dprintk("open dev=%s\n", video_device_node_name(vdev));
91         err = -EBUSY;
92         if (!mutex_trylock(&dev->empress_tsq.vb_lock))
93                 return err;
94         if (atomic_read(&dev->empress_users))
95                 goto done;
96
97         /* Unmute audio */
98         saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
99                 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
100
101         atomic_inc(&dev->empress_users);
102         file->private_data = dev;
103         err = 0;
104
105 done:
106         mutex_unlock(&dev->empress_tsq.vb_lock);
107         return err;
108 }
109
110 static int ts_release(struct file *file)
111 {
112         struct saa7134_dev *dev = file->private_data;
113
114         videobuf_stop(&dev->empress_tsq);
115         videobuf_mmap_free(&dev->empress_tsq);
116
117         /* stop the encoder */
118         ts_reset_encoder(dev);
119
120         /* Mute audio */
121         saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
122                 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
123
124         atomic_dec(&dev->empress_users);
125
126         return 0;
127 }
128
129 static ssize_t
130 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
131 {
132         struct saa7134_dev *dev = file->private_data;
133
134         if (!dev->empress_started)
135                 ts_init_encoder(dev);
136
137         return videobuf_read_stream(&dev->empress_tsq,
138                                     data, count, ppos, 0,
139                                     file->f_flags & O_NONBLOCK);
140 }
141
142 static unsigned int
143 ts_poll(struct file *file, struct poll_table_struct *wait)
144 {
145         struct saa7134_dev *dev = file->private_data;
146
147         return videobuf_poll_stream(file, &dev->empress_tsq, wait);
148 }
149
150
151 static int
152 ts_mmap(struct file *file, struct vm_area_struct * vma)
153 {
154         struct saa7134_dev *dev = file->private_data;
155
156         return videobuf_mmap_mapper(&dev->empress_tsq, vma);
157 }
158
159 /*
160  * This function is _not_ called directly, but from
161  * video_generic_ioctl (and maybe others).  userspace
162  * copying is done already, arg is a kernel pointer.
163  */
164
165 static int empress_querycap(struct file *file, void  *priv,
166                                         struct v4l2_capability *cap)
167 {
168         struct saa7134_dev *dev = file->private_data;
169
170         strcpy(cap->driver, "saa7134");
171         strlcpy(cap->card, saa7134_boards[dev->board].name,
172                 sizeof(cap->card));
173         sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
174         cap->capabilities =
175                 V4L2_CAP_VIDEO_CAPTURE |
176                 V4L2_CAP_READWRITE |
177                 V4L2_CAP_STREAMING;
178         return 0;
179 }
180
181 static int empress_enum_input(struct file *file, void *priv,
182                                         struct v4l2_input *i)
183 {
184         if (i->index != 0)
185                 return -EINVAL;
186
187         i->type = V4L2_INPUT_TYPE_CAMERA;
188         strcpy(i->name, "CCIR656");
189
190         return 0;
191 }
192
193 static int empress_g_input(struct file *file, void *priv, unsigned int *i)
194 {
195         *i = 0;
196         return 0;
197 }
198
199 static int empress_s_input(struct file *file, void *priv, unsigned int i)
200 {
201         if (i != 0)
202                 return -EINVAL;
203
204         return 0;
205 }
206
207 static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
208                                         struct v4l2_fmtdesc *f)
209 {
210         if (f->index != 0)
211                 return -EINVAL;
212
213         strlcpy(f->description, "MPEG TS", sizeof(f->description));
214         f->pixelformat = V4L2_PIX_FMT_MPEG;
215
216         return 0;
217 }
218
219 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
220                                 struct v4l2_format *f)
221 {
222         struct saa7134_dev *dev = file->private_data;
223         struct v4l2_mbus_framefmt mbus_fmt;
224
225         saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
226
227         v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
228         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
229         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
230
231         return 0;
232 }
233
234 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
235                                 struct v4l2_format *f)
236 {
237         struct saa7134_dev *dev = file->private_data;
238         struct v4l2_mbus_framefmt mbus_fmt;
239
240         v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
241         saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
242         v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
243
244         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
245         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
246
247         return 0;
248 }
249
250 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
251                                 struct v4l2_format *f)
252 {
253         struct saa7134_dev *dev = file->private_data;
254
255         f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
256         f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
257
258         return 0;
259 }
260
261 static int empress_reqbufs(struct file *file, void *priv,
262                                         struct v4l2_requestbuffers *p)
263 {
264         struct saa7134_dev *dev = file->private_data;
265
266         return videobuf_reqbufs(&dev->empress_tsq, p);
267 }
268
269 static int empress_querybuf(struct file *file, void *priv,
270                                         struct v4l2_buffer *b)
271 {
272         struct saa7134_dev *dev = file->private_data;
273
274         return videobuf_querybuf(&dev->empress_tsq, b);
275 }
276
277 static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
278 {
279         struct saa7134_dev *dev = file->private_data;
280
281         return videobuf_qbuf(&dev->empress_tsq, b);
282 }
283
284 static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
285 {
286         struct saa7134_dev *dev = file->private_data;
287
288         return videobuf_dqbuf(&dev->empress_tsq, b,
289                                 file->f_flags & O_NONBLOCK);
290 }
291
292 static int empress_streamon(struct file *file, void *priv,
293                                         enum v4l2_buf_type type)
294 {
295         struct saa7134_dev *dev = file->private_data;
296
297         return videobuf_streamon(&dev->empress_tsq);
298 }
299
300 static int empress_streamoff(struct file *file, void *priv,
301                                         enum v4l2_buf_type type)
302 {
303         struct saa7134_dev *dev = file->private_data;
304
305         return videobuf_streamoff(&dev->empress_tsq);
306 }
307
308 static int empress_s_ext_ctrls(struct file *file, void *priv,
309                                struct v4l2_ext_controls *ctrls)
310 {
311         struct saa7134_dev *dev = file->private_data;
312         int err;
313
314         /* count == 0 is abused in saa6752hs.c, so that special
315                 case is handled here explicitly. */
316         if (ctrls->count == 0)
317                 return 0;
318
319         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
320                 return -EINVAL;
321
322         err = saa_call_empress(dev, core, s_ext_ctrls, ctrls);
323         ts_init_encoder(dev);
324
325         return err;
326 }
327
328 static int empress_g_ext_ctrls(struct file *file, void *priv,
329                                struct v4l2_ext_controls *ctrls)
330 {
331         struct saa7134_dev *dev = file->private_data;
332
333         if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
334                 return -EINVAL;
335         return saa_call_empress(dev, core, g_ext_ctrls, ctrls);
336 }
337
338 static int empress_g_ctrl(struct file *file, void *priv,
339                                         struct v4l2_control *c)
340 {
341         struct saa7134_dev *dev = file->private_data;
342
343         return saa7134_g_ctrl_internal(dev, NULL, c);
344 }
345
346 static int empress_s_ctrl(struct file *file, void *priv,
347                                         struct v4l2_control *c)
348 {
349         struct saa7134_dev *dev = file->private_data;
350
351         return saa7134_s_ctrl_internal(dev, NULL, c);
352 }
353
354 static int empress_queryctrl(struct file *file, void *priv,
355                                         struct v4l2_queryctrl *c)
356 {
357         /* Must be sorted from low to high control ID! */
358         static const u32 user_ctrls[] = {
359                 V4L2_CID_USER_CLASS,
360                 V4L2_CID_BRIGHTNESS,
361                 V4L2_CID_CONTRAST,
362                 V4L2_CID_SATURATION,
363                 V4L2_CID_HUE,
364                 V4L2_CID_AUDIO_VOLUME,
365                 V4L2_CID_AUDIO_MUTE,
366                 V4L2_CID_HFLIP,
367                 0
368         };
369
370         /* Must be sorted from low to high control ID! */
371         static const u32 mpeg_ctrls[] = {
372                 V4L2_CID_MPEG_CLASS,
373                 V4L2_CID_MPEG_STREAM_TYPE,
374                 V4L2_CID_MPEG_STREAM_PID_PMT,
375                 V4L2_CID_MPEG_STREAM_PID_AUDIO,
376                 V4L2_CID_MPEG_STREAM_PID_VIDEO,
377                 V4L2_CID_MPEG_STREAM_PID_PCR,
378                 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
379                 V4L2_CID_MPEG_AUDIO_ENCODING,
380                 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
381                 V4L2_CID_MPEG_VIDEO_ENCODING,
382                 V4L2_CID_MPEG_VIDEO_ASPECT,
383                 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
384                 V4L2_CID_MPEG_VIDEO_BITRATE,
385                 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
386                 0
387         };
388         static const u32 *ctrl_classes[] = {
389                 user_ctrls,
390                 mpeg_ctrls,
391                 NULL
392         };
393         struct saa7134_dev *dev = file->private_data;
394
395         c->id = v4l2_ctrl_next(ctrl_classes, c->id);
396         if (c->id == 0)
397                 return -EINVAL;
398         if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
399                 return v4l2_ctrl_query_fill(c, 0, 0, 0, 0);
400         if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
401                 return saa7134_queryctrl(file, priv, c);
402         return saa_call_empress(dev, core, queryctrl, c);
403 }
404
405 static int empress_querymenu(struct file *file, void *priv,
406                                         struct v4l2_querymenu *c)
407 {
408         struct saa7134_dev *dev = file->private_data;
409
410         if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
411                 return -EINVAL;
412         return saa_call_empress(dev, core, querymenu, c);
413 }
414
415 static int empress_s_std(struct file *file, void *priv, v4l2_std_id id)
416 {
417         struct saa7134_dev *dev = file->private_data;
418
419         return saa7134_s_std_internal(dev, NULL, id);
420 }
421
422 static int empress_g_std(struct file *file, void *priv, v4l2_std_id *id)
423 {
424         struct saa7134_dev *dev = file->private_data;
425
426         *id = dev->tvnorm->id;
427         return 0;
428 }
429
430 static const struct v4l2_file_operations ts_fops =
431 {
432         .owner    = THIS_MODULE,
433         .open     = ts_open,
434         .release  = ts_release,
435         .read     = ts_read,
436         .poll     = ts_poll,
437         .mmap     = ts_mmap,
438         .ioctl    = video_ioctl2,
439 };
440
441 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
442         .vidioc_querycap                = empress_querycap,
443         .vidioc_enum_fmt_vid_cap        = empress_enum_fmt_vid_cap,
444         .vidioc_try_fmt_vid_cap         = empress_try_fmt_vid_cap,
445         .vidioc_s_fmt_vid_cap           = empress_s_fmt_vid_cap,
446         .vidioc_g_fmt_vid_cap           = empress_g_fmt_vid_cap,
447         .vidioc_reqbufs                 = empress_reqbufs,
448         .vidioc_querybuf                = empress_querybuf,
449         .vidioc_qbuf                    = empress_qbuf,
450         .vidioc_dqbuf                   = empress_dqbuf,
451         .vidioc_streamon                = empress_streamon,
452         .vidioc_streamoff               = empress_streamoff,
453         .vidioc_s_ext_ctrls             = empress_s_ext_ctrls,
454         .vidioc_g_ext_ctrls             = empress_g_ext_ctrls,
455         .vidioc_enum_input              = empress_enum_input,
456         .vidioc_g_input                 = empress_g_input,
457         .vidioc_s_input                 = empress_s_input,
458         .vidioc_queryctrl               = empress_queryctrl,
459         .vidioc_querymenu               = empress_querymenu,
460         .vidioc_g_ctrl                  = empress_g_ctrl,
461         .vidioc_s_ctrl                  = empress_s_ctrl,
462         .vidioc_s_std                   = empress_s_std,
463         .vidioc_g_std                   = empress_g_std,
464 };
465
466 /* ----------------------------------------------------------- */
467
468 static struct video_device saa7134_empress_template = {
469         .name          = "saa7134-empress",
470         .fops          = &ts_fops,
471         .ioctl_ops     = &ts_ioctl_ops,
472
473         .tvnorms                        = SAA7134_NORMS,
474 };
475
476 static void empress_signal_update(struct work_struct *work)
477 {
478         struct saa7134_dev* dev =
479                 container_of(work, struct saa7134_dev, empress_workqueue);
480
481         if (dev->nosignal) {
482                 dprintk("no video signal\n");
483         } else {
484                 dprintk("video signal acquired\n");
485         }
486 }
487
488 static void empress_signal_change(struct saa7134_dev *dev)
489 {
490         schedule_work(&dev->empress_workqueue);
491 }
492
493
494 static int empress_init(struct saa7134_dev *dev)
495 {
496         int err;
497
498         dprintk("%s: %s\n",dev->name,__func__);
499         dev->empress_dev = video_device_alloc();
500         if (NULL == dev->empress_dev)
501                 return -ENOMEM;
502         *(dev->empress_dev) = saa7134_empress_template;
503         dev->empress_dev->parent  = &dev->pci->dev;
504         dev->empress_dev->release = video_device_release;
505         snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
506                  "%s empress (%s)", dev->name,
507                  saa7134_boards[dev->board].name);
508
509         INIT_WORK(&dev->empress_workqueue, empress_signal_update);
510
511         video_set_drvdata(dev->empress_dev, dev);
512         err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
513                                     empress_nr[dev->nr]);
514         if (err < 0) {
515                 printk(KERN_INFO "%s: can't register video device\n",
516                        dev->name);
517                 video_device_release(dev->empress_dev);
518                 dev->empress_dev = NULL;
519                 return err;
520         }
521         printk(KERN_INFO "%s: registered device %s [mpeg]\n",
522                dev->name, video_device_node_name(dev->empress_dev));
523
524         videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
525                             &dev->pci->dev, &dev->slock,
526                             V4L2_BUF_TYPE_VIDEO_CAPTURE,
527                             V4L2_FIELD_ALTERNATE,
528                             sizeof(struct saa7134_buf),
529                             dev, NULL);
530
531         empress_signal_update(&dev->empress_workqueue);
532         return 0;
533 }
534
535 static int empress_fini(struct saa7134_dev *dev)
536 {
537         dprintk("%s: %s\n",dev->name,__func__);
538
539         if (NULL == dev->empress_dev)
540                 return 0;
541         flush_work(&dev->empress_workqueue);
542         video_unregister_device(dev->empress_dev);
543         dev->empress_dev = NULL;
544         return 0;
545 }
546
547 static struct saa7134_mpeg_ops empress_ops = {
548         .type          = SAA7134_MPEG_EMPRESS,
549         .init          = empress_init,
550         .fini          = empress_fini,
551         .signal_change = empress_signal_change,
552 };
553
554 static int __init empress_register(void)
555 {
556         return saa7134_ts_register(&empress_ops);
557 }
558
559 static void __exit empress_unregister(void)
560 {
561         saa7134_ts_unregister(&empress_ops);
562 }
563
564 module_init(empress_register);
565 module_exit(empress_unregister);
566
567 /* ----------------------------------------------------------- */
568 /*
569  * Local variables:
570  * c-basic-offset: 8
571  * End:
572  */