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>
31 #include <linux/slab.h>
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>
40 #include "cx231xx-vbi.h"
42 static inline void print_err_status(struct cx231xx
*dev
, int packet
, int status
)
44 char *errmsg
= "Unknown";
48 errmsg
= "unlinked synchronuously";
51 errmsg
= "unlinked asynchronuously";
54 errmsg
= "Buffer error (overrun)";
57 errmsg
= "Stalled (device not responding)";
60 errmsg
= "Babble (bad cable?)";
63 errmsg
= "Bit-stuff error (bad cable?)";
66 errmsg
= "CRC/Timeout (could be anything)";
69 errmsg
= "Device does not respond";
73 cx231xx_err(DRIVER_NAME
"URB status %d [%s].\n", status
,
76 cx231xx_err(DRIVER_NAME
"URB packet %d, status %d [%s].\n",
77 packet
, status
, errmsg
);
82 * Controls the isoc copy of each urb packet
84 static inline int cx231xx_isoc_vbi_copy(struct cx231xx
*dev
, struct urb
*urb
)
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
)
98 if (urb
->status
< 0) {
99 print_err_status(dev
, -1, urb
->status
);
100 if (urb
->status
== -ENOENT
)
104 /* get buffer pointer and length */
105 p_buffer
= urb
->transfer_buffer
;
106 buffer_size
= urb
->actual_length
;
108 if (buffer_size
> 0) {
111 if (dma_q
->is_partial_line
) {
112 /* Handle the case where we were working on a partial
114 sav_eav
= dma_q
->last_sav
;
116 /* Check for a SAV/EAV overlapping the
119 sav_eav
= cx231xx_find_boundary_SAV_EAV(p_buffer
,
125 /* Get the first line if we have some portion of an SAV/EAV from
126 the last buffer or a partial line */
128 bytes_parsed
+= cx231xx_get_vbi_line(dev
, dma_q
,
129 sav_eav
, /* SAV/EAV */
130 p_buffer
+ bytes_parsed
, /* p_buffer */
131 buffer_size
- bytes_parsed
); /* buffer size */
134 /* Now parse data that is completely in this buffer */
135 dma_q
->is_partial_line
= 0;
137 while (bytes_parsed
< buffer_size
) {
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
); /* bytes used to get SAV/EAV */
145 bytes_parsed
+= bytes_used
;
148 if (sav_eav
&& (bytes_parsed
< buffer_size
)) {
149 bytes_parsed
+= cx231xx_get_vbi_line(dev
,
150 dma_q
, sav_eav
, /* SAV/EAV */
151 p_buffer
+bytes_parsed
, /* p_buffer */
152 buffer_size
-bytes_parsed
);/*buf size*/
156 /* Save the last four bytes of the buffer so we can
157 check the buffer boundary condition next time */
158 memcpy(dma_q
->partial_buf
, p_buffer
+ buffer_size
- 4, 4);
165 /* ------------------------------------------------------------------
167 ------------------------------------------------------------------*/
170 vbi_buffer_setup(struct videobuf_queue
*vq
, unsigned int *count
,
173 struct cx231xx_fh
*fh
= vq
->priv_data
;
174 struct cx231xx
*dev
= fh
->dev
;
177 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
178 PAL_VBI_LINES
: NTSC_VBI_LINES
);
180 *size
= (dev
->width
* height
* 2 * 2);
182 *count
= CX231XX_DEF_VBI_BUF
;
184 if (*count
< CX231XX_MIN_BUF
)
185 *count
= CX231XX_MIN_BUF
;
190 /* This is called *without* dev->slock held; please keep it that way */
191 static void free_buffer(struct videobuf_queue
*vq
, struct cx231xx_buffer
*buf
)
193 struct cx231xx_fh
*fh
= vq
->priv_data
;
194 struct cx231xx
*dev
= fh
->dev
;
195 unsigned long flags
= 0;
199 /* We used to wait for the buffer to finish here, but this didn't work
200 because, as we were keeping the state as VIDEOBUF_QUEUED,
201 videobuf_queue_cancel marked it as finished for us.
202 (Also, it could wedge forever if the hardware was misconfigured.)
204 This should be safe; by the time we get here, the buffer isn't
205 queued anymore. If we ever start marking the buffers as
206 VIDEOBUF_ACTIVE, it won't be, though.
208 spin_lock_irqsave(&dev
->vbi_mode
.slock
, flags
);
209 if (dev
->vbi_mode
.bulk_ctl
.buf
== buf
)
210 dev
->vbi_mode
.bulk_ctl
.buf
= NULL
;
211 spin_unlock_irqrestore(&dev
->vbi_mode
.slock
, flags
);
213 videobuf_vmalloc_free(&buf
->vb
);
214 buf
->vb
.state
= VIDEOBUF_NEEDS_INIT
;
218 vbi_buffer_prepare(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
,
219 enum v4l2_field field
)
221 struct cx231xx_fh
*fh
= vq
->priv_data
;
222 struct cx231xx_buffer
*buf
=
223 container_of(vb
, struct cx231xx_buffer
, vb
);
224 struct cx231xx
*dev
= fh
->dev
;
225 int rc
= 0, urb_init
= 0;
228 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
229 PAL_VBI_LINES
: NTSC_VBI_LINES
);
230 buf
->vb
.size
= ((dev
->width
<< 1) * height
* 2);
232 if (0 != buf
->vb
.baddr
&& buf
->vb
.bsize
< buf
->vb
.size
)
235 buf
->vb
.width
= dev
->width
;
236 buf
->vb
.height
= height
;
237 buf
->vb
.field
= field
;
238 buf
->vb
.field
= V4L2_FIELD_SEQ_TB
;
240 if (VIDEOBUF_NEEDS_INIT
== buf
->vb
.state
) {
241 rc
= videobuf_iolock(vq
, &buf
->vb
, NULL
);
246 if (!dev
->vbi_mode
.bulk_ctl
.num_bufs
)
250 rc
= cx231xx_init_vbi_isoc(dev
, CX231XX_NUM_VBI_PACKETS
,
251 CX231XX_NUM_VBI_BUFS
,
252 dev
->vbi_mode
.alt_max_pkt_size
[0],
253 cx231xx_isoc_vbi_copy
);
258 buf
->vb
.state
= VIDEOBUF_PREPARED
;
262 free_buffer(vq
, buf
);
267 vbi_buffer_queue(struct videobuf_queue
*vq
, struct videobuf_buffer
*vb
)
269 struct cx231xx_buffer
*buf
=
270 container_of(vb
, struct cx231xx_buffer
, vb
);
271 struct cx231xx_fh
*fh
= vq
->priv_data
;
272 struct cx231xx
*dev
= fh
->dev
;
273 struct cx231xx_dmaqueue
*vidq
= &dev
->vbi_mode
.vidq
;
275 buf
->vb
.state
= VIDEOBUF_QUEUED
;
276 list_add_tail(&buf
->vb
.queue
, &vidq
->active
);
280 static void vbi_buffer_release(struct videobuf_queue
*vq
,
281 struct videobuf_buffer
*vb
)
283 struct cx231xx_buffer
*buf
=
284 container_of(vb
, struct cx231xx_buffer
, vb
);
287 free_buffer(vq
, buf
);
290 struct videobuf_queue_ops cx231xx_vbi_qops
= {
291 .buf_setup
= vbi_buffer_setup
,
292 .buf_prepare
= vbi_buffer_prepare
,
293 .buf_queue
= vbi_buffer_queue
,
294 .buf_release
= vbi_buffer_release
,
297 /* ------------------------------------------------------------------
299 ------------------------------------------------------------------*/
302 * IRQ callback, called by URB callback
304 static void cx231xx_irq_vbi_callback(struct urb
*urb
)
306 struct cx231xx_dmaqueue
*dma_q
= urb
->context
;
307 struct cx231xx_video_mode
*vmode
=
308 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
309 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, vbi_mode
);
311 switch (urb
->status
) {
312 case 0: /* success */
313 case -ETIMEDOUT
: /* NAK */
315 case -ECONNRESET
: /* kill */
320 cx231xx_err(DRIVER_NAME
"urb completition error %d.\n",
325 /* Copy data from URB */
326 spin_lock(&dev
->vbi_mode
.slock
);
327 dev
->vbi_mode
.bulk_ctl
.bulk_copy(dev
, urb
);
328 spin_unlock(&dev
->vbi_mode
.slock
);
333 urb
->status
= usb_submit_urb(urb
, GFP_ATOMIC
);
335 cx231xx_err(DRIVER_NAME
"urb resubmit failed (error=%i)\n",
341 * Stop and Deallocate URBs
343 void cx231xx_uninit_vbi_isoc(struct cx231xx
*dev
)
348 cx231xx_info(DRIVER_NAME
"cx231xx: called cx231xx_uninit_vbi_isoc\n");
350 dev
->vbi_mode
.bulk_ctl
.nfields
= -1;
351 for (i
= 0; i
< dev
->vbi_mode
.bulk_ctl
.num_bufs
; i
++) {
352 urb
= dev
->vbi_mode
.bulk_ctl
.urb
[i
];
354 if (!irqs_disabled())
359 if (dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
]) {
361 kfree(dev
->vbi_mode
.bulk_ctl
.
363 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
] =
367 dev
->vbi_mode
.bulk_ctl
.urb
[i
] = NULL
;
369 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
] = NULL
;
372 kfree(dev
->vbi_mode
.bulk_ctl
.urb
);
373 kfree(dev
->vbi_mode
.bulk_ctl
.transfer_buffer
);
375 dev
->vbi_mode
.bulk_ctl
.urb
= NULL
;
376 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
= NULL
;
377 dev
->vbi_mode
.bulk_ctl
.num_bufs
= 0;
379 cx231xx_capture_start(dev
, 0, Vbi
);
381 EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc
);
384 * Allocate URBs and start IRQ
386 int cx231xx_init_vbi_isoc(struct cx231xx
*dev
, int max_packets
,
387 int num_bufs
, int max_pkt_size
,
388 int (*bulk_copy
) (struct cx231xx
*dev
,
391 struct cx231xx_dmaqueue
*dma_q
= &dev
->vbi_mode
.vidq
;
397 cx231xx_info(DRIVER_NAME
"cx231xx: called cx231xx_prepare_isoc\n");
399 /* De-allocates all pending stuff */
400 cx231xx_uninit_vbi_isoc(dev
);
402 /* clear if any halt */
403 usb_clear_halt(dev
->udev
,
404 usb_rcvbulkpipe(dev
->udev
,
405 dev
->vbi_mode
.end_point_addr
));
407 dev
->vbi_mode
.bulk_ctl
.bulk_copy
= bulk_copy
;
408 dev
->vbi_mode
.bulk_ctl
.num_bufs
= num_bufs
;
410 dma_q
->is_partial_line
= 0;
412 dma_q
->current_field
= -1;
413 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
414 dma_q
->lines_per_field
= ((dev
->norm
& V4L2_STD_625_50
) ?
415 PAL_VBI_LINES
: NTSC_VBI_LINES
);
416 dma_q
->lines_completed
= 0;
417 for (i
= 0; i
< 8; i
++)
418 dma_q
->partial_buf
[i
] = 0;
420 dev
->vbi_mode
.bulk_ctl
.urb
= kzalloc(sizeof(void *) * num_bufs
,
422 if (!dev
->vbi_mode
.bulk_ctl
.urb
) {
423 cx231xx_errdev("cannot alloc memory for usb buffers\n");
427 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
=
428 kzalloc(sizeof(void *) * num_bufs
, GFP_KERNEL
);
429 if (!dev
->vbi_mode
.bulk_ctl
.transfer_buffer
) {
430 cx231xx_errdev("cannot allocate memory for usbtransfer\n");
431 kfree(dev
->vbi_mode
.bulk_ctl
.urb
);
435 dev
->vbi_mode
.bulk_ctl
.max_pkt_size
= max_pkt_size
;
436 dev
->vbi_mode
.bulk_ctl
.buf
= NULL
;
438 sb_size
= max_packets
* dev
->vbi_mode
.bulk_ctl
.max_pkt_size
;
440 /* allocate urbs and transfer buffers */
441 for (i
= 0; i
< dev
->vbi_mode
.bulk_ctl
.num_bufs
; i
++) {
443 urb
= usb_alloc_urb(0, GFP_KERNEL
);
445 cx231xx_err(DRIVER_NAME
446 ": cannot alloc bulk_ctl.urb %i\n", i
);
447 cx231xx_uninit_vbi_isoc(dev
);
450 dev
->vbi_mode
.bulk_ctl
.urb
[i
] = urb
;
451 urb
->transfer_flags
= 0;
453 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
] =
454 kzalloc(sb_size
, GFP_KERNEL
);
455 if (!dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
]) {
456 cx231xx_err(DRIVER_NAME
457 ": unable to allocate %i bytes for transfer"
458 " buffer %i%s\n", sb_size
, i
,
459 in_interrupt() ? " while in int" : "");
460 cx231xx_uninit_vbi_isoc(dev
);
464 pipe
= usb_rcvbulkpipe(dev
->udev
, dev
->vbi_mode
.end_point_addr
);
465 usb_fill_bulk_urb(urb
, dev
->udev
, pipe
,
466 dev
->vbi_mode
.bulk_ctl
.transfer_buffer
[i
],
467 sb_size
, cx231xx_irq_vbi_callback
, dma_q
);
470 init_waitqueue_head(&dma_q
->wq
);
472 /* submit urbs and enables IRQ */
473 for (i
= 0; i
< dev
->vbi_mode
.bulk_ctl
.num_bufs
; i
++) {
474 rc
= usb_submit_urb(dev
->vbi_mode
.bulk_ctl
.urb
[i
], GFP_ATOMIC
);
476 cx231xx_err(DRIVER_NAME
477 ": submit of urb %i failed (error=%i)\n", i
,
479 cx231xx_uninit_vbi_isoc(dev
);
484 cx231xx_capture_start(dev
, 1, Vbi
);
488 EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc
);
490 u32
cx231xx_get_vbi_line(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
491 u8 sav_eav
, u8
*p_buffer
, u32 buffer_size
)
493 u32 bytes_copied
= 0;
494 int current_field
= -1;
509 if (current_field
< 0)
512 dma_q
->last_sav
= sav_eav
;
515 cx231xx_copy_vbi_line(dev
, dma_q
, p_buffer
, buffer_size
,
522 * Announces that a buffer were filled and request the next
524 static inline void vbi_buffer_filled(struct cx231xx
*dev
,
525 struct cx231xx_dmaqueue
*dma_q
,
526 struct cx231xx_buffer
*buf
)
528 /* Advice that buffer was filled */
529 /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
531 buf
->vb
.state
= VIDEOBUF_DONE
;
532 buf
->vb
.field_count
++;
533 do_gettimeofday(&buf
->vb
.ts
);
535 dev
->vbi_mode
.bulk_ctl
.buf
= NULL
;
537 list_del(&buf
->vb
.queue
);
538 wake_up(&buf
->vb
.done
);
541 u32
cx231xx_copy_vbi_line(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
542 u8
*p_line
, u32 length
, int field_number
)
545 struct cx231xx_buffer
*buf
;
546 u32 _line_size
= dev
->width
* 2;
548 if (dma_q
->current_field
== -1) {
549 /* Just starting up */
550 cx231xx_reset_vbi_buffer(dev
, dma_q
);
553 if (dma_q
->current_field
!= field_number
)
554 dma_q
->lines_completed
= 0;
556 /* get the buffer pointer */
557 buf
= dev
->vbi_mode
.bulk_ctl
.buf
;
559 /* Remember the field number for next time */
560 dma_q
->current_field
= field_number
;
562 bytes_to_copy
= dma_q
->bytes_left_in_line
;
563 if (bytes_to_copy
> length
)
564 bytes_to_copy
= length
;
566 if (dma_q
->lines_completed
>= dma_q
->lines_per_field
) {
567 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
568 dma_q
->is_partial_line
=
569 (dma_q
->bytes_left_in_line
== 0) ? 0 : 1;
573 dma_q
->is_partial_line
= 1;
575 /* If we don't have a buffer, just return the number of bytes we would
576 have copied if we had a buffer. */
578 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
579 dma_q
->is_partial_line
=
580 (dma_q
->bytes_left_in_line
== 0) ? 0 : 1;
581 return bytes_to_copy
;
584 /* copy the data to video buffer */
585 cx231xx_do_vbi_copy(dev
, dma_q
, p_line
, bytes_to_copy
);
587 dma_q
->pos
+= bytes_to_copy
;
588 dma_q
->bytes_left_in_line
-= bytes_to_copy
;
590 if (dma_q
->bytes_left_in_line
== 0) {
592 dma_q
->bytes_left_in_line
= _line_size
;
593 dma_q
->lines_completed
++;
594 dma_q
->is_partial_line
= 0;
596 if (cx231xx_is_vbi_buffer_done(dev
, dma_q
) && buf
) {
598 vbi_buffer_filled(dev
, dma_q
, buf
);
601 dma_q
->lines_completed
= 0;
602 cx231xx_reset_vbi_buffer(dev
, dma_q
);
606 return bytes_to_copy
;
610 * video-buf generic routine to get the next available buffer
612 static inline void get_next_vbi_buf(struct cx231xx_dmaqueue
*dma_q
,
613 struct cx231xx_buffer
**buf
)
615 struct cx231xx_video_mode
*vmode
=
616 container_of(dma_q
, struct cx231xx_video_mode
, vidq
);
617 struct cx231xx
*dev
= container_of(vmode
, struct cx231xx
, vbi_mode
);
620 if (list_empty(&dma_q
->active
)) {
621 cx231xx_err(DRIVER_NAME
": No active queue to serve\n");
622 dev
->vbi_mode
.bulk_ctl
.buf
= NULL
;
627 /* Get the next buffer */
628 *buf
= list_entry(dma_q
->active
.next
, struct cx231xx_buffer
, vb
.queue
);
630 /* Cleans up buffer - Useful for testing for frame/URB loss */
631 outp
= videobuf_to_vmalloc(&(*buf
)->vb
);
632 memset(outp
, 0, (*buf
)->vb
.size
);
634 dev
->vbi_mode
.bulk_ctl
.buf
= *buf
;
639 void cx231xx_reset_vbi_buffer(struct cx231xx
*dev
,
640 struct cx231xx_dmaqueue
*dma_q
)
642 struct cx231xx_buffer
*buf
;
644 buf
= dev
->vbi_mode
.bulk_ctl
.buf
;
647 /* first try to get the buffer */
648 get_next_vbi_buf(dma_q
, &buf
);
651 dma_q
->current_field
= -1;
654 dma_q
->bytes_left_in_line
= dev
->width
<< 1;
655 dma_q
->lines_completed
= 0;
658 int cx231xx_do_vbi_copy(struct cx231xx
*dev
, struct cx231xx_dmaqueue
*dma_q
,
659 u8
*p_buffer
, u32 bytes_to_copy
)
661 u8
*p_out_buffer
= NULL
;
662 u32 current_line_bytes_copied
= 0;
663 struct cx231xx_buffer
*buf
;
664 u32 _line_size
= dev
->width
<< 1;
668 buf
= dev
->vbi_mode
.bulk_ctl
.buf
;
673 p_out_buffer
= videobuf_to_vmalloc(&buf
->vb
);
675 if (dma_q
->bytes_left_in_line
!= _line_size
) {
676 current_line_bytes_copied
=
677 _line_size
- dma_q
->bytes_left_in_line
;
680 offset
= (dma_q
->lines_completed
* _line_size
) +
681 current_line_bytes_copied
;
683 if (dma_q
->current_field
== 2) {
684 /* Populate the second half of the frame */
685 offset
+= (dev
->width
* 2 * dma_q
->lines_per_field
);
688 /* prepare destination address */
689 startwrite
= p_out_buffer
+ offset
;
691 lencopy
= dma_q
->bytes_left_in_line
> bytes_to_copy
?
692 bytes_to_copy
: dma_q
->bytes_left_in_line
;
694 memcpy(startwrite
, p_buffer
, lencopy
);
699 u8
cx231xx_is_vbi_buffer_done(struct cx231xx
*dev
,
700 struct cx231xx_dmaqueue
*dma_q
)
704 height
= ((dev
->norm
& V4L2_STD_625_50
) ?
705 PAL_VBI_LINES
: NTSC_VBI_LINES
);
706 if (dma_q
->lines_completed
== height
&& dma_q
->current_field
== 2)