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