V4L/DVB (10954): Add cx231xx USB driver
[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,
43                                      int packet, int status)
44 {
45         char *errmsg = "Unknown";
46
47         switch (status) {
48         case -ENOENT:
49                 errmsg = "unlinked synchronuously";
50                 break;
51         case -ECONNRESET:
52                 errmsg = "unlinked asynchronuously";
53                 break;
54         case -ENOSR:
55                 errmsg = "Buffer error (overrun)";
56                 break;
57         case -EPIPE:
58                 errmsg = "Stalled (device not responding)";
59                 break;
60         case -EOVERFLOW:
61                 errmsg = "Babble (bad cable?)";
62                 break;
63         case -EPROTO:
64                 errmsg = "Bit-stuff error (bad cable?)";
65                 break;
66         case -EILSEQ:
67                 errmsg = "CRC/Timeout (could be anything)";
68                 break;
69         case -ETIME:
70                 errmsg = "Device does not respond";
71                 break;
72         }
73         if (packet < 0) {
74                 cx231xx_err(DRIVER_NAME "URB status %d [%s].\n",        status, 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 = cx231xx_find_boundary_SAV_EAV(p_buffer, dma_q->partial_buf, &bytes_parsed);
121         }
122
123         sav_eav &= 0xF0;
124         /* Get the first line if we have some portion of an SAV/EAV from the last buffer
125            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         {
138             u32 bytes_used = 0;
139
140             sav_eav = cx231xx_find_next_SAV_EAV(
141                 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             {
150                 bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
151                     sav_eav,                        /* SAV/EAV */
152                     p_buffer + bytes_parsed,        /* p_buffer */
153                     buffer_size - bytes_parsed);    /* buffer size */
154             }
155         }
156
157         /* Save the last four bytes of the buffer so we can check the buffer boundary
158            condition next time */
159         memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
160         bytes_parsed = 0;
161         }
162
163         return rc;
164 }
165
166 /* ------------------------------------------------------------------
167         Vbi buf operations
168    ------------------------------------------------------------------*/
169
170 static int
171 vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
172 {
173         struct cx231xx_fh *fh = vq->priv_data;
174         struct cx231xx        *dev = fh->dev;
175     u32 height = 0;
176
177         height = ((dev->norm & V4L2_STD_625_50) ?
178                                PAL_VBI_LINES : NTSC_VBI_LINES) ;
179
180         *size = ( dev->width * height * 2);
181         if (0 == *count)
182                 *count = CX231XX_DEF_VBI_BUF;
183
184         if (*count < CX231XX_MIN_BUF)
185                 *count = CX231XX_MIN_BUF;
186
187     /* call VBI setup if required */
188         /* cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
189     */
190
191         return 0;
192 }
193
194 /* This is called *without* dev->slock held; please keep it that way */
195 static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
196 {
197         struct cx231xx_fh     *fh  = vq->priv_data;
198         struct cx231xx        *dev = fh->dev;
199         unsigned long flags = 0;
200         if (in_interrupt())
201                 BUG();
202
203         /* We used to wait for the buffer to finish here, but this didn't work
204            because, as we were keeping the state as VIDEOBUF_QUEUED,
205            videobuf_queue_cancel marked it as finished for us.
206            (Also, it could wedge forever if the hardware was misconfigured.)
207
208            This should be safe; by the time we get here, the buffer isn't
209            queued anymore. If we ever start marking the buffers as
210            VIDEOBUF_ACTIVE, it won't be, though.
211         */
212         spin_lock_irqsave(&dev->vbi_mode.slock, flags);
213         if (dev->vbi_mode.isoc_ctl.buf == buf)
214                 dev->vbi_mode.isoc_ctl.buf = NULL;
215         spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
216
217         videobuf_vmalloc_free(&buf->vb);
218         buf->vb.state = VIDEOBUF_NEEDS_INIT;
219 }
220
221 static int
222 vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
223                                                 enum v4l2_field field)
224 {
225         struct cx231xx_fh     *fh  = vq->priv_data;
226         struct cx231xx_buffer *buf = 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, dev->vbi_mode.alt_max_pkt_size[0],
255                                       cx231xx_isoc_vbi_copy);
256                 if (rc < 0)
257                         goto fail;
258         }
259
260         buf->vb.state = VIDEOBUF_PREPARED;
261         return 0;
262
263 fail:
264         free_buffer(vq, buf);
265         return rc;
266 }
267
268 static void
269 vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
270 {
271         struct cx231xx_buffer    *buf     = container_of(vb, struct cx231xx_buffer, vb);
272         struct cx231xx_fh        *fh      = vq->priv_data;
273         struct cx231xx           *dev     = fh->dev;
274         struct cx231xx_dmaqueue  *vidq    = &dev->vbi_mode.vidq;
275
276         buf->vb.state = VIDEOBUF_QUEUED;
277         list_add_tail(&buf->vb.queue, &vidq->active);
278
279 }
280
281 static void vbi_buffer_release(struct videobuf_queue *vq,
282                                 struct videobuf_buffer *vb)
283 {
284         struct cx231xx_buffer   *buf  = container_of(vb, struct cx231xx_buffer, vb);
285     /*
286         struct cx231xx_fh       *fh   = vq->priv_data;
287         struct cx231xx          *dev  = (struct cx231xx *)fh->dev;
288
289         cx231xx_info(DRIVER_NAME "cx231xx: called vbi_buffer_release\n");
290     */
291
292         free_buffer(vq, buf);
293 }
294
295
296 struct videobuf_queue_ops cx231xx_vbi_qops = {
297         .buf_setup      = vbi_buffer_setup,
298         .buf_prepare    = vbi_buffer_prepare,
299         .buf_queue      = vbi_buffer_queue,
300         .buf_release    = vbi_buffer_release,
301 };
302
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 = container_of(dma_q, struct cx231xx_video_mode, vidq);
316     struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
317         int rc;
318
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", urb->status);
330                     break;
331         }
332
333         /* Copy data from URB */
334         spin_lock(&dev->vbi_mode.slock);
335         rc = dev->vbi_mode.isoc_ctl.isoc_copy(dev, urb);
336         spin_unlock(&dev->vbi_mode.slock);
337
338         /* Reset status */
339         urb->status = 0;
340
341         urb->status = usb_submit_urb(urb, GFP_ATOMIC);
342         if (urb->status) {
343                 cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n",
344                                urb->status);
345         }
346 }
347
348 /*
349  * Stop and Deallocate URBs
350  */
351 void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
352 {
353         struct urb *urb;
354         int i;
355
356         cx231xx_info(DRIVER_NAME  "cx231xx: called cx231xx_uninit_vbi_isoc\n");
357
358         dev->vbi_mode.isoc_ctl.nfields = -1;
359         for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
360                 urb = dev->vbi_mode.isoc_ctl.urb[i];
361                 if (urb) {
362             if (!irqs_disabled())
363                             usb_kill_urb(urb);
364             else
365                             usb_unlink_urb(urb);
366
367                         if (dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
368
369                 kfree(dev->vbi_mode.isoc_ctl.transfer_buffer[i]);
370                         dev->vbi_mode.isoc_ctl.transfer_buffer[i] = NULL;
371                         }
372                         usb_free_urb(urb);
373                         dev->vbi_mode.isoc_ctl.urb[i] = NULL;
374                 }
375                 dev->vbi_mode.isoc_ctl.transfer_buffer[i] = NULL;
376         }
377
378         kfree(dev->vbi_mode.isoc_ctl.urb);
379         kfree(dev->vbi_mode.isoc_ctl.transfer_buffer);
380
381         dev->vbi_mode.isoc_ctl.urb = NULL;
382         dev->vbi_mode.isoc_ctl.transfer_buffer = NULL;
383         dev->vbi_mode.isoc_ctl.num_bufs = 0;
384
385         cx231xx_capture_start(dev, 0, Vbi);
386 }
387 EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
388
389 /*
390  * Allocate URBs and start IRQ
391  */
392 int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
393                      int num_bufs, int max_pkt_size,
394                      int (*isoc_copy) (struct cx231xx *dev, struct urb *urb))
395 {
396         struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
397         int i;
398         int sb_size, pipe;
399         struct urb *urb;
400         int rc;
401
402         cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n");
403
404         /* De-allocates all pending stuff */
405         cx231xx_uninit_vbi_isoc(dev);
406
407     /* clear if any halt */
408     usb_clear_halt(dev->udev, usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr));
409
410
411         dev->vbi_mode.isoc_ctl.isoc_copy = isoc_copy;
412         dev->vbi_mode.isoc_ctl.num_bufs = num_bufs;
413     dma_q->pos = 0;
414     dma_q->is_partial_line = 0;
415     dma_q->last_sav = 0;
416     dma_q->current_field = -1;
417     dma_q->bytes_left_in_line = dev->width << 1;
418     dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
419                                PAL_VBI_LINES : NTSC_VBI_LINES) ;
420     dma_q->lines_completed = 0;
421     for(i = 0; i < 8 ; i++)
422         dma_q->partial_buf[i] = 0;
423
424         dev->vbi_mode.isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
425         if (!dev->vbi_mode.isoc_ctl.urb) {
426                 cx231xx_errdev("cannot alloc memory for usb buffers\n");
427                 return -ENOMEM;
428         }
429
430         dev->vbi_mode.isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
431                                               GFP_KERNEL);
432         if (!dev->vbi_mode.isoc_ctl.transfer_buffer) {
433                 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
434                 kfree(dev->vbi_mode.isoc_ctl.urb);
435                 return -ENOMEM;
436         }
437
438         dev->vbi_mode.isoc_ctl.max_pkt_size = max_pkt_size;
439         dev->vbi_mode.isoc_ctl.buf = NULL;
440
441         sb_size = max_packets * dev->vbi_mode.isoc_ctl.max_pkt_size;
442
443         /* allocate urbs and transfer buffers */
444         for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
445
446                 urb = usb_alloc_urb(0, GFP_KERNEL);
447                 if (!urb) {
448             cx231xx_err(DRIVER_NAME ": cannot alloc isoc_ctl.urb %i\n", i);
449                         cx231xx_uninit_vbi_isoc(dev);
450                         return -ENOMEM;
451                 }
452                 dev->vbi_mode.isoc_ctl.urb[i] = urb;
453         urb->transfer_flags = 0;
454
455         dev->vbi_mode.isoc_ctl.transfer_buffer[i] = kzalloc(sb_size, GFP_KERNEL);
456                 if (!dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
457             cx231xx_err(DRIVER_NAME ": unable to allocate %i bytes for transfer"
458                                         " buffer %i%s\n",
459                                         sb_size, i,
460                                         in_interrupt()?" while in int":"");
461                         cx231xx_uninit_vbi_isoc(dev);
462                         return -ENOMEM;
463                 }
464
465                 pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
466         usb_fill_bulk_urb(urb, dev->udev, pipe,
467                                  dev->vbi_mode.isoc_ctl.transfer_buffer[i], sb_size,
468                                  cx231xx_irq_vbi_callback, dma_q);
469         }
470
471         init_waitqueue_head(&dma_q->wq);
472
473         /* submit urbs and enables IRQ */
474         for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
475                 rc = usb_submit_urb(dev->vbi_mode.isoc_ctl.urb[i], GFP_ATOMIC);
476                 if (rc) {
477             cx231xx_err(DRIVER_NAME ": submit of urb %i failed (error=%i)\n", i,
478                                    rc);
479                         cx231xx_uninit_vbi_isoc(dev);
480                         return rc;
481                 }
482         }
483
484     cx231xx_capture_start(dev, 1, Vbi);
485
486         return 0;
487 }
488 EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
489
490
491 u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue  *dma_q,
492                            u8 sav_eav, u8 *p_buffer, u32 buffer_size)
493 {
494     u32 bytes_copied = 0;
495     int current_field = -1;
496
497     switch(sav_eav) {
498
499         case SAV_VBI_FIELD1:
500             current_field = 1;
501             break;
502
503         case SAV_VBI_FIELD2:
504             current_field = 2;
505             break;
506         default:
507             break;
508     }
509
510     if(current_field < 0 )
511         return bytes_copied;
512
513     dma_q->last_sav = sav_eav;
514
515     bytes_copied = cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size, current_field);
516
517     return bytes_copied;
518 }
519
520 /*
521  * Announces that a buffer were filled and request the next
522  */
523 static inline void vbi_buffer_filled(struct cx231xx *dev,
524                                   struct cx231xx_dmaqueue *dma_q,
525                                   struct cx231xx_buffer *buf)
526 {
527         /* Advice that buffer was filled */
528         /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
529
530         buf->vb.state = VIDEOBUF_DONE;
531         buf->vb.field_count++;
532         do_gettimeofday(&buf->vb.ts);
533
534         dev->vbi_mode.isoc_ctl.buf = NULL;
535
536         list_del(&buf->vb.queue);
537         wake_up(&buf->vb.done);
538 }
539
540 u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue  *dma_q,
541                             u8 *p_line, u32 length, int field_number)
542 {
543     u32 bytes_to_copy;
544     struct cx231xx_buffer *buf;
545     u32 _line_size = dev->width * 2;
546
547     if( dma_q->current_field != field_number )  {
548         cx231xx_reset_vbi_buffer(dev, dma_q);
549     }
550
551     /* get the buffer pointer */
552     buf = dev->vbi_mode.isoc_ctl.buf;
553
554     /* Remember the field number for next time */
555     dma_q->current_field = field_number;
556
557     bytes_to_copy = dma_q->bytes_left_in_line;
558     if(bytes_to_copy > length)
559         bytes_to_copy = length;
560
561     if(dma_q->lines_completed >= dma_q->lines_per_field) {
562         dma_q->bytes_left_in_line -= bytes_to_copy;
563         dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
564         return 0;
565     }
566
567     dma_q->is_partial_line = 1;
568
569     /* If we don't have a buffer, just return the number of bytes we would
570        have copied if we had a buffer. */
571     if(!buf) {
572         dma_q->bytes_left_in_line -= bytes_to_copy;
573         dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ? 0 : 1;
574         return bytes_to_copy;
575     }
576
577     /* copy the data to video buffer */
578     cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
579
580     dma_q->pos += bytes_to_copy;
581     dma_q->bytes_left_in_line -= bytes_to_copy;
582
583     if(dma_q->bytes_left_in_line == 0) {
584
585         dma_q->bytes_left_in_line = _line_size;
586         dma_q->lines_completed++;
587         dma_q->is_partial_line = 0;
588
589         if(cx231xx_is_vbi_buffer_done(dev, dma_q) && buf ) {
590
591             vbi_buffer_filled(dev, dma_q, buf);
592
593             dma_q->pos = 0;
594             buf = NULL;
595             dma_q->lines_completed = 0;
596         }
597     }
598
599     return bytes_to_copy;
600 }
601
602 /*
603  * video-buf generic routine to get the next available buffer
604  */
605 static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
606                                           struct cx231xx_buffer **buf)
607 {
608     struct cx231xx_video_mode *vmode = container_of(dma_q, struct cx231xx_video_mode, vidq);
609     struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
610         char *outp;
611
612         if (list_empty(&dma_q->active)) {
613         cx231xx_err(DRIVER_NAME ": No active queue to serve\n");
614                 dev->vbi_mode.isoc_ctl.buf = NULL;
615                 *buf = NULL;
616                 return;
617         }
618
619         /* Get the next buffer */
620         *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
621
622         /* Cleans up buffer - Usefull for testing for frame/URB loss */
623         outp = videobuf_to_vmalloc(&(*buf)->vb);
624         memset(outp, 0, (*buf)->vb.size);
625
626         dev->vbi_mode.isoc_ctl.buf = *buf;
627
628         return;
629 }
630
631
632 void cx231xx_reset_vbi_buffer(struct cx231xx *dev, struct cx231xx_dmaqueue  *dma_q)
633 {
634     struct cx231xx_buffer *buf;
635
636     buf = dev->vbi_mode.isoc_ctl.buf;
637
638     if(buf == NULL) {
639
640         /* first try to get the buffer */
641         get_next_vbi_buf(dma_q, &buf);
642
643         dma_q->pos = 0;
644         dma_q->current_field = -1;
645     }
646
647     dma_q->bytes_left_in_line = dev->width << 1;
648     dma_q->lines_completed = 0;
649 }
650
651 int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
652                     u8 *p_buffer, u32 bytes_to_copy)
653 {
654     u8 *p_out_buffer = NULL;
655     u32 current_line_bytes_copied = 0;
656     struct cx231xx_buffer *buf;
657     u32 _line_size = dev->width << 1;
658     void *startwrite;
659         int  offset, lencopy;
660
661     buf = dev->vbi_mode.isoc_ctl.buf;
662
663     if (buf == NULL) {
664         return -1;
665     }
666
667         p_out_buffer = videobuf_to_vmalloc(&buf->vb);
668
669     if(dma_q->bytes_left_in_line != _line_size ) {
670         current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
671     }
672
673     offset = ( dma_q->lines_completed * _line_size ) + current_line_bytes_copied;
674
675     /* prepare destination address */
676         startwrite = p_out_buffer + offset;
677
678     lencopy =  dma_q->bytes_left_in_line > bytes_to_copy ? bytes_to_copy : dma_q->bytes_left_in_line;
679
680     memcpy(startwrite, p_buffer, lencopy);
681
682     return 0;
683 }
684
685
686 u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,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     return (dma_q->lines_completed == height)?1:0;
693 }