2 * usbvision-core.c - driver for NT100x USB video capture devices
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
6 * Dwaine Garden <dwainegarden@rogers.com>
8 * This module is part of usbvision driver project.
9 * Updates to driver completed by Dwaine P. Garden
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/list.h>
29 #include <linux/timer.h>
30 #include <linux/slab.h>
32 #include <linux/utsname.h>
33 #include <linux/highmem.h>
34 #include <linux/smp_lock.h>
35 #include <linux/videodev.h>
36 #include <linux/vmalloc.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/spinlock.h>
41 #include <linux/videodev2.h>
42 #include <linux/video_decoder.h>
43 #include <linux/i2c.h>
45 #include <media/saa7115.h>
46 #include <media/v4l2-common.h>
47 #include <media/tuner.h>
48 #include <media/audiochip.h>
50 #include <linux/moduleparam.h>
51 #include <linux/workqueue.h>
54 #include <linux/kmod.h>
57 #include "usbvision.h"
59 static unsigned int core_debug
= 0;
60 module_param(core_debug
,int,0644);
61 MODULE_PARM_DESC(core_debug
,"enable debug messages [core]");
63 static unsigned int force_testpattern
= 0;
64 module_param(force_testpattern
,int,0644);
65 MODULE_PARM_DESC(force_testpattern
,"enable test pattern display [core]");
67 static int adjustCompression
= 1; // Set the compression to be adaptive
68 module_param(adjustCompression
, int, 0444);
69 MODULE_PARM_DESC(adjustCompression
, " Set the ADPCM compression for the device. Default: 1 (On)");
71 static int SwitchSVideoInput
= 0; // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
72 module_param(SwitchSVideoInput
, int, 0444);
73 MODULE_PARM_DESC(SwitchSVideoInput
, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
75 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
78 #ifdef USBVISION_DEBUG
79 #define PDEBUG(level, fmt, args...) \
80 if (core_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
82 #define PDEBUG(level, fmt, args...) do {} while(0)
85 #define DBG_HEADER 1<<0
88 #define DBG_PARSE 1<<3
89 #define DBG_SCRATCH 1<<4
92 static const int max_imgwidth
= MAX_FRAME_WIDTH
;
93 static const int max_imgheight
= MAX_FRAME_HEIGHT
;
94 static const int min_imgwidth
= MIN_FRAME_WIDTH
;
95 static const int min_imgheight
= MIN_FRAME_HEIGHT
;
97 /* The value of 'scratch_buf_size' affects quality of the picture
98 * in many ways. Shorter buffers may cause loss of data when client
99 * is too slow. Larger buffers are memory-consuming and take longer
100 * to work with. This setting can be adjusted, but the default value
101 * should be OK for most desktop users.
103 #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
104 static const int scratch_buf_size
= DEFAULT_SCRATCH_BUF_SIZE
;
106 // Function prototypes
107 static int usbvision_request_intra (struct usb_usbvision
*usbvision
);
108 static int usbvision_unrequest_intra (struct usb_usbvision
*usbvision
);
109 static int usbvision_adjust_compression (struct usb_usbvision
*usbvision
);
110 static int usbvision_measure_bandwidth (struct usb_usbvision
*usbvision
);
112 /*******************************/
113 /* Memory management functions */
114 /*******************************/
117 * Here we want the physical address of the memory.
118 * This is used when initializing the contents of the area.
121 static void *usbvision_rvmalloc(unsigned long size
)
126 size
= PAGE_ALIGN(size
);
127 mem
= vmalloc_32(size
);
131 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
132 adr
= (unsigned long) mem
;
134 SetPageReserved(vmalloc_to_page((void *)adr
));
142 void usbvision_rvfree(void *mem
, unsigned long size
)
149 size
= PAGE_ALIGN(size
);
151 adr
= (unsigned long) mem
;
152 while ((long) size
> 0) {
153 ClearPageReserved(vmalloc_to_page((void *)adr
));
164 static void usbvision_hexdump(const unsigned char *data
, int len
)
169 for (i
= k
= 0; len
> 0; i
++, len
--) {
170 if (i
> 0 && (i
% 16 == 0)) {
174 k
+= sprintf(&tmp
[k
], "%02x ", data
[i
]);
181 /********************************
182 * scratch ring buffer handling
183 ********************************/
184 static int scratch_len(struct usb_usbvision
*usbvision
) /*This returns the amount of data actually in the buffer */
186 int len
= usbvision
->scratch_write_ptr
- usbvision
->scratch_read_ptr
;
188 len
+= scratch_buf_size
;
190 PDEBUG(DBG_SCRATCH
, "scratch_len() = %d\n", len
);
196 /* This returns the free space left in the buffer */
197 static int scratch_free(struct usb_usbvision
*usbvision
)
199 int free
= usbvision
->scratch_read_ptr
- usbvision
->scratch_write_ptr
;
201 free
+= scratch_buf_size
;
204 free
-= 1; /* at least one byte in the buffer must */
205 /* left blank, otherwise there is no chance to differ between full and empty */
207 PDEBUG(DBG_SCRATCH
, "return %d\n", free
);
213 /* This puts data into the buffer */
214 static int scratch_put(struct usb_usbvision
*usbvision
, unsigned char *data
,
219 if (usbvision
->scratch_write_ptr
+ len
< scratch_buf_size
) {
220 memcpy(usbvision
->scratch
+ usbvision
->scratch_write_ptr
, data
, len
);
221 usbvision
->scratch_write_ptr
+= len
;
224 len_part
= scratch_buf_size
- usbvision
->scratch_write_ptr
;
225 memcpy(usbvision
->scratch
+ usbvision
->scratch_write_ptr
, data
, len_part
);
226 if (len
== len_part
) {
227 usbvision
->scratch_write_ptr
= 0; /* just set write_ptr to zero */
230 memcpy(usbvision
->scratch
, data
+ len_part
, len
- len_part
);
231 usbvision
->scratch_write_ptr
= len
- len_part
;
235 PDEBUG(DBG_SCRATCH
, "len=%d, new write_ptr=%d\n", len
, usbvision
->scratch_write_ptr
);
240 /* This marks the write_ptr as position of new frame header */
241 static void scratch_mark_header(struct usb_usbvision
*usbvision
)
243 PDEBUG(DBG_SCRATCH
, "header at write_ptr=%d\n", usbvision
->scratch_headermarker_write_ptr
);
245 usbvision
->scratch_headermarker
[usbvision
->scratch_headermarker_write_ptr
] =
246 usbvision
->scratch_write_ptr
;
247 usbvision
->scratch_headermarker_write_ptr
+= 1;
248 usbvision
->scratch_headermarker_write_ptr
%= USBVISION_NUM_HEADERMARKER
;
251 /* This gets data from the buffer at the given "ptr" position */
252 static int scratch_get_extra(struct usb_usbvision
*usbvision
,
253 unsigned char *data
, int *ptr
, int len
)
256 if (*ptr
+ len
< scratch_buf_size
) {
257 memcpy(data
, usbvision
->scratch
+ *ptr
, len
);
261 len_part
= scratch_buf_size
- *ptr
;
262 memcpy(data
, usbvision
->scratch
+ *ptr
, len_part
);
263 if (len
== len_part
) {
264 *ptr
= 0; /* just set the y_ptr to zero */
267 memcpy(data
+ len_part
, usbvision
->scratch
, len
- len_part
);
268 *ptr
= len
- len_part
;
272 PDEBUG(DBG_SCRATCH
, "len=%d, new ptr=%d\n", len
, *ptr
);
278 /* This sets the scratch extra read pointer */
279 static void scratch_set_extra_ptr(struct usb_usbvision
*usbvision
, int *ptr
,
282 *ptr
= (usbvision
->scratch_read_ptr
+ len
)%scratch_buf_size
;
284 PDEBUG(DBG_SCRATCH
, "ptr=%d\n", *ptr
);
288 /*This increments the scratch extra read pointer */
289 static void scratch_inc_extra_ptr(int *ptr
, int len
)
291 *ptr
= (*ptr
+ len
) % scratch_buf_size
;
293 PDEBUG(DBG_SCRATCH
, "ptr=%d\n", *ptr
);
297 /* This gets data from the buffer */
298 static int scratch_get(struct usb_usbvision
*usbvision
, unsigned char *data
,
302 if (usbvision
->scratch_read_ptr
+ len
< scratch_buf_size
) {
303 memcpy(data
, usbvision
->scratch
+ usbvision
->scratch_read_ptr
, len
);
304 usbvision
->scratch_read_ptr
+= len
;
307 len_part
= scratch_buf_size
- usbvision
->scratch_read_ptr
;
308 memcpy(data
, usbvision
->scratch
+ usbvision
->scratch_read_ptr
, len_part
);
309 if (len
== len_part
) {
310 usbvision
->scratch_read_ptr
= 0; /* just set the read_ptr to zero */
313 memcpy(data
+ len_part
, usbvision
->scratch
, len
- len_part
);
314 usbvision
->scratch_read_ptr
= len
- len_part
;
318 PDEBUG(DBG_SCRATCH
, "len=%d, new read_ptr=%d\n", len
, usbvision
->scratch_read_ptr
);
324 /* This sets read pointer to next header and returns it */
325 static int scratch_get_header(struct usb_usbvision
*usbvision
,
326 struct usbvision_frame_header
*header
)
330 PDEBUG(DBG_SCRATCH
, "from read_ptr=%d", usbvision
->scratch_headermarker_read_ptr
);
332 while (usbvision
->scratch_headermarker_write_ptr
-
333 usbvision
->scratch_headermarker_read_ptr
!= 0) {
334 usbvision
->scratch_read_ptr
=
335 usbvision
->scratch_headermarker
[usbvision
->scratch_headermarker_read_ptr
];
336 usbvision
->scratch_headermarker_read_ptr
+= 1;
337 usbvision
->scratch_headermarker_read_ptr
%= USBVISION_NUM_HEADERMARKER
;
338 scratch_get(usbvision
, (unsigned char *)header
, USBVISION_HEADER_LENGTH
);
339 if ((header
->magic_1
== USBVISION_MAGIC_1
)
340 && (header
->magic_2
== USBVISION_MAGIC_2
)
341 && (header
->headerLength
== USBVISION_HEADER_LENGTH
)) {
342 errCode
= USBVISION_HEADER_LENGTH
;
343 header
->frameWidth
= header
->frameWidthLo
+ (header
->frameWidthHi
<< 8);
344 header
->frameHeight
= header
->frameHeightLo
+ (header
->frameHeightHi
<< 8);
353 /*This removes len bytes of old data from the buffer */
354 static void scratch_rm_old(struct usb_usbvision
*usbvision
, int len
)
357 usbvision
->scratch_read_ptr
+= len
;
358 usbvision
->scratch_read_ptr
%= scratch_buf_size
;
359 PDEBUG(DBG_SCRATCH
, "read_ptr is now %d\n", usbvision
->scratch_read_ptr
);
363 /*This resets the buffer - kills all data in it too */
364 static void scratch_reset(struct usb_usbvision
*usbvision
)
366 PDEBUG(DBG_SCRATCH
, "\n");
368 usbvision
->scratch_read_ptr
= 0;
369 usbvision
->scratch_write_ptr
= 0;
370 usbvision
->scratch_headermarker_read_ptr
= 0;
371 usbvision
->scratch_headermarker_write_ptr
= 0;
372 usbvision
->isocstate
= IsocState_NoFrame
;
375 int usbvision_scratch_alloc(struct usb_usbvision
*usbvision
)
377 usbvision
->scratch
= vmalloc_32(scratch_buf_size
);
378 scratch_reset(usbvision
);
379 if(usbvision
->scratch
== NULL
) {
380 err("%s: unable to allocate %d bytes for scratch",
381 __FUNCTION__
, scratch_buf_size
);
387 void usbvision_scratch_free(struct usb_usbvision
*usbvision
)
389 if (usbvision
->scratch
!= NULL
) {
390 vfree(usbvision
->scratch
);
391 usbvision
->scratch
= NULL
;
396 * usbvision_testpattern()
398 * Procedure forms a test pattern (yellow grid on blue background).
401 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
402 * continues from the current scanline.
403 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
404 * 1: Draw a colored grid
407 static void usbvision_testpattern(struct usb_usbvision
*usbvision
,
408 int fullframe
, int pmode
)
410 static const char proc
[] = "usbvision_testpattern";
411 struct usbvision_frame
*frame
;
415 static int num_pass
= 0;
417 if (usbvision
== NULL
) {
418 printk(KERN_ERR
"%s: usbvision == NULL\n", proc
);
421 if (usbvision
->curFrame
== NULL
) {
422 printk(KERN_ERR
"%s: usbvision->curFrame is NULL.\n", proc
);
426 /* Grab the current frame */
427 frame
= usbvision
->curFrame
;
429 /* Optionally start at the beginning */
432 frame
->scanlength
= 0;
435 /* Form every scan line */
436 for (; frame
->curline
< frame
->frmheight
; frame
->curline
++) {
439 f
= frame
->data
+ (usbvision
->curwidth
* 3 * frame
->curline
);
440 for (i
= 0; i
< usbvision
->curwidth
; i
++) {
441 unsigned char cb
= 0x80;
442 unsigned char cg
= 0;
443 unsigned char cr
= 0;
446 if (frame
->curline
% 32 == 0)
447 cb
= 0, cg
= cr
= 0xFF;
448 else if (i
% 32 == 0) {
449 if (frame
->curline
% 32 == 1)
451 cb
= 0, cg
= cr
= 0xFF;
458 num_pass
* 2) & 0xFF;
461 num_pass
* 3) & 0xFF;
464 /* Just the blue screen */
474 frame
->grabstate
= FrameState_Done
;
475 frame
->scanlength
+= scan_length
;
481 * usbvision_decompress_alloc()
483 * allocates intermediate buffer for decompression
485 int usbvision_decompress_alloc(struct usb_usbvision
*usbvision
)
487 int IFB_size
= MAX_FRAME_WIDTH
* MAX_FRAME_HEIGHT
* 3 / 2;
488 usbvision
->IntraFrameBuffer
= vmalloc_32(IFB_size
);
489 if (usbvision
->IntraFrameBuffer
== NULL
) {
490 err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__
, IFB_size
);
497 * usbvision_decompress_free()
499 * frees intermediate buffer for decompression
501 void usbvision_decompress_free(struct usb_usbvision
*usbvision
)
503 if (usbvision
->IntraFrameBuffer
!= NULL
) {
504 vfree(usbvision
->IntraFrameBuffer
);
505 usbvision
->IntraFrameBuffer
= NULL
;
509 /************************************************************
510 * Here comes the data parsing stuff that is run as interrupt
511 ************************************************************/
513 * usbvision_find_header()
515 * Locate one of supported header markers in the scratch buffer.
517 static enum ParseState
usbvision_find_header(struct usb_usbvision
*usbvision
)
519 struct usbvision_frame
*frame
;
522 frame
= usbvision
->curFrame
;
524 while (scratch_get_header(usbvision
, &frame
->isocHeader
) == USBVISION_HEADER_LENGTH
) {
525 // found header in scratch
526 PDEBUG(DBG_HEADER
, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
527 frame
->isocHeader
.magic_2
,
528 frame
->isocHeader
.magic_1
,
529 frame
->isocHeader
.headerLength
,
530 frame
->isocHeader
.frameNum
,
531 frame
->isocHeader
.framePhase
,
532 frame
->isocHeader
.frameLatency
,
533 frame
->isocHeader
.dataFormat
,
534 frame
->isocHeader
.formatParam
,
535 frame
->isocHeader
.frameWidth
,
536 frame
->isocHeader
.frameHeight
);
538 if (usbvision
->requestIntra
) {
539 if (frame
->isocHeader
.formatParam
& 0x80) {
541 usbvision
->lastIsocFrameNum
= -1; // do not check for lost frames this time
542 usbvision_unrequest_intra(usbvision
);
553 frame
->frmwidth
= frame
->isocHeader
.frameWidth
* usbvision
->stretch_width
;
554 frame
->frmheight
= frame
->isocHeader
.frameHeight
* usbvision
->stretch_height
;
555 frame
->v4l2_linesize
= (frame
->frmwidth
* frame
->v4l2_format
.depth
)>> 3;
557 else { // no header found
558 PDEBUG(DBG_HEADER
, "skipping scratch data, no header");
559 scratch_reset(usbvision
);
560 return ParseState_EndParse
;
564 if (frame
->isocHeader
.dataFormat
==ISOC_MODE_COMPRESS
) {
565 //check isocHeader.frameNum for lost frames
566 if (usbvision
->lastIsocFrameNum
>= 0) {
567 if (((usbvision
->lastIsocFrameNum
+ 1) % 32) != frame
->isocHeader
.frameNum
) {
568 // unexpected frame drop: need to request new intra frame
569 PDEBUG(DBG_HEADER
, "Lost frame before %d on USB", frame
->isocHeader
.frameNum
);
570 usbvision_request_intra(usbvision
);
571 return ParseState_NextFrame
;
574 usbvision
->lastIsocFrameNum
= frame
->isocHeader
.frameNum
;
576 usbvision
->header_count
++;
577 frame
->scanstate
= ScanState_Lines
;
580 if (force_testpattern
) {
581 usbvision_testpattern(usbvision
, 1, 1);
582 return ParseState_NextFrame
;
584 return ParseState_Continue
;
587 static enum ParseState
usbvision_parse_lines_422(struct usb_usbvision
*usbvision
,
590 volatile struct usbvision_frame
*frame
;
594 unsigned char yuyv
[4]={180, 128, 10, 128}; // YUV components
595 unsigned char rv
, gv
, bv
; // RGB components
596 int clipmask_index
, bytes_per_pixel
;
597 int stretch_bytes
, clipmask_add
;
599 frame
= usbvision
->curFrame
;
600 f
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
602 /* Make sure there's enough data for the entire line */
603 len
= (frame
->isocHeader
.frameWidth
* 2)+5;
604 if (scratch_len(usbvision
) < len
) {
605 PDEBUG(DBG_PARSE
, "out of data in line %d, need %u.\n", frame
->curline
, len
);
606 return ParseState_Out
;
609 if ((frame
->curline
+ 1) >= frame
->frmheight
) {
610 return ParseState_NextFrame
;
613 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
614 stretch_bytes
= (usbvision
->stretch_width
- 1) * bytes_per_pixel
;
615 clipmask_index
= frame
->curline
* MAX_FRAME_WIDTH
;
616 clipmask_add
= usbvision
->stretch_width
;
618 for (i
= 0; i
< frame
->frmwidth
; i
+=(2 * usbvision
->stretch_width
)) {
620 scratch_get(usbvision
, &yuyv
[0], 4);
622 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
628 YUV_TO_RGB_BY_THE_BOOK(yuyv
[0], yuyv
[1], yuyv
[3], rv
, gv
, bv
);
629 switch (frame
->v4l2_format
.format
) {
630 case V4L2_PIX_FMT_RGB565
:
631 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 3));
632 *f
++ = (0x07 & (gv
>> 5)) | (0xF8 & rv
);
634 case V4L2_PIX_FMT_RGB24
:
639 case V4L2_PIX_FMT_RGB32
:
645 case V4L2_PIX_FMT_RGB555
:
646 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 2));
647 *f
++ = (0x03 & (gv
>> 6)) | (0x7C & (rv
>> 1));
651 clipmask_index
+= clipmask_add
;
654 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
660 YUV_TO_RGB_BY_THE_BOOK(yuyv
[2], yuyv
[1], yuyv
[3], rv
, gv
, bv
);
661 switch (frame
->v4l2_format
.format
) {
662 case V4L2_PIX_FMT_RGB565
:
663 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 3));
664 *f
++ = (0x07 & (gv
>> 5)) | (0xF8 & rv
);
666 case V4L2_PIX_FMT_RGB24
:
671 case V4L2_PIX_FMT_RGB32
:
677 case V4L2_PIX_FMT_RGB555
:
678 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 2));
679 *f
++ = (0x03 & (gv
>> 6)) | (0x7C & (rv
>> 1));
683 clipmask_index
+= clipmask_add
;
687 frame
->curline
+= usbvision
->stretch_height
;
688 *pcopylen
+= frame
->v4l2_linesize
* usbvision
->stretch_height
;
690 if (frame
->curline
>= frame
->frmheight
) {
691 return ParseState_NextFrame
;
694 return ParseState_Continue
;
698 /* The decompression routine */
699 static int usbvision_decompress(struct usb_usbvision
*usbvision
,unsigned char *Compressed
,
700 unsigned char *Decompressed
, int *StartPos
,
701 int *BlockTypeStartPos
, int Len
)
703 int RestPixel
, Idx
, MaxPos
, Pos
, ExtraPos
, BlockLen
, BlockTypePos
, BlockTypeLen
;
704 unsigned char BlockByte
, BlockCode
, BlockType
, BlockTypeByte
, Integrator
;
708 BlockTypePos
= *BlockTypeStartPos
;
709 MaxPos
= 396; //Pos + Len;
719 for (Idx
= 0; Idx
< Len
; Idx
++) {
722 if (BlockTypeLen
==0) {
723 BlockTypeByte
= Compressed
[BlockTypePos
];
727 BlockType
= (BlockTypeByte
& 0xC0) >> 6;
730 usbvision
->ComprBlockTypes
[BlockType
]++;
733 if (BlockType
== 0) {
734 if(RestPixel
>= 24) {
737 Integrator
= Decompressed
[Idx
];
739 Idx
+= RestPixel
- 1;
743 BlockCode
= Compressed
[Pos
];
745 if (RestPixel
>= 24) {
748 BlockLen
= RestPixel
;
750 RestPixel
-= BlockLen
;
751 ExtraPos
= Pos
+ (BlockLen
/ 4);
757 if ((BlockLen
%4) == 0) {
758 BlockByte
= Compressed
[Pos
];
761 if (BlockType
== 1) { //inter Block
762 Integrator
= Decompressed
[Idx
];
764 switch (BlockByte
& 0xC0) {
766 Integrator
+= Compressed
[ExtraPos
];
770 Integrator
+= BlockCode
;
773 Integrator
-= BlockCode
;
776 Decompressed
[Idx
] = Integrator
;
781 *StartPos
= ExtraPos
;
782 *BlockTypeStartPos
= BlockTypePos
;
788 * usbvision_parse_compress()
790 * Parse compressed frame from the scratch buffer, put
791 * decoded RGB value into the current frame buffer and add the written
792 * number of bytes (RGB) to the *pcopylen.
795 static enum ParseState
usbvision_parse_compress(struct usb_usbvision
*usbvision
,
798 #define USBVISION_STRIP_MAGIC 0x5A
799 #define USBVISION_STRIP_LEN_MAX 400
800 #define USBVISION_STRIP_HEADER_LEN 3
802 struct usbvision_frame
*frame
;
803 unsigned char *f
,*u
= NULL
,*v
= NULL
;
804 unsigned char StripData
[USBVISION_STRIP_LEN_MAX
];
805 unsigned char StripHeader
[USBVISION_STRIP_HEADER_LEN
];
806 int Idx
, IdxEnd
, StripLen
, StripPtr
, StartBlockPos
, BlockPos
, BlockTypePos
;
807 int clipmask_index
, bytes_per_pixel
, rc
;
809 unsigned char rv
, gv
, bv
;
810 static unsigned char *Y
, *U
, *V
;
812 frame
= usbvision
->curFrame
;
813 imageSize
= frame
->frmwidth
* frame
->frmheight
;
814 if ( (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUV422P
) ||
815 (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
) ) { // this is a planar format
816 //... v4l2_linesize not used here.
817 f
= frame
->data
+ (frame
->width
* frame
->curline
);
819 f
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
821 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
){ //initialise u and v pointers
822 // get base of u and b planes add halfoffset
826 + (frame
->frmwidth
>>1) * frame
->curline
;
827 v
= u
+ (imageSize
>>1 );
829 } else if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
){
831 v
= frame
->data
+ imageSize
+ ((frame
->curline
* (frame
->width
))>>2) ;
832 u
= v
+ (imageSize
>>2) ;
835 if (frame
->curline
== 0) {
836 usbvision_adjust_compression(usbvision
);
839 if (scratch_len(usbvision
) < USBVISION_STRIP_HEADER_LEN
) {
840 return ParseState_Out
;
843 //get strip header without changing the scratch_read_ptr
844 scratch_set_extra_ptr(usbvision
, &StripPtr
, 0);
845 scratch_get_extra(usbvision
, &StripHeader
[0], &StripPtr
,
846 USBVISION_STRIP_HEADER_LEN
);
848 if (StripHeader
[0] != USBVISION_STRIP_MAGIC
) {
850 usbvision
->stripMagicErrors
++;
851 return ParseState_NextFrame
;
854 if (frame
->curline
!= (int)StripHeader
[2]) {
855 //line number missmatch error
856 usbvision
->stripLineNumberErrors
++;
859 StripLen
= 2 * (unsigned int)StripHeader
[1];
860 if (StripLen
> USBVISION_STRIP_LEN_MAX
) {
862 // I think this never happens
863 usbvision_request_intra(usbvision
);
866 if (scratch_len(usbvision
) < StripLen
) {
867 //there is not enough data for the strip
868 return ParseState_Out
;
871 if (usbvision
->IntraFrameBuffer
) {
872 Y
= usbvision
->IntraFrameBuffer
+ frame
->frmwidth
* frame
->curline
;
873 U
= usbvision
->IntraFrameBuffer
+ imageSize
+ (frame
->frmwidth
/ 2) * (frame
->curline
/ 2);
874 V
= usbvision
->IntraFrameBuffer
+ imageSize
/ 4 * 5 + (frame
->frmwidth
/ 2) * (frame
->curline
/ 2);
877 return ParseState_NextFrame
;
880 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
881 clipmask_index
= frame
->curline
* MAX_FRAME_WIDTH
;
883 scratch_get(usbvision
, StripData
, StripLen
);
885 IdxEnd
= frame
->frmwidth
;
886 BlockTypePos
= USBVISION_STRIP_HEADER_LEN
;
887 StartBlockPos
= BlockTypePos
+ (IdxEnd
- 1) / 96 + (IdxEnd
/ 2 - 1) / 96 + 2;
888 BlockPos
= StartBlockPos
;
890 usbvision
->BlockPos
= BlockPos
;
892 if ((rc
= usbvision_decompress(usbvision
, StripData
, Y
, &BlockPos
, &BlockTypePos
, IdxEnd
)) != IdxEnd
) {
893 //return ParseState_Continue;
895 if (StripLen
> usbvision
->maxStripLen
) {
896 usbvision
->maxStripLen
= StripLen
;
899 if (frame
->curline
%2) {
900 if ((rc
= usbvision_decompress(usbvision
, StripData
, V
, &BlockPos
, &BlockTypePos
, IdxEnd
/2)) != IdxEnd
/2) {
901 //return ParseState_Continue;
905 if ((rc
= usbvision_decompress(usbvision
, StripData
, U
, &BlockPos
, &BlockTypePos
, IdxEnd
/2)) != IdxEnd
/2) {
906 //return ParseState_Continue;
910 if (BlockPos
> usbvision
->comprBlockPos
) {
911 usbvision
->comprBlockPos
= BlockPos
;
913 if (BlockPos
> StripLen
) {
914 usbvision
->stripLenErrors
++;
917 for (Idx
= 0; Idx
< IdxEnd
; Idx
++) {
918 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
920 *f
++ = Idx
& 0x01 ? U
[Idx
/2] : V
[Idx
/2];
922 else if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUV422P
) {
929 else if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
) {
931 if ( !(( Idx
& 0x01 ) | ( frame
->curline
& 0x01 )) ){
933 /* only need do this for 1 in 4 pixels */
934 /* intraframe buffer is YUV420 format */
942 YUV_TO_RGB_BY_THE_BOOK(Y
[Idx
], U
[Idx
/2], V
[Idx
/2], rv
, gv
, bv
);
943 switch (frame
->v4l2_format
.format
) {
944 case V4L2_PIX_FMT_GREY
:
947 case V4L2_PIX_FMT_RGB555
:
948 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 2));
949 *f
++ = (0x03 & (gv
>> 6)) | (0x7C & (rv
>> 1));
951 case V4L2_PIX_FMT_RGB565
:
952 *f
++ = (0x1F & (bv
>> 3)) | (0xE0 & (gv
<< 3));
953 *f
++ = (0x07 & (gv
>> 5)) | (0xF8 & rv
);
955 case V4L2_PIX_FMT_RGB24
:
960 case V4L2_PIX_FMT_RGB32
:
970 /* Deal with non-integer no. of bytes for YUV420P */
971 if (frame
->v4l2_format
.format
!= V4L2_PIX_FMT_YVU420
)
972 *pcopylen
+= frame
->v4l2_linesize
;
974 *pcopylen
+= frame
->curline
& 0x01 ? frame
->v4l2_linesize
: frame
->v4l2_linesize
<< 1;
978 if (frame
->curline
>= frame
->frmheight
) {
979 return ParseState_NextFrame
;
982 return ParseState_Continue
;
989 * usbvision_parse_lines_420()
991 * Parse two lines from the scratch buffer, put
992 * decoded RGB value into the current frame buffer and add the written
993 * number of bytes (RGB) to the *pcopylen.
996 static enum ParseState
usbvision_parse_lines_420(struct usb_usbvision
*usbvision
,
999 struct usbvision_frame
*frame
;
1000 unsigned char *f_even
= NULL
, *f_odd
= NULL
;
1001 unsigned int pixel_per_line
, block
;
1002 int pixel
, block_split
;
1003 int y_ptr
, u_ptr
, v_ptr
, y_odd_offset
;
1004 const int y_block_size
= 128;
1005 const int uv_block_size
= 64;
1006 const int sub_block_size
= 32;
1007 const int y_step
[] = { 0, 0, 0, 2 }, y_step_size
= 4;
1008 const int uv_step
[]= { 0, 0, 0, 4 }, uv_step_size
= 4;
1009 unsigned char y
[2], u
, v
; /* YUV components */
1010 int y_
, u_
, v_
, vb
, uvg
, ur
;
1011 int r_
, g_
, b_
; /* RGB components */
1013 int clipmask_even_index
, clipmask_odd_index
, bytes_per_pixel
;
1014 int clipmask_add
, stretch_bytes
;
1016 frame
= usbvision
->curFrame
;
1017 f_even
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
1018 f_odd
= f_even
+ frame
->v4l2_linesize
* usbvision
->stretch_height
;
1020 /* Make sure there's enough data for the entire line */
1021 /* In this mode usbvision transfer 3 bytes for every 2 pixels */
1022 /* I need two lines to decode the color */
1023 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
1024 stretch_bytes
= (usbvision
->stretch_width
- 1) * bytes_per_pixel
;
1025 clipmask_even_index
= frame
->curline
* MAX_FRAME_WIDTH
;
1026 clipmask_odd_index
= clipmask_even_index
+ MAX_FRAME_WIDTH
;
1027 clipmask_add
= usbvision
->stretch_width
;
1028 pixel_per_line
= frame
->isocHeader
.frameWidth
;
1030 if (scratch_len(usbvision
) < (int)pixel_per_line
* 3) {
1031 //printk(KERN_DEBUG "out of data, need %d\n", len);
1032 return ParseState_Out
;
1035 if ((frame
->curline
+ 1) >= frame
->frmheight
) {
1036 return ParseState_NextFrame
;
1039 block_split
= (pixel_per_line
%y_block_size
) ? 1 : 0; //are some blocks splitted into different lines?
1041 y_odd_offset
= (pixel_per_line
/ y_block_size
) * (y_block_size
+ uv_block_size
)
1042 + block_split
* uv_block_size
;
1044 scratch_set_extra_ptr(usbvision
, &y_ptr
, y_odd_offset
);
1045 scratch_set_extra_ptr(usbvision
, &u_ptr
, y_block_size
);
1046 scratch_set_extra_ptr(usbvision
, &v_ptr
, y_odd_offset
1047 + (4 - block_split
) * sub_block_size
);
1049 for (block
= 0; block
< (pixel_per_line
/ sub_block_size
);
1053 for (pixel
= 0; pixel
< sub_block_size
; pixel
+=2) {
1054 scratch_get(usbvision
, &y
[0], 2);
1055 scratch_get_extra(usbvision
, &u
, &u_ptr
, 1);
1056 scratch_get_extra(usbvision
, &v
, &v_ptr
, 1);
1058 //I don't use the YUV_TO_RGB macro for better performance
1062 uvg
= -53281 * u_
- 25625 * v_
;
1065 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1070 y_
= 76284 * (y
[0] - 16);
1072 b_
= (y_
+ vb
) >> 16;
1073 g_
= (y_
+ uvg
)>> 16;
1074 r_
= (y_
+ ur
) >> 16;
1076 switch (frame
->v4l2_format
.format
) {
1077 case V4L2_PIX_FMT_RGB565
:
1079 *f_even
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 3));
1080 *f_even
++ = (0x07 & ( g
>> 5)) | (0xF8 & LIMIT_RGB(r_
));
1082 case V4L2_PIX_FMT_RGB24
:
1083 *f_even
++ = LIMIT_RGB(b_
);
1084 *f_even
++ = LIMIT_RGB(g_
);
1085 *f_even
++ = LIMIT_RGB(r_
);
1087 case V4L2_PIX_FMT_RGB32
:
1088 *f_even
++ = LIMIT_RGB(b_
);
1089 *f_even
++ = LIMIT_RGB(g_
);
1090 *f_even
++ = LIMIT_RGB(r_
);
1093 case V4L2_PIX_FMT_RGB555
:
1095 *f_even
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 2));
1096 *f_even
++ = (0x03 & ( g
>> 6)) |
1097 (0x7C & (LIMIT_RGB(r_
) >> 1));
1101 clipmask_even_index
+= clipmask_add
;
1102 f_even
+= stretch_bytes
;
1104 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1109 y_
= 76284 * (y
[1] - 16);
1111 b_
= (y_
+ vb
) >> 16;
1112 g_
= (y_
+ uvg
)>> 16;
1113 r_
= (y_
+ ur
) >> 16;
1115 switch (frame
->v4l2_format
.format
) {
1116 case V4L2_PIX_FMT_RGB565
:
1118 *f_even
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 3));
1119 *f_even
++ = (0x07 & ( g
>> 5)) | (0xF8 & LIMIT_RGB(r_
));
1121 case V4L2_PIX_FMT_RGB24
:
1122 *f_even
++ = LIMIT_RGB(b_
);
1123 *f_even
++ = LIMIT_RGB(g_
);
1124 *f_even
++ = LIMIT_RGB(r_
);
1126 case V4L2_PIX_FMT_RGB32
:
1127 *f_even
++ = LIMIT_RGB(b_
);
1128 *f_even
++ = LIMIT_RGB(g_
);
1129 *f_even
++ = LIMIT_RGB(r_
);
1132 case V4L2_PIX_FMT_RGB555
:
1134 *f_even
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 2));
1135 *f_even
++ = (0x03 & ( g
>> 6)) |
1136 (0x7C & (LIMIT_RGB(r_
) >> 1));
1140 clipmask_even_index
+= clipmask_add
;
1141 f_even
+= stretch_bytes
;
1143 scratch_get_extra(usbvision
, &y
[0], &y_ptr
, 2);
1145 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1150 y_
= 76284 * (y
[0] - 16);
1152 b_
= (y_
+ vb
) >> 16;
1153 g_
= (y_
+ uvg
)>> 16;
1154 r_
= (y_
+ ur
) >> 16;
1156 switch (frame
->v4l2_format
.format
) {
1157 case V4L2_PIX_FMT_RGB565
:
1159 *f_odd
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 3));
1160 *f_odd
++ = (0x07 & ( g
>> 5)) | (0xF8 & LIMIT_RGB(r_
));
1162 case V4L2_PIX_FMT_RGB24
:
1163 *f_odd
++ = LIMIT_RGB(b_
);
1164 *f_odd
++ = LIMIT_RGB(g_
);
1165 *f_odd
++ = LIMIT_RGB(r_
);
1167 case V4L2_PIX_FMT_RGB32
:
1168 *f_odd
++ = LIMIT_RGB(b_
);
1169 *f_odd
++ = LIMIT_RGB(g_
);
1170 *f_odd
++ = LIMIT_RGB(r_
);
1173 case V4L2_PIX_FMT_RGB555
:
1175 *f_odd
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 2));
1176 *f_odd
++ = (0x03 & ( g
>> 6)) |
1177 (0x7C & (LIMIT_RGB(r_
) >> 1));
1181 clipmask_odd_index
+= clipmask_add
;
1182 f_odd
+= stretch_bytes
;
1184 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1189 y_
= 76284 * (y
[1] - 16);
1191 b_
= (y_
+ vb
) >> 16;
1192 g_
= (y_
+ uvg
)>> 16;
1193 r_
= (y_
+ ur
) >> 16;
1195 switch (frame
->v4l2_format
.format
) {
1196 case V4L2_PIX_FMT_RGB565
:
1198 *f_odd
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 3));
1199 *f_odd
++ = (0x07 & ( g
>> 5)) | (0xF8 & LIMIT_RGB(r_
));
1201 case V4L2_PIX_FMT_RGB24
:
1202 *f_odd
++ = LIMIT_RGB(b_
);
1203 *f_odd
++ = LIMIT_RGB(g_
);
1204 *f_odd
++ = LIMIT_RGB(r_
);
1206 case V4L2_PIX_FMT_RGB32
:
1207 *f_odd
++ = LIMIT_RGB(b_
);
1208 *f_odd
++ = LIMIT_RGB(g_
);
1209 *f_odd
++ = LIMIT_RGB(r_
);
1212 case V4L2_PIX_FMT_RGB555
:
1214 *f_odd
++ = (0x1F & (LIMIT_RGB(b_
) >> 3)) | (0xE0 & (g
<< 2));
1215 *f_odd
++ = (0x03 & ( g
>> 6)) |
1216 (0x7C & (LIMIT_RGB(r_
) >> 1));
1220 clipmask_odd_index
+= clipmask_add
;
1221 f_odd
+= stretch_bytes
;
1224 scratch_rm_old(usbvision
,y_step
[block
% y_step_size
] * sub_block_size
);
1225 scratch_inc_extra_ptr(&y_ptr
, y_step
[(block
+ 2 * block_split
) % y_step_size
]
1227 scratch_inc_extra_ptr(&u_ptr
, uv_step
[block
% uv_step_size
]
1229 scratch_inc_extra_ptr(&v_ptr
, uv_step
[(block
+ 2 * block_split
) % uv_step_size
]
1233 scratch_rm_old(usbvision
, pixel_per_line
* 3 / 2
1234 + block_split
* sub_block_size
);
1236 frame
->curline
+= 2 * usbvision
->stretch_height
;
1237 *pcopylen
+= frame
->v4l2_linesize
* 2 * usbvision
->stretch_height
;
1239 if (frame
->curline
>= frame
->frmheight
)
1240 return ParseState_NextFrame
;
1242 return ParseState_Continue
;
1246 * usbvision_parse_data()
1248 * Generic routine to parse the scratch buffer. It employs either
1249 * usbvision_find_header() or usbvision_parse_lines() to do most
1253 static void usbvision_parse_data(struct usb_usbvision
*usbvision
)
1255 struct usbvision_frame
*frame
;
1256 enum ParseState newstate
;
1258 unsigned long lock_flags
;
1260 frame
= usbvision
->curFrame
;
1262 PDEBUG(DBG_PARSE
, "parsing len=%d\n", scratch_len(usbvision
));
1266 newstate
= ParseState_Out
;
1267 if (scratch_len(usbvision
)) {
1268 if (frame
->scanstate
== ScanState_Scanning
) {
1269 newstate
= usbvision_find_header(usbvision
);
1271 else if (frame
->scanstate
== ScanState_Lines
) {
1272 if (usbvision
->isocMode
== ISOC_MODE_YUV420
) {
1273 newstate
= usbvision_parse_lines_420(usbvision
, ©len
);
1275 else if (usbvision
->isocMode
== ISOC_MODE_YUV422
) {
1276 newstate
= usbvision_parse_lines_422(usbvision
, ©len
);
1278 else if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
1279 newstate
= usbvision_parse_compress(usbvision
, ©len
);
1284 if (newstate
== ParseState_Continue
) {
1287 else if ((newstate
== ParseState_NextFrame
) || (newstate
== ParseState_Out
)) {
1291 return; /* ParseState_EndParse */
1295 if (newstate
== ParseState_NextFrame
) {
1296 frame
->grabstate
= FrameState_Done
;
1297 do_gettimeofday(&(frame
->timestamp
));
1298 frame
->sequence
= usbvision
->frame_num
;
1300 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1301 list_move_tail(&(frame
->frame
), &usbvision
->outqueue
);
1302 usbvision
->curFrame
= NULL
;
1303 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1305 usbvision
->frame_num
++;
1307 /* This will cause the process to request another frame. */
1308 if (waitqueue_active(&usbvision
->wait_frame
)) {
1309 PDEBUG(DBG_PARSE
, "Wake up !");
1310 wake_up_interruptible(&usbvision
->wait_frame
);
1314 frame
->grabstate
= FrameState_Grabbing
;
1317 /* Update the frame's uncompressed length. */
1318 frame
->scanlength
+= copylen
;
1323 * Make all of the blocks of data contiguous
1325 static int usbvision_compress_isochronous(struct usb_usbvision
*usbvision
,
1328 unsigned char *packet_data
;
1331 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1332 int packet_len
= urb
->iso_frame_desc
[i
].actual_length
;
1333 int packet_stat
= urb
->iso_frame_desc
[i
].status
;
1335 packet_data
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
1337 /* Detect and ignore errored packets */
1338 if (packet_stat
) { // packet_stat != 0 ?????????????
1339 PDEBUG(DBG_ISOC
, "data error: [%d] len=%d, status=%X", i
, packet_len
, packet_stat
);
1340 usbvision
->isocErrCount
++;
1344 /* Detect and ignore empty packets */
1345 if (packet_len
< 0) {
1346 PDEBUG(DBG_ISOC
, "error packet [%d]", i
);
1347 usbvision
->isocSkipCount
++;
1350 else if (packet_len
== 0) { /* Frame end ????? */
1351 PDEBUG(DBG_ISOC
, "null packet [%d]", i
);
1352 usbvision
->isocstate
=IsocState_NoFrame
;
1353 usbvision
->isocSkipCount
++;
1356 else if (packet_len
> usbvision
->isocPacketSize
) {
1357 PDEBUG(DBG_ISOC
, "packet[%d] > isocPacketSize", i
);
1358 usbvision
->isocSkipCount
++;
1362 PDEBUG(DBG_ISOC
, "packet ok [%d] len=%d", i
, packet_len
);
1364 if (usbvision
->isocstate
==IsocState_NoFrame
) { //new frame begins
1365 usbvision
->isocstate
=IsocState_InFrame
;
1366 scratch_mark_header(usbvision
);
1367 usbvision_measure_bandwidth(usbvision
);
1368 PDEBUG(DBG_ISOC
, "packet with header");
1372 * If usbvision continues to feed us with data but there is no
1373 * consumption (if, for example, V4L client fell asleep) we
1374 * may overflow the buffer. We have to move old data over to
1375 * free room for new data. This is bad for old data. If we
1376 * just drop new data then it's bad for new data... choose
1377 * your favorite evil here.
1379 if (scratch_free(usbvision
) < packet_len
) {
1381 usbvision
->scratch_ovf_count
++;
1382 PDEBUG(DBG_ISOC
, "scratch buf overflow! scr_len: %d, n: %d",
1383 scratch_len(usbvision
), packet_len
);
1384 scratch_rm_old(usbvision
, packet_len
- scratch_free(usbvision
));
1387 /* Now we know that there is enough room in scratch buffer */
1388 scratch_put(usbvision
, packet_data
, packet_len
);
1389 totlen
+= packet_len
;
1390 usbvision
->isocDataCount
+= packet_len
;
1391 usbvision
->isocPacketCount
++;
1397 printk(KERN_DEBUG
"+%d.\n", usbvision
->scratchlen
);
1398 usbvision_hexdump(data0
, (totlen
> 64) ? 64 : totlen
);
1406 static void usbvision_isocIrq(struct urb
*urb
)
1410 struct usb_usbvision
*usbvision
= urb
->context
;
1412 unsigned long startTime
= jiffies
;
1413 struct usbvision_frame
**f
;
1415 /* We don't want to do anything if we are about to be removed! */
1416 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1419 f
= &usbvision
->curFrame
;
1421 /* Manage streaming interruption */
1422 if (usbvision
->streaming
== Stream_Interrupt
) {
1423 usbvision
->streaming
= Stream_Idle
;
1425 (*f
)->grabstate
= FrameState_Ready
;
1426 (*f
)->scanstate
= ScanState_Scanning
;
1428 PDEBUG(DBG_IRQ
, "stream interrupted");
1429 wake_up_interruptible(&usbvision
->wait_stream
);
1432 /* Copy the data received into our scratch buffer */
1433 len
= usbvision_compress_isochronous(usbvision
, urb
);
1435 usbvision
->isocUrbCount
++;
1436 usbvision
->urb_length
= len
;
1438 if (usbvision
->streaming
== Stream_On
) {
1440 /* If we collected enough data let's parse! */
1441 if (scratch_len(usbvision
) > USBVISION_HEADER_LENGTH
) { /* 12 == header_length */
1442 /*If we don't have a frame we're current working on, complain */
1443 if(!list_empty(&(usbvision
->inqueue
))) {
1445 (*f
) = list_entry(usbvision
->inqueue
.next
,struct usbvision_frame
, frame
);
1447 usbvision_parse_data(usbvision
);
1450 PDEBUG(DBG_IRQ
, "received data, but no one needs it");
1451 scratch_reset(usbvision
);
1456 PDEBUG(DBG_IRQ
, "received data, but no one needs it");
1457 scratch_reset(usbvision
);
1460 usbvision
->timeInIrq
+= jiffies
- startTime
;
1462 for (i
= 0; i
< USBVISION_URB_FRAMES
; i
++) {
1463 urb
->iso_frame_desc
[i
].status
= 0;
1464 urb
->iso_frame_desc
[i
].actual_length
= 0;
1468 urb
->dev
= usbvision
->dev
;
1469 errCode
= usb_submit_urb (urb
, GFP_ATOMIC
);
1471 /* Disable this warning. By design of the driver. */
1473 // err("%s: usb_submit_urb failed: error %d", __FUNCTION__, errCode);
1479 /*************************************/
1480 /* Low level usbvision access functions */
1481 /*************************************/
1484 * usbvision_read_reg()
1486 * return < 0 -> Error
1490 int usbvision_read_reg(struct usb_usbvision
*usbvision
, unsigned char reg
)
1493 unsigned char buffer
[1];
1495 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1498 errCode
= usb_control_msg(usbvision
->dev
, usb_rcvctrlpipe(usbvision
->dev
, 1),
1500 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
,
1501 0, (__u16
) reg
, buffer
, 1, HZ
);
1504 err("%s: failed: error %d", __FUNCTION__
, errCode
);
1511 * usbvision_write_reg()
1513 * return 1 -> Reg written
1514 * 0 -> usbvision is not yet ready
1515 * -1 -> Something went wrong
1518 int usbvision_write_reg(struct usb_usbvision
*usbvision
, unsigned char reg
,
1519 unsigned char value
)
1523 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1526 errCode
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1528 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1529 USB_RECIP_ENDPOINT
, 0, (__u16
) reg
, &value
, 1, HZ
);
1532 err("%s: failed: error %d", __FUNCTION__
, errCode
);
1538 static void usbvision_ctrlUrb_complete(struct urb
*urb
)
1540 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*)urb
->context
;
1542 PDEBUG(DBG_IRQ
, "");
1543 usbvision
->ctrlUrbBusy
= 0;
1544 if (waitqueue_active(&usbvision
->ctrlUrb_wq
)) {
1545 wake_up_interruptible(&usbvision
->ctrlUrb_wq
);
1550 static int usbvision_write_reg_irq(struct usb_usbvision
*usbvision
,int address
,
1551 unsigned char *data
, int len
)
1555 PDEBUG(DBG_IRQ
, "");
1559 // down(&usbvision->ctrlUrbLock);
1560 if (usbvision
->ctrlUrbBusy
) {
1561 // up(&usbvision->ctrlUrbLock);
1564 usbvision
->ctrlUrbBusy
= 1;
1565 // up(&usbvision->ctrlUrbLock);
1567 usbvision
->ctrlUrbSetup
.bRequestType
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
1568 usbvision
->ctrlUrbSetup
.bRequest
= USBVISION_OP_CODE
;
1569 usbvision
->ctrlUrbSetup
.wValue
= 0;
1570 usbvision
->ctrlUrbSetup
.wIndex
= cpu_to_le16(address
);
1571 usbvision
->ctrlUrbSetup
.wLength
= cpu_to_le16(len
);
1572 usb_fill_control_urb (usbvision
->ctrlUrb
, usbvision
->dev
,
1573 usb_sndctrlpipe(usbvision
->dev
, 1),
1574 (unsigned char *)&usbvision
->ctrlUrbSetup
,
1575 (void *)usbvision
->ctrlUrbBuffer
, len
,
1576 usbvision_ctrlUrb_complete
,
1579 memcpy(usbvision
->ctrlUrbBuffer
, data
, len
);
1581 errCode
= usb_submit_urb(usbvision
->ctrlUrb
, GFP_ATOMIC
);
1583 // error in usb_submit_urb()
1584 usbvision
->ctrlUrbBusy
= 0;
1586 PDEBUG(DBG_IRQ
, "submit %d byte: error %d", len
, errCode
);
1591 static int usbvision_init_compression(struct usb_usbvision
*usbvision
)
1595 usbvision
->lastIsocFrameNum
= -1;
1596 usbvision
->isocDataCount
= 0;
1597 usbvision
->isocPacketCount
= 0;
1598 usbvision
->isocSkipCount
= 0;
1599 usbvision
->comprLevel
= 50;
1600 usbvision
->lastComprLevel
= -1;
1601 usbvision
->isocUrbCount
= 0;
1602 usbvision
->requestIntra
= 1;
1603 usbvision
->isocMeasureBandwidthCount
= 0;
1608 /* this function measures the used bandwidth since last call
1609 * return: 0 : no error
1610 * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize
1612 static int usbvision_measure_bandwidth (struct usb_usbvision
*usbvision
)
1616 if (usbvision
->isocMeasureBandwidthCount
< 2) { // this gives an average bandwidth of 3 frames
1617 usbvision
->isocMeasureBandwidthCount
++;
1620 if ((usbvision
->isocPacketSize
> 0) && (usbvision
->isocPacketCount
> 0)) {
1621 usbvision
->usedBandwidth
= usbvision
->isocDataCount
/
1622 (usbvision
->isocPacketCount
+ usbvision
->isocSkipCount
) *
1623 100 / usbvision
->isocPacketSize
;
1625 usbvision
->isocMeasureBandwidthCount
= 0;
1626 usbvision
->isocDataCount
= 0;
1627 usbvision
->isocPacketCount
= 0;
1628 usbvision
->isocSkipCount
= 0;
1632 static int usbvision_adjust_compression (struct usb_usbvision
*usbvision
)
1635 unsigned char buffer
[6];
1637 PDEBUG(DBG_IRQ
, "");
1638 if ((adjustCompression
) && (usbvision
->usedBandwidth
> 0)) {
1639 usbvision
->comprLevel
+= (usbvision
->usedBandwidth
- 90) / 2;
1640 RESTRICT_TO_RANGE(usbvision
->comprLevel
, 0, 100);
1641 if (usbvision
->comprLevel
!= usbvision
->lastComprLevel
) {
1643 if (usbvision
->bridgeType
== BRIDGE_NT1004
|| usbvision
->bridgeType
== BRIDGE_NT1005
) {
1644 buffer
[0] = (unsigned char)(4 + 16 * usbvision
->comprLevel
/ 100); // PCM Threshold 1
1645 buffer
[1] = (unsigned char)(4 + 8 * usbvision
->comprLevel
/ 100); // PCM Threshold 2
1646 distorsion
= 7 + 248 * usbvision
->comprLevel
/ 100;
1647 buffer
[2] = (unsigned char)(distorsion
& 0xFF); // Average distorsion Threshold (inter)
1648 buffer
[3] = (unsigned char)(distorsion
& 0xFF); // Average distorsion Threshold (intra)
1649 distorsion
= 1 + 42 * usbvision
->comprLevel
/ 100;
1650 buffer
[4] = (unsigned char)(distorsion
& 0xFF); // Maximum distorsion Threshold (inter)
1651 buffer
[5] = (unsigned char)(distorsion
& 0xFF); // Maximum distorsion Threshold (intra)
1653 else { //BRIDGE_NT1003
1654 buffer
[0] = (unsigned char)(4 + 16 * usbvision
->comprLevel
/ 100); // PCM threshold 1
1655 buffer
[1] = (unsigned char)(4 + 8 * usbvision
->comprLevel
/ 100); // PCM threshold 2
1656 distorsion
= 2 + 253 * usbvision
->comprLevel
/ 100;
1657 buffer
[2] = (unsigned char)(distorsion
& 0xFF); // distorsion threshold bit0-7
1658 buffer
[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11
1659 distorsion
= 0 + 43 * usbvision
->comprLevel
/ 100;
1660 buffer
[4] = (unsigned char)(distorsion
& 0xFF); // maximum distorsion bit0-7
1661 buffer
[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8
1663 errCode
= usbvision_write_reg_irq(usbvision
, USBVISION_PCM_THR1
, buffer
, 6);
1665 PDEBUG(DBG_IRQ
, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer
[0],
1666 buffer
[1], buffer
[2], buffer
[3], buffer
[4], buffer
[5]);
1667 usbvision
->lastComprLevel
= usbvision
->comprLevel
;
1674 static int usbvision_request_intra (struct usb_usbvision
*usbvision
)
1677 unsigned char buffer
[1];
1679 PDEBUG(DBG_IRQ
, "");
1680 usbvision
->requestIntra
= 1;
1682 usbvision_write_reg_irq(usbvision
, USBVISION_FORCE_INTRA
, buffer
, 1);
1686 static int usbvision_unrequest_intra (struct usb_usbvision
*usbvision
)
1689 unsigned char buffer
[1];
1691 PDEBUG(DBG_IRQ
, "");
1692 usbvision
->requestIntra
= 0;
1694 usbvision_write_reg_irq(usbvision
, USBVISION_FORCE_INTRA
, buffer
, 1);
1698 /*******************************
1699 * usbvision utility functions
1700 *******************************/
1702 int usbvision_power_off(struct usb_usbvision
*usbvision
)
1706 PDEBUG(DBG_FUNC
, "");
1708 errCode
= usbvision_write_reg(usbvision
, USBVISION_PWR_REG
, USBVISION_SSPND_EN
);
1710 usbvision
->power
= 0;
1712 PDEBUG(DBG_FUNC
, "%s: errCode %d", (errCode
!=1)?"ERROR":"power is off", errCode
);
1717 * usbvision_set_video_format()
1720 static int usbvision_set_video_format(struct usb_usbvision
*usbvision
, int format
)
1722 static const char proc
[] = "usbvision_set_video_format";
1724 unsigned char value
[2];
1726 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1729 PDEBUG(DBG_FUNC
, "isocMode %#02x", format
);
1731 if ((format
!= ISOC_MODE_YUV422
)
1732 && (format
!= ISOC_MODE_YUV420
)
1733 && (format
!= ISOC_MODE_COMPRESS
)) {
1734 printk(KERN_ERR
"usbvision: unknown video format %02x, using default YUV420",
1736 format
= ISOC_MODE_YUV420
;
1738 value
[0] = 0x0A; //TODO: See the effect of the filter
1740 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1742 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1743 USB_RECIP_ENDPOINT
, 0,
1744 (__u16
) USBVISION_FILT_CONT
, value
, 2, HZ
);
1747 printk(KERN_ERR
"%s: ERROR=%d. USBVISION stopped - "
1748 "reconnect or reload driver.\n", proc
, rc
);
1750 usbvision
->isocMode
= format
;
1755 * usbvision_set_output()
1759 int usbvision_set_output(struct usb_usbvision
*usbvision
, int width
,
1763 int UsbWidth
, UsbHeight
;
1764 unsigned int frameRate
=0, frameDrop
=0;
1765 unsigned char value
[4];
1767 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1771 if (width
> MAX_USB_WIDTH
) {
1772 UsbWidth
= width
/ 2;
1773 usbvision
->stretch_width
= 2;
1777 usbvision
->stretch_width
= 1;
1780 if (height
> MAX_USB_HEIGHT
) {
1781 UsbHeight
= height
/ 2;
1782 usbvision
->stretch_height
= 2;
1786 usbvision
->stretch_height
= 1;
1789 RESTRICT_TO_RANGE(UsbWidth
, MIN_FRAME_WIDTH
, MAX_USB_WIDTH
);
1790 UsbWidth
&= ~(MIN_FRAME_WIDTH
-1);
1791 RESTRICT_TO_RANGE(UsbHeight
, MIN_FRAME_HEIGHT
, MAX_USB_HEIGHT
);
1794 PDEBUG(DBG_FUNC
, "usb %dx%d; screen %dx%d; stretch %dx%d",
1795 UsbWidth
, UsbHeight
, width
, height
,
1796 usbvision
->stretch_width
, usbvision
->stretch_height
);
1798 /* I'll not rewrite the same values */
1799 if ((UsbWidth
!= usbvision
->curwidth
) || (UsbHeight
!= usbvision
->curheight
)) {
1800 value
[0] = UsbWidth
& 0xff; //LSB
1801 value
[1] = (UsbWidth
>> 8) & 0x03; //MSB
1802 value
[2] = UsbHeight
& 0xff; //LSB
1803 value
[3] = (UsbHeight
>> 8) & 0x03; //MSB
1805 errCode
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1807 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
,
1808 0, (__u16
) USBVISION_LXSIZE_O
, value
, 4, HZ
);
1811 err("%s failed: error %d", __FUNCTION__
, errCode
);
1814 usbvision
->curwidth
= usbvision
->stretch_width
* UsbWidth
;
1815 usbvision
->curheight
= usbvision
->stretch_height
* UsbHeight
;
1818 if (usbvision
->isocMode
== ISOC_MODE_YUV422
) {
1819 frameRate
= (usbvision
->isocPacketSize
* 1000) / (UsbWidth
* UsbHeight
* 2);
1821 else if (usbvision
->isocMode
== ISOC_MODE_YUV420
) {
1822 frameRate
= (usbvision
->isocPacketSize
* 1000) / ((UsbWidth
* UsbHeight
* 12) / 8);
1825 frameRate
= FRAMERATE_MAX
;
1828 if (usbvision
->tvnorm
->id
& V4L2_STD_625_50
) {
1829 frameDrop
= frameRate
* 32 / 25 - 1;
1831 else if (usbvision
->tvnorm
->id
& V4L2_STD_525_60
) {
1832 frameDrop
= frameRate
* 32 / 30 - 1;
1835 RESTRICT_TO_RANGE(frameDrop
, FRAMERATE_MIN
, FRAMERATE_MAX
);
1837 PDEBUG(DBG_FUNC
, "frameRate %d fps, frameDrop %d", frameRate
, frameDrop
);
1839 frameDrop
= FRAMERATE_MAX
; // We can allow the maximum here, because dropping is controlled
1841 /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
1843 => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
1845 frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
1846 => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
1847 => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
1849 errCode
= usbvision_write_reg(usbvision
, USBVISION_FRM_RATE
, frameDrop
);
1855 * usbvision_frames_alloc
1856 * allocate the maximum frames this driver can manage
1858 int usbvision_frames_alloc(struct usb_usbvision
*usbvision
)
1862 /* Allocate memory for the frame buffers */
1863 usbvision
->max_frame_size
= MAX_FRAME_SIZE
;
1864 usbvision
->fbuf_size
= USBVISION_NUMFRAMES
* usbvision
->max_frame_size
;
1865 usbvision
->fbuf
= usbvision_rvmalloc(usbvision
->fbuf_size
);
1867 if(usbvision
->fbuf
== NULL
) {
1868 err("%s: unable to allocate %d bytes for fbuf ",
1869 __FUNCTION__
, usbvision
->fbuf_size
);
1872 spin_lock_init(&usbvision
->queue_lock
);
1873 init_waitqueue_head(&usbvision
->wait_frame
);
1874 init_waitqueue_head(&usbvision
->wait_stream
);
1876 /* Allocate all buffers */
1877 for (i
= 0; i
< USBVISION_NUMFRAMES
; i
++) {
1878 usbvision
->frame
[i
].index
= i
;
1879 usbvision
->frame
[i
].grabstate
= FrameState_Unused
;
1880 usbvision
->frame
[i
].data
= usbvision
->fbuf
+
1881 i
* usbvision
->max_frame_size
;
1883 * Set default sizes for read operation.
1885 usbvision
->stretch_width
= 1;
1886 usbvision
->stretch_height
= 1;
1887 usbvision
->frame
[i
].width
= usbvision
->curwidth
;
1888 usbvision
->frame
[i
].height
= usbvision
->curheight
;
1889 usbvision
->frame
[i
].bytes_read
= 0;
1895 * usbvision_frames_free
1896 * frees memory allocated for the frames
1898 void usbvision_frames_free(struct usb_usbvision
*usbvision
)
1900 /* Have to free all that memory */
1901 if (usbvision
->fbuf
!= NULL
) {
1902 usbvision_rvfree(usbvision
->fbuf
, usbvision
->fbuf_size
);
1903 usbvision
->fbuf
= NULL
;
1907 * usbvision_empty_framequeues()
1908 * prepare queues for incoming and outgoing frames
1910 void usbvision_empty_framequeues(struct usb_usbvision
*usbvision
)
1914 INIT_LIST_HEAD(&(usbvision
->inqueue
));
1915 INIT_LIST_HEAD(&(usbvision
->outqueue
));
1917 for (i
= 0; i
< USBVISION_NUMFRAMES
; i
++) {
1918 usbvision
->frame
[i
].grabstate
= FrameState_Unused
;
1919 usbvision
->frame
[i
].bytes_read
= 0;
1924 * usbvision_stream_interrupt()
1927 int usbvision_stream_interrupt(struct usb_usbvision
*usbvision
)
1931 /* stop reading from the device */
1933 usbvision
->streaming
= Stream_Interrupt
;
1934 ret
= wait_event_timeout(usbvision
->wait_stream
,
1935 (usbvision
->streaming
== Stream_Idle
),
1936 msecs_to_jiffies(USBVISION_NUMSBUF
*USBVISION_URB_FRAMES
));
1941 * usbvision_set_compress_params()
1945 static int usbvision_set_compress_params(struct usb_usbvision
*usbvision
)
1947 static const char proc
[] = "usbvision_set_compresion_params: ";
1949 unsigned char value
[6];
1951 value
[0] = 0x0F; // Intra-Compression cycle
1952 value
[1] = 0x01; // Reg.45 one line per strip
1953 value
[2] = 0x00; // Reg.46 Force intra mode on all new frames
1954 value
[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
1955 value
[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
1956 value
[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
1958 //catched values for NT1004
1959 // value[0] = 0xFF; // Never apply intra mode automatically
1960 // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
1961 // value[2] = 0x01; // Force intra mode on all new frames
1962 // value[3] = 0x00; // Strip size 400 Bytes; do not force up
1963 // value[4] = 0xA2; //
1964 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1967 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1969 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1970 USB_RECIP_ENDPOINT
, 0,
1971 (__u16
) USBVISION_INTRA_CYC
, value
, 5, HZ
);
1974 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
1975 "reconnect or reload driver.\n", proc
, rc
);
1979 if (usbvision
->bridgeType
== BRIDGE_NT1004
) {
1980 value
[0] = 20; // PCM Threshold 1
1981 value
[1] = 12; // PCM Threshold 2
1982 value
[2] = 255; // Distorsion Threshold inter
1983 value
[3] = 255; // Distorsion Threshold intra
1984 value
[4] = 43; // Max Distorsion inter
1985 value
[5] = 43; // Max Distorsion intra
1988 value
[0] = 20; // PCM Threshold 1
1989 value
[1] = 12; // PCM Threshold 2
1990 value
[2] = 255; // Distorsion Threshold d7-d0
1991 value
[3] = 0; // Distorsion Threshold d11-d8
1992 value
[4] = 43; // Max Distorsion d7-d0
1993 value
[5] = 0; // Max Distorsion d8
1996 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1999 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2001 USB_DIR_OUT
| USB_TYPE_VENDOR
|
2002 USB_RECIP_ENDPOINT
, 0,
2003 (__u16
) USBVISION_PCM_THR1
, value
, 6, HZ
);
2006 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2007 "reconnect or reload driver.\n", proc
, rc
);
2017 * usbvision_set_input()
2019 * Set the input (saa711x, ...) size x y and other misc input params
2020 * I've no idea if this parameters are right
2023 int usbvision_set_input(struct usb_usbvision
*usbvision
)
2025 static const char proc
[] = "usbvision_set_input: ";
2027 unsigned char value
[8];
2028 unsigned char dvi_yuv_value
;
2030 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2033 /* Set input format expected from decoder*/
2034 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg1
>= 0) {
2035 value
[0] = usbvision_device_data
[usbvision
->DevModel
].Vin_Reg1
& 0xff;
2036 } else if(usbvision_device_data
[usbvision
->DevModel
].Codec
== CODEC_SAA7113
) {
2037 /* SAA7113 uses 8 bit output */
2038 value
[0] = USBVISION_8_422_SYNC
;
2040 /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
2041 * as that is how saa7111 is configured */
2042 value
[0] = USBVISION_16_422_SYNC
;
2043 /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
2046 rc
= usbvision_write_reg(usbvision
, USBVISION_VIN_REG1
, value
[0]);
2048 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2049 "reconnect or reload driver.\n", proc
, rc
);
2054 if (usbvision
->tvnorm
->id
& V4L2_STD_PAL
) {
2056 value
[1] = 0x02; //0x02C0 -> 704 Input video line length
2058 value
[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2060 value
[5] = 0x00; //0x0060 -> 96 Input video h offset
2062 value
[7] = 0x00; //0x0016 -> 22 Input video v offset
2063 } else if (usbvision
->tvnorm
->id
& V4L2_STD_SECAM
) {
2065 value
[1] = 0x02; //0x02C0 -> 704 Input video line length
2067 value
[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2069 value
[5] = 0x00; //0x0001 -> 01 Input video h offset
2071 value
[7] = 0x00; //0x0001 -> 01 Input video v offset
2072 } else { /* V4L2_STD_NTSC */
2074 value
[1] = 0x02; //0x02D0 -> 720 Input video line length
2076 value
[3] = 0x00; //0x00F0 -> 240 Input video number of lines
2078 value
[5] = 0x00; //0x0050 -> 80 Input video h offset
2080 value
[7] = 0x00; //0x0010 -> 16 Input video v offset
2083 if (usbvision_device_data
[usbvision
->DevModel
].X_Offset
>= 0) {
2084 value
[4]=usbvision_device_data
[usbvision
->DevModel
].X_Offset
& 0xff;
2085 value
[5]=(usbvision_device_data
[usbvision
->DevModel
].X_Offset
& 0x0300) >> 8;
2088 if (usbvision_device_data
[usbvision
->DevModel
].Y_Offset
>= 0) {
2089 value
[6]=usbvision_device_data
[usbvision
->DevModel
].Y_Offset
& 0xff;
2090 value
[7]=(usbvision_device_data
[usbvision
->DevModel
].Y_Offset
& 0x0300) >> 8;
2093 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2094 USBVISION_OP_CODE
, /* USBVISION specific code */
2095 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
, 0,
2096 (__u16
) USBVISION_LXSIZE_I
, value
, 8, HZ
);
2098 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2099 "reconnect or reload driver.\n", proc
, rc
);
2104 dvi_yuv_value
= 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
2106 if(usbvision_device_data
[usbvision
->DevModel
].Dvi_yuv
>= 0){
2107 dvi_yuv_value
= usbvision_device_data
[usbvision
->DevModel
].Dvi_yuv
& 0xff;
2109 else if(usbvision_device_data
[usbvision
->DevModel
].Codec
== CODEC_SAA7113
) {
2110 /* This changes as the fine sync control changes. Further investigation necessary */
2111 dvi_yuv_value
= 0x06;
2114 return (usbvision_write_reg(usbvision
, USBVISION_DVI_YUV
, dvi_yuv_value
));
2119 * usbvision_set_dram_settings()
2121 * Set the buffer address needed by the usbvision dram to operate
2122 * This values has been taken with usbsnoop.
2126 static int usbvision_set_dram_settings(struct usb_usbvision
*usbvision
)
2129 unsigned char value
[8];
2131 if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
2140 // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
2141 // FDL: 0x00000-0x0E099 = 57498 Words
2142 // VDW: 0x0E3FF-0x3FFFF
2154 /* These are the values of the address of the video buffer,
2155 * they have to be loaded into the USBVISION_DRM_PRM1-8
2157 * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
2158 * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
2159 * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
2160 * Only used in compressed mode
2161 * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
2162 * Only used in compressed mode
2163 * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
2164 * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
2167 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2170 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2171 USBVISION_OP_CODE
, /* USBVISION specific code */
2172 USB_DIR_OUT
| USB_TYPE_VENDOR
|
2173 USB_RECIP_ENDPOINT
, 0,
2174 (__u16
) USBVISION_DRM_PRM1
, value
, 8, HZ
);
2177 err("%sERROR=%d", __FUNCTION__
, rc
);
2181 /* Restart the video buffer logic */
2182 if ((rc
= usbvision_write_reg(usbvision
, USBVISION_DRM_CONT
, USBVISION_RES_UR
|
2183 USBVISION_RES_FDL
| USBVISION_RES_VDW
)) < 0)
2185 rc
= usbvision_write_reg(usbvision
, USBVISION_DRM_CONT
, 0x00);
2193 * Power on the device, enables suspend-resume logic
2194 * & reset the isoc End-Point
2198 int usbvision_power_on(struct usb_usbvision
*usbvision
)
2202 PDEBUG(DBG_FUNC
, "");
2204 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
, USBVISION_SSPND_EN
);
2205 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2206 USBVISION_SSPND_EN
| USBVISION_RES2
);
2208 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2209 USBVISION_SSPND_EN
| USBVISION_PWR_VID
);
2210 errCode
= usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2211 USBVISION_SSPND_EN
| USBVISION_PWR_VID
| USBVISION_RES2
);
2213 usbvision
->power
= 1;
2215 PDEBUG(DBG_FUNC
, "%s: errCode %d", (errCode
<0)?"ERROR":"power is on", errCode
);
2221 * usbvision timer stuff
2224 // to call usbvision_power_off from task queue
2225 static void call_usbvision_power_off(struct work_struct
*work
)
2227 struct usb_usbvision
*usbvision
= container_of(work
, struct usb_usbvision
, powerOffWork
);
2229 PDEBUG(DBG_FUNC
, "");
2230 down_interruptible(&usbvision
->lock
);
2231 if(usbvision
->user
== 0) {
2232 usbvision_i2c_usb_del_bus(&usbvision
->i2c_adap
);
2234 usbvision_power_off(usbvision
);
2235 usbvision
->initialized
= 0;
2237 up(&usbvision
->lock
);
2240 static void usbvision_powerOffTimer(unsigned long data
)
2242 struct usb_usbvision
*usbvision
= (void *) data
;
2244 PDEBUG(DBG_FUNC
, "");
2245 del_timer(&usbvision
->powerOffTimer
);
2246 INIT_WORK(&usbvision
->powerOffWork
, call_usbvision_power_off
);
2247 (void) schedule_work(&usbvision
->powerOffWork
);
2251 void usbvision_init_powerOffTimer(struct usb_usbvision
*usbvision
)
2253 init_timer(&usbvision
->powerOffTimer
);
2254 usbvision
->powerOffTimer
.data
= (long) usbvision
;
2255 usbvision
->powerOffTimer
.function
= usbvision_powerOffTimer
;
2258 void usbvision_set_powerOffTimer(struct usb_usbvision
*usbvision
)
2260 mod_timer(&usbvision
->powerOffTimer
, jiffies
+ USBVISION_POWEROFF_TIME
);
2263 void usbvision_reset_powerOffTimer(struct usb_usbvision
*usbvision
)
2265 if (timer_pending(&usbvision
->powerOffTimer
)) {
2266 del_timer(&usbvision
->powerOffTimer
);
2271 * usbvision_begin_streaming()
2272 * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
2273 * idea about the rest
2275 int usbvision_begin_streaming(struct usb_usbvision
*usbvision
)
2279 if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
2280 usbvision_init_compression(usbvision
);
2282 errCode
= usbvision_write_reg(usbvision
, USBVISION_VIN_REG2
, USBVISION_NOHVALID
|
2283 usbvision
->Vin_Reg2_Preset
);
2288 * usbvision_restart_isoc()
2289 * Not sure yet if touching here PWR_REG make loose the config
2292 int usbvision_restart_isoc(struct usb_usbvision
*usbvision
)
2298 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2299 USBVISION_SSPND_EN
| USBVISION_PWR_VID
)) < 0)
2303 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2304 USBVISION_SSPND_EN
| USBVISION_PWR_VID
|
2305 USBVISION_RES2
)) < 0)
2309 usbvision_write_reg(usbvision
, USBVISION_VIN_REG2
,
2310 USBVISION_KEEP_BLANK
| USBVISION_NOHVALID
|
2311 usbvision
->Vin_Reg2_Preset
)) < 0) return ret
;
2313 /* TODO: schedule timeout */
2314 while ((usbvision_read_reg(usbvision
, USBVISION_STATUS_REG
) & 0x01) != 1);
2319 int usbvision_audio_off(struct usb_usbvision
*usbvision
)
2321 if (usbvision_write_reg(usbvision
, USBVISION_IOPIN_REG
, USBVISION_AUDIO_MUTE
) < 0) {
2322 printk(KERN_ERR
"usbvision_audio_off: can't wirte reg\n");
2325 usbvision
->AudioMute
= 0;
2326 usbvision
->AudioChannel
= USBVISION_AUDIO_MUTE
;
2330 int usbvision_set_audio(struct usb_usbvision
*usbvision
, int AudioChannel
)
2332 if (!usbvision
->AudioMute
) {
2333 if (usbvision_write_reg(usbvision
, USBVISION_IOPIN_REG
, AudioChannel
) < 0) {
2334 printk(KERN_ERR
"usbvision_set_audio: can't write iopin register for audio switching\n");
2338 usbvision
->AudioChannel
= AudioChannel
;
2342 int usbvision_setup(struct usb_usbvision
*usbvision
,int format
)
2344 usbvision_set_video_format(usbvision
, format
);
2345 usbvision_set_dram_settings(usbvision
);
2346 usbvision_set_compress_params(usbvision
);
2347 usbvision_set_input(usbvision
);
2348 usbvision_set_output(usbvision
, MAX_USB_WIDTH
, MAX_USB_HEIGHT
);
2349 usbvision_restart_isoc(usbvision
);
2352 return USBVISION_IS_OPERATIONAL(usbvision
);
2356 * usbvision_init_isoc()
2359 int usbvision_init_isoc(struct usb_usbvision
*usbvision
)
2361 struct usb_device
*dev
= usbvision
->dev
;
2362 int bufIdx
, errCode
, regValue
;
2363 const int sb_size
= USBVISION_URB_FRAMES
* USBVISION_MAX_ISOC_PACKET_SIZE
;
2365 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2368 usbvision
->curFrame
= NULL
;
2369 scratch_reset(usbvision
);
2371 /* Alternate interface 1 is is the biggest frame size */
2372 errCode
= usb_set_interface(dev
, usbvision
->iface
, usbvision
->ifaceAltActive
);
2374 usbvision
->last_error
= errCode
;
2378 regValue
= (16 - usbvision_read_reg(usbvision
, USBVISION_ALTER_REG
)) & 0x0F;
2379 usbvision
->isocPacketSize
= (regValue
== 0) ? 0 : (regValue
* 64) - 1;
2380 PDEBUG(DBG_ISOC
, "ISO Packet Length:%d", usbvision
->isocPacketSize
);
2382 usbvision
->usb_bandwidth
= regValue
>> 1;
2383 PDEBUG(DBG_ISOC
, "USB Bandwidth Usage: %dMbit/Sec", usbvision
->usb_bandwidth
);
2387 /* We double buffer the Iso lists */
2389 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2393 urb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
2395 err("%s: usb_alloc_urb() failed", __FUNCTION__
);
2398 usbvision
->sbuf
[bufIdx
].urb
= urb
;
2399 usbvision
->sbuf
[bufIdx
].data
= usb_buffer_alloc(usbvision
->dev
, sb_size
, GFP_KERNEL
,&urb
->transfer_dma
);
2401 urb
->context
= usbvision
;
2402 urb
->pipe
= usb_rcvisocpipe(dev
, usbvision
->video_endp
);
2403 urb
->transfer_flags
= URB_ISO_ASAP
;
2405 urb
->transfer_buffer
= usbvision
->sbuf
[bufIdx
].data
;
2406 urb
->complete
= usbvision_isocIrq
;
2407 urb
->number_of_packets
= USBVISION_URB_FRAMES
;
2408 urb
->transfer_buffer_length
=
2409 usbvision
->isocPacketSize
* USBVISION_URB_FRAMES
;
2410 for (j
= k
= 0; j
< USBVISION_URB_FRAMES
; j
++,
2411 k
+= usbvision
->isocPacketSize
) {
2412 urb
->iso_frame_desc
[j
].offset
= k
;
2413 urb
->iso_frame_desc
[j
].length
= usbvision
->isocPacketSize
;
2418 /* Submit all URBs */
2419 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2420 errCode
= usb_submit_urb(usbvision
->sbuf
[bufIdx
].urb
, GFP_KERNEL
);
2422 err("%s: usb_submit_urb(%d) failed: error %d", __FUNCTION__
, bufIdx
, errCode
);
2426 usbvision
->streaming
= Stream_Idle
;
2427 PDEBUG(DBG_ISOC
, "%s: streaming=1 usbvision->video_endp=$%02x", __FUNCTION__
, usbvision
->video_endp
);
2432 * usbvision_stop_isoc()
2434 * This procedure stops streaming and deallocates URBs. Then it
2435 * activates zero-bandwidth alt. setting of the video interface.
2438 void usbvision_stop_isoc(struct usb_usbvision
*usbvision
)
2440 int bufIdx
, errCode
, regValue
;
2441 const int sb_size
= USBVISION_URB_FRAMES
* USBVISION_MAX_ISOC_PACKET_SIZE
;
2443 if ((usbvision
->streaming
== Stream_Off
) || (usbvision
->dev
== NULL
))
2446 /* Unschedule all of the iso td's */
2447 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2448 usb_kill_urb(usbvision
->sbuf
[bufIdx
].urb
);
2449 if (usbvision
->sbuf
[bufIdx
].data
){
2450 usb_buffer_free(usbvision
->dev
,
2452 usbvision
->sbuf
[bufIdx
].data
,
2453 usbvision
->sbuf
[bufIdx
].urb
->transfer_dma
);
2455 usb_free_urb(usbvision
->sbuf
[bufIdx
].urb
);
2456 usbvision
->sbuf
[bufIdx
].urb
= NULL
;
2460 PDEBUG(DBG_ISOC
, "%s: streaming=Stream_Off\n", __FUNCTION__
);
2461 usbvision
->streaming
= Stream_Off
;
2463 if (!usbvision
->remove_pending
) {
2465 /* Set packet size to 0 */
2466 errCode
= usb_set_interface(usbvision
->dev
, usbvision
->iface
,
2467 usbvision
->ifaceAltInactive
);
2469 err("%s: usb_set_interface() failed: error %d", __FUNCTION__
, errCode
);
2470 usbvision
->last_error
= errCode
;
2472 regValue
= (16 - usbvision_read_reg(usbvision
, USBVISION_ALTER_REG
)) & 0x0F;
2473 usbvision
->isocPacketSize
= (regValue
== 0) ? 0 : (regValue
* 64) - 1;
2474 PDEBUG(DBG_ISOC
, "ISO Packet Length:%d", usbvision
->isocPacketSize
);
2476 usbvision
->usb_bandwidth
= regValue
>> 1;
2477 PDEBUG(DBG_ISOC
, "USB Bandwidth Usage: %dMbit/Sec", usbvision
->usb_bandwidth
);
2481 int usbvision_muxsel(struct usb_usbvision
*usbvision
, int channel
)
2484 int audio
[]= {1, 0, 0, 0};
2485 struct v4l2_routing route
;
2486 //channel 0 is TV with audiochannel 1 (tuner mono)
2487 //channel 1 is Composite with audio channel 0 (line in)
2488 //channel 2 is S-Video with audio channel 0 (line in)
2489 //channel 3 is additional video inputs to the device with audio channel 0 (line in)
2491 RESTRICT_TO_RANGE(channel
, 0, usbvision
->video_inputs
);
2492 usbvision
->ctl_input
= channel
;
2493 route
.input
= SAA7115_COMPOSITE1
;
2494 call_i2c_clients(usbvision
, VIDIOC_INT_S_VIDEO_ROUTING
,&route
);
2495 call_i2c_clients(usbvision
, VIDIOC_S_INPUT
, &usbvision
->ctl_input
);
2497 // set the new channel
2498 // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
2499 // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
2501 switch (usbvision_device_data
[usbvision
->DevModel
].Codec
) {
2503 if (SwitchSVideoInput
) { // To handle problems with S-Video Input for some devices. Use SwitchSVideoInput parameter when loading the module.
2509 if (usbvision_device_data
[usbvision
->DevModel
].VideoChannels
== 4) {
2510 mode
[0] = 0; mode
[1] = 2; mode
[3] = 3; // Special for four input devices
2513 mode
[0] = 0; mode
[1] = 2; //modes for regular saa7113 devices
2517 mode
[0] = 0; mode
[1] = 1; mode
[2] = 7; //modes for saa7111
2520 mode
[0] = 0; mode
[1] = 1; mode
[2] = 7; //default modes
2522 route
.input
= mode
[channel
];
2523 call_i2c_clients(usbvision
, VIDIOC_INT_S_VIDEO_ROUTING
,&route
);
2524 usbvision
->channel
= channel
;
2525 usbvision_set_audio(usbvision
, audio
[channel
]);
2530 * Overrides for Emacs so that we follow Linus's tabbing style.
2531 * ---------------------------------------------------------------------------