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 */
55 // saa7164_api_configure_dif(port, port->encodernorm.id);
57 // /* Ensure the audio decoder is correct configured */
58 // saa7164_api_set_audio_std(port);
59 dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
62 static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
64 struct list_head *c, *n, *p, *q, *l, *v;
65 struct saa7164_dev *dev = port->dev;
66 struct saa7164_buffer *buf;
67 struct saa7164_user_buffer *ubuf;
69 /* Remove any allocated buffers */
70 mutex_lock(&port->dmaqueue_lock);
72 dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
73 list_for_each_safe(c, n, &port->dmaqueue.list) {
74 buf = list_entry(c, struct saa7164_buffer, list);
76 saa7164_buffer_dealloc(buf);
79 dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
80 list_for_each_safe(p, q, &port->list_buf_used.list) {
81 ubuf = list_entry(p, struct saa7164_user_buffer, list);
83 saa7164_buffer_dealloc_user(ubuf);
86 dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
87 list_for_each_safe(l, v, &port->list_buf_free.list) {
88 ubuf = list_entry(l, struct saa7164_user_buffer, list);
90 saa7164_buffer_dealloc_user(ubuf);
93 mutex_unlock(&port->dmaqueue_lock);
94 dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
99 /* Dynamic buffer switch at vbi start time */
100 static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
102 struct saa7164_dev *dev = port->dev;
103 struct saa7164_buffer *buf;
104 struct saa7164_user_buffer *ubuf;
105 struct tmHWStreamParameters *params = &port->hw_streamingparams;
106 int result = -ENODEV, i;
109 dprintk(DBGLVL_VBI, "%s()\n", __func__);
111 /* TODO: NTSC SPECIFIC */
112 /* Init and establish defaults */
113 params->samplesperline = 1440;
114 params->numberoflines = 12;
115 params->numberoflines = 18;
116 params->pitch = 1600;
117 params->pitch = 1440;
118 params->numpagetables = 2 +
119 ((params->numberoflines * params->pitch) / PAGE_SIZE);
120 params->bitspersample = 8;
121 params->linethreshold = 0;
122 params->pagetablelistvirt = 0;
123 params->pagetablelistphys = 0;
124 params->numpagetableentries = port->hwcfg.buffercount;
126 /* Allocate the PCI resources, buffers (hard) */
127 for (i = 0; i < port->hwcfg.buffercount; i++) {
128 buf = saa7164_buffer_alloc(port,
129 params->numberoflines *
133 printk(KERN_ERR "%s() failed "
134 "(errno = %d), unable to allocate buffer\n",
140 mutex_lock(&port->dmaqueue_lock);
141 list_add_tail(&buf->list, &port->dmaqueue.list);
142 mutex_unlock(&port->dmaqueue_lock);
147 /* Allocate some kenrel kernel buffers for copying
150 len = params->numberoflines * params->pitch;
152 if (vbi_buffers < 16)
154 if (vbi_buffers > 512)
157 for (i = 0; i < vbi_buffers; i++) {
159 ubuf = saa7164_buffer_alloc_user(dev, len);
161 mutex_lock(&port->dmaqueue_lock);
162 list_add_tail(&ubuf->list, &port->list_buf_free.list);
163 mutex_unlock(&port->dmaqueue_lock);
175 static int saa7164_vbi_initialize(struct saa7164_port *port)
177 saa7164_vbi_configure(port);
181 /* -- V4L2 --------------------------------------------------------- */
182 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *id)
184 struct saa7164_vbi_fh *fh = file->private_data;
185 struct saa7164_port *port = fh->port;
186 struct saa7164_dev *dev = port->dev;
189 dprintk(DBGLVL_VBI, "%s(id=0x%x)\n", __func__, (u32)*id);
191 for (i = 0; i < ARRAY_SIZE(saa7164_tvnorms); i++) {
192 if (*id & saa7164_tvnorms[i].id)
195 if (i == ARRAY_SIZE(saa7164_tvnorms))
198 port->encodernorm = saa7164_tvnorms[i];
200 /* Update the audio decoder while is not running in
203 saa7164_api_set_audio_std(port);
205 dprintk(DBGLVL_VBI, "%s(id=0x%x) OK\n", __func__, (u32)*id);
210 static int vidioc_enum_input(struct file *file, void *priv,
211 struct v4l2_input *i)
215 char *inputs[] = { "tuner", "composite", "svideo", "aux",
216 "composite 2", "svideo 2", "aux 2" };
221 strcpy(i->name, inputs[i->index]);
224 i->type = V4L2_INPUT_TYPE_TUNER;
226 i->type = V4L2_INPUT_TYPE_CAMERA;
228 for (n = 0; n < ARRAY_SIZE(saa7164_tvnorms); n++)
229 i->std |= saa7164_tvnorms[n].id;
234 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
236 struct saa7164_vbi_fh *fh = file->private_data;
237 struct saa7164_port *port = fh->port;
238 struct saa7164_dev *dev = port->dev;
240 if (saa7164_api_get_videomux(port) != SAA_OK)
243 *i = (port->mux_input - 1);
245 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, *i);
250 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
252 struct saa7164_vbi_fh *fh = file->private_data;
253 struct saa7164_port *port = fh->port;
254 struct saa7164_dev *dev = port->dev;
256 dprintk(DBGLVL_VBI, "%s() input=%d\n", __func__, i);
261 port->mux_input = i + 1;
263 if (saa7164_api_set_videomux(port) != SAA_OK)
269 static int vidioc_g_tuner(struct file *file, void *priv,
270 struct v4l2_tuner *t)
272 struct saa7164_vbi_fh *fh = file->private_data;
273 struct saa7164_port *port = fh->port;
274 struct saa7164_dev *dev = port->dev;
279 strcpy(t->name, "tuner");
280 t->type = V4L2_TUNER_ANALOG_TV;
281 t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO;
283 dprintk(DBGLVL_VBI, "VIDIOC_G_TUNER: tuner type %d\n", t->type);
288 static int vidioc_s_tuner(struct file *file, void *priv,
289 struct v4l2_tuner *t)
291 /* Update the A/V core */
295 static int vidioc_g_frequency(struct file *file, void *priv,
296 struct v4l2_frequency *f)
298 struct saa7164_vbi_fh *fh = file->private_data;
299 struct saa7164_port *port = fh->port;
301 f->type = V4L2_TUNER_ANALOG_TV;
302 f->frequency = port->freq;
307 static int vidioc_s_frequency(struct file *file, void *priv,
308 struct v4l2_frequency *f)
310 struct saa7164_vbi_fh *fh = file->private_data;
311 struct saa7164_port *port = fh->port;
312 struct saa7164_dev *dev = port->dev;
313 struct saa7164_port *tsport;
314 struct dvb_frontend *fe;
316 /* TODO: Pull this for the std */
317 struct analog_parameters params = {
318 .mode = V4L2_TUNER_ANALOG_TV,
319 .audmode = V4L2_TUNER_MODE_STEREO,
320 .std = port->encodernorm.id,
321 .frequency = f->frequency
324 /* Stop the encoder */
325 dprintk(DBGLVL_VBI, "%s() frequency=%d tuner=%d\n", __func__,
326 f->frequency, f->tuner);
331 if (f->type != V4L2_TUNER_ANALOG_TV)
334 port->freq = f->frequency;
336 /* Update the hardware */
337 if (port->nr == SAA7164_PORT_VBI1)
338 tsport = &dev->ports[SAA7164_PORT_TS1];
340 if (port->nr == SAA7164_PORT_VBI2)
341 tsport = &dev->ports[SAA7164_PORT_TS2];
345 fe = tsport->dvb.frontend;
347 if (fe && fe->ops.tuner_ops.set_analog_params)
348 fe->ops.tuner_ops.set_analog_params(fe, ¶ms);
350 printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__);
352 saa7164_vbi_initialize(port);
357 static int vidioc_g_ctrl(struct file *file, void *priv,
358 struct v4l2_control *ctl)
360 struct saa7164_vbi_fh *fh = file->private_data;
361 struct saa7164_port *port = fh->port;
362 struct saa7164_dev *dev = port->dev;
364 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
365 ctl->id, ctl->value);
368 case V4L2_CID_BRIGHTNESS:
369 ctl->value = port->ctl_brightness;
371 case V4L2_CID_CONTRAST:
372 ctl->value = port->ctl_contrast;
374 case V4L2_CID_SATURATION:
375 ctl->value = port->ctl_saturation;
378 ctl->value = port->ctl_hue;
380 case V4L2_CID_SHARPNESS:
381 ctl->value = port->ctl_sharpness;
383 case V4L2_CID_AUDIO_VOLUME:
384 ctl->value = port->ctl_volume;
393 static int vidioc_s_ctrl(struct file *file, void *priv,
394 struct v4l2_control *ctl)
396 struct saa7164_vbi_fh *fh = file->private_data;
397 struct saa7164_port *port = fh->port;
398 struct saa7164_dev *dev = port->dev;
401 dprintk(DBGLVL_VBI, "%s(id=%d, value=%d)\n", __func__,
402 ctl->id, ctl->value);
405 case V4L2_CID_BRIGHTNESS:
406 if ((ctl->value >= 0) && (ctl->value <= 255)) {
407 port->ctl_brightness = ctl->value;
408 saa7164_api_set_usercontrol(port,
409 PU_BRIGHTNESS_CONTROL);
413 case V4L2_CID_CONTRAST:
414 if ((ctl->value >= 0) && (ctl->value <= 255)) {
415 port->ctl_contrast = ctl->value;
416 saa7164_api_set_usercontrol(port, PU_CONTRAST_CONTROL);
420 case V4L2_CID_SATURATION:
421 if ((ctl->value >= 0) && (ctl->value <= 255)) {
422 port->ctl_saturation = ctl->value;
423 saa7164_api_set_usercontrol(port,
424 PU_SATURATION_CONTROL);
429 if ((ctl->value >= 0) && (ctl->value <= 255)) {
430 port->ctl_hue = ctl->value;
431 saa7164_api_set_usercontrol(port, PU_HUE_CONTROL);
435 case V4L2_CID_SHARPNESS:
436 if ((ctl->value >= 0) && (ctl->value <= 255)) {
437 port->ctl_sharpness = ctl->value;
438 saa7164_api_set_usercontrol(port, PU_SHARPNESS_CONTROL);
442 case V4L2_CID_AUDIO_VOLUME:
443 if ((ctl->value >= -83) && (ctl->value <= 24)) {
444 port->ctl_volume = ctl->value;
445 saa7164_api_set_audio_volume(port, port->ctl_volume);
456 static int saa7164_get_ctrl(struct saa7164_port *port,
457 struct v4l2_ext_control *ctrl)
459 struct saa7164_vbi_params *params = &port->vbi_params;
462 case V4L2_CID_MPEG_STREAM_TYPE:
463 ctrl->value = params->stream_type;
465 case V4L2_CID_MPEG_AUDIO_MUTE:
466 ctrl->value = params->ctl_mute;
468 case V4L2_CID_MPEG_VIDEO_ASPECT:
469 ctrl->value = params->ctl_aspect;
471 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
472 ctrl->value = params->refdist;
474 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
475 ctrl->value = params->gop_size;
483 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
484 struct v4l2_ext_controls *ctrls)
486 struct saa7164_vbi_fh *fh = file->private_data;
487 struct saa7164_port *port = fh->port;
490 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
491 for (i = 0; i < ctrls->count; i++) {
492 struct v4l2_ext_control *ctrl = ctrls->controls + i;
494 err = saa7164_get_ctrl(port, ctrl);
496 ctrls->error_idx = i;
507 static int saa7164_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
512 case V4L2_CID_MPEG_STREAM_TYPE:
513 if ((ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_PS) ||
514 (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS))
517 case V4L2_CID_MPEG_AUDIO_MUTE:
518 if ((ctrl->value >= 0) &&
522 case V4L2_CID_MPEG_VIDEO_ASPECT:
523 if ((ctrl->value >= V4L2_MPEG_VIDEO_ASPECT_1x1) &&
524 (ctrl->value <= V4L2_MPEG_VIDEO_ASPECT_221x100))
527 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
528 if ((ctrl->value >= 0) &&
529 (ctrl->value <= 255))
532 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
533 if ((ctrl->value >= 1) &&
544 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
545 struct v4l2_ext_controls *ctrls)
549 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
550 for (i = 0; i < ctrls->count; i++) {
551 struct v4l2_ext_control *ctrl = ctrls->controls + i;
553 err = saa7164_try_ctrl(ctrl, 0);
555 ctrls->error_idx = i;
565 static int saa7164_set_ctrl(struct saa7164_port *port,
566 struct v4l2_ext_control *ctrl)
568 struct saa7164_vbi_params *params = &port->vbi_params;
572 case V4L2_CID_MPEG_STREAM_TYPE:
573 params->stream_type = ctrl->value;
575 case V4L2_CID_MPEG_AUDIO_MUTE:
576 params->ctl_mute = ctrl->value;
577 ret = saa7164_api_audio_mute(port, params->ctl_mute);
579 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
584 case V4L2_CID_MPEG_VIDEO_ASPECT:
585 params->ctl_aspect = ctrl->value;
586 ret = saa7164_api_set_aspect_ratio(port);
588 printk(KERN_ERR "%s() error, ret = 0x%x\n", __func__,
593 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
594 params->refdist = ctrl->value;
596 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
597 params->gop_size = ctrl->value;
603 /* TODO: Update the hardware */
608 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
609 struct v4l2_ext_controls *ctrls)
611 struct saa7164_vbi_fh *fh = file->private_data;
612 struct saa7164_port *port = fh->port;
615 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
616 for (i = 0; i < ctrls->count; i++) {
617 struct v4l2_ext_control *ctrl = ctrls->controls + i;
619 err = saa7164_try_ctrl(ctrl, 0);
621 ctrls->error_idx = i;
624 err = saa7164_set_ctrl(port, ctrl);
626 ctrls->error_idx = i;
637 static int vidioc_querycap(struct file *file, void *priv,
638 struct v4l2_capability *cap)
640 struct saa7164_vbi_fh *fh = file->private_data;
641 struct saa7164_port *port = fh->port;
642 struct saa7164_dev *dev = port->dev;
644 strcpy(cap->driver, dev->name);
645 strlcpy(cap->card, saa7164_boards[dev->board].name,
647 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
650 V4L2_CAP_VBI_CAPTURE |
654 cap->capabilities |= V4L2_CAP_TUNER;
660 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
661 struct v4l2_fmtdesc *f)
666 strlcpy(f->description, "VBI", sizeof(f->description));
667 f->pixelformat = V4L2_PIX_FMT_MPEG;
672 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
673 struct v4l2_format *f)
675 struct saa7164_vbi_fh *fh = file->private_data;
676 struct saa7164_port *port = fh->port;
677 struct saa7164_dev *dev = port->dev;
679 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
680 f->fmt.pix.bytesperline = 0;
681 f->fmt.pix.sizeimage =
682 port->ts_packet_size * port->ts_packet_count;
683 f->fmt.pix.colorspace = 0;
684 f->fmt.pix.width = port->width;
685 f->fmt.pix.height = port->height;
687 dprintk(DBGLVL_VBI, "VIDIOC_G_FMT: w: %d, h: %d\n",
688 port->width, port->height);
693 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
694 struct v4l2_format *f)
696 struct saa7164_vbi_fh *fh = file->private_data;
697 struct saa7164_port *port = fh->port;
698 struct saa7164_dev *dev = port->dev;
700 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
701 f->fmt.pix.bytesperline = 0;
702 f->fmt.pix.sizeimage =
703 port->ts_packet_size * port->ts_packet_count;
704 f->fmt.pix.colorspace = 0;
705 dprintk(DBGLVL_VBI, "VIDIOC_TRY_FMT: w: %d, h: %d\n",
706 port->width, port->height);
710 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
711 struct v4l2_format *f)
713 struct saa7164_vbi_fh *fh = file->private_data;
714 struct saa7164_port *port = fh->port;
715 struct saa7164_dev *dev = port->dev;
717 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
718 f->fmt.pix.bytesperline = 0;
719 f->fmt.pix.sizeimage =
720 port->ts_packet_size * port->ts_packet_count;
721 f->fmt.pix.colorspace = 0;
723 dprintk(DBGLVL_VBI, "VIDIOC_S_FMT: w: %d, h: %d, f: %d\n",
724 f->fmt.pix.width, f->fmt.pix.height, f->fmt.pix.field);
729 static int vidioc_log_status(struct file *file, void *priv)
734 static int fill_queryctrl(struct saa7164_vbi_params *params,
735 struct v4l2_queryctrl *c)
738 case V4L2_CID_BRIGHTNESS:
739 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 127);
740 case V4L2_CID_CONTRAST:
741 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 66);
742 case V4L2_CID_SATURATION:
743 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 62);
745 return v4l2_ctrl_query_fill(c, 0x0, 0xff, 1, 128);
746 case V4L2_CID_SHARPNESS:
747 return v4l2_ctrl_query_fill(c, 0x0, 0x0f, 1, 8);
748 case V4L2_CID_MPEG_AUDIO_MUTE:
749 return v4l2_ctrl_query_fill(c, 0x0, 0x01, 1, 0);
750 case V4L2_CID_AUDIO_VOLUME:
751 return v4l2_ctrl_query_fill(c, -83, 24, 1, 20);
752 case V4L2_CID_MPEG_STREAM_TYPE:
753 return v4l2_ctrl_query_fill(c,
754 V4L2_MPEG_STREAM_TYPE_MPEG2_PS,
755 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
756 1, V4L2_MPEG_STREAM_TYPE_MPEG2_PS);
757 case V4L2_CID_MPEG_VIDEO_ASPECT:
758 return v4l2_ctrl_query_fill(c,
759 V4L2_MPEG_VIDEO_ASPECT_1x1,
760 V4L2_MPEG_VIDEO_ASPECT_221x100,
761 1, V4L2_MPEG_VIDEO_ASPECT_4x3);
762 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
763 return v4l2_ctrl_query_fill(c, 1, 255, 1, 15);
764 case V4L2_CID_MPEG_VIDEO_B_FRAMES:
765 return v4l2_ctrl_query_fill(c,
772 static int vidioc_queryctrl(struct file *file, void *priv,
773 struct v4l2_queryctrl *c)
775 struct saa7164_vbi_fh *fh = priv;
776 struct saa7164_port *port = fh->port;
780 memset(c, 0, sizeof(*c));
782 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
783 c->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
785 for (i = 0; i < ARRAY_SIZE(saa7164_v4l2_ctrls); i++) {
787 if (c->id < saa7164_v4l2_ctrls[i])
788 c->id = saa7164_v4l2_ctrls[i];
793 if (c->id == saa7164_v4l2_ctrls[i])
794 return fill_queryctrl(&port->vbi_params, c);
796 if (c->id < saa7164_v4l2_ctrls[i])
803 static int saa7164_vbi_stop_port(struct saa7164_port *port)
805 struct saa7164_dev *dev = port->dev;
808 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
809 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
810 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
814 dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
821 static int saa7164_vbi_acquire_port(struct saa7164_port *port)
823 struct saa7164_dev *dev = port->dev;
826 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
827 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
828 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
832 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
839 static int saa7164_vbi_pause_port(struct saa7164_port *port)
841 struct saa7164_dev *dev = port->dev;
844 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
845 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
846 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
850 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
857 /* Firmware is very windows centric, meaning you have to transition
858 * the part through AVStream / KS Windows stages, forwards or backwards.
859 * States are: stopped, acquired (h/w), paused, started.
860 * We have to leave here will all of the soft buffers on the free list,
861 * else the cfg_post() func won't have soft buffers to correctly configure.
863 static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
865 struct saa7164_dev *dev = port->dev;
866 struct saa7164_buffer *buf;
867 struct saa7164_user_buffer *ubuf;
868 struct list_head *c, *n;
871 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
873 ret = saa7164_vbi_pause_port(port);
874 ret = saa7164_vbi_acquire_port(port);
875 ret = saa7164_vbi_stop_port(port);
877 dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
880 /* Reset the state of any allocated buffer resources */
881 mutex_lock(&port->dmaqueue_lock);
883 /* Reset the hard and soft buffer state */
884 list_for_each_safe(c, n, &port->dmaqueue.list) {
885 buf = list_entry(c, struct saa7164_buffer, list);
886 buf->flags = SAA7164_BUFFER_FREE;
890 list_for_each_safe(c, n, &port->list_buf_used.list) {
891 ubuf = list_entry(c, struct saa7164_user_buffer, list);
893 list_move_tail(&ubuf->list, &port->list_buf_free.list);
896 mutex_unlock(&port->dmaqueue_lock);
898 /* Free any allocated resources */
899 saa7164_vbi_buffers_dealloc(port);
901 dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
906 static int saa7164_vbi_start_streaming(struct saa7164_port *port)
908 struct saa7164_dev *dev = port->dev;
911 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
913 port->done_first_interrupt = 0;
915 /* allocate all of the PCIe DMA buffer resources on the fly,
916 * allowing switching between TS and PS payloads without
917 * requiring a complete driver reload.
919 saa7164_vbi_buffers_alloc(port);
921 /* Configure the encoder with any cache values */
922 // saa7164_api_set_encoder(port);
923 // saa7164_api_get_encoder(port);
925 /* Place the empty buffers on the hardware */
926 saa7164_buffer_cfg_port(port);
928 /* Negotiate format */
929 if (saa7164_api_set_vbi_format(port) != SAA_OK) {
930 printk(KERN_ERR "%s() No supported VBI format\n", __func__);
935 /* Acquire the hardware */
936 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
937 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
938 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
944 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
946 /* Pause the hardware */
947 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
948 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
949 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
952 /* Stop the hardware, regardless */
953 result = saa7164_vbi_stop_port(port);
954 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
955 printk(KERN_ERR "%s() pause/forced stop transition "
956 "failed, res = 0x%x\n", __func__, result);
962 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
964 /* Start the hardware */
965 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
966 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
967 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
970 /* Stop the hardware, regardless */
971 result = saa7164_vbi_acquire_port(port);
972 result = saa7164_vbi_stop_port(port);
973 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
974 printk(KERN_ERR "%s() run/forced stop transition "
975 "failed, res = 0x%x\n", __func__, result);
980 dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
986 int saa7164_vbi_fmt(struct file *file, void *priv, struct v4l2_format *f)
989 f->fmt.vbi.samples_per_line = 1600;
990 f->fmt.vbi.samples_per_line = 1440;
991 f->fmt.vbi.sampling_rate = 27000000;
992 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
993 f->fmt.vbi.offset = 0;
994 f->fmt.vbi.flags = 0;
995 f->fmt.vbi.start[0] = 10;
996 f->fmt.vbi.count[0] = 18;
997 f->fmt.vbi.start[1] = 263 + 10 + 1;
998 f->fmt.vbi.count[1] = 18;
1002 static int fops_open(struct file *file)
1004 struct saa7164_dev *dev;
1005 struct saa7164_port *port;
1006 struct saa7164_vbi_fh *fh;
1008 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
1014 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1016 /* allocate + initialize per filehandle data */
1017 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
1021 file->private_data = fh;
1027 static int fops_release(struct file *file)
1029 struct saa7164_vbi_fh *fh = file->private_data;
1030 struct saa7164_port *port = fh->port;
1031 struct saa7164_dev *dev = port->dev;
1033 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1035 /* Shut device down on last close */
1036 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
1037 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
1038 /* stop vbi capture then cancel buffers */
1039 saa7164_vbi_stop_streaming(port);
1043 file->private_data = NULL;
1049 struct saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
1051 struct saa7164_user_buffer *ubuf = 0;
1052 struct saa7164_dev *dev = port->dev;
1055 mutex_lock(&port->dmaqueue_lock);
1056 if (!list_empty(&port->list_buf_used.list)) {
1057 ubuf = list_first_entry(&port->list_buf_used.list,
1058 struct saa7164_user_buffer, list);
1061 crc = crc32(0, ubuf->data, ubuf->actual_size);
1062 if (crc != ubuf->crc) {
1063 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n", __func__,
1064 ubuf, ubuf->crc, crc);
1069 mutex_unlock(&port->dmaqueue_lock);
1071 dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
1076 static ssize_t fops_read(struct file *file, char __user *buffer,
1077 size_t count, loff_t *pos)
1079 struct saa7164_vbi_fh *fh = file->private_data;
1080 struct saa7164_port *port = fh->port;
1081 struct saa7164_user_buffer *ubuf = NULL;
1082 struct saa7164_dev *dev = port->dev;
1087 port->last_read_msecs_diff = port->last_read_msecs;
1088 port->last_read_msecs = jiffies_to_msecs(jiffies);
1089 port->last_read_msecs_diff = port->last_read_msecs -
1090 port->last_read_msecs_diff;
1092 saa7164_histogram_update(&port->read_interval,
1093 port->last_read_msecs_diff);
1096 printk(KERN_ERR "%s() ESPIPE\n", __func__);
1100 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1101 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1103 if (saa7164_vbi_initialize(port) < 0) {
1104 printk(KERN_ERR "%s() EINVAL\n", __func__);
1108 saa7164_vbi_start_streaming(port);
1113 /* blocking wait for buffer */
1114 if ((file->f_flags & O_NONBLOCK) == 0) {
1115 if (wait_event_interruptible(port->wait_read,
1116 saa7164_vbi_next_buf(port))) {
1117 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
1118 return -ERESTARTSYS;
1122 /* Pull the first buffer from the used list */
1123 ubuf = saa7164_vbi_next_buf(port);
1125 while ((count > 0) && ubuf) {
1127 /* set remaining bytes to copy */
1128 rem = ubuf->actual_size - ubuf->pos;
1129 cnt = rem > count ? count : rem;
1131 p = ubuf->data + ubuf->pos;
1134 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
1135 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
1137 if (copy_to_user(buffer, p, cnt)) {
1138 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
1140 printk(KERN_ERR "%s() EFAULT\n", __func__);
1151 if (ubuf->pos > ubuf->actual_size) {
1152 printk(KERN_ERR "read() pos > actual, huh?\n");
1155 if (ubuf->pos == ubuf->actual_size) {
1157 /* finished with current buffer, take next buffer */
1159 /* Requeue the buffer on the free list */
1162 mutex_lock(&port->dmaqueue_lock);
1163 list_move_tail(&ubuf->list, &port->list_buf_free.list);
1164 mutex_unlock(&port->dmaqueue_lock);
1167 if ((file->f_flags & O_NONBLOCK) == 0) {
1168 if (wait_event_interruptible(port->wait_read,
1169 saa7164_vbi_next_buf(port))) {
1173 ubuf = saa7164_vbi_next_buf(port);
1177 if (!ret && !ubuf) {
1178 printk(KERN_ERR "%s() EAGAIN\n", __func__);
1185 static unsigned int fops_poll(struct file *file, poll_table *wait)
1187 struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
1188 struct saa7164_port *port = fh->port;
1189 struct saa7164_user_buffer *ubuf;
1190 unsigned int mask = 0;
1192 port->last_poll_msecs_diff = port->last_poll_msecs;
1193 port->last_poll_msecs = jiffies_to_msecs(jiffies);
1194 port->last_poll_msecs_diff = port->last_poll_msecs -
1195 port->last_poll_msecs_diff;
1197 saa7164_histogram_update(&port->poll_interval,
1198 port->last_poll_msecs_diff);
1200 if (!video_is_registered(port->v4l_device)) {
1204 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
1205 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
1206 if (saa7164_vbi_initialize(port) < 0)
1208 saa7164_vbi_start_streaming(port);
1213 /* blocking wait for buffer */
1214 if ((file->f_flags & O_NONBLOCK) == 0) {
1215 if (wait_event_interruptible(port->wait_read,
1216 saa7164_vbi_next_buf(port))) {
1217 return -ERESTARTSYS;
1221 /* Pull the first buffer from the used list */
1222 ubuf = list_first_entry(&port->list_buf_used.list,
1223 struct saa7164_user_buffer, list);
1226 mask |= POLLIN | POLLRDNORM;
1230 static const struct v4l2_file_operations vbi_fops = {
1231 .owner = THIS_MODULE,
1233 .release = fops_release,
1236 .unlocked_ioctl = video_ioctl2,
1239 static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
1240 .vidioc_s_std = vidioc_s_std,
1241 .vidioc_enum_input = vidioc_enum_input,
1242 .vidioc_g_input = vidioc_g_input,
1243 .vidioc_s_input = vidioc_s_input,
1244 .vidioc_g_tuner = vidioc_g_tuner,
1245 .vidioc_s_tuner = vidioc_s_tuner,
1246 .vidioc_g_frequency = vidioc_g_frequency,
1247 .vidioc_s_frequency = vidioc_s_frequency,
1248 .vidioc_s_ctrl = vidioc_s_ctrl,
1249 .vidioc_g_ctrl = vidioc_g_ctrl,
1250 .vidioc_querycap = vidioc_querycap,
1251 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1252 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1253 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
1254 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
1255 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1256 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1257 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1258 .vidioc_log_status = vidioc_log_status,
1259 .vidioc_queryctrl = vidioc_queryctrl,
1260 // .vidioc_g_chip_ident = saa7164_g_chip_ident,
1261 #ifdef CONFIG_VIDEO_ADV_DEBUG
1262 // .vidioc_g_register = saa7164_g_register,
1263 // .vidioc_s_register = saa7164_s_register,
1265 .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
1266 .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
1267 .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
1270 static struct video_device saa7164_vbi_template = {
1273 .ioctl_ops = &vbi_ioctl_ops,
1275 .tvnorms = SAA7164_NORMS,
1276 .current_norm = V4L2_STD_NTSC_M,
1279 static struct video_device *saa7164_vbi_alloc(
1280 struct saa7164_port *port,
1281 struct pci_dev *pci,
1282 struct video_device *template,
1285 struct video_device *vfd;
1286 struct saa7164_dev *dev = port->dev;
1288 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1290 vfd = video_device_alloc();
1295 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
1296 type, saa7164_boards[dev->board].name);
1298 vfd->parent = &pci->dev;
1299 vfd->release = video_device_release;
1303 int saa7164_vbi_register(struct saa7164_port *port)
1305 struct saa7164_dev *dev = port->dev;
1306 int result = -ENODEV;
1308 dprintk(DBGLVL_VBI, "%s()\n", __func__);
1310 if (port->type != SAA7164_MPEG_VBI)
1313 /* Sanity check that the PCI configuration space is active */
1314 if (port->hwcfg.BARLocation == 0) {
1315 printk(KERN_ERR "%s() failed "
1316 "(errno = %d), NO PCI configuration\n",
1322 /* Establish VBI defaults here */
1324 /* Allocate and register the video device node */
1325 port->v4l_device = saa7164_vbi_alloc(port,
1326 dev->pci, &saa7164_vbi_template, "vbi");
1328 if (port->v4l_device == NULL) {
1329 printk(KERN_INFO "%s: can't allocate vbi device\n",
1335 video_set_drvdata(port->v4l_device, port);
1336 result = video_register_device(port->v4l_device,
1339 printk(KERN_INFO "%s: can't register vbi device\n",
1341 /* TODO: We're going to leak here if we don't dealloc
1342 The buffers above. The unreg function can't deal wit it.
1347 printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
1348 dev->name, port->v4l_device->num);
1350 /* Configure the hardware defaults */
1357 void saa7164_vbi_unregister(struct saa7164_port *port)
1359 struct saa7164_dev *dev = port->dev;
1361 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
1363 if (port->type != SAA7164_MPEG_VBI)
1366 if (port->v4l_device) {
1367 if (port->v4l_device->minor != -1)
1368 video_unregister_device(port->v4l_device);
1370 video_device_release(port->v4l_device);
1372 port->v4l_device = NULL;