xref: /linux/drivers/media/pci/saa7164/saa7164-vbi.c (revision 32786fdc9506aeba98278c1844d4bfb766863832)
1 /*
2  *  Driver for the NXP SAA7164 PCIe bridge
3  *
4  *  Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
5  *
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.
10  *
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
14  *
15  *  GNU General Public License for more details.
16  *
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.
20  */
21 
22 #include "saa7164.h"
23 
24 /* Take the encoder configuration from the port struct and
25  * flush it to the hardware.
26  */
27 static void saa7164_vbi_configure(struct saa7164_port *port)
28 {
29 	struct saa7164_dev *dev = port->dev;
30 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
31 
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;
36 
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__);
40 }
41 
42 static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
43 {
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;
48 
49 	/* Remove any allocated buffers */
50 	mutex_lock(&port->dmaqueue_lock);
51 
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);
55 		list_del(c);
56 		saa7164_buffer_dealloc(buf);
57 	}
58 
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);
62 		list_del(p);
63 		saa7164_buffer_dealloc_user(ubuf);
64 	}
65 
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);
69 		list_del(l);
70 		saa7164_buffer_dealloc_user(ubuf);
71 	}
72 
73 	mutex_unlock(&port->dmaqueue_lock);
74 	dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
75 
76 	return 0;
77 }
78 
79 /* Dynamic buffer switch at vbi start time */
80 static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
81 {
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;
87 	int len = 0;
88 
89 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
90 
91 	/* TODO: NTSC SPECIFIC */
92 	/* Init and establish defaults */
93 	params->samplesperline = 1440;
94 	params->numberoflines = 12;
95 	params->numberoflines = 18;
96 	params->pitch = 1600;
97 	params->pitch = 1440;
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;
105 
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 *
110 			params->pitch);
111 
112 		if (!buf) {
113 			printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
114 				__func__, result);
115 			result = -ENOMEM;
116 			goto failed;
117 		} else {
118 
119 			mutex_lock(&port->dmaqueue_lock);
120 			list_add_tail(&buf->list, &port->dmaqueue.list);
121 			mutex_unlock(&port->dmaqueue_lock);
122 
123 		}
124 	}
125 
126 	/* Allocate some kernel buffers for copying
127 	 * to userpsace.
128 	 */
129 	len = params->numberoflines * params->pitch;
130 
131 	if (vbi_buffers < 16)
132 		vbi_buffers = 16;
133 	if (vbi_buffers > 512)
134 		vbi_buffers = 512;
135 
136 	for (i = 0; i < vbi_buffers; i++) {
137 
138 		ubuf = saa7164_buffer_alloc_user(dev, len);
139 		if (ubuf) {
140 			mutex_lock(&port->dmaqueue_lock);
141 			list_add_tail(&ubuf->list, &port->list_buf_free.list);
142 			mutex_unlock(&port->dmaqueue_lock);
143 		}
144 
145 	}
146 
147 	result = 0;
148 
149 failed:
150 	return result;
151 }
152 
153 
154 static int saa7164_vbi_initialize(struct saa7164_port *port)
155 {
156 	saa7164_vbi_configure(port);
157 	return 0;
158 }
159 
160 /* -- V4L2 --------------------------------------------------------- */
161 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
162 {
163 	struct saa7164_vbi_fh *fh = file->private_data;
164 
165 	return saa7164_s_std(fh->port->enc_port, id);
166 }
167 
168 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
169 {
170 	struct saa7164_encoder_fh *fh = file->private_data;
171 
172 	return saa7164_g_std(fh->port->enc_port, id);
173 }
174 
175 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
176 {
177 	struct saa7164_vbi_fh *fh = file->private_data;
178 
179 	return saa7164_g_input(fh->port->enc_port, i);
180 }
181 
182 static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
183 {
184 	struct saa7164_vbi_fh *fh = file->private_data;
185 
186 	return saa7164_s_input(fh->port->enc_port, i);
187 }
188 
189 static int vidioc_g_frequency(struct file *file, void *priv,
190 	struct v4l2_frequency *f)
191 {
192 	struct saa7164_vbi_fh *fh = file->private_data;
193 
194 	return saa7164_g_frequency(fh->port->enc_port, f);
195 }
196 
197 static int vidioc_s_frequency(struct file *file, void *priv,
198 	const struct v4l2_frequency *f)
199 {
200 	struct saa7164_vbi_fh *fh = file->private_data;
201 	int ret = saa7164_s_frequency(fh->port->enc_port, f);
202 
203 	if (ret == 0)
204 		saa7164_vbi_initialize(fh->port);
205 	return ret;
206 }
207 
208 static int vidioc_querycap(struct file *file, void  *priv,
209 	struct v4l2_capability *cap)
210 {
211 	struct saa7164_vbi_fh *fh = file->private_data;
212 	struct saa7164_port *port = fh->port;
213 	struct saa7164_dev *dev = port->dev;
214 
215 	strcpy(cap->driver, dev->name);
216 	strlcpy(cap->card, saa7164_boards[dev->board].name,
217 		sizeof(cap->card));
218 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
219 
220 	cap->device_caps =
221 		V4L2_CAP_VBI_CAPTURE |
222 		V4L2_CAP_READWRITE |
223 		V4L2_CAP_TUNER;
224 
225 	cap->capabilities = cap->device_caps |
226 		V4L2_CAP_VIDEO_CAPTURE |
227 		V4L2_CAP_DEVICE_CAPS;
228 
229 	return 0;
230 }
231 
232 static int saa7164_vbi_stop_port(struct saa7164_port *port)
233 {
234 	struct saa7164_dev *dev = port->dev;
235 	int ret;
236 
237 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
238 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
239 		printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
240 			__func__, ret);
241 		ret = -EIO;
242 	} else {
243 		dprintk(DBGLVL_VBI, "%s()    Stopped\n", __func__);
244 		ret = 0;
245 	}
246 
247 	return ret;
248 }
249 
250 static int saa7164_vbi_acquire_port(struct saa7164_port *port)
251 {
252 	struct saa7164_dev *dev = port->dev;
253 	int ret;
254 
255 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
256 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
257 		printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
258 			__func__, ret);
259 		ret = -EIO;
260 	} else {
261 		dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
262 		ret = 0;
263 	}
264 
265 	return ret;
266 }
267 
268 static int saa7164_vbi_pause_port(struct saa7164_port *port)
269 {
270 	struct saa7164_dev *dev = port->dev;
271 	int ret;
272 
273 	ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
274 	if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
275 		printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
276 			__func__, ret);
277 		ret = -EIO;
278 	} else {
279 		dprintk(DBGLVL_VBI, "%s()   Paused\n", __func__);
280 		ret = 0;
281 	}
282 
283 	return ret;
284 }
285 
286 /* Firmware is very windows centric, meaning you have to transition
287  * the part through AVStream / KS Windows stages, forwards or backwards.
288  * States are: stopped, acquired (h/w), paused, started.
289  * We have to leave here will all of the soft buffers on the free list,
290  * else the cfg_post() func won't have soft buffers to correctly configure.
291  */
292 static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
293 {
294 	struct saa7164_dev *dev = port->dev;
295 	struct saa7164_buffer *buf;
296 	struct saa7164_user_buffer *ubuf;
297 	struct list_head *c, *n;
298 	int ret;
299 
300 	dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
301 
302 	ret = saa7164_vbi_pause_port(port);
303 	ret = saa7164_vbi_acquire_port(port);
304 	ret = saa7164_vbi_stop_port(port);
305 
306 	dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
307 		port->nr);
308 
309 	/* Reset the state of any allocated buffer resources */
310 	mutex_lock(&port->dmaqueue_lock);
311 
312 	/* Reset the hard and soft buffer state */
313 	list_for_each_safe(c, n, &port->dmaqueue.list) {
314 		buf = list_entry(c, struct saa7164_buffer, list);
315 		buf->flags = SAA7164_BUFFER_FREE;
316 		buf->pos = 0;
317 	}
318 
319 	list_for_each_safe(c, n, &port->list_buf_used.list) {
320 		ubuf = list_entry(c, struct saa7164_user_buffer, list);
321 		ubuf->pos = 0;
322 		list_move_tail(&ubuf->list, &port->list_buf_free.list);
323 	}
324 
325 	mutex_unlock(&port->dmaqueue_lock);
326 
327 	/* Free any allocated resources */
328 	saa7164_vbi_buffers_dealloc(port);
329 
330 	dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
331 
332 	return ret;
333 }
334 
335 static int saa7164_vbi_start_streaming(struct saa7164_port *port)
336 {
337 	struct saa7164_dev *dev = port->dev;
338 	int result, ret = 0;
339 
340 	dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
341 
342 	port->done_first_interrupt = 0;
343 
344 	/* allocate all of the PCIe DMA buffer resources on the fly,
345 	 * allowing switching between TS and PS payloads without
346 	 * requiring a complete driver reload.
347 	 */
348 	saa7164_vbi_buffers_alloc(port);
349 
350 	/* Configure the encoder with any cache values */
351 #if 0
352 	saa7164_api_set_encoder(port);
353 	saa7164_api_get_encoder(port);
354 #endif
355 
356 	/* Place the empty buffers on the hardware */
357 	saa7164_buffer_cfg_port(port);
358 
359 	/* Negotiate format */
360 	if (saa7164_api_set_vbi_format(port) != SAA_OK) {
361 		printk(KERN_ERR "%s() No supported VBI format\n", __func__);
362 		ret = -EIO;
363 		goto out;
364 	}
365 
366 	/* Acquire the hardware */
367 	result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
368 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
369 		printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
370 			__func__, result);
371 
372 		ret = -EIO;
373 		goto out;
374 	} else
375 		dprintk(DBGLVL_VBI, "%s()   Acquired\n", __func__);
376 
377 	/* Pause the hardware */
378 	result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
379 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
380 		printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
381 				__func__, result);
382 
383 		/* Stop the hardware, regardless */
384 		result = saa7164_vbi_stop_port(port);
385 		if (result != SAA_OK) {
386 			printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
387 			       __func__, result);
388 		}
389 
390 		ret = -EIO;
391 		goto out;
392 	} else
393 		dprintk(DBGLVL_VBI, "%s()   Paused\n", __func__);
394 
395 	/* Start the hardware */
396 	result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
397 	if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
398 		printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
399 				__func__, result);
400 
401 		/* Stop the hardware, regardless */
402 		result = saa7164_vbi_acquire_port(port);
403 		result = saa7164_vbi_stop_port(port);
404 		if (result != SAA_OK) {
405 			printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
406 			       __func__, result);
407 		}
408 
409 		ret = -EIO;
410 	} else
411 		dprintk(DBGLVL_VBI, "%s()   Running\n", __func__);
412 
413 out:
414 	return ret;
415 }
416 
417 static int saa7164_vbi_fmt(struct file *file, void *priv,
418 			   struct v4l2_format *f)
419 {
420 	/* ntsc */
421 	f->fmt.vbi.samples_per_line = 1440;
422 	f->fmt.vbi.sampling_rate = 27000000;
423 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
424 	f->fmt.vbi.offset = 0;
425 	f->fmt.vbi.flags = 0;
426 	f->fmt.vbi.start[0] = 10;
427 	f->fmt.vbi.count[0] = 18;
428 	f->fmt.vbi.start[1] = 263 + 10 + 1;
429 	f->fmt.vbi.count[1] = 18;
430 	memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
431 	return 0;
432 }
433 
434 static int fops_open(struct file *file)
435 {
436 	struct saa7164_dev *dev;
437 	struct saa7164_port *port;
438 	struct saa7164_vbi_fh *fh;
439 
440 	port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
441 	if (!port)
442 		return -ENODEV;
443 
444 	dev = port->dev;
445 
446 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
447 
448 	/* allocate + initialize per filehandle data */
449 	fh = kzalloc(sizeof(*fh), GFP_KERNEL);
450 	if (NULL == fh)
451 		return -ENOMEM;
452 
453 	fh->port = port;
454 	v4l2_fh_init(&fh->fh, video_devdata(file));
455 	v4l2_fh_add(&fh->fh);
456 	file->private_data = fh;
457 
458 	return 0;
459 }
460 
461 static int fops_release(struct file *file)
462 {
463 	struct saa7164_vbi_fh *fh = file->private_data;
464 	struct saa7164_port *port = fh->port;
465 	struct saa7164_dev *dev = port->dev;
466 
467 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
468 
469 	/* Shut device down on last close */
470 	if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
471 		if (atomic_dec_return(&port->v4l_reader_count) == 0) {
472 			/* stop vbi capture then cancel buffers */
473 			saa7164_vbi_stop_streaming(port);
474 		}
475 	}
476 
477 	v4l2_fh_del(&fh->fh);
478 	v4l2_fh_exit(&fh->fh);
479 	kfree(fh);
480 
481 	return 0;
482 }
483 
484 static struct
485 saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
486 {
487 	struct saa7164_user_buffer *ubuf = NULL;
488 	struct saa7164_dev *dev = port->dev;
489 	u32 crc;
490 
491 	mutex_lock(&port->dmaqueue_lock);
492 	if (!list_empty(&port->list_buf_used.list)) {
493 		ubuf = list_first_entry(&port->list_buf_used.list,
494 			struct saa7164_user_buffer, list);
495 
496 		if (crc_checking) {
497 			crc = crc32(0, ubuf->data, ubuf->actual_size);
498 			if (crc != ubuf->crc) {
499 				printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
500 					__func__,
501 					ubuf, ubuf->crc, crc);
502 			}
503 		}
504 
505 	}
506 	mutex_unlock(&port->dmaqueue_lock);
507 
508 	dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
509 
510 	return ubuf;
511 }
512 
513 static ssize_t fops_read(struct file *file, char __user *buffer,
514 	size_t count, loff_t *pos)
515 {
516 	struct saa7164_vbi_fh *fh = file->private_data;
517 	struct saa7164_port *port = fh->port;
518 	struct saa7164_user_buffer *ubuf = NULL;
519 	struct saa7164_dev *dev = port->dev;
520 	int ret = 0;
521 	int rem, cnt;
522 	u8 *p;
523 
524 	port->last_read_msecs_diff = port->last_read_msecs;
525 	port->last_read_msecs = jiffies_to_msecs(jiffies);
526 	port->last_read_msecs_diff = port->last_read_msecs -
527 		port->last_read_msecs_diff;
528 
529 	saa7164_histogram_update(&port->read_interval,
530 		port->last_read_msecs_diff);
531 
532 	if (*pos) {
533 		printk(KERN_ERR "%s() ESPIPE\n", __func__);
534 		return -ESPIPE;
535 	}
536 
537 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
538 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
539 
540 			if (saa7164_vbi_initialize(port) < 0) {
541 				printk(KERN_ERR "%s() EINVAL\n", __func__);
542 				return -EINVAL;
543 			}
544 
545 			saa7164_vbi_start_streaming(port);
546 			msleep(200);
547 		}
548 	}
549 
550 	/* blocking wait for buffer */
551 	if ((file->f_flags & O_NONBLOCK) == 0) {
552 		if (wait_event_interruptible(port->wait_read,
553 			saa7164_vbi_next_buf(port))) {
554 				printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
555 				return -ERESTARTSYS;
556 		}
557 	}
558 
559 	/* Pull the first buffer from the used list */
560 	ubuf = saa7164_vbi_next_buf(port);
561 
562 	while ((count > 0) && ubuf) {
563 
564 		/* set remaining bytes to copy */
565 		rem = ubuf->actual_size - ubuf->pos;
566 		cnt = rem > count ? count : rem;
567 
568 		p = ubuf->data + ubuf->pos;
569 
570 		dprintk(DBGLVL_VBI,
571 			"%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
572 			__func__, (int)count, cnt, rem, ubuf, ubuf->pos);
573 
574 		if (copy_to_user(buffer, p, cnt)) {
575 			printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
576 			if (!ret) {
577 				printk(KERN_ERR "%s() EFAULT\n", __func__);
578 				ret = -EFAULT;
579 			}
580 			goto err;
581 		}
582 
583 		ubuf->pos += cnt;
584 		count -= cnt;
585 		buffer += cnt;
586 		ret += cnt;
587 
588 		if (ubuf->pos > ubuf->actual_size)
589 			printk(KERN_ERR "read() pos > actual, huh?\n");
590 
591 		if (ubuf->pos == ubuf->actual_size) {
592 
593 			/* finished with current buffer, take next buffer */
594 
595 			/* Requeue the buffer on the free list */
596 			ubuf->pos = 0;
597 
598 			mutex_lock(&port->dmaqueue_lock);
599 			list_move_tail(&ubuf->list, &port->list_buf_free.list);
600 			mutex_unlock(&port->dmaqueue_lock);
601 
602 			/* Dequeue next */
603 			if ((file->f_flags & O_NONBLOCK) == 0) {
604 				if (wait_event_interruptible(port->wait_read,
605 					saa7164_vbi_next_buf(port))) {
606 						break;
607 				}
608 			}
609 			ubuf = saa7164_vbi_next_buf(port);
610 		}
611 	}
612 err:
613 	if (!ret && !ubuf) {
614 		printk(KERN_ERR "%s() EAGAIN\n", __func__);
615 		ret = -EAGAIN;
616 	}
617 
618 	return ret;
619 }
620 
621 static unsigned int fops_poll(struct file *file, poll_table *wait)
622 {
623 	struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
624 	struct saa7164_port *port = fh->port;
625 	unsigned int mask = 0;
626 
627 	port->last_poll_msecs_diff = port->last_poll_msecs;
628 	port->last_poll_msecs = jiffies_to_msecs(jiffies);
629 	port->last_poll_msecs_diff = port->last_poll_msecs -
630 		port->last_poll_msecs_diff;
631 
632 	saa7164_histogram_update(&port->poll_interval,
633 		port->last_poll_msecs_diff);
634 
635 	if (!video_is_registered(port->v4l_device))
636 		return -EIO;
637 
638 	if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
639 		if (atomic_inc_return(&port->v4l_reader_count) == 1) {
640 			if (saa7164_vbi_initialize(port) < 0)
641 				return -EINVAL;
642 			saa7164_vbi_start_streaming(port);
643 			msleep(200);
644 		}
645 	}
646 
647 	/* blocking wait for buffer */
648 	if ((file->f_flags & O_NONBLOCK) == 0) {
649 		if (wait_event_interruptible(port->wait_read,
650 			saa7164_vbi_next_buf(port))) {
651 				return -ERESTARTSYS;
652 		}
653 	}
654 
655 	/* Pull the first buffer from the used list */
656 	if (!list_empty(&port->list_buf_used.list))
657 		mask |= POLLIN | POLLRDNORM;
658 
659 	return mask;
660 }
661 static const struct v4l2_file_operations vbi_fops = {
662 	.owner		= THIS_MODULE,
663 	.open		= fops_open,
664 	.release	= fops_release,
665 	.read		= fops_read,
666 	.poll		= fops_poll,
667 	.unlocked_ioctl	= video_ioctl2,
668 };
669 
670 static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
671 	.vidioc_s_std		 = vidioc_s_std,
672 	.vidioc_g_std		 = vidioc_g_std,
673 	.vidioc_enum_input	 = saa7164_enum_input,
674 	.vidioc_g_input		 = vidioc_g_input,
675 	.vidioc_s_input		 = vidioc_s_input,
676 	.vidioc_g_tuner		 = saa7164_g_tuner,
677 	.vidioc_s_tuner		 = saa7164_s_tuner,
678 	.vidioc_g_frequency	 = vidioc_g_frequency,
679 	.vidioc_s_frequency	 = vidioc_s_frequency,
680 	.vidioc_querycap	 = vidioc_querycap,
681 	.vidioc_g_fmt_vbi_cap	 = saa7164_vbi_fmt,
682 	.vidioc_try_fmt_vbi_cap	 = saa7164_vbi_fmt,
683 	.vidioc_s_fmt_vbi_cap	 = saa7164_vbi_fmt,
684 };
685 
686 static struct video_device saa7164_vbi_template = {
687 	.name          = "saa7164",
688 	.fops          = &vbi_fops,
689 	.ioctl_ops     = &vbi_ioctl_ops,
690 	.minor         = -1,
691 	.tvnorms       = SAA7164_NORMS,
692 };
693 
694 static struct video_device *saa7164_vbi_alloc(
695 	struct saa7164_port *port,
696 	struct pci_dev *pci,
697 	struct video_device *template,
698 	char *type)
699 {
700 	struct video_device *vfd;
701 	struct saa7164_dev *dev = port->dev;
702 
703 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
704 
705 	vfd = video_device_alloc();
706 	if (NULL == vfd)
707 		return NULL;
708 
709 	*vfd = *template;
710 	snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
711 		type, saa7164_boards[dev->board].name);
712 
713 	vfd->v4l2_dev  = &dev->v4l2_dev;
714 	vfd->release = video_device_release;
715 	return vfd;
716 }
717 
718 int saa7164_vbi_register(struct saa7164_port *port)
719 {
720 	struct saa7164_dev *dev = port->dev;
721 	int result = -ENODEV;
722 
723 	dprintk(DBGLVL_VBI, "%s()\n", __func__);
724 
725 	if (port->type != SAA7164_MPEG_VBI)
726 		BUG();
727 
728 	/* Sanity check that the PCI configuration space is active */
729 	if (port->hwcfg.BARLocation == 0) {
730 		printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
731 			__func__, result);
732 		result = -ENOMEM;
733 		goto failed;
734 	}
735 
736 	/* Establish VBI defaults here */
737 
738 	/* Allocate and register the video device node */
739 	port->v4l_device = saa7164_vbi_alloc(port,
740 		dev->pci, &saa7164_vbi_template, "vbi");
741 
742 	if (!port->v4l_device) {
743 		printk(KERN_INFO "%s: can't allocate vbi device\n",
744 			dev->name);
745 		result = -ENOMEM;
746 		goto failed;
747 	}
748 
749 	port->enc_port = &dev->ports[port->nr - 2];
750 	video_set_drvdata(port->v4l_device, port);
751 	result = video_register_device(port->v4l_device,
752 		VFL_TYPE_VBI, -1);
753 	if (result < 0) {
754 		printk(KERN_INFO "%s: can't register vbi device\n",
755 			dev->name);
756 		/* TODO: We're going to leak here if we don't dealloc
757 		 The buffers above. The unreg function can't deal wit it.
758 		*/
759 		goto failed;
760 	}
761 
762 	printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
763 		dev->name, port->v4l_device->num);
764 
765 	/* Configure the hardware defaults */
766 
767 	result = 0;
768 failed:
769 	return result;
770 }
771 
772 void saa7164_vbi_unregister(struct saa7164_port *port)
773 {
774 	struct saa7164_dev *dev = port->dev;
775 
776 	dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
777 
778 	if (port->type != SAA7164_MPEG_VBI)
779 		BUG();
780 
781 	if (port->v4l_device) {
782 		if (port->v4l_device->minor != -1)
783 			video_unregister_device(port->v4l_device);
784 		else
785 			video_device_release(port->v4l_device);
786 
787 		port->v4l_device = NULL;
788 	}
789 
790 }
791