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