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