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