1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3    cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
4 
5    Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
6 	Based on cx88 driver
7 
8  */
9 
10 #include "cx231xx.h"
11 #include <linux/init.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/bitmap.h>
16 #include <linux/i2c.h>
17 #include <linux/mm.h>
18 #include <linux/mutex.h>
19 #include <linux/slab.h>
20 
21 #include <media/v4l2-common.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/drv-intf/msp3400.h>
24 #include <media/tuner.h>
25 
26 #include "cx231xx-vbi.h"
27 
28 static inline void print_err_status(struct cx231xx *dev, int packet, int status)
29 {
30 	char *errmsg = "Unknown";
31 
32 	switch (status) {
33 	case -ENOENT:
34 		errmsg = "unlinked synchronously";
35 		break;
36 	case -ECONNRESET:
37 		errmsg = "unlinked asynchronously";
38 		break;
39 	case -ENOSR:
40 		errmsg = "Buffer error (overrun)";
41 		break;
42 	case -EPIPE:
43 		errmsg = "Stalled (device not responding)";
44 		break;
45 	case -EOVERFLOW:
46 		errmsg = "Babble (bad cable?)";
47 		break;
48 	case -EPROTO:
49 		errmsg = "Bit-stuff error (bad cable?)";
50 		break;
51 	case -EILSEQ:
52 		errmsg = "CRC/Timeout (could be anything)";
53 		break;
54 	case -ETIME:
55 		errmsg = "Device does not respond";
56 		break;
57 	}
58 	if (packet < 0) {
59 		dev_err(dev->dev,
60 			"URB status %d [%s].\n", status, errmsg);
61 	} else {
62 		dev_err(dev->dev,
63 			"URB packet %d, status %d [%s].\n",
64 			packet, status, errmsg);
65 	}
66 }
67 
68 /*
69  * Controls the isoc copy of each urb packet
70  */
71 static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
72 {
73 	struct cx231xx_dmaqueue *dma_q = urb->context;
74 	int rc = 1;
75 	unsigned char *p_buffer;
76 	u32 bytes_parsed = 0, buffer_size = 0;
77 	u8 sav_eav = 0;
78 
79 	if (!dev)
80 		return 0;
81 
82 	if (dev->state & DEV_DISCONNECTED)
83 		return 0;
84 
85 	if (urb->status < 0) {
86 		print_err_status(dev, -1, urb->status);
87 		if (urb->status == -ENOENT)
88 			return 0;
89 	}
90 
91 	/* get buffer pointer and length */
92 	p_buffer = urb->transfer_buffer;
93 	buffer_size = urb->actual_length;
94 
95 	if (buffer_size > 0) {
96 		bytes_parsed = 0;
97 
98 		if (dma_q->is_partial_line) {
99 			/* Handle the case where we were working on a partial
100 			   line */
101 			sav_eav = dma_q->last_sav;
102 		} else {
103 			/* Check for a SAV/EAV overlapping the
104 			   buffer boundary */
105 
106 			sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
107 							  dma_q->partial_buf,
108 							  &bytes_parsed);
109 		}
110 
111 		sav_eav &= 0xF0;
112 		/* Get the first line if we have some portion of an SAV/EAV from
113 		   the last buffer or a partial line */
114 		if (sav_eav) {
115 			bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
116 				sav_eav,		       /* SAV/EAV */
117 				p_buffer + bytes_parsed,       /* p_buffer */
118 				buffer_size - bytes_parsed);   /* buffer size */
119 		}
120 
121 		/* Now parse data that is completely in this buffer */
122 		dma_q->is_partial_line = 0;
123 
124 		while (bytes_parsed < buffer_size) {
125 			u32 bytes_used = 0;
126 
127 			sav_eav = cx231xx_find_next_SAV_EAV(
128 				p_buffer + bytes_parsed,	/* p_buffer */
129 				buffer_size - bytes_parsed, /* buffer size */
130 				&bytes_used);	/* bytes used to get SAV/EAV */
131 
132 			bytes_parsed += bytes_used;
133 
134 			sav_eav &= 0xF0;
135 			if (sav_eav && (bytes_parsed < buffer_size)) {
136 				bytes_parsed += cx231xx_get_vbi_line(dev,
137 					dma_q, sav_eav,	/* SAV/EAV */
138 					p_buffer+bytes_parsed, /* p_buffer */
139 					buffer_size-bytes_parsed);/*buf size*/
140 			}
141 		}
142 
143 		/* Save the last four bytes of the buffer so we can
144 		check the buffer boundary condition next time */
145 		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
146 		bytes_parsed = 0;
147 	}
148 
149 	return rc;
150 }
151 
152 /* ------------------------------------------------------------------
153 	Vbi buf operations
154    ------------------------------------------------------------------*/
155 
156 static int
157 vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
158 		 unsigned int *size)
159 {
160 	struct cx231xx_fh *fh = vq->priv_data;
161 	struct cx231xx *dev = fh->dev;
162 	u32 height = 0;
163 
164 	height = ((dev->norm & V4L2_STD_625_50) ?
165 		  PAL_VBI_LINES : NTSC_VBI_LINES);
166 
167 	*size = (dev->width * height * 2 * 2);
168 	if (0 == *count)
169 		*count = CX231XX_DEF_VBI_BUF;
170 
171 	if (*count < CX231XX_MIN_BUF)
172 		*count = CX231XX_MIN_BUF;
173 
174 	return 0;
175 }
176 
177 /* This is called *without* dev->slock held; please keep it that way */
178 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
179 {
180 	struct cx231xx_fh *fh = vq->priv_data;
181 	struct cx231xx *dev = fh->dev;
182 	unsigned long flags = 0;
183 	BUG_ON(in_interrupt());
184 
185 	/* We used to wait for the buffer to finish here, but this didn't work
186 	   because, as we were keeping the state as VIDEOBUF_QUEUED,
187 	   videobuf_queue_cancel marked it as finished for us.
188 	   (Also, it could wedge forever if the hardware was misconfigured.)
189 
190 	   This should be safe; by the time we get here, the buffer isn't
191 	   queued anymore. If we ever start marking the buffers as
192 	   VIDEOBUF_ACTIVE, it won't be, though.
193 	 */
194 	spin_lock_irqsave(&dev->vbi_mode.slock, flags);
195 	if (dev->vbi_mode.bulk_ctl.buf == buf)
196 		dev->vbi_mode.bulk_ctl.buf = NULL;
197 	spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
198 
199 	videobuf_vmalloc_free(&buf->vb);
200 	buf->vb.state = VIDEOBUF_NEEDS_INIT;
201 }
202 
203 static int
204 vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
205 		   enum v4l2_field field)
206 {
207 	struct cx231xx_fh *fh = vq->priv_data;
208 	struct cx231xx_buffer *buf =
209 	    container_of(vb, struct cx231xx_buffer, vb);
210 	struct cx231xx *dev = fh->dev;
211 	int rc = 0, urb_init = 0;
212 	u32 height = 0;
213 
214 	height = ((dev->norm & V4L2_STD_625_50) ?
215 		  PAL_VBI_LINES : NTSC_VBI_LINES);
216 	buf->vb.size = ((dev->width << 1) * height * 2);
217 
218 	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
219 		return -EINVAL;
220 
221 	buf->vb.width = dev->width;
222 	buf->vb.height = height;
223 	buf->vb.field = field;
224 	buf->vb.field = V4L2_FIELD_SEQ_TB;
225 
226 	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
227 		rc = videobuf_iolock(vq, &buf->vb, NULL);
228 		if (rc < 0)
229 			goto fail;
230 	}
231 
232 	if (!dev->vbi_mode.bulk_ctl.num_bufs)
233 		urb_init = 1;
234 
235 	if (urb_init) {
236 		rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
237 					   CX231XX_NUM_VBI_BUFS,
238 					   dev->vbi_mode.alt_max_pkt_size[0],
239 					   cx231xx_isoc_vbi_copy);
240 		if (rc < 0)
241 			goto fail;
242 	}
243 
244 	buf->vb.state = VIDEOBUF_PREPARED;
245 	return 0;
246 
247 fail:
248 	free_buffer(vq, buf);
249 	return rc;
250 }
251 
252 static void
253 vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
254 {
255 	struct cx231xx_buffer *buf =
256 	    container_of(vb, struct cx231xx_buffer, vb);
257 	struct cx231xx_fh *fh = vq->priv_data;
258 	struct cx231xx *dev = fh->dev;
259 	struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
260 
261 	buf->vb.state = VIDEOBUF_QUEUED;
262 	list_add_tail(&buf->vb.queue, &vidq->active);
263 
264 }
265 
266 static void vbi_buffer_release(struct videobuf_queue *vq,
267 			       struct videobuf_buffer *vb)
268 {
269 	struct cx231xx_buffer *buf =
270 	    container_of(vb, struct cx231xx_buffer, vb);
271 
272 
273 	free_buffer(vq, buf);
274 }
275 
276 const struct videobuf_queue_ops cx231xx_vbi_qops = {
277 	.buf_setup   = vbi_buffer_setup,
278 	.buf_prepare = vbi_buffer_prepare,
279 	.buf_queue   = vbi_buffer_queue,
280 	.buf_release = vbi_buffer_release,
281 };
282 
283 /* ------------------------------------------------------------------
284 	URB control
285    ------------------------------------------------------------------*/
286 
287 /*
288  * IRQ callback, called by URB callback
289  */
290 static void cx231xx_irq_vbi_callback(struct urb *urb)
291 {
292 	struct cx231xx_dmaqueue *dma_q = urb->context;
293 	struct cx231xx_video_mode *vmode =
294 	    container_of(dma_q, struct cx231xx_video_mode, vidq);
295 	struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
296 	unsigned long flags;
297 
298 	switch (urb->status) {
299 	case 0:		/* success */
300 	case -ETIMEDOUT:	/* NAK */
301 		break;
302 	case -ECONNRESET:	/* kill */
303 	case -ENOENT:
304 	case -ESHUTDOWN:
305 		return;
306 	default:		/* error */
307 		dev_err(dev->dev,
308 			"urb completion error %d.\n", urb->status);
309 		break;
310 	}
311 
312 	/* Copy data from URB */
313 	spin_lock_irqsave(&dev->vbi_mode.slock, flags);
314 	dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
315 	spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
316 
317 	/* Reset status */
318 	urb->status = 0;
319 
320 	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
321 	if (urb->status) {
322 		dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
323 			urb->status);
324 	}
325 }
326 
327 /*
328  * Stop and Deallocate URBs
329  */
330 void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
331 {
332 	struct urb *urb;
333 	int i;
334 
335 	dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
336 
337 	dev->vbi_mode.bulk_ctl.nfields = -1;
338 	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
339 		urb = dev->vbi_mode.bulk_ctl.urb[i];
340 		if (urb) {
341 			if (!irqs_disabled())
342 				usb_kill_urb(urb);
343 			else
344 				usb_unlink_urb(urb);
345 
346 			if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
347 
348 				kfree(dev->vbi_mode.bulk_ctl.
349 				      transfer_buffer[i]);
350 				dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
351 				    NULL;
352 			}
353 			usb_free_urb(urb);
354 			dev->vbi_mode.bulk_ctl.urb[i] = NULL;
355 		}
356 		dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
357 	}
358 
359 	kfree(dev->vbi_mode.bulk_ctl.urb);
360 	kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
361 
362 	dev->vbi_mode.bulk_ctl.urb = NULL;
363 	dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
364 	dev->vbi_mode.bulk_ctl.num_bufs = 0;
365 
366 	cx231xx_capture_start(dev, 0, Vbi);
367 }
368 EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
369 
370 /*
371  * Allocate URBs and start IRQ
372  */
373 int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
374 			  int num_bufs, int max_pkt_size,
375 			  int (*bulk_copy) (struct cx231xx *dev,
376 					    struct urb *urb))
377 {
378 	struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
379 	int i;
380 	int sb_size, pipe;
381 	struct urb *urb;
382 	int rc;
383 
384 	dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
385 
386 	/* De-allocates all pending stuff */
387 	cx231xx_uninit_vbi_isoc(dev);
388 
389 	/* clear if any halt */
390 	usb_clear_halt(dev->udev,
391 		       usb_rcvbulkpipe(dev->udev,
392 				       dev->vbi_mode.end_point_addr));
393 
394 	dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
395 	dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
396 	dma_q->pos = 0;
397 	dma_q->is_partial_line = 0;
398 	dma_q->last_sav = 0;
399 	dma_q->current_field = -1;
400 	dma_q->bytes_left_in_line = dev->width << 1;
401 	dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
402 				  PAL_VBI_LINES : NTSC_VBI_LINES);
403 	dma_q->lines_completed = 0;
404 	for (i = 0; i < 8; i++)
405 		dma_q->partial_buf[i] = 0;
406 
407 	dev->vbi_mode.bulk_ctl.urb = kcalloc(num_bufs, sizeof(void *),
408 					     GFP_KERNEL);
409 	if (!dev->vbi_mode.bulk_ctl.urb) {
410 		dev_err(dev->dev,
411 			"cannot alloc memory for usb buffers\n");
412 		return -ENOMEM;
413 	}
414 
415 	dev->vbi_mode.bulk_ctl.transfer_buffer =
416 	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
417 	if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
418 		dev_err(dev->dev,
419 			"cannot allocate memory for usbtransfer\n");
420 		kfree(dev->vbi_mode.bulk_ctl.urb);
421 		return -ENOMEM;
422 	}
423 
424 	dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
425 	dev->vbi_mode.bulk_ctl.buf = NULL;
426 
427 	sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
428 
429 	/* allocate urbs and transfer buffers */
430 	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
431 
432 		urb = usb_alloc_urb(0, GFP_KERNEL);
433 		if (!urb) {
434 			cx231xx_uninit_vbi_isoc(dev);
435 			return -ENOMEM;
436 		}
437 		dev->vbi_mode.bulk_ctl.urb[i] = urb;
438 		urb->transfer_flags = 0;
439 
440 		dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
441 		    kzalloc(sb_size, GFP_KERNEL);
442 		if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
443 			dev_err(dev->dev,
444 				"unable to allocate %i bytes for transfer buffer %i%s\n",
445 				sb_size, i,
446 				in_interrupt() ? " while in int" : "");
447 			cx231xx_uninit_vbi_isoc(dev);
448 			return -ENOMEM;
449 		}
450 
451 		pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
452 		usb_fill_bulk_urb(urb, dev->udev, pipe,
453 				  dev->vbi_mode.bulk_ctl.transfer_buffer[i],
454 				  sb_size, cx231xx_irq_vbi_callback, dma_q);
455 	}
456 
457 	init_waitqueue_head(&dma_q->wq);
458 
459 	/* submit urbs and enables IRQ */
460 	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
461 		rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
462 		if (rc) {
463 			dev_err(dev->dev,
464 				"submit of urb %i failed (error=%i)\n", i, rc);
465 			cx231xx_uninit_vbi_isoc(dev);
466 			return rc;
467 		}
468 	}
469 
470 	cx231xx_capture_start(dev, 1, Vbi);
471 
472 	return 0;
473 }
474 EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
475 
476 u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
477 			 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
478 {
479 	u32 bytes_copied = 0;
480 	int current_field = -1;
481 
482 	switch (sav_eav) {
483 
484 	case SAV_VBI_FIELD1:
485 		current_field = 1;
486 		break;
487 
488 	case SAV_VBI_FIELD2:
489 		current_field = 2;
490 		break;
491 	default:
492 		break;
493 	}
494 
495 	if (current_field < 0)
496 		return bytes_copied;
497 
498 	dma_q->last_sav = sav_eav;
499 
500 	bytes_copied =
501 	    cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
502 				  current_field);
503 
504 	return bytes_copied;
505 }
506 
507 /*
508  * Announces that a buffer were filled and request the next
509  */
510 static inline void vbi_buffer_filled(struct cx231xx *dev,
511 				     struct cx231xx_dmaqueue *dma_q,
512 				     struct cx231xx_buffer *buf)
513 {
514 	/* Advice that buffer was filled */
515 	/* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
516 
517 	buf->vb.state = VIDEOBUF_DONE;
518 	buf->vb.field_count++;
519 	buf->vb.ts = ktime_get_ns();
520 
521 	dev->vbi_mode.bulk_ctl.buf = NULL;
522 
523 	list_del(&buf->vb.queue);
524 	wake_up(&buf->vb.done);
525 }
526 
527 u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
528 			  u8 *p_line, u32 length, int field_number)
529 {
530 	u32 bytes_to_copy;
531 	struct cx231xx_buffer *buf;
532 	u32 _line_size = dev->width * 2;
533 
534 	if (dma_q->current_field == -1) {
535 		/* Just starting up */
536 		cx231xx_reset_vbi_buffer(dev, dma_q);
537 	}
538 
539 	if (dma_q->current_field != field_number)
540 		dma_q->lines_completed = 0;
541 
542 	/* get the buffer pointer */
543 	buf = dev->vbi_mode.bulk_ctl.buf;
544 
545 	/* Remember the field number for next time */
546 	dma_q->current_field = field_number;
547 
548 	bytes_to_copy = dma_q->bytes_left_in_line;
549 	if (bytes_to_copy > length)
550 		bytes_to_copy = length;
551 
552 	if (dma_q->lines_completed >= dma_q->lines_per_field) {
553 		dma_q->bytes_left_in_line -= bytes_to_copy;
554 		dma_q->is_partial_line =
555 		    (dma_q->bytes_left_in_line == 0) ? 0 : 1;
556 		return 0;
557 	}
558 
559 	dma_q->is_partial_line = 1;
560 
561 	/* If we don't have a buffer, just return the number of bytes we would
562 	   have copied if we had a buffer. */
563 	if (!buf) {
564 		dma_q->bytes_left_in_line -= bytes_to_copy;
565 		dma_q->is_partial_line =
566 		    (dma_q->bytes_left_in_line == 0) ? 0 : 1;
567 		return bytes_to_copy;
568 	}
569 
570 	/* copy the data to video buffer */
571 	cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
572 
573 	dma_q->pos += bytes_to_copy;
574 	dma_q->bytes_left_in_line -= bytes_to_copy;
575 
576 	if (dma_q->bytes_left_in_line == 0) {
577 
578 		dma_q->bytes_left_in_line = _line_size;
579 		dma_q->lines_completed++;
580 		dma_q->is_partial_line = 0;
581 
582 		if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
583 
584 			vbi_buffer_filled(dev, dma_q, buf);
585 
586 			dma_q->pos = 0;
587 			dma_q->lines_completed = 0;
588 			cx231xx_reset_vbi_buffer(dev, dma_q);
589 		}
590 	}
591 
592 	return bytes_to_copy;
593 }
594 
595 /*
596  * video-buf generic routine to get the next available buffer
597  */
598 static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
599 				    struct cx231xx_buffer **buf)
600 {
601 	struct cx231xx_video_mode *vmode =
602 	    container_of(dma_q, struct cx231xx_video_mode, vidq);
603 	struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
604 	char *outp;
605 
606 	if (list_empty(&dma_q->active)) {
607 		dev_err(dev->dev, "No active queue to serve\n");
608 		dev->vbi_mode.bulk_ctl.buf = NULL;
609 		*buf = NULL;
610 		return;
611 	}
612 
613 	/* Get the next buffer */
614 	*buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
615 
616 	/* Cleans up buffer - Useful for testing for frame/URB loss */
617 	outp = videobuf_to_vmalloc(&(*buf)->vb);
618 	memset(outp, 0, (*buf)->vb.size);
619 
620 	dev->vbi_mode.bulk_ctl.buf = *buf;
621 
622 	return;
623 }
624 
625 void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
626 			      struct cx231xx_dmaqueue *dma_q)
627 {
628 	struct cx231xx_buffer *buf;
629 
630 	buf = dev->vbi_mode.bulk_ctl.buf;
631 
632 	if (buf == NULL) {
633 		/* first try to get the buffer */
634 		get_next_vbi_buf(dma_q, &buf);
635 
636 		dma_q->pos = 0;
637 		dma_q->current_field = -1;
638 	}
639 
640 	dma_q->bytes_left_in_line = dev->width << 1;
641 	dma_q->lines_completed = 0;
642 }
643 
644 int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
645 			u8 *p_buffer, u32 bytes_to_copy)
646 {
647 	u8 *p_out_buffer = NULL;
648 	u32 current_line_bytes_copied = 0;
649 	struct cx231xx_buffer *buf;
650 	u32 _line_size = dev->width << 1;
651 	void *startwrite;
652 	int offset, lencopy;
653 
654 	buf = dev->vbi_mode.bulk_ctl.buf;
655 
656 	if (buf == NULL)
657 		return -EINVAL;
658 
659 	p_out_buffer = videobuf_to_vmalloc(&buf->vb);
660 
661 	if (dma_q->bytes_left_in_line != _line_size) {
662 		current_line_bytes_copied =
663 		    _line_size - dma_q->bytes_left_in_line;
664 	}
665 
666 	offset = (dma_q->lines_completed * _line_size) +
667 		 current_line_bytes_copied;
668 
669 	if (dma_q->current_field == 2) {
670 		/* Populate the second half of the frame */
671 		offset += (dev->width * 2 * dma_q->lines_per_field);
672 	}
673 
674 	/* prepare destination address */
675 	startwrite = p_out_buffer + offset;
676 
677 	lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
678 		  bytes_to_copy : dma_q->bytes_left_in_line;
679 
680 	memcpy(startwrite, p_buffer, lencopy);
681 
682 	return 0;
683 }
684 
685 u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
686 			      struct cx231xx_dmaqueue *dma_q)
687 {
688 	u32 height = 0;
689 
690 	height = ((dev->norm & V4L2_STD_625_50) ?
691 		  PAL_VBI_LINES : NTSC_VBI_LINES);
692 	if (dma_q->lines_completed == height && dma_q->current_field == 2)
693 		return 1;
694 	else
695 		return 0;
696 }
697