2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
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
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 static struct saa7164_tvnorm saa7164_tvnorms
[] = {
27 .id
= V4L2_STD_NTSC_M
,
30 .id
= V4L2_STD_NTSC_M_JP
,
34 static const u32 saa7164_v4l2_ctrls
[] = {
38 /* Take the encoder configuration from the port struct and
39 * flush it to the hardware.
41 static void saa7164_vbi_configure(struct saa7164_port
*port
)
43 struct saa7164_dev
*dev
= port
->dev
;
44 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
46 port
->vbi_params
.width
= port
->width
;
47 port
->vbi_params
.height
= port
->height
;
48 port
->vbi_params
.is_50hz
=
49 (port
->encodernorm
.id
& V4L2_STD_625_50
) != 0;
51 /* Set up the DIF (enable it) for analog mode by default */
52 saa7164_api_initialize_dif(port
);
54 /* Configure the correct video standard */
56 saa7164_api_configure_dif(port
, port
->encodernorm
.id
);
60 /* Ensure the audio decoder is correct configured */
61 saa7164_api_set_audio_std(port
);
63 dprintk(DBGLVL_VBI
, "%s() ends\n", __func__
);
66 static int saa7164_vbi_buffers_dealloc(struct saa7164_port
*port
)
68 struct list_head
*c
, *n
, *p
, *q
, *l
, *v
;
69 struct saa7164_dev
*dev
= port
->dev
;
70 struct saa7164_buffer
*buf
;
71 struct saa7164_user_buffer
*ubuf
;
73 /* Remove any allocated buffers */
74 mutex_lock(&port
->dmaqueue_lock
);
76 dprintk(DBGLVL_VBI
, "%s(port=%d) dmaqueue\n", __func__
, port
->nr
);
77 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
78 buf
= list_entry(c
, struct saa7164_buffer
, list
);
80 saa7164_buffer_dealloc(buf
);
83 dprintk(DBGLVL_VBI
, "%s(port=%d) used\n", __func__
, port
->nr
);
84 list_for_each_safe(p
, q
, &port
->list_buf_used
.list
) {
85 ubuf
= list_entry(p
, struct saa7164_user_buffer
, list
);
87 saa7164_buffer_dealloc_user(ubuf
);
90 dprintk(DBGLVL_VBI
, "%s(port=%d) free\n", __func__
, port
->nr
);
91 list_for_each_safe(l
, v
, &port
->list_buf_free
.list
) {
92 ubuf
= list_entry(l
, struct saa7164_user_buffer
, list
);
94 saa7164_buffer_dealloc_user(ubuf
);
97 mutex_unlock(&port
->dmaqueue_lock
);
98 dprintk(DBGLVL_VBI
, "%s(port=%d) done\n", __func__
, port
->nr
);
103 /* Dynamic buffer switch at vbi start time */
104 static int saa7164_vbi_buffers_alloc(struct saa7164_port
*port
)
106 struct saa7164_dev
*dev
= port
->dev
;
107 struct saa7164_buffer
*buf
;
108 struct saa7164_user_buffer
*ubuf
;
109 struct tmHWStreamParameters
*params
= &port
->hw_streamingparams
;
110 int result
= -ENODEV
, i
;
113 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
115 /* TODO: NTSC SPECIFIC */
116 /* Init and establish defaults */
117 params
->samplesperline
= 1440;
118 params
->numberoflines
= 12;
119 params
->numberoflines
= 18;
120 params
->pitch
= 1600;
121 params
->pitch
= 1440;
122 params
->numpagetables
= 2 +
123 ((params
->numberoflines
* params
->pitch
) / PAGE_SIZE
);
124 params
->bitspersample
= 8;
125 params
->linethreshold
= 0;
126 params
->pagetablelistvirt
= NULL
;
127 params
->pagetablelistphys
= NULL
;
128 params
->numpagetableentries
= port
->hwcfg
.buffercount
;
130 /* Allocate the PCI resources, buffers (hard) */
131 for (i
= 0; i
< port
->hwcfg
.buffercount
; i
++) {
132 buf
= saa7164_buffer_alloc(port
,
133 params
->numberoflines
*
137 printk(KERN_ERR
"%s() failed "
138 "(errno = %d), unable to allocate buffer\n",
144 mutex_lock(&port
->dmaqueue_lock
);
145 list_add_tail(&buf
->list
, &port
->dmaqueue
.list
);
146 mutex_unlock(&port
->dmaqueue_lock
);
151 /* Allocate some kernel buffers for copying
154 len
= params
->numberoflines
* params
->pitch
;
156 if (vbi_buffers
< 16)
158 if (vbi_buffers
> 512)
161 for (i
= 0; i
< vbi_buffers
; i
++) {
163 ubuf
= saa7164_buffer_alloc_user(dev
, len
);
165 mutex_lock(&port
->dmaqueue_lock
);
166 list_add_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
167 mutex_unlock(&port
->dmaqueue_lock
);
179 static int saa7164_vbi_initialize(struct saa7164_port
*port
)
181 saa7164_vbi_configure(port
);
185 /* -- V4L2 --------------------------------------------------------- */
186 static int vidioc_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
188 struct saa7164_vbi_fh
*fh
= file
->private_data
;
189 struct saa7164_port
*port
= fh
->port
;
190 struct saa7164_dev
*dev
= port
->dev
;
193 dprintk(DBGLVL_VBI
, "%s(id=0x%x)\n", __func__
, (u32
)id
);
195 for (i
= 0; i
< ARRAY_SIZE(saa7164_tvnorms
); i
++) {
196 if (id
& saa7164_tvnorms
[i
].id
)
199 if (i
== ARRAY_SIZE(saa7164_tvnorms
))
202 port
->encodernorm
= saa7164_tvnorms
[i
];
205 /* Update the audio decoder while is not running in
208 saa7164_api_set_audio_std(port
);
210 dprintk(DBGLVL_VBI
, "%s(id=0x%x) OK\n", __func__
, (u32
)id
);
215 static int vidioc_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
217 struct saa7164_encoder_fh
*fh
= file
->private_data
;
218 struct saa7164_port
*port
= fh
->port
;
224 static int vidioc_enum_input(struct file
*file
, void *priv
,
225 struct v4l2_input
*i
)
229 char *inputs
[] = { "tuner", "composite", "svideo", "aux",
230 "composite 2", "svideo 2", "aux 2" };
235 strcpy(i
->name
, inputs
[i
->index
]);
238 i
->type
= V4L2_INPUT_TYPE_TUNER
;
240 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
242 for (n
= 0; n
< ARRAY_SIZE(saa7164_tvnorms
); n
++)
243 i
->std
|= saa7164_tvnorms
[n
].id
;
248 static int vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
250 struct saa7164_vbi_fh
*fh
= file
->private_data
;
251 struct saa7164_port
*port
= fh
->port
;
252 struct saa7164_dev
*dev
= port
->dev
;
254 if (saa7164_api_get_videomux(port
) != SAA_OK
)
257 *i
= (port
->mux_input
- 1);
259 dprintk(DBGLVL_VBI
, "%s() input=%d\n", __func__
, *i
);
264 static int vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
266 struct saa7164_vbi_fh
*fh
= file
->private_data
;
267 struct saa7164_port
*port
= fh
->port
;
268 struct saa7164_dev
*dev
= port
->dev
;
270 dprintk(DBGLVL_VBI
, "%s() input=%d\n", __func__
, i
);
275 port
->mux_input
= i
+ 1;
277 if (saa7164_api_set_videomux(port
) != SAA_OK
)
283 static int vidioc_g_tuner(struct file
*file
, void *priv
,
284 struct v4l2_tuner
*t
)
286 struct saa7164_vbi_fh
*fh
= file
->private_data
;
287 struct saa7164_port
*port
= fh
->port
;
288 struct saa7164_dev
*dev
= port
->dev
;
293 strcpy(t
->name
, "tuner");
294 t
->type
= V4L2_TUNER_ANALOG_TV
;
295 t
->capability
= V4L2_TUNER_CAP_NORM
| V4L2_TUNER_CAP_STEREO
;
297 dprintk(DBGLVL_VBI
, "VIDIOC_G_TUNER: tuner type %d\n", t
->type
);
302 static int vidioc_s_tuner(struct file
*file
, void *priv
,
303 const struct v4l2_tuner
*t
)
305 /* Update the A/V core */
309 static int vidioc_g_frequency(struct file
*file
, void *priv
,
310 struct v4l2_frequency
*f
)
312 struct saa7164_vbi_fh
*fh
= file
->private_data
;
313 struct saa7164_port
*port
= fh
->port
;
315 f
->type
= V4L2_TUNER_ANALOG_TV
;
316 f
->frequency
= port
->freq
;
321 static int vidioc_s_frequency(struct file
*file
, void *priv
,
322 const struct v4l2_frequency
*f
)
324 struct saa7164_vbi_fh
*fh
= file
->private_data
;
325 struct saa7164_port
*port
= fh
->port
;
326 struct saa7164_dev
*dev
= port
->dev
;
327 struct saa7164_port
*tsport
;
328 struct dvb_frontend
*fe
;
330 /* TODO: Pull this for the std */
331 struct analog_parameters params
= {
332 .mode
= V4L2_TUNER_ANALOG_TV
,
333 .audmode
= V4L2_TUNER_MODE_STEREO
,
334 .std
= port
->encodernorm
.id
,
335 .frequency
= f
->frequency
338 /* Stop the encoder */
339 dprintk(DBGLVL_VBI
, "%s() frequency=%d tuner=%d\n", __func__
,
340 f
->frequency
, f
->tuner
);
345 if (f
->type
!= V4L2_TUNER_ANALOG_TV
)
348 port
->freq
= f
->frequency
;
350 /* Update the hardware */
351 if (port
->nr
== SAA7164_PORT_VBI1
)
352 tsport
= &dev
->ports
[SAA7164_PORT_TS1
];
354 if (port
->nr
== SAA7164_PORT_VBI2
)
355 tsport
= &dev
->ports
[SAA7164_PORT_TS2
];
359 fe
= tsport
->dvb
.frontend
;
361 if (fe
&& fe
->ops
.tuner_ops
.set_analog_params
)
362 fe
->ops
.tuner_ops
.set_analog_params(fe
, ¶ms
);
364 printk(KERN_ERR
"%s() No analog tuner, aborting\n", __func__
);
366 saa7164_vbi_initialize(port
);
371 static int vidioc_g_ctrl(struct file
*file
, void *priv
,
372 struct v4l2_control
*ctl
)
374 struct saa7164_vbi_fh
*fh
= file
->private_data
;
375 struct saa7164_port
*port
= fh
->port
;
376 struct saa7164_dev
*dev
= port
->dev
;
378 dprintk(DBGLVL_VBI
, "%s(id=%d, value=%d)\n", __func__
,
379 ctl
->id
, ctl
->value
);
382 case V4L2_CID_BRIGHTNESS
:
383 ctl
->value
= port
->ctl_brightness
;
385 case V4L2_CID_CONTRAST
:
386 ctl
->value
= port
->ctl_contrast
;
388 case V4L2_CID_SATURATION
:
389 ctl
->value
= port
->ctl_saturation
;
392 ctl
->value
= port
->ctl_hue
;
394 case V4L2_CID_SHARPNESS
:
395 ctl
->value
= port
->ctl_sharpness
;
397 case V4L2_CID_AUDIO_VOLUME
:
398 ctl
->value
= port
->ctl_volume
;
407 static int vidioc_s_ctrl(struct file
*file
, void *priv
,
408 struct v4l2_control
*ctl
)
410 struct saa7164_vbi_fh
*fh
= file
->private_data
;
411 struct saa7164_port
*port
= fh
->port
;
412 struct saa7164_dev
*dev
= port
->dev
;
415 dprintk(DBGLVL_VBI
, "%s(id=%d, value=%d)\n", __func__
,
416 ctl
->id
, ctl
->value
);
419 case V4L2_CID_BRIGHTNESS
:
420 if ((ctl
->value
>= 0) && (ctl
->value
<= 255)) {
421 port
->ctl_brightness
= ctl
->value
;
422 saa7164_api_set_usercontrol(port
,
423 PU_BRIGHTNESS_CONTROL
);
427 case V4L2_CID_CONTRAST
:
428 if ((ctl
->value
>= 0) && (ctl
->value
<= 255)) {
429 port
->ctl_contrast
= ctl
->value
;
430 saa7164_api_set_usercontrol(port
, PU_CONTRAST_CONTROL
);
434 case V4L2_CID_SATURATION
:
435 if ((ctl
->value
>= 0) && (ctl
->value
<= 255)) {
436 port
->ctl_saturation
= ctl
->value
;
437 saa7164_api_set_usercontrol(port
,
438 PU_SATURATION_CONTROL
);
443 if ((ctl
->value
>= 0) && (ctl
->value
<= 255)) {
444 port
->ctl_hue
= ctl
->value
;
445 saa7164_api_set_usercontrol(port
, PU_HUE_CONTROL
);
449 case V4L2_CID_SHARPNESS
:
450 if ((ctl
->value
>= 0) && (ctl
->value
<= 255)) {
451 port
->ctl_sharpness
= ctl
->value
;
452 saa7164_api_set_usercontrol(port
, PU_SHARPNESS_CONTROL
);
456 case V4L2_CID_AUDIO_VOLUME
:
457 if ((ctl
->value
>= -83) && (ctl
->value
<= 24)) {
458 port
->ctl_volume
= ctl
->value
;
459 saa7164_api_set_audio_volume(port
, port
->ctl_volume
);
470 static int saa7164_get_ctrl(struct saa7164_port
*port
,
471 struct v4l2_ext_control
*ctrl
)
473 struct saa7164_vbi_params
*params
= &port
->vbi_params
;
476 case V4L2_CID_MPEG_STREAM_TYPE
:
477 ctrl
->value
= params
->stream_type
;
479 case V4L2_CID_MPEG_AUDIO_MUTE
:
480 ctrl
->value
= params
->ctl_mute
;
482 case V4L2_CID_MPEG_VIDEO_ASPECT
:
483 ctrl
->value
= params
->ctl_aspect
;
485 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
486 ctrl
->value
= params
->refdist
;
488 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
489 ctrl
->value
= params
->gop_size
;
497 static int vidioc_g_ext_ctrls(struct file
*file
, void *priv
,
498 struct v4l2_ext_controls
*ctrls
)
500 struct saa7164_vbi_fh
*fh
= file
->private_data
;
501 struct saa7164_port
*port
= fh
->port
;
504 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
505 for (i
= 0; i
< ctrls
->count
; i
++) {
506 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
508 err
= saa7164_get_ctrl(port
, ctrl
);
510 ctrls
->error_idx
= i
;
521 static int saa7164_try_ctrl(struct v4l2_ext_control
*ctrl
, int ac3
)
526 case V4L2_CID_MPEG_STREAM_TYPE
:
527 if ((ctrl
->value
== V4L2_MPEG_STREAM_TYPE_MPEG2_PS
) ||
528 (ctrl
->value
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
))
531 case V4L2_CID_MPEG_AUDIO_MUTE
:
532 if ((ctrl
->value
>= 0) &&
536 case V4L2_CID_MPEG_VIDEO_ASPECT
:
537 if ((ctrl
->value
>= V4L2_MPEG_VIDEO_ASPECT_1x1
) &&
538 (ctrl
->value
<= V4L2_MPEG_VIDEO_ASPECT_221x100
))
541 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
542 if ((ctrl
->value
>= 0) &&
543 (ctrl
->value
<= 255))
546 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
547 if ((ctrl
->value
>= 1) &&
558 static int vidioc_try_ext_ctrls(struct file
*file
, void *priv
,
559 struct v4l2_ext_controls
*ctrls
)
563 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
564 for (i
= 0; i
< ctrls
->count
; i
++) {
565 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
567 err
= saa7164_try_ctrl(ctrl
, 0);
569 ctrls
->error_idx
= i
;
579 static int saa7164_set_ctrl(struct saa7164_port
*port
,
580 struct v4l2_ext_control
*ctrl
)
582 struct saa7164_vbi_params
*params
= &port
->vbi_params
;
586 case V4L2_CID_MPEG_STREAM_TYPE
:
587 params
->stream_type
= ctrl
->value
;
589 case V4L2_CID_MPEG_AUDIO_MUTE
:
590 params
->ctl_mute
= ctrl
->value
;
591 ret
= saa7164_api_audio_mute(port
, params
->ctl_mute
);
593 printk(KERN_ERR
"%s() error, ret = 0x%x\n", __func__
,
598 case V4L2_CID_MPEG_VIDEO_ASPECT
:
599 params
->ctl_aspect
= ctrl
->value
;
600 ret
= saa7164_api_set_aspect_ratio(port
);
602 printk(KERN_ERR
"%s() error, ret = 0x%x\n", __func__
,
607 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
608 params
->refdist
= ctrl
->value
;
610 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
611 params
->gop_size
= ctrl
->value
;
617 /* TODO: Update the hardware */
622 static int vidioc_s_ext_ctrls(struct file
*file
, void *priv
,
623 struct v4l2_ext_controls
*ctrls
)
625 struct saa7164_vbi_fh
*fh
= file
->private_data
;
626 struct saa7164_port
*port
= fh
->port
;
629 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
630 for (i
= 0; i
< ctrls
->count
; i
++) {
631 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
633 err
= saa7164_try_ctrl(ctrl
, 0);
635 ctrls
->error_idx
= i
;
638 err
= saa7164_set_ctrl(port
, ctrl
);
640 ctrls
->error_idx
= i
;
651 static int vidioc_querycap(struct file
*file
, void *priv
,
652 struct v4l2_capability
*cap
)
654 struct saa7164_vbi_fh
*fh
= file
->private_data
;
655 struct saa7164_port
*port
= fh
->port
;
656 struct saa7164_dev
*dev
= port
->dev
;
658 strcpy(cap
->driver
, dev
->name
);
659 strlcpy(cap
->card
, saa7164_boards
[dev
->board
].name
,
661 sprintf(cap
->bus_info
, "PCI:%s", pci_name(dev
->pci
));
664 V4L2_CAP_VBI_CAPTURE
|
668 cap
->capabilities
|= V4L2_CAP_TUNER
;
674 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *priv
,
675 struct v4l2_fmtdesc
*f
)
680 strlcpy(f
->description
, "VBI", sizeof(f
->description
));
681 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
686 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
687 struct v4l2_format
*f
)
689 struct saa7164_vbi_fh
*fh
= file
->private_data
;
690 struct saa7164_port
*port
= fh
->port
;
691 struct saa7164_dev
*dev
= port
->dev
;
693 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
694 f
->fmt
.pix
.bytesperline
= 0;
695 f
->fmt
.pix
.sizeimage
=
696 port
->ts_packet_size
* port
->ts_packet_count
;
697 f
->fmt
.pix
.colorspace
= 0;
698 f
->fmt
.pix
.width
= port
->width
;
699 f
->fmt
.pix
.height
= port
->height
;
701 dprintk(DBGLVL_VBI
, "VIDIOC_G_FMT: w: %d, h: %d\n",
702 port
->width
, port
->height
);
707 static int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
708 struct v4l2_format
*f
)
710 struct saa7164_vbi_fh
*fh
= file
->private_data
;
711 struct saa7164_port
*port
= fh
->port
;
712 struct saa7164_dev
*dev
= port
->dev
;
714 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
715 f
->fmt
.pix
.bytesperline
= 0;
716 f
->fmt
.pix
.sizeimage
=
717 port
->ts_packet_size
* port
->ts_packet_count
;
718 f
->fmt
.pix
.colorspace
= 0;
719 dprintk(DBGLVL_VBI
, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
720 port
->width
, port
->height
);
724 static int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
725 struct v4l2_format
*f
)
727 struct saa7164_vbi_fh
*fh
= file
->private_data
;
728 struct saa7164_port
*port
= fh
->port
;
729 struct saa7164_dev
*dev
= port
->dev
;
731 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
732 f
->fmt
.pix
.bytesperline
= 0;
733 f
->fmt
.pix
.sizeimage
=
734 port
->ts_packet_size
* port
->ts_packet_count
;
735 f
->fmt
.pix
.colorspace
= 0;
737 dprintk(DBGLVL_VBI
, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
738 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
, f
->fmt
.pix
.field
);
743 static int fill_queryctrl(struct saa7164_vbi_params
*params
,
744 struct v4l2_queryctrl
*c
)
747 case V4L2_CID_BRIGHTNESS
:
748 return v4l2_ctrl_query_fill(c
, 0x0, 0xff, 1, 127);
749 case V4L2_CID_CONTRAST
:
750 return v4l2_ctrl_query_fill(c
, 0x0, 0xff, 1, 66);
751 case V4L2_CID_SATURATION
:
752 return v4l2_ctrl_query_fill(c
, 0x0, 0xff, 1, 62);
754 return v4l2_ctrl_query_fill(c
, 0x0, 0xff, 1, 128);
755 case V4L2_CID_SHARPNESS
:
756 return v4l2_ctrl_query_fill(c
, 0x0, 0x0f, 1, 8);
757 case V4L2_CID_MPEG_AUDIO_MUTE
:
758 return v4l2_ctrl_query_fill(c
, 0x0, 0x01, 1, 0);
759 case V4L2_CID_AUDIO_VOLUME
:
760 return v4l2_ctrl_query_fill(c
, -83, 24, 1, 20);
761 case V4L2_CID_MPEG_STREAM_TYPE
:
762 return v4l2_ctrl_query_fill(c
,
763 V4L2_MPEG_STREAM_TYPE_MPEG2_PS
,
764 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
,
765 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS
);
766 case V4L2_CID_MPEG_VIDEO_ASPECT
:
767 return v4l2_ctrl_query_fill(c
,
768 V4L2_MPEG_VIDEO_ASPECT_1x1
,
769 V4L2_MPEG_VIDEO_ASPECT_221x100
,
770 1, V4L2_MPEG_VIDEO_ASPECT_4x3
);
771 case V4L2_CID_MPEG_VIDEO_GOP_SIZE
:
772 return v4l2_ctrl_query_fill(c
, 1, 255, 1, 15);
773 case V4L2_CID_MPEG_VIDEO_B_FRAMES
:
774 return v4l2_ctrl_query_fill(c
,
781 static int vidioc_queryctrl(struct file
*file
, void *priv
,
782 struct v4l2_queryctrl
*c
)
784 struct saa7164_vbi_fh
*fh
= priv
;
785 struct saa7164_port
*port
= fh
->port
;
789 memset(c
, 0, sizeof(*c
));
791 next
= !!(id
& V4L2_CTRL_FLAG_NEXT_CTRL
);
792 c
->id
= id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
;
794 for (i
= 0; i
< ARRAY_SIZE(saa7164_v4l2_ctrls
); i
++) {
796 if (c
->id
< saa7164_v4l2_ctrls
[i
])
797 c
->id
= saa7164_v4l2_ctrls
[i
];
802 if (c
->id
== saa7164_v4l2_ctrls
[i
])
803 return fill_queryctrl(&port
->vbi_params
, c
);
805 if (c
->id
< saa7164_v4l2_ctrls
[i
])
812 static int saa7164_vbi_stop_port(struct saa7164_port
*port
)
814 struct saa7164_dev
*dev
= port
->dev
;
817 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_STOP
);
818 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
819 printk(KERN_ERR
"%s() stop transition failed, ret = 0x%x\n",
823 dprintk(DBGLVL_VBI
, "%s() Stopped\n", __func__
);
830 static int saa7164_vbi_acquire_port(struct saa7164_port
*port
)
832 struct saa7164_dev
*dev
= port
->dev
;
835 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_ACQUIRE
);
836 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
837 printk(KERN_ERR
"%s() acquire transition failed, ret = 0x%x\n",
841 dprintk(DBGLVL_VBI
, "%s() Acquired\n", __func__
);
848 static int saa7164_vbi_pause_port(struct saa7164_port
*port
)
850 struct saa7164_dev
*dev
= port
->dev
;
853 ret
= saa7164_api_transition_port(port
, SAA_DMASTATE_PAUSE
);
854 if ((ret
!= SAA_OK
) && (ret
!= SAA_ERR_ALREADY_STOPPED
)) {
855 printk(KERN_ERR
"%s() pause transition failed, ret = 0x%x\n",
859 dprintk(DBGLVL_VBI
, "%s() Paused\n", __func__
);
866 /* Firmware is very windows centric, meaning you have to transition
867 * the part through AVStream / KS Windows stages, forwards or backwards.
868 * States are: stopped, acquired (h/w), paused, started.
869 * We have to leave here will all of the soft buffers on the free list,
870 * else the cfg_post() func won't have soft buffers to correctly configure.
872 static int saa7164_vbi_stop_streaming(struct saa7164_port
*port
)
874 struct saa7164_dev
*dev
= port
->dev
;
875 struct saa7164_buffer
*buf
;
876 struct saa7164_user_buffer
*ubuf
;
877 struct list_head
*c
, *n
;
880 dprintk(DBGLVL_VBI
, "%s(port=%d)\n", __func__
, port
->nr
);
882 ret
= saa7164_vbi_pause_port(port
);
883 ret
= saa7164_vbi_acquire_port(port
);
884 ret
= saa7164_vbi_stop_port(port
);
886 dprintk(DBGLVL_VBI
, "%s(port=%d) Hardware stopped\n", __func__
,
889 /* Reset the state of any allocated buffer resources */
890 mutex_lock(&port
->dmaqueue_lock
);
892 /* Reset the hard and soft buffer state */
893 list_for_each_safe(c
, n
, &port
->dmaqueue
.list
) {
894 buf
= list_entry(c
, struct saa7164_buffer
, list
);
895 buf
->flags
= SAA7164_BUFFER_FREE
;
899 list_for_each_safe(c
, n
, &port
->list_buf_used
.list
) {
900 ubuf
= list_entry(c
, struct saa7164_user_buffer
, list
);
902 list_move_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
905 mutex_unlock(&port
->dmaqueue_lock
);
907 /* Free any allocated resources */
908 saa7164_vbi_buffers_dealloc(port
);
910 dprintk(DBGLVL_VBI
, "%s(port=%d) Released\n", __func__
, port
->nr
);
915 static int saa7164_vbi_start_streaming(struct saa7164_port
*port
)
917 struct saa7164_dev
*dev
= port
->dev
;
920 dprintk(DBGLVL_VBI
, "%s(port=%d)\n", __func__
, port
->nr
);
922 port
->done_first_interrupt
= 0;
924 /* allocate all of the PCIe DMA buffer resources on the fly,
925 * allowing switching between TS and PS payloads without
926 * requiring a complete driver reload.
928 saa7164_vbi_buffers_alloc(port
);
930 /* Configure the encoder with any cache values */
932 saa7164_api_set_encoder(port
);
933 saa7164_api_get_encoder(port
);
936 /* Place the empty buffers on the hardware */
937 saa7164_buffer_cfg_port(port
);
939 /* Negotiate format */
940 if (saa7164_api_set_vbi_format(port
) != SAA_OK
) {
941 printk(KERN_ERR
"%s() No supported VBI format\n", __func__
);
946 /* Acquire the hardware */
947 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_ACQUIRE
);
948 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
949 printk(KERN_ERR
"%s() acquire transition failed, res = 0x%x\n",
955 dprintk(DBGLVL_VBI
, "%s() Acquired\n", __func__
);
957 /* Pause the hardware */
958 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_PAUSE
);
959 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
960 printk(KERN_ERR
"%s() pause transition failed, res = 0x%x\n",
963 /* Stop the hardware, regardless */
964 result
= saa7164_vbi_stop_port(port
);
965 if (result
!= SAA_OK
) {
966 printk(KERN_ERR
"%s() pause/forced stop transition "
967 "failed, res = 0x%x\n", __func__
, result
);
973 dprintk(DBGLVL_VBI
, "%s() Paused\n", __func__
);
975 /* Start the hardware */
976 result
= saa7164_api_transition_port(port
, SAA_DMASTATE_RUN
);
977 if ((result
!= SAA_OK
) && (result
!= SAA_ERR_ALREADY_STOPPED
)) {
978 printk(KERN_ERR
"%s() run transition failed, result = 0x%x\n",
981 /* Stop the hardware, regardless */
982 result
= saa7164_vbi_acquire_port(port
);
983 result
= saa7164_vbi_stop_port(port
);
984 if (result
!= SAA_OK
) {
985 printk(KERN_ERR
"%s() run/forced stop transition "
986 "failed, res = 0x%x\n", __func__
, result
);
991 dprintk(DBGLVL_VBI
, "%s() Running\n", __func__
);
997 static int saa7164_vbi_fmt(struct file
*file
, void *priv
,
998 struct v4l2_format
*f
)
1001 f
->fmt
.vbi
.samples_per_line
= 1600;
1002 f
->fmt
.vbi
.samples_per_line
= 1440;
1003 f
->fmt
.vbi
.sampling_rate
= 27000000;
1004 f
->fmt
.vbi
.sample_format
= V4L2_PIX_FMT_GREY
;
1005 f
->fmt
.vbi
.offset
= 0;
1006 f
->fmt
.vbi
.flags
= 0;
1007 f
->fmt
.vbi
.start
[0] = 10;
1008 f
->fmt
.vbi
.count
[0] = 18;
1009 f
->fmt
.vbi
.start
[1] = 263 + 10 + 1;
1010 f
->fmt
.vbi
.count
[1] = 18;
1014 static int fops_open(struct file
*file
)
1016 struct saa7164_dev
*dev
;
1017 struct saa7164_port
*port
;
1018 struct saa7164_vbi_fh
*fh
;
1020 port
= (struct saa7164_port
*)video_get_drvdata(video_devdata(file
));
1026 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
1028 /* allocate + initialize per filehandle data */
1029 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
1033 file
->private_data
= fh
;
1039 static int fops_release(struct file
*file
)
1041 struct saa7164_vbi_fh
*fh
= file
->private_data
;
1042 struct saa7164_port
*port
= fh
->port
;
1043 struct saa7164_dev
*dev
= port
->dev
;
1045 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
1047 /* Shut device down on last close */
1048 if (atomic_cmpxchg(&fh
->v4l_reading
, 1, 0) == 1) {
1049 if (atomic_dec_return(&port
->v4l_reader_count
) == 0) {
1050 /* stop vbi capture then cancel buffers */
1051 saa7164_vbi_stop_streaming(port
);
1055 file
->private_data
= NULL
;
1062 saa7164_user_buffer
*saa7164_vbi_next_buf(struct saa7164_port
*port
)
1064 struct saa7164_user_buffer
*ubuf
= NULL
;
1065 struct saa7164_dev
*dev
= port
->dev
;
1068 mutex_lock(&port
->dmaqueue_lock
);
1069 if (!list_empty(&port
->list_buf_used
.list
)) {
1070 ubuf
= list_first_entry(&port
->list_buf_used
.list
,
1071 struct saa7164_user_buffer
, list
);
1074 crc
= crc32(0, ubuf
->data
, ubuf
->actual_size
);
1075 if (crc
!= ubuf
->crc
) {
1076 printk(KERN_ERR
"%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
1078 ubuf
, ubuf
->crc
, crc
);
1083 mutex_unlock(&port
->dmaqueue_lock
);
1085 dprintk(DBGLVL_VBI
, "%s() returns %p\n", __func__
, ubuf
);
1090 static ssize_t
fops_read(struct file
*file
, char __user
*buffer
,
1091 size_t count
, loff_t
*pos
)
1093 struct saa7164_vbi_fh
*fh
= file
->private_data
;
1094 struct saa7164_port
*port
= fh
->port
;
1095 struct saa7164_user_buffer
*ubuf
= NULL
;
1096 struct saa7164_dev
*dev
= port
->dev
;
1101 port
->last_read_msecs_diff
= port
->last_read_msecs
;
1102 port
->last_read_msecs
= jiffies_to_msecs(jiffies
);
1103 port
->last_read_msecs_diff
= port
->last_read_msecs
-
1104 port
->last_read_msecs_diff
;
1106 saa7164_histogram_update(&port
->read_interval
,
1107 port
->last_read_msecs_diff
);
1110 printk(KERN_ERR
"%s() ESPIPE\n", __func__
);
1114 if (atomic_cmpxchg(&fh
->v4l_reading
, 0, 1) == 0) {
1115 if (atomic_inc_return(&port
->v4l_reader_count
) == 1) {
1117 if (saa7164_vbi_initialize(port
) < 0) {
1118 printk(KERN_ERR
"%s() EINVAL\n", __func__
);
1122 saa7164_vbi_start_streaming(port
);
1127 /* blocking wait for buffer */
1128 if ((file
->f_flags
& O_NONBLOCK
) == 0) {
1129 if (wait_event_interruptible(port
->wait_read
,
1130 saa7164_vbi_next_buf(port
))) {
1131 printk(KERN_ERR
"%s() ERESTARTSYS\n", __func__
);
1132 return -ERESTARTSYS
;
1136 /* Pull the first buffer from the used list */
1137 ubuf
= saa7164_vbi_next_buf(port
);
1139 while ((count
> 0) && ubuf
) {
1141 /* set remaining bytes to copy */
1142 rem
= ubuf
->actual_size
- ubuf
->pos
;
1143 cnt
= rem
> count
? count
: rem
;
1145 p
= ubuf
->data
+ ubuf
->pos
;
1148 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1149 __func__
, (int)count
, cnt
, rem
, ubuf
, ubuf
->pos
);
1151 if (copy_to_user(buffer
, p
, cnt
)) {
1152 printk(KERN_ERR
"%s() copy_to_user failed\n", __func__
);
1154 printk(KERN_ERR
"%s() EFAULT\n", __func__
);
1165 if (ubuf
->pos
> ubuf
->actual_size
)
1166 printk(KERN_ERR
"read() pos > actual, huh?\n");
1168 if (ubuf
->pos
== ubuf
->actual_size
) {
1170 /* finished with current buffer, take next buffer */
1172 /* Requeue the buffer on the free list */
1175 mutex_lock(&port
->dmaqueue_lock
);
1176 list_move_tail(&ubuf
->list
, &port
->list_buf_free
.list
);
1177 mutex_unlock(&port
->dmaqueue_lock
);
1180 if ((file
->f_flags
& O_NONBLOCK
) == 0) {
1181 if (wait_event_interruptible(port
->wait_read
,
1182 saa7164_vbi_next_buf(port
))) {
1186 ubuf
= saa7164_vbi_next_buf(port
);
1190 if (!ret
&& !ubuf
) {
1191 printk(KERN_ERR
"%s() EAGAIN\n", __func__
);
1198 static unsigned int fops_poll(struct file
*file
, poll_table
*wait
)
1200 struct saa7164_vbi_fh
*fh
= (struct saa7164_vbi_fh
*)file
->private_data
;
1201 struct saa7164_port
*port
= fh
->port
;
1202 unsigned int mask
= 0;
1204 port
->last_poll_msecs_diff
= port
->last_poll_msecs
;
1205 port
->last_poll_msecs
= jiffies_to_msecs(jiffies
);
1206 port
->last_poll_msecs_diff
= port
->last_poll_msecs
-
1207 port
->last_poll_msecs_diff
;
1209 saa7164_histogram_update(&port
->poll_interval
,
1210 port
->last_poll_msecs_diff
);
1212 if (!video_is_registered(port
->v4l_device
))
1215 if (atomic_cmpxchg(&fh
->v4l_reading
, 0, 1) == 0) {
1216 if (atomic_inc_return(&port
->v4l_reader_count
) == 1) {
1217 if (saa7164_vbi_initialize(port
) < 0)
1219 saa7164_vbi_start_streaming(port
);
1224 /* blocking wait for buffer */
1225 if ((file
->f_flags
& O_NONBLOCK
) == 0) {
1226 if (wait_event_interruptible(port
->wait_read
,
1227 saa7164_vbi_next_buf(port
))) {
1228 return -ERESTARTSYS
;
1232 /* Pull the first buffer from the used list */
1233 if (!list_empty(&port
->list_buf_used
.list
))
1234 mask
|= POLLIN
| POLLRDNORM
;
1238 static const struct v4l2_file_operations vbi_fops
= {
1239 .owner
= THIS_MODULE
,
1241 .release
= fops_release
,
1244 .unlocked_ioctl
= video_ioctl2
,
1247 static const struct v4l2_ioctl_ops vbi_ioctl_ops
= {
1248 .vidioc_s_std
= vidioc_s_std
,
1249 .vidioc_g_std
= vidioc_g_std
,
1250 .vidioc_enum_input
= vidioc_enum_input
,
1251 .vidioc_g_input
= vidioc_g_input
,
1252 .vidioc_s_input
= vidioc_s_input
,
1253 .vidioc_g_tuner
= vidioc_g_tuner
,
1254 .vidioc_s_tuner
= vidioc_s_tuner
,
1255 .vidioc_g_frequency
= vidioc_g_frequency
,
1256 .vidioc_s_frequency
= vidioc_s_frequency
,
1257 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1258 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1259 .vidioc_querycap
= vidioc_querycap
,
1260 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1261 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1262 .vidioc_try_fmt_vid_cap
= vidioc_try_fmt_vid_cap
,
1263 .vidioc_s_fmt_vid_cap
= vidioc_s_fmt_vid_cap
,
1264 .vidioc_g_ext_ctrls
= vidioc_g_ext_ctrls
,
1265 .vidioc_s_ext_ctrls
= vidioc_s_ext_ctrls
,
1266 .vidioc_try_ext_ctrls
= vidioc_try_ext_ctrls
,
1267 .vidioc_queryctrl
= vidioc_queryctrl
,
1268 .vidioc_g_fmt_vbi_cap
= saa7164_vbi_fmt
,
1269 .vidioc_try_fmt_vbi_cap
= saa7164_vbi_fmt
,
1270 .vidioc_s_fmt_vbi_cap
= saa7164_vbi_fmt
,
1273 static struct video_device saa7164_vbi_template
= {
1276 .ioctl_ops
= &vbi_ioctl_ops
,
1278 .tvnorms
= SAA7164_NORMS
,
1281 static struct video_device
*saa7164_vbi_alloc(
1282 struct saa7164_port
*port
,
1283 struct pci_dev
*pci
,
1284 struct video_device
*template,
1287 struct video_device
*vfd
;
1288 struct saa7164_dev
*dev
= port
->dev
;
1290 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
1292 vfd
= video_device_alloc();
1297 snprintf(vfd
->name
, sizeof(vfd
->name
), "%s %s (%s)", dev
->name
,
1298 type
, saa7164_boards
[dev
->board
].name
);
1300 vfd
->v4l2_dev
= &dev
->v4l2_dev
;
1301 vfd
->release
= video_device_release
;
1305 int saa7164_vbi_register(struct saa7164_port
*port
)
1307 struct saa7164_dev
*dev
= port
->dev
;
1308 int result
= -ENODEV
;
1310 dprintk(DBGLVL_VBI
, "%s()\n", __func__
);
1312 if (port
->type
!= SAA7164_MPEG_VBI
)
1315 /* Sanity check that the PCI configuration space is active */
1316 if (port
->hwcfg
.BARLocation
== 0) {
1317 printk(KERN_ERR
"%s() failed "
1318 "(errno = %d), NO PCI configuration\n",
1324 /* Establish VBI defaults here */
1326 /* Allocate and register the video device node */
1327 port
->v4l_device
= saa7164_vbi_alloc(port
,
1328 dev
->pci
, &saa7164_vbi_template
, "vbi");
1330 if (!port
->v4l_device
) {
1331 printk(KERN_INFO
"%s: can't allocate vbi device\n",
1337 port
->std
= V4L2_STD_NTSC_M
;
1338 video_set_drvdata(port
->v4l_device
, port
);
1339 result
= video_register_device(port
->v4l_device
,
1342 printk(KERN_INFO
"%s: can't register vbi device\n",
1344 /* TODO: We're going to leak here if we don't dealloc
1345 The buffers above. The unreg function can't deal wit it.
1350 printk(KERN_INFO
"%s: registered device vbi%d [vbi]\n",
1351 dev
->name
, port
->v4l_device
->num
);
1353 /* Configure the hardware defaults */
1360 void saa7164_vbi_unregister(struct saa7164_port
*port
)
1362 struct saa7164_dev
*dev
= port
->dev
;
1364 dprintk(DBGLVL_VBI
, "%s(port=%d)\n", __func__
, port
->nr
);
1366 if (port
->type
!= SAA7164_MPEG_VBI
)
1369 if (port
->v4l_device
) {
1370 if (port
->v4l_device
->minor
!= -1)
1371 video_unregister_device(port
->v4l_device
);
1373 video_device_release(port
->v4l_device
);
1375 port
->v4l_device
= NULL
;