2 cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
4 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
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.
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.
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.
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>
30 #include <linux/mutex.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
34 #include <media/v4l2-chip-ident.h>
35 #include <media/msp3400.h>
36 #include <media/tuner.h>
39 #include "cx231xx-vbi.h"
41 static inline void print_err_status(struct cx231xx
*dev
, int packet
, int status
)
43 char *errmsg
= "Unknown";
47 errmsg
= "unlinked synchronuously";
50 errmsg
= "unlinked asynchronuously";
53 errmsg
= "Buffer error (overrun)";
56 errmsg
= "Stalled (device not responding)";
59 errmsg
= "Babble (bad cable?)";
62 errmsg
= "Bit-stuff error (bad cable?)";
65 errmsg
= "CRC/Timeout (could be anything)";
68 errmsg
= "Device does not respond";
72 cx231xx_err(DRIVER_NAME
"URB status %d [%s].\n", status
,
75 cx231xx_err(DRIVER_NAME
"URB packet %d, status %d [%s].\n",
76 packet
, status
, errmsg
);
81 * Controls the isoc copy of each urb packet
83 static inline int cx231xx_isoc_vbi_copy(struct cx231xx
*dev
, struct urb
*urb
)
85 struct cx231xx_buffer
*buf
;
86 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
88 unsigned char *p_buffer
;
89 u32 bytes_parsed
= 0, buffer_size
= 0;
95 if ((dev
->state
& DEV_DISCONNECTED
) || (dev
->state
& DEV_MISCONFIGURED
))
98 if (urb
->status
< 0) {
99 print_err_status(dev
, -1, urb
->status
);
100 if (urb
->status
== -ENOENT
)
104 buf
= dev
->vbi_mode
.isoc_ctl
.buf
;
106 /* get buffer pointer and length */
107 p_buffer
= urb
->transfer_buffer
;
108 buffer_size
= urb
->actual_length
;
110 if (buffer_size
> 0) {
113 if (dma_q
->is_partial_line
) {
114 /* Handle the case where we were working on a partial
116 sav_eav
= dma_q
->last_sav
;
118 /* Check for a SAV/EAV overlapping the
121 sav_eav
= cx231xx_find_boundary_SAV_EAV(p_buffer
,
127 /* Get the first line if we have some portion of an SAV/EAV from
128 the last buffer or a partial line */
130 bytes_parsed
+= cx231xx_get_vbi_line(dev
, dma_q
,
131 sav_eav
, /* SAV/EAV */
132 p_buffer
+ bytes_parsed
, /* p_buffer */
133 buffer_size
- bytes_parsed
); /* buffer size */
136 /* Now parse data that is completely in this buffer */
137 dma_q
->is_partial_line
= 0;
139 while (bytes_parsed
< buffer_size
) {
142 sav_eav
= cx231xx_find_next_SAV_EAV(
143 p_buffer
+ bytes_parsed
, /* p_buffer */
144 buffer_size
- bytes_parsed
, /* buffer size */
145 &bytes_used
); /* bytes used to get SAV/EAV */
147 bytes_parsed
+= bytes_used
;
150 if (sav_eav
&& (bytes_parsed
< buffer_size
)) {
151 bytes_parsed
+= cx231xx_get_vbi_line(dev
,
152 dma_q
, sav_eav
, /* SAV/EAV */
153 p_buffer
+bytes_parsed
, /* p_buffer */
154 buffer_size
-bytes_parsed
);/*buf size*/
158 /* Save the last four bytes of the buffer so we can
159 check the buffer boundary condition next time */
160 memcpy(dma_q
->partial_buf
, p_buffer
+ buffer_size
- 4, 4);
167 /* ------------------------------------------------------------------
169 ------------------------------------------------------------------*/
172 vbi_buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
175 struct cx231xx_fh
*fh
= vq
->priv_data
;
176 struct cx231xx
*dev
= fh
->dev
;
179 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
180 PAL_VBI_LINES
: NTSC_VBI_LINES
);
182 *size
= (dev
->width
* height
* 2);
184 *count
= CX231XX_DEF_VBI_BUF
;
186 if (*count
< CX231XX_MIN_BUF
)
187 *count
= CX231XX_MIN_BUF
;
192 /* This is called *without* dev->slock held; please keep it that way */
193 static void free_buffer(struct videobuf_queue
*vq
, struct cx231xx_buffer
*buf
)
195 struct cx231xx_fh
*fh
= vq
->priv_data
;
196 struct cx231xx
*dev
= fh
->dev
;
197 unsigned long flags
= 0;
201 /* We used to wait for the buffer to finish here, but this didn't work
202 because, as we were keeping the state as VIDEOBUF_QUEUED,
203 videobuf_queue_cancel marked it as finished for us.
204 (Also, it could wedge forever if the hardware was misconfigured.)
206 This should be safe; by the time we get here, the buffer isn't
207 queued anymore. If we ever start marking the buffers as
208 VIDEOBUF_ACTIVE, it won't be, though.
210 spin_lock_irqsave(&dev
->vbi_mode
.slock
, flags
);
211 if (dev
->vbi_mode
.isoc_ctl
.buf
== buf
)
212 dev
->vbi_mode
.isoc_ctl
.buf
= NULL
;
213 spin_unlock_irqrestore(&dev
->vbi_mode
.slock
, flags
);
215 videobuf_vmalloc_free(&buf
->vb
);
216 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
220 vbi_buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
221 enum v4l2_field field
)
223 struct cx231xx_fh
*fh
= vq
->priv_data
;
224 struct cx231xx_buffer
*buf
=
225 container_of(vb
, struct cx231xx_buffer
, vb
);
226 struct cx231xx
*dev
= fh
->dev
;
227 int rc
= 0, urb_init
= 0;
230 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
231 PAL_VBI_LINES
: NTSC_VBI_LINES
);
232 buf
->vb
.size
= ((dev
->width
<< 1) * height
);
234 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
237 buf
->vb
.width
= dev
->width
;
238 buf
->vb
.height
= height
;
239 buf
->vb
.field
= field
;
240 buf
->vb
.field
= V4L2_FIELD_SEQ_TB
;
242 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
243 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
248 if (!dev
->vbi_mode
.isoc_ctl
.num_bufs
)
252 rc
= cx231xx_init_vbi_isoc(dev
, CX231XX_NUM_VBI_PACKETS
,
253 CX231XX_NUM_VBI_BUFS
,
254 dev
->vbi_mode
.alt_max_pkt_size
[0],
255 cx231xx_isoc_vbi_copy
);
260 buf
->vb
.state
= VIDEOBUF_PREPARED
;
264 free_buffer(vq
, buf
);
269 vbi_buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
271 struct cx231xx_buffer
*buf
=
272 container_of(vb
, struct cx231xx_buffer
, vb
);
273 struct cx231xx_fh
*fh
= vq
->priv_data
;
274 struct cx231xx
*dev
= fh
->dev
;
275 struct cx231xx_dmaqueue
*vidq
= &dev
->vbi_mode
.vidq
;
277 buf
->vb
.state
= VIDEOBUF_QUEUED
;
278 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
282 static void vbi_buffer_release(struct videobuf_queue
*vq
,
283 struct videobuf_buffer
*vb
)
285 struct cx231xx_buffer
*buf
=
286 container_of(vb
, struct cx231xx_buffer
, vb
);
289 free_buffer(vq
, buf
);
292 struct videobuf_queue_ops cx231xx_vbi_qops
= {
293 .buf_setup
= vbi_buffer_setup
,
294 .buf_prepare
= vbi_buffer_prepare
,
295 .buf_queue
= vbi_buffer_queue
,
296 .buf_release
= vbi_buffer_release
,
299 /* ------------------------------------------------------------------
301 ------------------------------------------------------------------*/
304 * IRQ callback, called by URB callback
306 static void cx231xx_irq_vbi_callback(struct urb
*urb
)
308 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
309 struct cx231xx_video_mode
*vmode
=
310 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
311 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, vbi_mode
);
314 switch (urb
->status
) {
315 case 0: /* success */
316 case -ETIMEDOUT
: /* NAK */
318 case -ECONNRESET
: /* kill */
323 cx231xx_err(DRIVER_NAME
"urb completition error %d.\n",
328 /* Copy data from URB */
329 spin_lock(&dev
->vbi_mode
.slock
);
330 rc
= dev
->vbi_mode
.isoc_ctl
.isoc_copy(dev
, urb
);
331 spin_unlock(&dev
->vbi_mode
.slock
);
336 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
338 cx231xx_err(DRIVER_NAME
"urb resubmit failed (error=%i)\n",
344 * Stop and Deallocate URBs
346 void cx231xx_uninit_vbi_isoc(struct cx231xx
*dev
)
351 cx231xx_info(DRIVER_NAME
"cx231xx: called cx231xx_uninit_vbi_isoc\n");
353 dev
->vbi_mode
.isoc_ctl
.nfields
= -1;
354 for (i
= 0; i
< dev
->vbi_mode
.isoc_ctl
.num_bufs
; i
++) {
355 urb
= dev
->vbi_mode
.isoc_ctl
.urb
[i
];
357 if (!irqs_disabled())
362 if (dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
]) {
364 kfree(dev
->vbi_mode
.isoc_ctl
.
366 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
] =
370 dev
->vbi_mode
.isoc_ctl
.urb
[i
] = NULL
;
372 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
] = NULL
;
375 kfree(dev
->vbi_mode
.isoc_ctl
.urb
);
376 kfree(dev
->vbi_mode
.isoc_ctl
.transfer_buffer
);
378 dev
->vbi_mode
.isoc_ctl
.urb
= NULL
;
379 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
= NULL
;
380 dev
->vbi_mode
.isoc_ctl
.num_bufs
= 0;
382 cx231xx_capture_start(dev
, 0, Vbi
);
384 EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc
);
387 * Allocate URBs and start IRQ
389 int cx231xx_init_vbi_isoc(struct cx231xx
*dev
, int max_packets
,
390 int num_bufs
, int max_pkt_size
,
391 int (*isoc_copy
) (struct cx231xx
*dev
,
394 struct cx231xx_dmaqueue
*dma_q
= &dev
->vbi_mode
.vidq
;
400 cx231xx_info(DRIVER_NAME
"cx231xx: called cx231xx_prepare_isoc\n");
402 /* De-allocates all pending stuff */
403 cx231xx_uninit_vbi_isoc(dev
);
405 /* clear if any halt */
406 usb_clear_halt(dev
->udev
,
407 usb_rcvbulkpipe(dev
->udev
,
408 dev
->vbi_mode
.end_point_addr
));
410 dev
->vbi_mode
.isoc_ctl
.isoc_copy
= isoc_copy
;
411 dev
->vbi_mode
.isoc_ctl
.num_bufs
= num_bufs
;
413 dma_q
->is_partial_line
= 0;
415 dma_q
->current_field
= -1;
416 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
417 dma_q
->lines_per_field
= ((dev
->norm
& V4L2_STD_625_50
) ?
418 PAL_VBI_LINES
: NTSC_VBI_LINES
);
419 dma_q
->lines_completed
= 0;
420 for (i
= 0; i
< 8; i
++)
421 dma_q
->partial_buf
[i
] = 0;
423 dev
->vbi_mode
.isoc_ctl
.urb
= kzalloc(sizeof(void *) * num_bufs
,
425 if (!dev
->vbi_mode
.isoc_ctl
.urb
) {
426 cx231xx_errdev("cannot alloc memory for usb buffers\n");
430 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
=
431 kzalloc(sizeof(void *) * num_bufs
, 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
);
438 dev
->vbi_mode
.isoc_ctl
.max_pkt_size
= max_pkt_size
;
439 dev
->vbi_mode
.isoc_ctl
.buf
= NULL
;
441 sb_size
= max_packets
* dev
->vbi_mode
.isoc_ctl
.max_pkt_size
;
443 /* allocate urbs and transfer buffers */
444 for (i
= 0; i
< dev
->vbi_mode
.isoc_ctl
.num_bufs
; i
++) {
446 urb
= usb_alloc_urb(0, GFP_KERNEL
);
448 cx231xx_err(DRIVER_NAME
449 ": cannot alloc isoc_ctl.urb %i\n", i
);
450 cx231xx_uninit_vbi_isoc(dev
);
453 dev
->vbi_mode
.isoc_ctl
.urb
[i
] = urb
;
454 urb
->transfer_flags
= 0;
456 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
] =
457 kzalloc(sb_size
, GFP_KERNEL
);
458 if (!dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
]) {
459 cx231xx_err(DRIVER_NAME
460 ": unable to allocate %i bytes for transfer"
461 " buffer %i%s\n", sb_size
, i
,
462 in_interrupt() ? " while in int" : "");
463 cx231xx_uninit_vbi_isoc(dev
);
467 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->vbi_mode
.end_point_addr
);
468 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
469 dev
->vbi_mode
.isoc_ctl
.transfer_buffer
[i
],
470 sb_size
, cx231xx_irq_vbi_callback
, dma_q
);
473 init_waitqueue_head(&dma_q
->wq
);
475 /* submit urbs and enables IRQ */
476 for (i
= 0; i
< dev
->vbi_mode
.isoc_ctl
.num_bufs
; i
++) {
477 rc
= usb_submit_urb(dev
->vbi_mode
.isoc_ctl
.urb
[i
], GFP_ATOMIC
);
479 cx231xx_err(DRIVER_NAME
480 ": submit of urb %i failed (error=%i)\n", i
,
482 cx231xx_uninit_vbi_isoc(dev
);
487 cx231xx_capture_start(dev
, 1, Vbi
);
491 EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc
);
493 u32
cx231xx_get_vbi_line(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
494 u8 sav_eav
, u8
*p_buffer
, u32 buffer_size
)
496 u32 bytes_copied
= 0;
497 int current_field
= -1;
512 if (current_field
< 0)
515 dma_q
->last_sav
= sav_eav
;
518 cx231xx_copy_vbi_line(dev
, dma_q
, p_buffer
, buffer_size
,
525 * Announces that a buffer were filled and request the next
527 static inline void vbi_buffer_filled(struct cx231xx
*dev
,
528 struct cx231xx_dmaqueue
*dma_q
,
529 struct cx231xx_buffer
*buf
)
531 /* Advice that buffer was filled */
532 /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
534 buf
->vb
.state
= VIDEOBUF_DONE
;
535 buf
->vb
.field_count
++;
536 do_gettimeofday(&buf
->vb
.ts
);
538 dev
->vbi_mode
.isoc_ctl
.buf
= NULL
;
540 list_del(&buf
->vb
.queue
);
541 wake_up(&buf
->vb
.done
);
544 u32
cx231xx_copy_vbi_line(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
545 u8
*p_line
, u32 length
, int field_number
)
548 struct cx231xx_buffer
*buf
;
549 u32 _line_size
= dev
->width
* 2;
551 if (dma_q
->current_field
!= field_number
)
552 cx231xx_reset_vbi_buffer(dev
, dma_q
);
554 /* get the buffer pointer */
555 buf
= dev
->vbi_mode
.isoc_ctl
.buf
;
557 /* Remember the field number for next time */
558 dma_q
->current_field
= field_number
;
560 bytes_to_copy
= dma_q
->bytes_left_in_line
;
561 if (bytes_to_copy
> length
)
562 bytes_to_copy
= length
;
564 if (dma_q
->lines_completed
>= dma_q
->lines_per_field
) {
565 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
566 dma_q
->is_partial_line
=
567 (dma_q
->bytes_left_in_line
== 0) ? 0 : 1;
571 dma_q
->is_partial_line
= 1;
573 /* If we don't have a buffer, just return the number of bytes we would
574 have copied if we had a buffer. */
576 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
577 dma_q
->is_partial_line
=
578 (dma_q
->bytes_left_in_line
== 0) ? 0 : 1;
579 return bytes_to_copy
;
582 /* copy the data to video buffer */
583 cx231xx_do_vbi_copy(dev
, dma_q
, p_line
, bytes_to_copy
);
585 dma_q
->pos
+= bytes_to_copy
;
586 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
588 if (dma_q
->bytes_left_in_line
== 0) {
590 dma_q
->bytes_left_in_line
= _line_size
;
591 dma_q
->lines_completed
++;
592 dma_q
->is_partial_line
= 0;
594 if (cx231xx_is_vbi_buffer_done(dev
, dma_q
) && buf
) {
596 vbi_buffer_filled(dev
, dma_q
, buf
);
600 dma_q
->lines_completed
= 0;
604 return bytes_to_copy
;
608 * video-buf generic routine to get the next available buffer
610 static inline void get_next_vbi_buf(struct cx231xx_dmaqueue
*dma_q
,
611 struct cx231xx_buffer
**buf
)
613 struct cx231xx_video_mode
*vmode
=
614 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
615 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, vbi_mode
);
618 if (list_empty(&dma_q
->active
)) {
619 cx231xx_err(DRIVER_NAME
": No active queue to serve\n");
620 dev
->vbi_mode
.isoc_ctl
.buf
= NULL
;
625 /* Get the next buffer */
626 *buf
= list_entry(dma_q
->active
.next
, struct cx231xx_buffer
, vb
.queue
);
628 /* Cleans up buffer - Usefull for testing for frame/URB loss */
629 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
630 memset(outp
, 0, (*buf
)->vb
.size
);
632 dev
->vbi_mode
.isoc_ctl
.buf
= *buf
;
637 void cx231xx_reset_vbi_buffer(struct cx231xx
*dev
,
638 struct cx231xx_dmaqueue
*dma_q
)
640 struct cx231xx_buffer
*buf
;
642 buf
= dev
->vbi_mode
.isoc_ctl
.buf
;
645 /* first try to get the buffer */
646 get_next_vbi_buf(dma_q
, &buf
);
649 dma_q
->current_field
= -1;
652 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
653 dma_q
->lines_completed
= 0;
656 int cx231xx_do_vbi_copy(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
657 u8
*p_buffer
, u32 bytes_to_copy
)
659 u8
*p_out_buffer
= NULL
;
660 u32 current_line_bytes_copied
= 0;
661 struct cx231xx_buffer
*buf
;
662 u32 _line_size
= dev
->width
<< 1;
666 buf
= dev
->vbi_mode
.isoc_ctl
.buf
;
671 p_out_buffer
= videobuf_to_vmalloc(&buf
->vb
);
673 if (dma_q
->bytes_left_in_line
!= _line_size
) {
674 current_line_bytes_copied
=
675 _line_size
- dma_q
->bytes_left_in_line
;
678 offset
= (dma_q
->lines_completed
* _line_size
) +
679 current_line_bytes_copied
;
681 /* prepare destination address */
682 startwrite
= p_out_buffer
+ offset
;
684 lencopy
= dma_q
->bytes_left_in_line
> bytes_to_copy
?
685 bytes_to_copy
: dma_q
->bytes_left_in_line
;
687 memcpy(startwrite
, p_buffer
, lencopy
);
692 u8
cx231xx_is_vbi_buffer_done(struct cx231xx
*dev
,
693 struct cx231xx_dmaqueue
*dma_q
)
697 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
698 PAL_VBI_LINES
: NTSC_VBI_LINES
);
699 return (dma_q
->lines_completed
== height
) ? 1 : 0;