2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010-2015 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 /* Take the encoder configuration from the port struct and
25 * flush it to the hardware.
27 static void saa7164_vbi_configure(struct saa7164_port *port)
29 struct saa7164_dev *dev = port->dev;
30 dprintk(DBGLVL_VBI, "%s()\n", __func__);
32 port->vbi_params.width = port->enc_port->width;
33 port->vbi_params.height = port->enc_port->height;
34 port->vbi_params.is_50hz =
35 (port->enc_port->encodernorm.id & V4L2_STD_625_50) != 0;
37 /* Set up the DIF (enable it) for analog mode by default */
38 saa7164_api_initialize_dif(port);
39 dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
42 static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
44 struct list_head *c, *n, *p, *q, *l, *v;
45 struct saa7164_dev *dev = port->dev;
46 struct saa7164_buffer *buf;
47 struct saa7164_user_buffer *ubuf;
49 /* Remove any allocated buffers */
50 mutex_lock(&port->dmaqueue_lock);
52 dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
53 list_for_each_safe(c, n, &port->dmaqueue.list) {
54 buf = list_entry(c, struct saa7164_buffer, list);
56 saa7164_buffer_dealloc(buf);
59 dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
60 list_for_each_safe(p, q, &port->list_buf_used.list) {
61 ubuf = list_entry(p, struct saa7164_user_buffer, list);
63 saa7164_buffer_dealloc_user(ubuf);
66 dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
67 list_for_each_safe(l, v, &port->list_buf_free.list) {
68 ubuf = list_entry(l, struct saa7164_user_buffer, list);
70 saa7164_buffer_dealloc_user(ubuf);
73 mutex_unlock(&port->dmaqueue_lock);
74 dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
79 /* Dynamic buffer switch at vbi start time */
80 static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
82 struct saa7164_dev *dev = port->dev;
83 struct saa7164_buffer *buf;
84 struct saa7164_user_buffer *ubuf;
85 struct tmHWStreamParameters *params = &port->hw_streamingparams;
86 int result = -ENODEV, i;
89 dprintk(DBGLVL_VBI, "%s()\n", __func__);
91 /* TODO: NTSC SPECIFIC */
92 /* Init and establish defaults */
93 params->samplesperline = 1440;
94 params->numberoflines = 12;
95 params->numberoflines = 18;
98 params->numpagetables = 2 +
99 ((params->numberoflines * params->pitch) / PAGE_SIZE);
100 params->bitspersample = 8;
101 params->linethreshold = 0;
102 params->pagetablelistvirt = NULL;
103 params->pagetablelistphys = NULL;
104 params->numpagetableentries = port->hwcfg.buffercount;
106 /* Allocate the PCI resources, buffers (hard) */
107 for (i = 0; i < port->hwcfg.buffercount; i++) {
108 buf = saa7164_buffer_alloc(port,
109 params->numberoflines *
113 printk(KERN_ERR "%s() failed "
114 "(errno = %d), unable to allocate buffer\n",
120 mutex_lock(&port->dmaqueue_lock);
121 list_add_tail(&buf->list, &port->dmaqueue.list);
122 mutex_unlock(&port->dmaqueue_lock);
127 /* Allocate some kernel buffers for copying
130 len = params->numberoflines * params->pitch;
132 if (vbi_buffers < 16)
134 if (vbi_buffers > 512)
137 for (i = 0; i < vbi_buffers; i++) {
139 ubuf = saa7164_buffer_alloc_user(dev, len);
141 mutex_lock(&port->dmaqueue_lock);
142 list_add_tail(&ubuf->list, &port->list_buf_free.list);
143 mutex_unlock(&port->dmaqueue_lock);
155 static int saa7164_vbi_initialize(struct saa7164_port *port)
157 saa7164_vbi_configure(port);
161 /* -- V4L2 --------------------------------------------------------- */
162 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
164 struct saa7164_vbi_fh *fh = file->private_data;
166 return saa7164_s_std(fh->port->enc_port, id);
169 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
171 struct saa7164_encoder_fh *fh = file->private_data;
173 return saa7164_g_std(fh->port->enc_port, id);
176 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
178 struct saa7164_vbi_fh *fh = file->private_data;
180 return saa7164_g_input(fh->port->enc_port, i);
183 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
185 struct saa7164_vbi_fh *fh = file->private_data;
187 return saa7164_s_input(fh->port->enc_port, i);
190 static int vidioc_g_frequency(struct file *file, void *priv,
191 struct v4l2_frequency *f)
193 struct saa7164_vbi_fh *fh = file->private_data;
195 return saa7164_g_frequency(fh->port->enc_port, f);
198 static int vidioc_s_frequency(struct file *file, void *priv,
199 const struct v4l2_frequency *f)
201 struct saa7164_vbi_fh *fh = file->private_data;
202 int ret = saa7164_s_frequency(fh->port->enc_port, f);
205 saa7164_vbi_initialize(fh->port);
209 static int vidioc_querycap(struct file *file, void *priv,
210 struct v4l2_capability *cap)
212 struct saa7164_vbi_fh *fh = file->private_data;
213 struct saa7164_port *port = fh->port;
214 struct saa7164_dev *dev = port->dev;
216 strcpy(cap->driver, dev->name);
217 strlcpy(cap->card, saa7164_boards[dev->board].name,
219 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
222 V4L2_CAP_VBI_CAPTURE |
226 cap->capabilities = cap->device_caps |
227 V4L2_CAP_VIDEO_CAPTURE |
228 V4L2_CAP_DEVICE_CAPS;
233 static int saa7164_vbi_stop_port(struct saa7164_port *port)
235 struct saa7164_dev *dev = port->dev;
238 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
239 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
240 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
244 dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
251 static int saa7164_vbi_acquire_port(struct saa7164_port *port)
253 struct saa7164_dev *dev = port->dev;
256 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
257 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
258 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
262 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
269 static int saa7164_vbi_pause_port(struct saa7164_port *port)
271 struct saa7164_dev *dev = port->dev;
274 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
275 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
276 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
280 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
287 /* Firmware is very windows centric, meaning you have to transition
288 * the part through AVStream / KS Windows stages, forwards or backwards.
289 * States are: stopped, acquired (h/w), paused, started.
290 * We have to leave here will all of the soft buffers on the free list,
291 * else the cfg_post() func won't have soft buffers to correctly configure.
293 static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
295 struct saa7164_dev *dev = port->dev;
296 struct saa7164_buffer *buf;
297 struct saa7164_user_buffer *ubuf;
298 struct list_head *c, *n;
301 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
303 ret = saa7164_vbi_pause_port(port);
304 ret = saa7164_vbi_acquire_port(port);
305 ret = saa7164_vbi_stop_port(port);
307 dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
310 /* Reset the state of any allocated buffer resources */
311 mutex_lock(&port->dmaqueue_lock);
313 /* Reset the hard and soft buffer state */
314 list_for_each_safe(c, n, &port->dmaqueue.list) {
315 buf = list_entry(c, struct saa7164_buffer, list);
316 buf->flags = SAA7164_BUFFER_FREE;
320 list_for_each_safe(c, n, &port->list_buf_used.list) {
321 ubuf = list_entry(c, struct saa7164_user_buffer, list);
323 list_move_tail(&ubuf->list, &port->list_buf_free.list);
326 mutex_unlock(&port->dmaqueue_lock);
328 /* Free any allocated resources */
329 saa7164_vbi_buffers_dealloc(port);
331 dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
336 static int saa7164_vbi_start_streaming(struct saa7164_port *port)
338 struct saa7164_dev *dev = port->dev;
341 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
343 port->done_first_interrupt = 0;
345 /* allocate all of the PCIe DMA buffer resources on the fly,
346 * allowing switching between TS and PS payloads without
347 * requiring a complete driver reload.
349 saa7164_vbi_buffers_alloc(port);
351 /* Configure the encoder with any cache values */
353 saa7164_api_set_encoder(port);
354 saa7164_api_get_encoder(port);
357 /* Place the empty buffers on the hardware */
358 saa7164_buffer_cfg_port(port);
360 /* Negotiate format */
361 if (saa7164_api_set_vbi_format(port) != SAA_OK) {
362 printk(KERN_ERR "%s() No supported VBI format\n", __func__);
367 /* Acquire the hardware */
368 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
369 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
370 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
376 dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
378 /* Pause the hardware */
379 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
380 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
381 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
384 /* Stop the hardware, regardless */
385 result = saa7164_vbi_stop_port(port);
386 if (result != SAA_OK) {
387 printk(KERN_ERR "%s() pause/forced stop transition "
388 "failed, res = 0x%x\n", __func__, result);
394 dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
396 /* Start the hardware */
397 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
398 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
399 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
402 /* Stop the hardware, regardless */
403 result = saa7164_vbi_acquire_port(port);
404 result = saa7164_vbi_stop_port(port);
405 if (result != SAA_OK) {
406 printk(KERN_ERR "%s() run/forced stop transition "
407 "failed, res = 0x%x\n", __func__, result);
412 dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
418 static int saa7164_vbi_fmt(struct file *file, void *priv,
419 struct v4l2_format *f)
422 f->fmt.vbi.samples_per_line = 1440;
423 f->fmt.vbi.sampling_rate = 27000000;
424 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
425 f->fmt.vbi.offset = 0;
426 f->fmt.vbi.flags = 0;
427 f->fmt.vbi.start[0] = 10;
428 f->fmt.vbi.count[0] = 18;
429 f->fmt.vbi.start[1] = 263 + 10 + 1;
430 f->fmt.vbi.count[1] = 18;
431 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
435 static int fops_open(struct file *file)
437 struct saa7164_dev *dev;
438 struct saa7164_port *port;
439 struct saa7164_vbi_fh *fh;
441 port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
447 dprintk(DBGLVL_VBI, "%s()\n", __func__);
449 /* allocate + initialize per filehandle data */
450 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
455 v4l2_fh_init(&fh->fh, video_devdata(file));
456 v4l2_fh_add(&fh->fh);
457 file->private_data = fh;
462 static int fops_release(struct file *file)
464 struct saa7164_vbi_fh *fh = file->private_data;
465 struct saa7164_port *port = fh->port;
466 struct saa7164_dev *dev = port->dev;
468 dprintk(DBGLVL_VBI, "%s()\n", __func__);
470 /* Shut device down on last close */
471 if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
472 if (atomic_dec_return(&port->v4l_reader_count) == 0) {
473 /* stop vbi capture then cancel buffers */
474 saa7164_vbi_stop_streaming(port);
478 v4l2_fh_del(&fh->fh);
479 v4l2_fh_exit(&fh->fh);
486 saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
488 struct saa7164_user_buffer *ubuf = NULL;
489 struct saa7164_dev *dev = port->dev;
492 mutex_lock(&port->dmaqueue_lock);
493 if (!list_empty(&port->list_buf_used.list)) {
494 ubuf = list_first_entry(&port->list_buf_used.list,
495 struct saa7164_user_buffer, list);
498 crc = crc32(0, ubuf->data, ubuf->actual_size);
499 if (crc != ubuf->crc) {
500 printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
502 ubuf, ubuf->crc, crc);
507 mutex_unlock(&port->dmaqueue_lock);
509 dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
514 static ssize_t fops_read(struct file *file, char __user *buffer,
515 size_t count, loff_t *pos)
517 struct saa7164_vbi_fh *fh = file->private_data;
518 struct saa7164_port *port = fh->port;
519 struct saa7164_user_buffer *ubuf = NULL;
520 struct saa7164_dev *dev = port->dev;
525 port->last_read_msecs_diff = port->last_read_msecs;
526 port->last_read_msecs = jiffies_to_msecs(jiffies);
527 port->last_read_msecs_diff = port->last_read_msecs -
528 port->last_read_msecs_diff;
530 saa7164_histogram_update(&port->read_interval,
531 port->last_read_msecs_diff);
534 printk(KERN_ERR "%s() ESPIPE\n", __func__);
538 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
539 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
541 if (saa7164_vbi_initialize(port) < 0) {
542 printk(KERN_ERR "%s() EINVAL\n", __func__);
546 saa7164_vbi_start_streaming(port);
551 /* blocking wait for buffer */
552 if ((file->f_flags & O_NONBLOCK) == 0) {
553 if (wait_event_interruptible(port->wait_read,
554 saa7164_vbi_next_buf(port))) {
555 printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
560 /* Pull the first buffer from the used list */
561 ubuf = saa7164_vbi_next_buf(port);
563 while ((count > 0) && ubuf) {
565 /* set remaining bytes to copy */
566 rem = ubuf->actual_size - ubuf->pos;
567 cnt = rem > count ? count : rem;
569 p = ubuf->data + ubuf->pos;
572 "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
573 __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
575 if (copy_to_user(buffer, p, cnt)) {
576 printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
578 printk(KERN_ERR "%s() EFAULT\n", __func__);
589 if (ubuf->pos > ubuf->actual_size)
590 printk(KERN_ERR "read() pos > actual, huh?\n");
592 if (ubuf->pos == ubuf->actual_size) {
594 /* finished with current buffer, take next buffer */
596 /* Requeue the buffer on the free list */
599 mutex_lock(&port->dmaqueue_lock);
600 list_move_tail(&ubuf->list, &port->list_buf_free.list);
601 mutex_unlock(&port->dmaqueue_lock);
604 if ((file->f_flags & O_NONBLOCK) == 0) {
605 if (wait_event_interruptible(port->wait_read,
606 saa7164_vbi_next_buf(port))) {
610 ubuf = saa7164_vbi_next_buf(port);
615 printk(KERN_ERR "%s() EAGAIN\n", __func__);
622 static unsigned int fops_poll(struct file *file, poll_table *wait)
624 struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
625 struct saa7164_port *port = fh->port;
626 unsigned int mask = 0;
628 port->last_poll_msecs_diff = port->last_poll_msecs;
629 port->last_poll_msecs = jiffies_to_msecs(jiffies);
630 port->last_poll_msecs_diff = port->last_poll_msecs -
631 port->last_poll_msecs_diff;
633 saa7164_histogram_update(&port->poll_interval,
634 port->last_poll_msecs_diff);
636 if (!video_is_registered(port->v4l_device))
639 if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
640 if (atomic_inc_return(&port->v4l_reader_count) == 1) {
641 if (saa7164_vbi_initialize(port) < 0)
643 saa7164_vbi_start_streaming(port);
648 /* blocking wait for buffer */
649 if ((file->f_flags & O_NONBLOCK) == 0) {
650 if (wait_event_interruptible(port->wait_read,
651 saa7164_vbi_next_buf(port))) {
656 /* Pull the first buffer from the used list */
657 if (!list_empty(&port->list_buf_used.list))
658 mask |= POLLIN | POLLRDNORM;
662 static const struct v4l2_file_operations vbi_fops = {
663 .owner = THIS_MODULE,
665 .release = fops_release,
668 .unlocked_ioctl = video_ioctl2,
671 static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
672 .vidioc_s_std = vidioc_s_std,
673 .vidioc_g_std = vidioc_g_std,
674 .vidioc_enum_input = saa7164_enum_input,
675 .vidioc_g_input = vidioc_g_input,
676 .vidioc_s_input = vidioc_s_input,
677 .vidioc_g_tuner = saa7164_g_tuner,
678 .vidioc_s_tuner = saa7164_s_tuner,
679 .vidioc_g_frequency = vidioc_g_frequency,
680 .vidioc_s_frequency = vidioc_s_frequency,
681 .vidioc_querycap = vidioc_querycap,
682 .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
683 .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
684 .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
687 static struct video_device saa7164_vbi_template = {
690 .ioctl_ops = &vbi_ioctl_ops,
692 .tvnorms = SAA7164_NORMS,
695 static struct video_device *saa7164_vbi_alloc(
696 struct saa7164_port *port,
698 struct video_device *template,
701 struct video_device *vfd;
702 struct saa7164_dev *dev = port->dev;
704 dprintk(DBGLVL_VBI, "%s()\n", __func__);
706 vfd = video_device_alloc();
711 snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
712 type, saa7164_boards[dev->board].name);
714 vfd->v4l2_dev = &dev->v4l2_dev;
715 vfd->release = video_device_release;
719 int saa7164_vbi_register(struct saa7164_port *port)
721 struct saa7164_dev *dev = port->dev;
722 int result = -ENODEV;
724 dprintk(DBGLVL_VBI, "%s()\n", __func__);
726 if (port->type != SAA7164_MPEG_VBI)
729 /* Sanity check that the PCI configuration space is active */
730 if (port->hwcfg.BARLocation == 0) {
731 printk(KERN_ERR "%s() failed "
732 "(errno = %d), NO PCI configuration\n",
738 /* Establish VBI defaults here */
740 /* Allocate and register the video device node */
741 port->v4l_device = saa7164_vbi_alloc(port,
742 dev->pci, &saa7164_vbi_template, "vbi");
744 if (!port->v4l_device) {
745 printk(KERN_INFO "%s: can't allocate vbi device\n",
751 port->enc_port = &dev->ports[port->nr - 2];
752 video_set_drvdata(port->v4l_device, port);
753 result = video_register_device(port->v4l_device,
756 printk(KERN_INFO "%s: can't register vbi device\n",
758 /* TODO: We're going to leak here if we don't dealloc
759 The buffers above. The unreg function can't deal wit it.
764 printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
765 dev->name, port->v4l_device->num);
767 /* Configure the hardware defaults */
774 void saa7164_vbi_unregister(struct saa7164_port *port)
776 struct saa7164_dev *dev = port->dev;
778 dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
780 if (port->type != SAA7164_MPEG_VBI)
783 if (port->v4l_device) {
784 if (port->v4l_device->minor != -1)
785 video_unregister_device(port->v4l_device);
787 video_device_release(port->v4l_device);
789 port->v4l_device = NULL;