cx231xx-vbi.c 17.4 KB
Newer Older
1 2 3 4
/*
   cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices

   Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
5
	Based on cx88 driver
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

22
#include "cx231xx.h"
23 24 25 26 27 28 29 30
#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/i2c.h>
#include <linux/mm.h>
#include <linux/mutex.h>
31
#include <linux/slab.h>
32 33 34

#include <media/v4l2-common.h>
#include <media/v4l2-ioctl.h>
35
#include <media/drv-intf/msp3400.h>
36 37 38 39
#include <media/tuner.h>

#include "cx231xx-vbi.h"

40
static inline void print_err_status(struct cx231xx *dev, int packet, int status)
41 42 43 44 45
{
	char *errmsg = "Unknown";

	switch (status) {
	case -ENOENT:
46
		errmsg = "unlinked synchronously";
47 48
		break;
	case -ECONNRESET:
49
		errmsg = "unlinked asynchronously";
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
		break;
	case -ENOSR:
		errmsg = "Buffer error (overrun)";
		break;
	case -EPIPE:
		errmsg = "Stalled (device not responding)";
		break;
	case -EOVERFLOW:
		errmsg = "Babble (bad cable?)";
		break;
	case -EPROTO:
		errmsg = "Bit-stuff error (bad cable?)";
		break;
	case -EILSEQ:
		errmsg = "CRC/Timeout (could be anything)";
		break;
	case -ETIME:
		errmsg = "Device does not respond";
		break;
	}
	if (packet < 0) {
71
		dev_err(dev->dev,
72
			"URB status %d [%s].\n", status, errmsg);
73
	} else {
74
		dev_err(dev->dev,
75
			"URB packet %d, status %d [%s].\n",
76
			packet, status, errmsg);
77 78 79 80 81 82 83 84
	}
}

/*
 * Controls the isoc copy of each urb packet
 */
static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
{
85 86
	struct cx231xx_dmaqueue *dma_q = urb->context;
	int rc = 1;
87
	unsigned char *p_buffer;
88 89
	u32 bytes_parsed = 0, buffer_size = 0;
	u8 sav_eav = 0;
90 91 92 93

	if (!dev)
		return 0;

94
	if (dev->state & DEV_DISCONNECTED)
95 96 97 98 99 100 101 102
		return 0;

	if (urb->status < 0) {
		print_err_status(dev, -1, urb->status);
		if (urb->status == -ENOENT)
			return 0;
	}

103 104 105 106 107 108 109 110
	/* get buffer pointer and length */
	p_buffer = urb->transfer_buffer;
	buffer_size = urb->actual_length;

	if (buffer_size > 0) {
		bytes_parsed = 0;

		if (dma_q->is_partial_line) {
111 112
			/* Handle the case where we were working on a partial
			   line */
113 114
			sav_eav = dma_q->last_sav;
		} else {
115 116 117 118
			/* Check for a SAV/EAV overlapping the
			   buffer boundary */

			sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
119 120 121 122 123
							  dma_q->partial_buf,
							  &bytes_parsed);
		}

		sav_eav &= 0xF0;
124 125
		/* Get the first line if we have some portion of an SAV/EAV from
		   the last buffer or a partial line */
126
		if (sav_eav) {
127 128 129 130
			bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
				sav_eav,		       /* SAV/EAV */
				p_buffer + bytes_parsed,       /* p_buffer */
				buffer_size - bytes_parsed);   /* buffer size */
131 132 133 134 135 136 137 138
		}

		/* Now parse data that is completely in this buffer */
		dma_q->is_partial_line = 0;

		while (bytes_parsed < buffer_size) {
			u32 bytes_used = 0;

139 140 141 142
			sav_eav = cx231xx_find_next_SAV_EAV(
				p_buffer + bytes_parsed,	/* p_buffer */
				buffer_size - bytes_parsed, /* buffer size */
				&bytes_used);	/* bytes used to get SAV/EAV */
143 144 145 146 147

			bytes_parsed += bytes_used;

			sav_eav &= 0xF0;
			if (sav_eav && (bytes_parsed < buffer_size)) {
148
				bytes_parsed += cx231xx_get_vbi_line(dev,
149 150 151
					dma_q, sav_eav,	/* SAV/EAV */
					p_buffer+bytes_parsed, /* p_buffer */
					buffer_size-bytes_parsed);/*buf size*/
152 153 154
			}
		}

155 156
		/* Save the last four bytes of the buffer so we can
		check the buffer boundary condition next time */
157 158
		memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
		bytes_parsed = 0;
159 160 161 162 163 164 165 166 167 168
	}

	return rc;
}

/* ------------------------------------------------------------------
	Vbi buf operations
   ------------------------------------------------------------------*/

static int
169 170
vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
		 unsigned int *size)
171 172
{
	struct cx231xx_fh *fh = vq->priv_data;
173 174
	struct cx231xx *dev = fh->dev;
	u32 height = 0;
175 176

	height = ((dev->norm & V4L2_STD_625_50) ?
177
		  PAL_VBI_LINES : NTSC_VBI_LINES);
178

179
	*size = (dev->width * height * 2 * 2);
180 181 182 183 184 185 186 187 188 189 190 191
	if (0 == *count)
		*count = CX231XX_DEF_VBI_BUF;

	if (*count < CX231XX_MIN_BUF)
		*count = CX231XX_MIN_BUF;

	return 0;
}

/* This is called *without* dev->slock held; please keep it that way */
static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
{
192 193
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = fh->dev;
194
	unsigned long flags = 0;
195
	BUG_ON(in_interrupt());
196 197 198 199 200 201 202 203 204

	/* We used to wait for the buffer to finish here, but this didn't work
	   because, as we were keeping the state as VIDEOBUF_QUEUED,
	   videobuf_queue_cancel marked it as finished for us.
	   (Also, it could wedge forever if the hardware was misconfigured.)

	   This should be safe; by the time we get here, the buffer isn't
	   queued anymore. If we ever start marking the buffers as
	   VIDEOBUF_ACTIVE, it won't be, though.
205
	 */
206
	spin_lock_irqsave(&dev->vbi_mode.slock, flags);
207 208
	if (dev->vbi_mode.bulk_ctl.buf == buf)
		dev->vbi_mode.bulk_ctl.buf = NULL;
209 210 211 212 213 214 215 216
	spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);

	videobuf_vmalloc_free(&buf->vb);
	buf->vb.state = VIDEOBUF_NEEDS_INIT;
}

static int
vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
217
		   enum v4l2_field field)
218
{
219 220 221 222 223 224
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
	struct cx231xx *dev = fh->dev;
	int rc = 0, urb_init = 0;
	u32 height = 0;
225 226

	height = ((dev->norm & V4L2_STD_625_50) ?
227
		  PAL_VBI_LINES : NTSC_VBI_LINES);
228
	buf->vb.size = ((dev->width << 1) * height * 2);
229

230
	if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
231 232
		return -EINVAL;

233
	buf->vb.width = dev->width;
234
	buf->vb.height = height;
235 236
	buf->vb.field = field;
	buf->vb.field = V4L2_FIELD_SEQ_TB;
237 238 239 240 241 242 243

	if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
		rc = videobuf_iolock(vq, &buf->vb, NULL);
		if (rc < 0)
			goto fail;
	}

244
	if (!dev->vbi_mode.bulk_ctl.num_bufs)
245 246 247 248
		urb_init = 1;

	if (urb_init) {
		rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
249 250 251
					   CX231XX_NUM_VBI_BUFS,
					   dev->vbi_mode.alt_max_pkt_size[0],
					   cx231xx_isoc_vbi_copy);
252 253 254 255 256 257 258
		if (rc < 0)
			goto fail;
	}

	buf->vb.state = VIDEOBUF_PREPARED;
	return 0;

259
fail:
260 261 262 263 264 265 266
	free_buffer(vq, buf);
	return rc;
}

static void
vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
{
267 268 269 270 271
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
	struct cx231xx_fh *fh = vq->priv_data;
	struct cx231xx *dev = fh->dev;
	struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
272 273 274 275 276 277 278

	buf->vb.state = VIDEOBUF_QUEUED;
	list_add_tail(&buf->vb.queue, &vidq->active);

}

static void vbi_buffer_release(struct videobuf_queue *vq,
279
			       struct videobuf_buffer *vb)
280
{
281 282
	struct cx231xx_buffer *buf =
	    container_of(vb, struct cx231xx_buffer, vb);
283 284 285 286 287


	free_buffer(vq, buf);
}

288
const struct videobuf_queue_ops cx231xx_vbi_qops = {
289
	.buf_setup   = vbi_buffer_setup,
290
	.buf_prepare = vbi_buffer_prepare,
291
	.buf_queue   = vbi_buffer_queue,
292
	.buf_release = vbi_buffer_release,
293 294 295 296 297 298 299 300 301 302 303
};

/* ------------------------------------------------------------------
	URB control
   ------------------------------------------------------------------*/

/*
 * IRQ callback, called by URB callback
 */
static void cx231xx_irq_vbi_callback(struct urb *urb)
{
304 305 306 307
	struct cx231xx_dmaqueue *dma_q = urb->context;
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
308
	unsigned long flags;
309

310 311 312 313 314 315 316 317 318
	switch (urb->status) {
	case 0:		/* success */
	case -ETIMEDOUT:	/* NAK */
		break;
	case -ECONNRESET:	/* kill */
	case -ENOENT:
	case -ESHUTDOWN:
		return;
	default:		/* error */
319
		dev_err(dev->dev,
320
			"urb completion error %d.\n", urb->status);
321
		break;
322 323 324
	}

	/* Copy data from URB */
325
	spin_lock_irqsave(&dev->vbi_mode.slock, flags);
326
	dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
327
	spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
328 329 330 331 332 333

	/* Reset status */
	urb->status = 0;

	urb->status = usb_submit_urb(urb, GFP_ATOMIC);
	if (urb->status) {
334
		dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
335
			urb->status);
336 337 338 339 340 341 342 343 344 345 346
	}
}

/*
 * Stop and Deallocate URBs
 */
void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
{
	struct urb *urb;
	int i;

347
	dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
348

349 350 351
	dev->vbi_mode.bulk_ctl.nfields = -1;
	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
		urb = dev->vbi_mode.bulk_ctl.urb[i];
352
		if (urb) {
353 354 355 356
			if (!irqs_disabled())
				usb_kill_urb(urb);
			else
				usb_unlink_urb(urb);
357

358
			if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
359

360
				kfree(dev->vbi_mode.bulk_ctl.
361
				      transfer_buffer[i]);
362
				dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
363
				    NULL;
364 365
			}
			usb_free_urb(urb);
366
			dev->vbi_mode.bulk_ctl.urb[i] = NULL;
367
		}
368
		dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
369 370
	}

371 372
	kfree(dev->vbi_mode.bulk_ctl.urb);
	kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
373

374 375 376
	dev->vbi_mode.bulk_ctl.urb = NULL;
	dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
	dev->vbi_mode.bulk_ctl.num_bufs = 0;
377 378 379 380 381 382 383 384 385

	cx231xx_capture_start(dev, 0, Vbi);
}
EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);

/*
 * Allocate URBs and start IRQ
 */
int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
386
			  int num_bufs, int max_pkt_size,
387
			  int (*bulk_copy) (struct cx231xx *dev,
388
					    struct urb *urb))
389 390 391 392 393 394 395
{
	struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
	int i;
	int sb_size, pipe;
	struct urb *urb;
	int rc;

396
	dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
397 398 399 400

	/* De-allocates all pending stuff */
	cx231xx_uninit_vbi_isoc(dev);

401 402 403 404
	/* clear if any halt */
	usb_clear_halt(dev->udev,
		       usb_rcvbulkpipe(dev->udev,
				       dev->vbi_mode.end_point_addr));
405

406 407
	dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
	dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
408 409 410 411 412 413 414 415 416 417 418
	dma_q->pos = 0;
	dma_q->is_partial_line = 0;
	dma_q->last_sav = 0;
	dma_q->current_field = -1;
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
				  PAL_VBI_LINES : NTSC_VBI_LINES);
	dma_q->lines_completed = 0;
	for (i = 0; i < 8; i++)
		dma_q->partial_buf[i] = 0;

K
Kees Cook 已提交
419
	dev->vbi_mode.bulk_ctl.urb = kcalloc(num_bufs, sizeof(void *),
420
					     GFP_KERNEL);
421
	if (!dev->vbi_mode.bulk_ctl.urb) {
422
		dev_err(dev->dev,
423
			"cannot alloc memory for usb buffers\n");
424 425 426
		return -ENOMEM;
	}

427
	dev->vbi_mode.bulk_ctl.transfer_buffer =
K
Kees Cook 已提交
428
	    kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
429
	if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
430
		dev_err(dev->dev,
431
			"cannot allocate memory for usbtransfer\n");
432
		kfree(dev->vbi_mode.bulk_ctl.urb);
433 434 435
		return -ENOMEM;
	}

436 437
	dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
	dev->vbi_mode.bulk_ctl.buf = NULL;
438

439
	sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
440 441

	/* allocate urbs and transfer buffers */
442
	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
443 444 445 446 447 448

		urb = usb_alloc_urb(0, GFP_KERNEL);
		if (!urb) {
			cx231xx_uninit_vbi_isoc(dev);
			return -ENOMEM;
		}
449
		dev->vbi_mode.bulk_ctl.urb[i] = urb;
450
		urb->transfer_flags = 0;
451

452
		dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
453
		    kzalloc(sb_size, GFP_KERNEL);
454
		if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
455
			dev_err(dev->dev,
456
				"unable to allocate %i bytes for transfer buffer %i%s\n",
457 458
				sb_size, i,
				in_interrupt() ? " while in int" : "");
459 460 461 462 463
			cx231xx_uninit_vbi_isoc(dev);
			return -ENOMEM;
		}

		pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
464
		usb_fill_bulk_urb(urb, dev->udev, pipe,
465
				  dev->vbi_mode.bulk_ctl.transfer_buffer[i],
466
				  sb_size, cx231xx_irq_vbi_callback, dma_q);
467 468 469 470 471
	}

	init_waitqueue_head(&dma_q->wq);

	/* submit urbs and enables IRQ */
472 473
	for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
		rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
474
		if (rc) {
475
			dev_err(dev->dev,
476
				"submit of urb %i failed (error=%i)\n", i, rc);
477 478 479 480 481
			cx231xx_uninit_vbi_isoc(dev);
			return rc;
		}
	}

482
	cx231xx_capture_start(dev, 1, Vbi);
483 484 485

	return 0;
}
486
EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
487

488 489
u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
			 u8 sav_eav, u8 *p_buffer, u32 buffer_size)
490
{
491 492
	u32 bytes_copied = 0;
	int current_field = -1;
493

494
	switch (sav_eav) {
495

496 497 498
	case SAV_VBI_FIELD1:
		current_field = 1;
		break;
499

500 501 502 503 504 505
	case SAV_VBI_FIELD2:
		current_field = 2;
		break;
	default:
		break;
	}
506

507 508
	if (current_field < 0)
		return bytes_copied;
509

510
	dma_q->last_sav = sav_eav;
511

512 513 514
	bytes_copied =
	    cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
				  current_field);
515

516
	return bytes_copied;
517 518 519 520 521 522
}

/*
 * Announces that a buffer were filled and request the next
 */
static inline void vbi_buffer_filled(struct cx231xx *dev,
523 524
				     struct cx231xx_dmaqueue *dma_q,
				     struct cx231xx_buffer *buf)
525 526
{
	/* Advice that buffer was filled */
527
	/* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
528 529 530

	buf->vb.state = VIDEOBUF_DONE;
	buf->vb.field_count++;
531
	buf->vb.ts = ktime_get_ns();
532

533
	dev->vbi_mode.bulk_ctl.buf = NULL;
534 535 536 537 538

	list_del(&buf->vb.queue);
	wake_up(&buf->vb.done);
}

539
u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
540
			  u8 *p_line, u32 length, int field_number)
541
{
542 543 544
	u32 bytes_to_copy;
	struct cx231xx_buffer *buf;
	u32 _line_size = dev->width * 2;
545

546 547
	if (dma_q->current_field == -1) {
		/* Just starting up */
548
		cx231xx_reset_vbi_buffer(dev, dma_q);
549 550 551 552
	}

	if (dma_q->current_field != field_number)
		dma_q->lines_completed = 0;
553

554
	/* get the buffer pointer */
555
	buf = dev->vbi_mode.bulk_ctl.buf;
556

557 558
	/* Remember the field number for next time */
	dma_q->current_field = field_number;
559

560 561 562
	bytes_to_copy = dma_q->bytes_left_in_line;
	if (bytes_to_copy > length)
		bytes_to_copy = length;
563

564 565 566 567 568 569
	if (dma_q->lines_completed >= dma_q->lines_per_field) {
		dma_q->bytes_left_in_line -= bytes_to_copy;
		dma_q->is_partial_line =
		    (dma_q->bytes_left_in_line == 0) ? 0 : 1;
		return 0;
	}
570

571
	dma_q->is_partial_line = 1;
572

573 574 575 576 577 578 579 580
	/* If we don't have a buffer, just return the number of bytes we would
	   have copied if we had a buffer. */
	if (!buf) {
		dma_q->bytes_left_in_line -= bytes_to_copy;
		dma_q->is_partial_line =
		    (dma_q->bytes_left_in_line == 0) ? 0 : 1;
		return bytes_to_copy;
	}
581

582 583
	/* copy the data to video buffer */
	cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
584

585 586
	dma_q->pos += bytes_to_copy;
	dma_q->bytes_left_in_line -= bytes_to_copy;
587

588
	if (dma_q->bytes_left_in_line == 0) {
589

590 591 592
		dma_q->bytes_left_in_line = _line_size;
		dma_q->lines_completed++;
		dma_q->is_partial_line = 0;
593

594
		if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
595

596
			vbi_buffer_filled(dev, dma_q, buf);
597

598 599
			dma_q->pos = 0;
			dma_q->lines_completed = 0;
600
			cx231xx_reset_vbi_buffer(dev, dma_q);
601 602
		}
	}
603

604
	return bytes_to_copy;
605 606 607 608 609 610
}

/*
 * video-buf generic routine to get the next available buffer
 */
static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
611
				    struct cx231xx_buffer **buf)
612
{
613 614 615
	struct cx231xx_video_mode *vmode =
	    container_of(dma_q, struct cx231xx_video_mode, vidq);
	struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
616 617 618
	char *outp;

	if (list_empty(&dma_q->active)) {
619
		dev_err(dev->dev, "No active queue to serve\n");
620
		dev->vbi_mode.bulk_ctl.buf = NULL;
621 622 623 624 625 626 627
		*buf = NULL;
		return;
	}

	/* Get the next buffer */
	*buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);

L
Lucas De Marchi 已提交
628
	/* Cleans up buffer - Useful for testing for frame/URB loss */
629 630 631
	outp = videobuf_to_vmalloc(&(*buf)->vb);
	memset(outp, 0, (*buf)->vb.size);

632
	dev->vbi_mode.bulk_ctl.buf = *buf;
633 634 635 636

	return;
}

637 638
void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
			      struct cx231xx_dmaqueue *dma_q)
639
{
640
	struct cx231xx_buffer *buf;
641

642
	buf = dev->vbi_mode.bulk_ctl.buf;
643

644 645 646
	if (buf == NULL) {
		/* first try to get the buffer */
		get_next_vbi_buf(dma_q, &buf);
647

648 649 650
		dma_q->pos = 0;
		dma_q->current_field = -1;
	}
651

652 653
	dma_q->bytes_left_in_line = dev->width << 1;
	dma_q->lines_completed = 0;
654 655 656
}

int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
657
			u8 *p_buffer, u32 bytes_to_copy)
658
{
659 660 661 662 663 664
	u8 *p_out_buffer = NULL;
	u32 current_line_bytes_copied = 0;
	struct cx231xx_buffer *buf;
	u32 _line_size = dev->width << 1;
	void *startwrite;
	int offset, lencopy;
665

666
	buf = dev->vbi_mode.bulk_ctl.buf;
667

668 669
	if (buf == NULL)
		return -EINVAL;
670 671 672

	p_out_buffer = videobuf_to_vmalloc(&buf->vb);

673 674 675 676
	if (dma_q->bytes_left_in_line != _line_size) {
		current_line_bytes_copied =
		    _line_size - dma_q->bytes_left_in_line;
	}
677

678 679
	offset = (dma_q->lines_completed * _line_size) +
		 current_line_bytes_copied;
680

681 682 683 684 685
	if (dma_q->current_field == 2) {
		/* Populate the second half of the frame */
		offset += (dev->width * 2 * dma_q->lines_per_field);
	}

686
	/* prepare destination address */
687 688
	startwrite = p_out_buffer + offset;

689 690
	lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
		  bytes_to_copy : dma_q->bytes_left_in_line;
691

692
	memcpy(startwrite, p_buffer, lencopy);
693

694
	return 0;
695 696
}

697 698
u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
			      struct cx231xx_dmaqueue *dma_q)
699
{
700
	u32 height = 0;
701 702

	height = ((dev->norm & V4L2_STD_625_50) ?
703
		  PAL_VBI_LINES : NTSC_VBI_LINES);
704 705 706 707
	if (dma_q->lines_completed == height && dma_q->current_field == 2)
		return 1;
	else
		return 0;
708
}