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/list.h>
28 #include <linux/timer.h>
29 #include <linux/slab.h>
31 #include <linux/utsname.h>
32 #include <linux/highmem.h>
33 #include <linux/vmalloc.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/spinlock.h>
38 #include <linux/videodev2.h>
39 #include <linux/video_decoder.h>
40 #include <linux/i2c.h>
42 #include <media/saa7115.h>
43 #include <media/v4l2-common.h>
44 #include <media/tuner.h>
46 #include <linux/workqueue.h>
48 #include "usbvision.h"
50 static unsigned int core_debug
;
51 module_param(core_debug
,int,0644);
52 MODULE_PARM_DESC(core_debug
,"enable debug messages [core]");
54 static unsigned int force_testpattern
;
55 module_param(force_testpattern
,int,0644);
56 MODULE_PARM_DESC(force_testpattern
,"enable test pattern display [core]");
58 static int adjustCompression
= 1; /* Set the compression to be adaptive */
59 module_param(adjustCompression
, int, 0444);
60 MODULE_PARM_DESC(adjustCompression
, " Set the ADPCM compression for the device. Default: 1 (On)");
62 /* To help people with Black and White output with using s-video input.
63 * Some cables and input device are wired differently. */
64 static int SwitchSVideoInput
;
65 module_param(SwitchSVideoInput
, int, 0444);
66 MODULE_PARM_DESC(SwitchSVideoInput
, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
68 static unsigned int adjust_X_Offset
= -1;
69 module_param(adjust_X_Offset
, int, 0644);
70 MODULE_PARM_DESC(adjust_X_Offset
, "adjust X offset display [core]");
72 static unsigned int adjust_Y_Offset
= -1;
73 module_param(adjust_Y_Offset
, int, 0644);
74 MODULE_PARM_DESC(adjust_Y_Offset
, "adjust Y offset display [core]");
77 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
80 #ifdef USBVISION_DEBUG
81 #define PDEBUG(level, fmt, args...) { \
82 if (core_debug & (level)) \
83 printk(KERN_INFO KBUILD_MODNAME ":[%s:%d] " fmt, \
84 __func__, __LINE__ , ## args); \
87 #define PDEBUG(level, fmt, args...) do {} while(0)
90 #define DBG_HEADER 1<<0
93 #define DBG_PARSE 1<<3
94 #define DBG_SCRATCH 1<<4
97 static const int max_imgwidth
= MAX_FRAME_WIDTH
;
98 static const int max_imgheight
= MAX_FRAME_HEIGHT
;
99 static const int min_imgwidth
= MIN_FRAME_WIDTH
;
100 static const int min_imgheight
= MIN_FRAME_HEIGHT
;
102 /* The value of 'scratch_buf_size' affects quality of the picture
103 * in many ways. Shorter buffers may cause loss of data when client
104 * is too slow. Larger buffers are memory-consuming and take longer
105 * to work with. This setting can be adjusted, but the default value
106 * should be OK for most desktop users.
108 #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
109 static const int scratch_buf_size
= DEFAULT_SCRATCH_BUF_SIZE
;
111 // Function prototypes
112 static int usbvision_request_intra (struct usb_usbvision
*usbvision
);
113 static int usbvision_unrequest_intra (struct usb_usbvision
*usbvision
);
114 static int usbvision_adjust_compression (struct usb_usbvision
*usbvision
);
115 static int usbvision_measure_bandwidth (struct usb_usbvision
*usbvision
);
117 /*******************************/
118 /* Memory management functions */
119 /*******************************/
122 * Here we want the physical address of the memory.
123 * This is used when initializing the contents of the area.
126 static void *usbvision_rvmalloc(unsigned long size
)
131 size
= PAGE_ALIGN(size
);
132 mem
= vmalloc_32(size
);
136 memset(mem
, 0, size
); /* Clear the ram out, no junk to the user */
137 adr
= (unsigned long) mem
;
139 SetPageReserved(vmalloc_to_page((void *)adr
));
147 static void usbvision_rvfree(void *mem
, unsigned long size
)
154 size
= PAGE_ALIGN(size
);
156 adr
= (unsigned long) mem
;
157 while ((long) size
> 0) {
158 ClearPageReserved(vmalloc_to_page((void *)adr
));
168 static void usbvision_hexdump(const unsigned char *data
, int len
)
173 for (i
= k
= 0; len
> 0; i
++, len
--) {
174 if (i
> 0 && (i
% 16 == 0)) {
178 k
+= sprintf(&tmp
[k
], "%02x ", data
[i
]);
185 /********************************
186 * scratch ring buffer handling
187 ********************************/
188 static int scratch_len(struct usb_usbvision
*usbvision
) /*This returns the amount of data actually in the buffer */
190 int len
= usbvision
->scratch_write_ptr
- usbvision
->scratch_read_ptr
;
192 len
+= scratch_buf_size
;
194 PDEBUG(DBG_SCRATCH
, "scratch_len() = %d\n", len
);
200 /* This returns the free space left in the buffer */
201 static int scratch_free(struct usb_usbvision
*usbvision
)
203 int free
= usbvision
->scratch_read_ptr
- usbvision
->scratch_write_ptr
;
205 free
+= scratch_buf_size
;
208 free
-= 1; /* at least one byte in the buffer must */
209 /* left blank, otherwise there is no chance to differ between full and empty */
211 PDEBUG(DBG_SCRATCH
, "return %d\n", free
);
217 /* This puts data into the buffer */
218 static int scratch_put(struct usb_usbvision
*usbvision
, unsigned char *data
,
223 if (usbvision
->scratch_write_ptr
+ len
< scratch_buf_size
) {
224 memcpy(usbvision
->scratch
+ usbvision
->scratch_write_ptr
, data
, len
);
225 usbvision
->scratch_write_ptr
+= len
;
228 len_part
= scratch_buf_size
- usbvision
->scratch_write_ptr
;
229 memcpy(usbvision
->scratch
+ usbvision
->scratch_write_ptr
, data
, len_part
);
230 if (len
== len_part
) {
231 usbvision
->scratch_write_ptr
= 0; /* just set write_ptr to zero */
234 memcpy(usbvision
->scratch
, data
+ len_part
, len
- len_part
);
235 usbvision
->scratch_write_ptr
= len
- len_part
;
239 PDEBUG(DBG_SCRATCH
, "len=%d, new write_ptr=%d\n", len
, usbvision
->scratch_write_ptr
);
244 /* This marks the write_ptr as position of new frame header */
245 static void scratch_mark_header(struct usb_usbvision
*usbvision
)
247 PDEBUG(DBG_SCRATCH
, "header at write_ptr=%d\n", usbvision
->scratch_headermarker_write_ptr
);
249 usbvision
->scratch_headermarker
[usbvision
->scratch_headermarker_write_ptr
] =
250 usbvision
->scratch_write_ptr
;
251 usbvision
->scratch_headermarker_write_ptr
+= 1;
252 usbvision
->scratch_headermarker_write_ptr
%= USBVISION_NUM_HEADERMARKER
;
255 /* This gets data from the buffer at the given "ptr" position */
256 static int scratch_get_extra(struct usb_usbvision
*usbvision
,
257 unsigned char *data
, int *ptr
, int len
)
260 if (*ptr
+ len
< scratch_buf_size
) {
261 memcpy(data
, usbvision
->scratch
+ *ptr
, len
);
265 len_part
= scratch_buf_size
- *ptr
;
266 memcpy(data
, usbvision
->scratch
+ *ptr
, len_part
);
267 if (len
== len_part
) {
268 *ptr
= 0; /* just set the y_ptr to zero */
271 memcpy(data
+ len_part
, usbvision
->scratch
, len
- len_part
);
272 *ptr
= len
- len_part
;
276 PDEBUG(DBG_SCRATCH
, "len=%d, new ptr=%d\n", len
, *ptr
);
282 /* This sets the scratch extra read pointer */
283 static void scratch_set_extra_ptr(struct usb_usbvision
*usbvision
, int *ptr
,
286 *ptr
= (usbvision
->scratch_read_ptr
+ len
)%scratch_buf_size
;
288 PDEBUG(DBG_SCRATCH
, "ptr=%d\n", *ptr
);
292 /*This increments the scratch extra read pointer */
293 static void scratch_inc_extra_ptr(int *ptr
, int len
)
295 *ptr
= (*ptr
+ len
) % scratch_buf_size
;
297 PDEBUG(DBG_SCRATCH
, "ptr=%d\n", *ptr
);
301 /* This gets data from the buffer */
302 static int scratch_get(struct usb_usbvision
*usbvision
, unsigned char *data
,
306 if (usbvision
->scratch_read_ptr
+ len
< scratch_buf_size
) {
307 memcpy(data
, usbvision
->scratch
+ usbvision
->scratch_read_ptr
, len
);
308 usbvision
->scratch_read_ptr
+= len
;
311 len_part
= scratch_buf_size
- usbvision
->scratch_read_ptr
;
312 memcpy(data
, usbvision
->scratch
+ usbvision
->scratch_read_ptr
, len_part
);
313 if (len
== len_part
) {
314 usbvision
->scratch_read_ptr
= 0; /* just set the read_ptr to zero */
317 memcpy(data
+ len_part
, usbvision
->scratch
, len
- len_part
);
318 usbvision
->scratch_read_ptr
= len
- len_part
;
322 PDEBUG(DBG_SCRATCH
, "len=%d, new read_ptr=%d\n", len
, usbvision
->scratch_read_ptr
);
328 /* This sets read pointer to next header and returns it */
329 static int scratch_get_header(struct usb_usbvision
*usbvision
,
330 struct usbvision_frame_header
*header
)
334 PDEBUG(DBG_SCRATCH
, "from read_ptr=%d", usbvision
->scratch_headermarker_read_ptr
);
336 while (usbvision
->scratch_headermarker_write_ptr
-
337 usbvision
->scratch_headermarker_read_ptr
!= 0) {
338 usbvision
->scratch_read_ptr
=
339 usbvision
->scratch_headermarker
[usbvision
->scratch_headermarker_read_ptr
];
340 usbvision
->scratch_headermarker_read_ptr
+= 1;
341 usbvision
->scratch_headermarker_read_ptr
%= USBVISION_NUM_HEADERMARKER
;
342 scratch_get(usbvision
, (unsigned char *)header
, USBVISION_HEADER_LENGTH
);
343 if ((header
->magic_1
== USBVISION_MAGIC_1
)
344 && (header
->magic_2
== USBVISION_MAGIC_2
)
345 && (header
->headerLength
== USBVISION_HEADER_LENGTH
)) {
346 errCode
= USBVISION_HEADER_LENGTH
;
347 header
->frameWidth
= header
->frameWidthLo
+ (header
->frameWidthHi
<< 8);
348 header
->frameHeight
= header
->frameHeightLo
+ (header
->frameHeightHi
<< 8);
357 /*This removes len bytes of old data from the buffer */
358 static void scratch_rm_old(struct usb_usbvision
*usbvision
, int len
)
361 usbvision
->scratch_read_ptr
+= len
;
362 usbvision
->scratch_read_ptr
%= scratch_buf_size
;
363 PDEBUG(DBG_SCRATCH
, "read_ptr is now %d\n", usbvision
->scratch_read_ptr
);
367 /*This resets the buffer - kills all data in it too */
368 static void scratch_reset(struct usb_usbvision
*usbvision
)
370 PDEBUG(DBG_SCRATCH
, "\n");
372 usbvision
->scratch_read_ptr
= 0;
373 usbvision
->scratch_write_ptr
= 0;
374 usbvision
->scratch_headermarker_read_ptr
= 0;
375 usbvision
->scratch_headermarker_write_ptr
= 0;
376 usbvision
->isocstate
= IsocState_NoFrame
;
379 int usbvision_scratch_alloc(struct usb_usbvision
*usbvision
)
381 usbvision
->scratch
= vmalloc_32(scratch_buf_size
);
382 scratch_reset(usbvision
);
383 if(usbvision
->scratch
== NULL
) {
384 dev_err(&usbvision
->dev
->dev
,
385 "%s: unable to allocate %d bytes for scratch\n",
386 __func__
, scratch_buf_size
);
392 void usbvision_scratch_free(struct usb_usbvision
*usbvision
)
394 if (usbvision
->scratch
!= NULL
) {
395 vfree(usbvision
->scratch
);
396 usbvision
->scratch
= NULL
;
401 * usbvision_testpattern()
403 * Procedure forms a test pattern (yellow grid on blue background).
406 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
407 * continues from the current scanline.
408 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
409 * 1: Draw a colored grid
412 static void usbvision_testpattern(struct usb_usbvision
*usbvision
,
413 int fullframe
, int pmode
)
415 static const char proc
[] = "usbvision_testpattern";
416 struct usbvision_frame
*frame
;
422 if (usbvision
== NULL
) {
423 printk(KERN_ERR
"%s: usbvision == NULL\n", proc
);
426 if (usbvision
->curFrame
== NULL
) {
427 printk(KERN_ERR
"%s: usbvision->curFrame is NULL.\n", proc
);
431 /* Grab the current frame */
432 frame
= usbvision
->curFrame
;
434 /* Optionally start at the beginning */
437 frame
->scanlength
= 0;
440 /* Form every scan line */
441 for (; frame
->curline
< frame
->frmheight
; frame
->curline
++) {
444 f
= frame
->data
+ (usbvision
->curwidth
* 3 * frame
->curline
);
445 for (i
= 0; i
< usbvision
->curwidth
; i
++) {
446 unsigned char cb
= 0x80;
447 unsigned char cg
= 0;
448 unsigned char cr
= 0;
451 if (frame
->curline
% 32 == 0)
452 cb
= 0, cg
= cr
= 0xFF;
453 else if (i
% 32 == 0) {
454 if (frame
->curline
% 32 == 1)
456 cb
= 0, cg
= cr
= 0xFF;
463 num_pass
* 2) & 0xFF;
466 num_pass
* 3) & 0xFF;
469 /* Just the blue screen */
479 frame
->grabstate
= FrameState_Done
;
480 frame
->scanlength
+= scan_length
;
486 * usbvision_decompress_alloc()
488 * allocates intermediate buffer for decompression
490 int usbvision_decompress_alloc(struct usb_usbvision
*usbvision
)
492 int IFB_size
= MAX_FRAME_WIDTH
* MAX_FRAME_HEIGHT
* 3 / 2;
493 usbvision
->IntraFrameBuffer
= vmalloc_32(IFB_size
);
494 if (usbvision
->IntraFrameBuffer
== NULL
) {
495 dev_err(&usbvision
->dev
->dev
,
496 "%s: unable to allocate %d for compr. frame buffer\n",
504 * usbvision_decompress_free()
506 * frees intermediate buffer for decompression
508 void usbvision_decompress_free(struct usb_usbvision
*usbvision
)
510 if (usbvision
->IntraFrameBuffer
!= NULL
) {
511 vfree(usbvision
->IntraFrameBuffer
);
512 usbvision
->IntraFrameBuffer
= NULL
;
516 /************************************************************
517 * Here comes the data parsing stuff that is run as interrupt
518 ************************************************************/
520 * usbvision_find_header()
522 * Locate one of supported header markers in the scratch buffer.
524 static enum ParseState
usbvision_find_header(struct usb_usbvision
*usbvision
)
526 struct usbvision_frame
*frame
;
529 frame
= usbvision
->curFrame
;
531 while (scratch_get_header(usbvision
, &frame
->isocHeader
) == USBVISION_HEADER_LENGTH
) {
532 // found header in scratch
533 PDEBUG(DBG_HEADER
, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
534 frame
->isocHeader
.magic_2
,
535 frame
->isocHeader
.magic_1
,
536 frame
->isocHeader
.headerLength
,
537 frame
->isocHeader
.frameNum
,
538 frame
->isocHeader
.framePhase
,
539 frame
->isocHeader
.frameLatency
,
540 frame
->isocHeader
.dataFormat
,
541 frame
->isocHeader
.formatParam
,
542 frame
->isocHeader
.frameWidth
,
543 frame
->isocHeader
.frameHeight
);
545 if (usbvision
->requestIntra
) {
546 if (frame
->isocHeader
.formatParam
& 0x80) {
548 usbvision
->lastIsocFrameNum
= -1; // do not check for lost frames this time
549 usbvision_unrequest_intra(usbvision
);
560 frame
->frmwidth
= frame
->isocHeader
.frameWidth
* usbvision
->stretch_width
;
561 frame
->frmheight
= frame
->isocHeader
.frameHeight
* usbvision
->stretch_height
;
562 frame
->v4l2_linesize
= (frame
->frmwidth
* frame
->v4l2_format
.depth
)>> 3;
564 else { // no header found
565 PDEBUG(DBG_HEADER
, "skipping scratch data, no header");
566 scratch_reset(usbvision
);
567 return ParseState_EndParse
;
571 if (frame
->isocHeader
.dataFormat
==ISOC_MODE_COMPRESS
) {
572 //check isocHeader.frameNum for lost frames
573 if (usbvision
->lastIsocFrameNum
>= 0) {
574 if (((usbvision
->lastIsocFrameNum
+ 1) % 32) != frame
->isocHeader
.frameNum
) {
575 // unexpected frame drop: need to request new intra frame
576 PDEBUG(DBG_HEADER
, "Lost frame before %d on USB", frame
->isocHeader
.frameNum
);
577 usbvision_request_intra(usbvision
);
578 return ParseState_NextFrame
;
581 usbvision
->lastIsocFrameNum
= frame
->isocHeader
.frameNum
;
583 usbvision
->header_count
++;
584 frame
->scanstate
= ScanState_Lines
;
587 if (force_testpattern
) {
588 usbvision_testpattern(usbvision
, 1, 1);
589 return ParseState_NextFrame
;
591 return ParseState_Continue
;
594 static enum ParseState
usbvision_parse_lines_422(struct usb_usbvision
*usbvision
,
597 volatile struct usbvision_frame
*frame
;
601 unsigned char yuyv
[4]={180, 128, 10, 128}; // YUV components
602 unsigned char rv
, gv
, bv
; // RGB components
603 int clipmask_index
, bytes_per_pixel
;
604 int stretch_bytes
, clipmask_add
;
606 frame
= usbvision
->curFrame
;
607 f
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
609 /* Make sure there's enough data for the entire line */
610 len
= (frame
->isocHeader
.frameWidth
* 2)+5;
611 if (scratch_len(usbvision
) < len
) {
612 PDEBUG(DBG_PARSE
, "out of data in line %d, need %u.\n", frame
->curline
, len
);
613 return ParseState_Out
;
616 if ((frame
->curline
+ 1) >= frame
->frmheight
) {
617 return ParseState_NextFrame
;
620 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
621 stretch_bytes
= (usbvision
->stretch_width
- 1) * bytes_per_pixel
;
622 clipmask_index
= frame
->curline
* MAX_FRAME_WIDTH
;
623 clipmask_add
= usbvision
->stretch_width
;
625 for (i
= 0; i
< frame
->frmwidth
; i
+=(2 * usbvision
->stretch_width
)) {
627 scratch_get(usbvision
, &yuyv
[0], 4);
629 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
635 YUV_TO_RGB_BY_THE_BOOK(yuyv
[0], yuyv
[1], yuyv
[3], rv
, gv
, bv
);
636 switch (frame
->v4l2_format
.format
) {
637 case V4L2_PIX_FMT_RGB565
:
640 *f
++ = (0x07 & (gv
>> 3)) |
643 case V4L2_PIX_FMT_RGB24
:
648 case V4L2_PIX_FMT_RGB32
:
654 case V4L2_PIX_FMT_RGB555
:
657 *f
++ = (0x03 & (gv
>> 3)) |
662 clipmask_index
+= clipmask_add
;
665 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
671 YUV_TO_RGB_BY_THE_BOOK(yuyv
[2], yuyv
[1], yuyv
[3], rv
, gv
, bv
);
672 switch (frame
->v4l2_format
.format
) {
673 case V4L2_PIX_FMT_RGB565
:
676 *f
++ = (0x07 & (gv
>> 3)) |
679 case V4L2_PIX_FMT_RGB24
:
684 case V4L2_PIX_FMT_RGB32
:
690 case V4L2_PIX_FMT_RGB555
:
693 *f
++ = (0x03 & (gv
>> 3)) |
698 clipmask_index
+= clipmask_add
;
702 frame
->curline
+= usbvision
->stretch_height
;
703 *pcopylen
+= frame
->v4l2_linesize
* usbvision
->stretch_height
;
705 if (frame
->curline
>= frame
->frmheight
) {
706 return ParseState_NextFrame
;
709 return ParseState_Continue
;
713 /* The decompression routine */
714 static int usbvision_decompress(struct usb_usbvision
*usbvision
,unsigned char *Compressed
,
715 unsigned char *Decompressed
, int *StartPos
,
716 int *BlockTypeStartPos
, int Len
)
718 int RestPixel
, Idx
, MaxPos
, Pos
, ExtraPos
, BlockLen
, BlockTypePos
, BlockTypeLen
;
719 unsigned char BlockByte
, BlockCode
, BlockType
, BlockTypeByte
, Integrator
;
723 BlockTypePos
= *BlockTypeStartPos
;
724 MaxPos
= 396; //Pos + Len;
734 for (Idx
= 0; Idx
< Len
; Idx
++) {
737 if (BlockTypeLen
==0) {
738 BlockTypeByte
= Compressed
[BlockTypePos
];
742 BlockType
= (BlockTypeByte
& 0xC0) >> 6;
745 usbvision
->ComprBlockTypes
[BlockType
]++;
748 if (BlockType
== 0) {
749 if(RestPixel
>= 24) {
752 Integrator
= Decompressed
[Idx
];
754 Idx
+= RestPixel
- 1;
758 BlockCode
= Compressed
[Pos
];
760 if (RestPixel
>= 24) {
763 BlockLen
= RestPixel
;
765 RestPixel
-= BlockLen
;
766 ExtraPos
= Pos
+ (BlockLen
/ 4);
772 if ((BlockLen
%4) == 0) {
773 BlockByte
= Compressed
[Pos
];
776 if (BlockType
== 1) { //inter Block
777 Integrator
= Decompressed
[Idx
];
779 switch (BlockByte
& 0xC0) {
781 Integrator
+= Compressed
[ExtraPos
];
785 Integrator
+= BlockCode
;
788 Integrator
-= BlockCode
;
791 Decompressed
[Idx
] = Integrator
;
796 *StartPos
= ExtraPos
;
797 *BlockTypeStartPos
= BlockTypePos
;
803 * usbvision_parse_compress()
805 * Parse compressed frame from the scratch buffer, put
806 * decoded RGB value into the current frame buffer and add the written
807 * number of bytes (RGB) to the *pcopylen.
810 static enum ParseState
usbvision_parse_compress(struct usb_usbvision
*usbvision
,
813 #define USBVISION_STRIP_MAGIC 0x5A
814 #define USBVISION_STRIP_LEN_MAX 400
815 #define USBVISION_STRIP_HEADER_LEN 3
817 struct usbvision_frame
*frame
;
818 unsigned char *f
,*u
= NULL
,*v
= NULL
;
819 unsigned char StripData
[USBVISION_STRIP_LEN_MAX
];
820 unsigned char StripHeader
[USBVISION_STRIP_HEADER_LEN
];
821 int Idx
, IdxEnd
, StripLen
, StripPtr
, StartBlockPos
, BlockPos
, BlockTypePos
;
822 int clipmask_index
, bytes_per_pixel
, rc
;
824 unsigned char rv
, gv
, bv
;
825 static unsigned char *Y
, *U
, *V
;
827 frame
= usbvision
->curFrame
;
828 imageSize
= frame
->frmwidth
* frame
->frmheight
;
829 if ( (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUV422P
) ||
830 (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
) ) { // this is a planar format
831 //... v4l2_linesize not used here.
832 f
= frame
->data
+ (frame
->width
* frame
->curline
);
834 f
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
836 if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
){ //initialise u and v pointers
837 // get base of u and b planes add halfoffset
841 + (frame
->frmwidth
>>1) * frame
->curline
;
842 v
= u
+ (imageSize
>>1 );
844 } else if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
){
846 v
= frame
->data
+ imageSize
+ ((frame
->curline
* (frame
->width
))>>2) ;
847 u
= v
+ (imageSize
>>2) ;
850 if (frame
->curline
== 0) {
851 usbvision_adjust_compression(usbvision
);
854 if (scratch_len(usbvision
) < USBVISION_STRIP_HEADER_LEN
) {
855 return ParseState_Out
;
858 //get strip header without changing the scratch_read_ptr
859 scratch_set_extra_ptr(usbvision
, &StripPtr
, 0);
860 scratch_get_extra(usbvision
, &StripHeader
[0], &StripPtr
,
861 USBVISION_STRIP_HEADER_LEN
);
863 if (StripHeader
[0] != USBVISION_STRIP_MAGIC
) {
865 usbvision
->stripMagicErrors
++;
866 return ParseState_NextFrame
;
869 if (frame
->curline
!= (int)StripHeader
[2]) {
870 //line number missmatch error
871 usbvision
->stripLineNumberErrors
++;
874 StripLen
= 2 * (unsigned int)StripHeader
[1];
875 if (StripLen
> USBVISION_STRIP_LEN_MAX
) {
877 // I think this never happens
878 usbvision_request_intra(usbvision
);
881 if (scratch_len(usbvision
) < StripLen
) {
882 //there is not enough data for the strip
883 return ParseState_Out
;
886 if (usbvision
->IntraFrameBuffer
) {
887 Y
= usbvision
->IntraFrameBuffer
+ frame
->frmwidth
* frame
->curline
;
888 U
= usbvision
->IntraFrameBuffer
+ imageSize
+ (frame
->frmwidth
/ 2) * (frame
->curline
/ 2);
889 V
= usbvision
->IntraFrameBuffer
+ imageSize
/ 4 * 5 + (frame
->frmwidth
/ 2) * (frame
->curline
/ 2);
892 return ParseState_NextFrame
;
895 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
896 clipmask_index
= frame
->curline
* MAX_FRAME_WIDTH
;
898 scratch_get(usbvision
, StripData
, StripLen
);
900 IdxEnd
= frame
->frmwidth
;
901 BlockTypePos
= USBVISION_STRIP_HEADER_LEN
;
902 StartBlockPos
= BlockTypePos
+ (IdxEnd
- 1) / 96 + (IdxEnd
/ 2 - 1) / 96 + 2;
903 BlockPos
= StartBlockPos
;
905 usbvision
->BlockPos
= BlockPos
;
907 if ((rc
= usbvision_decompress(usbvision
, StripData
, Y
, &BlockPos
, &BlockTypePos
, IdxEnd
)) != IdxEnd
) {
908 //return ParseState_Continue;
910 if (StripLen
> usbvision
->maxStripLen
) {
911 usbvision
->maxStripLen
= StripLen
;
914 if (frame
->curline
%2) {
915 if ((rc
= usbvision_decompress(usbvision
, StripData
, V
, &BlockPos
, &BlockTypePos
, IdxEnd
/2)) != IdxEnd
/2) {
916 //return ParseState_Continue;
920 if ((rc
= usbvision_decompress(usbvision
, StripData
, U
, &BlockPos
, &BlockTypePos
, IdxEnd
/2)) != IdxEnd
/2) {
921 //return ParseState_Continue;
925 if (BlockPos
> usbvision
->comprBlockPos
) {
926 usbvision
->comprBlockPos
= BlockPos
;
928 if (BlockPos
> StripLen
) {
929 usbvision
->stripLenErrors
++;
932 for (Idx
= 0; Idx
< IdxEnd
; Idx
++) {
933 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
935 *f
++ = Idx
& 0x01 ? U
[Idx
/2] : V
[Idx
/2];
937 else if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUV422P
) {
944 else if (frame
->v4l2_format
.format
== V4L2_PIX_FMT_YVU420
) {
946 if ( !(( Idx
& 0x01 ) | ( frame
->curline
& 0x01 )) ){
948 /* only need do this for 1 in 4 pixels */
949 /* intraframe buffer is YUV420 format */
957 YUV_TO_RGB_BY_THE_BOOK(Y
[Idx
], U
[Idx
/2], V
[Idx
/2], rv
, gv
, bv
);
958 switch (frame
->v4l2_format
.format
) {
959 case V4L2_PIX_FMT_GREY
:
962 case V4L2_PIX_FMT_RGB555
:
965 *f
++ = (0x03 & (gv
>> 3)) |
968 case V4L2_PIX_FMT_RGB565
:
971 *f
++ = (0x07 & (gv
>> 3)) |
974 case V4L2_PIX_FMT_RGB24
:
979 case V4L2_PIX_FMT_RGB32
:
989 /* Deal with non-integer no. of bytes for YUV420P */
990 if (frame
->v4l2_format
.format
!= V4L2_PIX_FMT_YVU420
)
991 *pcopylen
+= frame
->v4l2_linesize
;
993 *pcopylen
+= frame
->curline
& 0x01 ? frame
->v4l2_linesize
: frame
->v4l2_linesize
<< 1;
997 if (frame
->curline
>= frame
->frmheight
) {
998 return ParseState_NextFrame
;
1001 return ParseState_Continue
;
1008 * usbvision_parse_lines_420()
1010 * Parse two lines from the scratch buffer, put
1011 * decoded RGB value into the current frame buffer and add the written
1012 * number of bytes (RGB) to the *pcopylen.
1015 static enum ParseState
usbvision_parse_lines_420(struct usb_usbvision
*usbvision
,
1018 struct usbvision_frame
*frame
;
1019 unsigned char *f_even
= NULL
, *f_odd
= NULL
;
1020 unsigned int pixel_per_line
, block
;
1021 int pixel
, block_split
;
1022 int y_ptr
, u_ptr
, v_ptr
, y_odd_offset
;
1023 const int y_block_size
= 128;
1024 const int uv_block_size
= 64;
1025 const int sub_block_size
= 32;
1026 const int y_step
[] = { 0, 0, 0, 2 }, y_step_size
= 4;
1027 const int uv_step
[]= { 0, 0, 0, 4 }, uv_step_size
= 4;
1028 unsigned char y
[2], u
, v
; /* YUV components */
1029 int y_
, u_
, v_
, vb
, uvg
, ur
;
1030 int r_
, g_
, b_
; /* RGB components */
1032 int clipmask_even_index
, clipmask_odd_index
, bytes_per_pixel
;
1033 int clipmask_add
, stretch_bytes
;
1035 frame
= usbvision
->curFrame
;
1036 f_even
= frame
->data
+ (frame
->v4l2_linesize
* frame
->curline
);
1037 f_odd
= f_even
+ frame
->v4l2_linesize
* usbvision
->stretch_height
;
1039 /* Make sure there's enough data for the entire line */
1040 /* In this mode usbvision transfer 3 bytes for every 2 pixels */
1041 /* I need two lines to decode the color */
1042 bytes_per_pixel
= frame
->v4l2_format
.bytes_per_pixel
;
1043 stretch_bytes
= (usbvision
->stretch_width
- 1) * bytes_per_pixel
;
1044 clipmask_even_index
= frame
->curline
* MAX_FRAME_WIDTH
;
1045 clipmask_odd_index
= clipmask_even_index
+ MAX_FRAME_WIDTH
;
1046 clipmask_add
= usbvision
->stretch_width
;
1047 pixel_per_line
= frame
->isocHeader
.frameWidth
;
1049 if (scratch_len(usbvision
) < (int)pixel_per_line
* 3) {
1050 //printk(KERN_DEBUG "out of data, need %d\n", len);
1051 return ParseState_Out
;
1054 if ((frame
->curline
+ 1) >= frame
->frmheight
) {
1055 return ParseState_NextFrame
;
1058 block_split
= (pixel_per_line
%y_block_size
) ? 1 : 0; //are some blocks splitted into different lines?
1060 y_odd_offset
= (pixel_per_line
/ y_block_size
) * (y_block_size
+ uv_block_size
)
1061 + block_split
* uv_block_size
;
1063 scratch_set_extra_ptr(usbvision
, &y_ptr
, y_odd_offset
);
1064 scratch_set_extra_ptr(usbvision
, &u_ptr
, y_block_size
);
1065 scratch_set_extra_ptr(usbvision
, &v_ptr
, y_odd_offset
1066 + (4 - block_split
) * sub_block_size
);
1068 for (block
= 0; block
< (pixel_per_line
/ sub_block_size
);
1072 for (pixel
= 0; pixel
< sub_block_size
; pixel
+=2) {
1073 scratch_get(usbvision
, &y
[0], 2);
1074 scratch_get_extra(usbvision
, &u
, &u_ptr
, 1);
1075 scratch_get_extra(usbvision
, &v
, &v_ptr
, 1);
1077 //I don't use the YUV_TO_RGB macro for better performance
1081 uvg
= -53281 * u_
- 25625 * v_
;
1084 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1089 y_
= 76284 * (y
[0] - 16);
1091 b_
= (y_
+ vb
) >> 16;
1092 g_
= (y_
+ uvg
)>> 16;
1093 r_
= (y_
+ ur
) >> 16;
1095 switch (frame
->v4l2_format
.format
) {
1096 case V4L2_PIX_FMT_RGB565
:
1099 (0x1F & LIMIT_RGB(r_
)) |
1103 (0xF8 & LIMIT_RGB(b_
));
1105 case V4L2_PIX_FMT_RGB24
:
1106 *f_even
++ = LIMIT_RGB(r_
);
1107 *f_even
++ = LIMIT_RGB(g_
);
1108 *f_even
++ = LIMIT_RGB(b_
);
1110 case V4L2_PIX_FMT_RGB32
:
1111 *f_even
++ = LIMIT_RGB(r_
);
1112 *f_even
++ = LIMIT_RGB(g_
);
1113 *f_even
++ = LIMIT_RGB(b_
);
1116 case V4L2_PIX_FMT_RGB555
:
1118 *f_even
++ = (0x1F & LIMIT_RGB(r_
)) |
1120 *f_even
++ = (0x03 & (g
>> 3)) |
1121 (0x7C & (LIMIT_RGB(b_
) << 2));
1125 clipmask_even_index
+= clipmask_add
;
1126 f_even
+= stretch_bytes
;
1128 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1133 y_
= 76284 * (y
[1] - 16);
1135 b_
= (y_
+ vb
) >> 16;
1136 g_
= (y_
+ uvg
)>> 16;
1137 r_
= (y_
+ ur
) >> 16;
1139 switch (frame
->v4l2_format
.format
) {
1140 case V4L2_PIX_FMT_RGB565
:
1143 (0x1F & LIMIT_RGB(r_
)) |
1147 (0xF8 & LIMIT_RGB(b_
));
1149 case V4L2_PIX_FMT_RGB24
:
1150 *f_even
++ = LIMIT_RGB(r_
);
1151 *f_even
++ = LIMIT_RGB(g_
);
1152 *f_even
++ = LIMIT_RGB(b_
);
1154 case V4L2_PIX_FMT_RGB32
:
1155 *f_even
++ = LIMIT_RGB(r_
);
1156 *f_even
++ = LIMIT_RGB(g_
);
1157 *f_even
++ = LIMIT_RGB(b_
);
1160 case V4L2_PIX_FMT_RGB555
:
1162 *f_even
++ = (0x1F & LIMIT_RGB(r_
)) |
1164 *f_even
++ = (0x03 & (g
>> 3)) |
1165 (0x7C & (LIMIT_RGB(b_
) << 2));
1169 clipmask_even_index
+= clipmask_add
;
1170 f_even
+= stretch_bytes
;
1172 scratch_get_extra(usbvision
, &y
[0], &y_ptr
, 2);
1174 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1179 y_
= 76284 * (y
[0] - 16);
1181 b_
= (y_
+ vb
) >> 16;
1182 g_
= (y_
+ uvg
)>> 16;
1183 r_
= (y_
+ ur
) >> 16;
1185 switch (frame
->v4l2_format
.format
) {
1186 case V4L2_PIX_FMT_RGB565
:
1189 (0x1F & LIMIT_RGB(r_
)) |
1193 (0xF8 & LIMIT_RGB(b_
));
1195 case V4L2_PIX_FMT_RGB24
:
1196 *f_odd
++ = LIMIT_RGB(r_
);
1197 *f_odd
++ = LIMIT_RGB(g_
);
1198 *f_odd
++ = LIMIT_RGB(b_
);
1200 case V4L2_PIX_FMT_RGB32
:
1201 *f_odd
++ = LIMIT_RGB(r_
);
1202 *f_odd
++ = LIMIT_RGB(g_
);
1203 *f_odd
++ = LIMIT_RGB(b_
);
1206 case V4L2_PIX_FMT_RGB555
:
1208 *f_odd
++ = (0x1F & LIMIT_RGB(r_
)) |
1210 *f_odd
++ = (0x03 & (g
>> 3)) |
1211 (0x7C & (LIMIT_RGB(b_
) << 2));
1215 clipmask_odd_index
+= clipmask_add
;
1216 f_odd
+= stretch_bytes
;
1218 if(frame
->v4l2_format
.format
== V4L2_PIX_FMT_YUYV
) {
1223 y_
= 76284 * (y
[1] - 16);
1225 b_
= (y_
+ vb
) >> 16;
1226 g_
= (y_
+ uvg
)>> 16;
1227 r_
= (y_
+ ur
) >> 16;
1229 switch (frame
->v4l2_format
.format
) {
1230 case V4L2_PIX_FMT_RGB565
:
1233 (0x1F & LIMIT_RGB(r_
)) |
1237 (0xF8 & LIMIT_RGB(b_
));
1239 case V4L2_PIX_FMT_RGB24
:
1240 *f_odd
++ = LIMIT_RGB(r_
);
1241 *f_odd
++ = LIMIT_RGB(g_
);
1242 *f_odd
++ = LIMIT_RGB(b_
);
1244 case V4L2_PIX_FMT_RGB32
:
1245 *f_odd
++ = LIMIT_RGB(r_
);
1246 *f_odd
++ = LIMIT_RGB(g_
);
1247 *f_odd
++ = LIMIT_RGB(b_
);
1250 case V4L2_PIX_FMT_RGB555
:
1252 *f_odd
++ = (0x1F & LIMIT_RGB(r_
)) |
1254 *f_odd
++ = (0x03 & (g
>> 3)) |
1255 (0x7C & (LIMIT_RGB(b_
) << 2));
1259 clipmask_odd_index
+= clipmask_add
;
1260 f_odd
+= stretch_bytes
;
1263 scratch_rm_old(usbvision
,y_step
[block
% y_step_size
] * sub_block_size
);
1264 scratch_inc_extra_ptr(&y_ptr
, y_step
[(block
+ 2 * block_split
) % y_step_size
]
1266 scratch_inc_extra_ptr(&u_ptr
, uv_step
[block
% uv_step_size
]
1268 scratch_inc_extra_ptr(&v_ptr
, uv_step
[(block
+ 2 * block_split
) % uv_step_size
]
1272 scratch_rm_old(usbvision
, pixel_per_line
* 3 / 2
1273 + block_split
* sub_block_size
);
1275 frame
->curline
+= 2 * usbvision
->stretch_height
;
1276 *pcopylen
+= frame
->v4l2_linesize
* 2 * usbvision
->stretch_height
;
1278 if (frame
->curline
>= frame
->frmheight
)
1279 return ParseState_NextFrame
;
1281 return ParseState_Continue
;
1285 * usbvision_parse_data()
1287 * Generic routine to parse the scratch buffer. It employs either
1288 * usbvision_find_header() or usbvision_parse_lines() to do most
1292 static void usbvision_parse_data(struct usb_usbvision
*usbvision
)
1294 struct usbvision_frame
*frame
;
1295 enum ParseState newstate
;
1297 unsigned long lock_flags
;
1299 frame
= usbvision
->curFrame
;
1301 PDEBUG(DBG_PARSE
, "parsing len=%d\n", scratch_len(usbvision
));
1305 newstate
= ParseState_Out
;
1306 if (scratch_len(usbvision
)) {
1307 if (frame
->scanstate
== ScanState_Scanning
) {
1308 newstate
= usbvision_find_header(usbvision
);
1310 else if (frame
->scanstate
== ScanState_Lines
) {
1311 if (usbvision
->isocMode
== ISOC_MODE_YUV420
) {
1312 newstate
= usbvision_parse_lines_420(usbvision
, ©len
);
1314 else if (usbvision
->isocMode
== ISOC_MODE_YUV422
) {
1315 newstate
= usbvision_parse_lines_422(usbvision
, ©len
);
1317 else if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
1318 newstate
= usbvision_parse_compress(usbvision
, ©len
);
1323 if (newstate
== ParseState_Continue
) {
1326 else if ((newstate
== ParseState_NextFrame
) || (newstate
== ParseState_Out
)) {
1330 return; /* ParseState_EndParse */
1334 if (newstate
== ParseState_NextFrame
) {
1335 frame
->grabstate
= FrameState_Done
;
1336 do_gettimeofday(&(frame
->timestamp
));
1337 frame
->sequence
= usbvision
->frame_num
;
1339 spin_lock_irqsave(&usbvision
->queue_lock
, lock_flags
);
1340 list_move_tail(&(frame
->frame
), &usbvision
->outqueue
);
1341 usbvision
->curFrame
= NULL
;
1342 spin_unlock_irqrestore(&usbvision
->queue_lock
, lock_flags
);
1344 usbvision
->frame_num
++;
1346 /* This will cause the process to request another frame. */
1347 if (waitqueue_active(&usbvision
->wait_frame
)) {
1348 PDEBUG(DBG_PARSE
, "Wake up !");
1349 wake_up_interruptible(&usbvision
->wait_frame
);
1353 frame
->grabstate
= FrameState_Grabbing
;
1356 /* Update the frame's uncompressed length. */
1357 frame
->scanlength
+= copylen
;
1362 * Make all of the blocks of data contiguous
1364 static int usbvision_compress_isochronous(struct usb_usbvision
*usbvision
,
1367 unsigned char *packet_data
;
1370 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
1371 int packet_len
= urb
->iso_frame_desc
[i
].actual_length
;
1372 int packet_stat
= urb
->iso_frame_desc
[i
].status
;
1374 packet_data
= urb
->transfer_buffer
+ urb
->iso_frame_desc
[i
].offset
;
1376 /* Detect and ignore errored packets */
1377 if (packet_stat
) { // packet_stat != 0 ?????????????
1378 PDEBUG(DBG_ISOC
, "data error: [%d] len=%d, status=%X", i
, packet_len
, packet_stat
);
1379 usbvision
->isocErrCount
++;
1383 /* Detect and ignore empty packets */
1384 if (packet_len
< 0) {
1385 PDEBUG(DBG_ISOC
, "error packet [%d]", i
);
1386 usbvision
->isocSkipCount
++;
1389 else if (packet_len
== 0) { /* Frame end ????? */
1390 PDEBUG(DBG_ISOC
, "null packet [%d]", i
);
1391 usbvision
->isocstate
=IsocState_NoFrame
;
1392 usbvision
->isocSkipCount
++;
1395 else if (packet_len
> usbvision
->isocPacketSize
) {
1396 PDEBUG(DBG_ISOC
, "packet[%d] > isocPacketSize", i
);
1397 usbvision
->isocSkipCount
++;
1401 PDEBUG(DBG_ISOC
, "packet ok [%d] len=%d", i
, packet_len
);
1403 if (usbvision
->isocstate
==IsocState_NoFrame
) { //new frame begins
1404 usbvision
->isocstate
=IsocState_InFrame
;
1405 scratch_mark_header(usbvision
);
1406 usbvision_measure_bandwidth(usbvision
);
1407 PDEBUG(DBG_ISOC
, "packet with header");
1411 * If usbvision continues to feed us with data but there is no
1412 * consumption (if, for example, V4L client fell asleep) we
1413 * may overflow the buffer. We have to move old data over to
1414 * free room for new data. This is bad for old data. If we
1415 * just drop new data then it's bad for new data... choose
1416 * your favorite evil here.
1418 if (scratch_free(usbvision
) < packet_len
) {
1420 usbvision
->scratch_ovf_count
++;
1421 PDEBUG(DBG_ISOC
, "scratch buf overflow! scr_len: %d, n: %d",
1422 scratch_len(usbvision
), packet_len
);
1423 scratch_rm_old(usbvision
, packet_len
- scratch_free(usbvision
));
1426 /* Now we know that there is enough room in scratch buffer */
1427 scratch_put(usbvision
, packet_data
, packet_len
);
1428 totlen
+= packet_len
;
1429 usbvision
->isocDataCount
+= packet_len
;
1430 usbvision
->isocPacketCount
++;
1436 printk(KERN_DEBUG
"+%d.\n", usbvision
->scratchlen
);
1437 usbvision_hexdump(data0
, (totlen
> 64) ? 64 : totlen
);
1445 static void usbvision_isocIrq(struct urb
*urb
)
1449 struct usb_usbvision
*usbvision
= urb
->context
;
1451 unsigned long startTime
= jiffies
;
1452 struct usbvision_frame
**f
;
1454 /* We don't want to do anything if we are about to be removed! */
1455 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1458 /* any urb with wrong status is ignored without acknowledgement */
1459 if (urb
->status
== -ENOENT
) {
1463 f
= &usbvision
->curFrame
;
1465 /* Manage streaming interruption */
1466 if (usbvision
->streaming
== Stream_Interrupt
) {
1467 usbvision
->streaming
= Stream_Idle
;
1469 (*f
)->grabstate
= FrameState_Ready
;
1470 (*f
)->scanstate
= ScanState_Scanning
;
1472 PDEBUG(DBG_IRQ
, "stream interrupted");
1473 wake_up_interruptible(&usbvision
->wait_stream
);
1476 /* Copy the data received into our scratch buffer */
1477 len
= usbvision_compress_isochronous(usbvision
, urb
);
1479 usbvision
->isocUrbCount
++;
1480 usbvision
->urb_length
= len
;
1482 if (usbvision
->streaming
== Stream_On
) {
1484 /* If we collected enough data let's parse! */
1485 if ((scratch_len(usbvision
) > USBVISION_HEADER_LENGTH
) &&
1486 (!list_empty(&(usbvision
->inqueue
))) ) {
1488 (*f
) = list_entry(usbvision
->inqueue
.next
,
1489 struct usbvision_frame
,
1492 usbvision_parse_data(usbvision
);
1495 /*If we don't have a frame
1496 we're current working on, complain */
1498 "received data, but no one needs it");
1499 scratch_reset(usbvision
);
1503 PDEBUG(DBG_IRQ
, "received data, but no one needs it");
1504 scratch_reset(usbvision
);
1507 usbvision
->timeInIrq
+= jiffies
- startTime
;
1509 for (i
= 0; i
< USBVISION_URB_FRAMES
; i
++) {
1510 urb
->iso_frame_desc
[i
].status
= 0;
1511 urb
->iso_frame_desc
[i
].actual_length
= 0;
1515 urb
->dev
= usbvision
->dev
;
1516 errCode
= usb_submit_urb (urb
, GFP_ATOMIC
);
1519 dev_err(&usbvision
->dev
->dev
,
1520 "%s: usb_submit_urb failed: error %d\n",
1527 /*************************************/
1528 /* Low level usbvision access functions */
1529 /*************************************/
1532 * usbvision_read_reg()
1534 * return < 0 -> Error
1538 int usbvision_read_reg(struct usb_usbvision
*usbvision
, unsigned char reg
)
1541 unsigned char buffer
[1];
1543 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1546 errCode
= usb_control_msg(usbvision
->dev
, usb_rcvctrlpipe(usbvision
->dev
, 1),
1548 USB_DIR_IN
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
,
1549 0, (__u16
) reg
, buffer
, 1, HZ
);
1552 dev_err(&usbvision
->dev
->dev
,
1553 "%s: failed: error %d\n", __func__
, errCode
);
1560 * usbvision_write_reg()
1562 * return 1 -> Reg written
1563 * 0 -> usbvision is not yet ready
1564 * -1 -> Something went wrong
1567 int usbvision_write_reg(struct usb_usbvision
*usbvision
, unsigned char reg
,
1568 unsigned char value
)
1572 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1575 errCode
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1577 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1578 USB_RECIP_ENDPOINT
, 0, (__u16
) reg
, &value
, 1, HZ
);
1581 dev_err(&usbvision
->dev
->dev
,
1582 "%s: failed: error %d\n", __func__
, errCode
);
1588 static void usbvision_ctrlUrb_complete(struct urb
*urb
)
1590 struct usb_usbvision
*usbvision
= (struct usb_usbvision
*)urb
->context
;
1592 PDEBUG(DBG_IRQ
, "");
1593 usbvision
->ctrlUrbBusy
= 0;
1594 if (waitqueue_active(&usbvision
->ctrlUrb_wq
)) {
1595 wake_up_interruptible(&usbvision
->ctrlUrb_wq
);
1600 static int usbvision_write_reg_irq(struct usb_usbvision
*usbvision
,int address
,
1601 unsigned char *data
, int len
)
1605 PDEBUG(DBG_IRQ
, "");
1609 if (usbvision
->ctrlUrbBusy
) {
1612 usbvision
->ctrlUrbBusy
= 1;
1614 usbvision
->ctrlUrbSetup
.bRequestType
= USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
;
1615 usbvision
->ctrlUrbSetup
.bRequest
= USBVISION_OP_CODE
;
1616 usbvision
->ctrlUrbSetup
.wValue
= 0;
1617 usbvision
->ctrlUrbSetup
.wIndex
= cpu_to_le16(address
);
1618 usbvision
->ctrlUrbSetup
.wLength
= cpu_to_le16(len
);
1619 usb_fill_control_urb (usbvision
->ctrlUrb
, usbvision
->dev
,
1620 usb_sndctrlpipe(usbvision
->dev
, 1),
1621 (unsigned char *)&usbvision
->ctrlUrbSetup
,
1622 (void *)usbvision
->ctrlUrbBuffer
, len
,
1623 usbvision_ctrlUrb_complete
,
1626 memcpy(usbvision
->ctrlUrbBuffer
, data
, len
);
1628 errCode
= usb_submit_urb(usbvision
->ctrlUrb
, GFP_ATOMIC
);
1630 // error in usb_submit_urb()
1631 usbvision
->ctrlUrbBusy
= 0;
1633 PDEBUG(DBG_IRQ
, "submit %d byte: error %d", len
, errCode
);
1638 static int usbvision_init_compression(struct usb_usbvision
*usbvision
)
1642 usbvision
->lastIsocFrameNum
= -1;
1643 usbvision
->isocDataCount
= 0;
1644 usbvision
->isocPacketCount
= 0;
1645 usbvision
->isocSkipCount
= 0;
1646 usbvision
->comprLevel
= 50;
1647 usbvision
->lastComprLevel
= -1;
1648 usbvision
->isocUrbCount
= 0;
1649 usbvision
->requestIntra
= 1;
1650 usbvision
->isocMeasureBandwidthCount
= 0;
1655 /* this function measures the used bandwidth since last call
1656 * return: 0 : no error
1657 * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize
1659 static int usbvision_measure_bandwidth (struct usb_usbvision
*usbvision
)
1663 if (usbvision
->isocMeasureBandwidthCount
< 2) { // this gives an average bandwidth of 3 frames
1664 usbvision
->isocMeasureBandwidthCount
++;
1667 if ((usbvision
->isocPacketSize
> 0) && (usbvision
->isocPacketCount
> 0)) {
1668 usbvision
->usedBandwidth
= usbvision
->isocDataCount
/
1669 (usbvision
->isocPacketCount
+ usbvision
->isocSkipCount
) *
1670 100 / usbvision
->isocPacketSize
;
1672 usbvision
->isocMeasureBandwidthCount
= 0;
1673 usbvision
->isocDataCount
= 0;
1674 usbvision
->isocPacketCount
= 0;
1675 usbvision
->isocSkipCount
= 0;
1679 static int usbvision_adjust_compression (struct usb_usbvision
*usbvision
)
1682 unsigned char buffer
[6];
1684 PDEBUG(DBG_IRQ
, "");
1685 if ((adjustCompression
) && (usbvision
->usedBandwidth
> 0)) {
1686 usbvision
->comprLevel
+= (usbvision
->usedBandwidth
- 90) / 2;
1687 RESTRICT_TO_RANGE(usbvision
->comprLevel
, 0, 100);
1688 if (usbvision
->comprLevel
!= usbvision
->lastComprLevel
) {
1690 if (usbvision
->bridgeType
== BRIDGE_NT1004
|| usbvision
->bridgeType
== BRIDGE_NT1005
) {
1691 buffer
[0] = (unsigned char)(4 + 16 * usbvision
->comprLevel
/ 100); // PCM Threshold 1
1692 buffer
[1] = (unsigned char)(4 + 8 * usbvision
->comprLevel
/ 100); // PCM Threshold 2
1693 distorsion
= 7 + 248 * usbvision
->comprLevel
/ 100;
1694 buffer
[2] = (unsigned char)(distorsion
& 0xFF); // Average distorsion Threshold (inter)
1695 buffer
[3] = (unsigned char)(distorsion
& 0xFF); // Average distorsion Threshold (intra)
1696 distorsion
= 1 + 42 * usbvision
->comprLevel
/ 100;
1697 buffer
[4] = (unsigned char)(distorsion
& 0xFF); // Maximum distorsion Threshold (inter)
1698 buffer
[5] = (unsigned char)(distorsion
& 0xFF); // Maximum distorsion Threshold (intra)
1700 else { //BRIDGE_NT1003
1701 buffer
[0] = (unsigned char)(4 + 16 * usbvision
->comprLevel
/ 100); // PCM threshold 1
1702 buffer
[1] = (unsigned char)(4 + 8 * usbvision
->comprLevel
/ 100); // PCM threshold 2
1703 distorsion
= 2 + 253 * usbvision
->comprLevel
/ 100;
1704 buffer
[2] = (unsigned char)(distorsion
& 0xFF); // distorsion threshold bit0-7
1705 buffer
[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11
1706 distorsion
= 0 + 43 * usbvision
->comprLevel
/ 100;
1707 buffer
[4] = (unsigned char)(distorsion
& 0xFF); // maximum distorsion bit0-7
1708 buffer
[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8
1710 errCode
= usbvision_write_reg_irq(usbvision
, USBVISION_PCM_THR1
, buffer
, 6);
1712 PDEBUG(DBG_IRQ
, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer
[0],
1713 buffer
[1], buffer
[2], buffer
[3], buffer
[4], buffer
[5]);
1714 usbvision
->lastComprLevel
= usbvision
->comprLevel
;
1721 static int usbvision_request_intra (struct usb_usbvision
*usbvision
)
1724 unsigned char buffer
[1];
1726 PDEBUG(DBG_IRQ
, "");
1727 usbvision
->requestIntra
= 1;
1729 usbvision_write_reg_irq(usbvision
, USBVISION_FORCE_INTRA
, buffer
, 1);
1733 static int usbvision_unrequest_intra (struct usb_usbvision
*usbvision
)
1736 unsigned char buffer
[1];
1738 PDEBUG(DBG_IRQ
, "");
1739 usbvision
->requestIntra
= 0;
1741 usbvision_write_reg_irq(usbvision
, USBVISION_FORCE_INTRA
, buffer
, 1);
1745 /*******************************
1746 * usbvision utility functions
1747 *******************************/
1749 int usbvision_power_off(struct usb_usbvision
*usbvision
)
1753 PDEBUG(DBG_FUNC
, "");
1755 errCode
= usbvision_write_reg(usbvision
, USBVISION_PWR_REG
, USBVISION_SSPND_EN
);
1757 usbvision
->power
= 0;
1759 PDEBUG(DBG_FUNC
, "%s: errCode %d", (errCode
!=1)?"ERROR":"power is off", errCode
);
1764 * usbvision_set_video_format()
1767 static int usbvision_set_video_format(struct usb_usbvision
*usbvision
, int format
)
1769 static const char proc
[] = "usbvision_set_video_format";
1771 unsigned char value
[2];
1773 if (!USBVISION_IS_OPERATIONAL(usbvision
))
1776 PDEBUG(DBG_FUNC
, "isocMode %#02x", format
);
1778 if ((format
!= ISOC_MODE_YUV422
)
1779 && (format
!= ISOC_MODE_YUV420
)
1780 && (format
!= ISOC_MODE_COMPRESS
)) {
1781 printk(KERN_ERR
"usbvision: unknown video format %02x, using default YUV420",
1783 format
= ISOC_MODE_YUV420
;
1785 value
[0] = 0x0A; //TODO: See the effect of the filter
1786 value
[1] = format
; // Sets the VO_MODE register which follows FILT_CONT
1787 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1789 USB_DIR_OUT
| USB_TYPE_VENDOR
|
1790 USB_RECIP_ENDPOINT
, 0,
1791 (__u16
) USBVISION_FILT_CONT
, value
, 2, HZ
);
1794 printk(KERN_ERR
"%s: ERROR=%d. USBVISION stopped - "
1795 "reconnect or reload driver.\n", proc
, rc
);
1797 usbvision
->isocMode
= format
;
1802 * usbvision_set_output()
1806 int usbvision_set_output(struct usb_usbvision
*usbvision
, int width
,
1810 int UsbWidth
, UsbHeight
;
1811 unsigned int frameRate
=0, frameDrop
=0;
1812 unsigned char value
[4];
1814 if (!USBVISION_IS_OPERATIONAL(usbvision
)) {
1818 if (width
> MAX_USB_WIDTH
) {
1819 UsbWidth
= width
/ 2;
1820 usbvision
->stretch_width
= 2;
1824 usbvision
->stretch_width
= 1;
1827 if (height
> MAX_USB_HEIGHT
) {
1828 UsbHeight
= height
/ 2;
1829 usbvision
->stretch_height
= 2;
1833 usbvision
->stretch_height
= 1;
1836 RESTRICT_TO_RANGE(UsbWidth
, MIN_FRAME_WIDTH
, MAX_USB_WIDTH
);
1837 UsbWidth
&= ~(MIN_FRAME_WIDTH
-1);
1838 RESTRICT_TO_RANGE(UsbHeight
, MIN_FRAME_HEIGHT
, MAX_USB_HEIGHT
);
1841 PDEBUG(DBG_FUNC
, "usb %dx%d; screen %dx%d; stretch %dx%d",
1842 UsbWidth
, UsbHeight
, width
, height
,
1843 usbvision
->stretch_width
, usbvision
->stretch_height
);
1845 /* I'll not rewrite the same values */
1846 if ((UsbWidth
!= usbvision
->curwidth
) || (UsbHeight
!= usbvision
->curheight
)) {
1847 value
[0] = UsbWidth
& 0xff; //LSB
1848 value
[1] = (UsbWidth
>> 8) & 0x03; //MSB
1849 value
[2] = UsbHeight
& 0xff; //LSB
1850 value
[3] = (UsbHeight
>> 8) & 0x03; //MSB
1852 errCode
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
1854 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
,
1855 0, (__u16
) USBVISION_LXSIZE_O
, value
, 4, HZ
);
1858 dev_err(&usbvision
->dev
->dev
,
1859 "%s failed: error %d\n", __func__
, errCode
);
1862 usbvision
->curwidth
= usbvision
->stretch_width
* UsbWidth
;
1863 usbvision
->curheight
= usbvision
->stretch_height
* UsbHeight
;
1866 if (usbvision
->isocMode
== ISOC_MODE_YUV422
) {
1867 frameRate
= (usbvision
->isocPacketSize
* 1000) / (UsbWidth
* UsbHeight
* 2);
1869 else if (usbvision
->isocMode
== ISOC_MODE_YUV420
) {
1870 frameRate
= (usbvision
->isocPacketSize
* 1000) / ((UsbWidth
* UsbHeight
* 12) / 8);
1873 frameRate
= FRAMERATE_MAX
;
1876 if (usbvision
->tvnormId
& V4L2_STD_625_50
) {
1877 frameDrop
= frameRate
* 32 / 25 - 1;
1879 else if (usbvision
->tvnormId
& V4L2_STD_525_60
) {
1880 frameDrop
= frameRate
* 32 / 30 - 1;
1883 RESTRICT_TO_RANGE(frameDrop
, FRAMERATE_MIN
, FRAMERATE_MAX
);
1885 PDEBUG(DBG_FUNC
, "frameRate %d fps, frameDrop %d", frameRate
, frameDrop
);
1887 frameDrop
= FRAMERATE_MAX
; // We can allow the maximum here, because dropping is controlled
1889 /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
1891 => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
1893 frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
1894 => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
1895 => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
1897 errCode
= usbvision_write_reg(usbvision
, USBVISION_FRM_RATE
, frameDrop
);
1903 * usbvision_frames_alloc
1904 * allocate the required frames
1906 int usbvision_frames_alloc(struct usb_usbvision
*usbvision
, int number_of_frames
)
1910 /*needs to be page aligned cause the buffers can be mapped individually! */
1911 usbvision
->max_frame_size
= PAGE_ALIGN(usbvision
->curwidth
*
1912 usbvision
->curheight
*
1913 usbvision
->palette
.bytes_per_pixel
);
1915 /* Try to do my best to allocate the frames the user want in the remaining memory */
1916 usbvision
->num_frames
= number_of_frames
;
1917 while (usbvision
->num_frames
> 0) {
1918 usbvision
->fbuf_size
= usbvision
->num_frames
* usbvision
->max_frame_size
;
1919 if((usbvision
->fbuf
= usbvision_rvmalloc(usbvision
->fbuf_size
))) {
1922 usbvision
->num_frames
--;
1925 spin_lock_init(&usbvision
->queue_lock
);
1926 init_waitqueue_head(&usbvision
->wait_frame
);
1927 init_waitqueue_head(&usbvision
->wait_stream
);
1929 /* Allocate all buffers */
1930 for (i
= 0; i
< usbvision
->num_frames
; i
++) {
1931 usbvision
->frame
[i
].index
= i
;
1932 usbvision
->frame
[i
].grabstate
= FrameState_Unused
;
1933 usbvision
->frame
[i
].data
= usbvision
->fbuf
+
1934 i
* usbvision
->max_frame_size
;
1936 * Set default sizes for read operation.
1938 usbvision
->stretch_width
= 1;
1939 usbvision
->stretch_height
= 1;
1940 usbvision
->frame
[i
].width
= usbvision
->curwidth
;
1941 usbvision
->frame
[i
].height
= usbvision
->curheight
;
1942 usbvision
->frame
[i
].bytes_read
= 0;
1944 PDEBUG(DBG_FUNC
, "allocated %d frames (%d bytes per frame)",usbvision
->num_frames
,usbvision
->max_frame_size
);
1945 return usbvision
->num_frames
;
1949 * usbvision_frames_free
1950 * frees memory allocated for the frames
1952 void usbvision_frames_free(struct usb_usbvision
*usbvision
)
1954 /* Have to free all that memory */
1955 PDEBUG(DBG_FUNC
, "free %d frames",usbvision
->num_frames
);
1957 if (usbvision
->fbuf
!= NULL
) {
1958 usbvision_rvfree(usbvision
->fbuf
, usbvision
->fbuf_size
);
1959 usbvision
->fbuf
= NULL
;
1961 usbvision
->num_frames
= 0;
1965 * usbvision_empty_framequeues()
1966 * prepare queues for incoming and outgoing frames
1968 void usbvision_empty_framequeues(struct usb_usbvision
*usbvision
)
1972 INIT_LIST_HEAD(&(usbvision
->inqueue
));
1973 INIT_LIST_HEAD(&(usbvision
->outqueue
));
1975 for (i
= 0; i
< USBVISION_NUMFRAMES
; i
++) {
1976 usbvision
->frame
[i
].grabstate
= FrameState_Unused
;
1977 usbvision
->frame
[i
].bytes_read
= 0;
1982 * usbvision_stream_interrupt()
1985 int usbvision_stream_interrupt(struct usb_usbvision
*usbvision
)
1989 /* stop reading from the device */
1991 usbvision
->streaming
= Stream_Interrupt
;
1992 ret
= wait_event_timeout(usbvision
->wait_stream
,
1993 (usbvision
->streaming
== Stream_Idle
),
1994 msecs_to_jiffies(USBVISION_NUMSBUF
*USBVISION_URB_FRAMES
));
1999 * usbvision_set_compress_params()
2003 static int usbvision_set_compress_params(struct usb_usbvision
*usbvision
)
2005 static const char proc
[] = "usbvision_set_compresion_params: ";
2007 unsigned char value
[6];
2009 value
[0] = 0x0F; // Intra-Compression cycle
2010 value
[1] = 0x01; // Reg.45 one line per strip
2011 value
[2] = 0x00; // Reg.46 Force intra mode on all new frames
2012 value
[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
2013 value
[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
2014 value
[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
2016 //catched values for NT1004
2017 // value[0] = 0xFF; // Never apply intra mode automatically
2018 // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
2019 // value[2] = 0x01; // Force intra mode on all new frames
2020 // value[3] = 0x00; // Strip size 400 Bytes; do not force up
2021 // value[4] = 0xA2; //
2022 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2025 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2027 USB_DIR_OUT
| USB_TYPE_VENDOR
|
2028 USB_RECIP_ENDPOINT
, 0,
2029 (__u16
) USBVISION_INTRA_CYC
, value
, 5, HZ
);
2032 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2033 "reconnect or reload driver.\n", proc
, rc
);
2037 if (usbvision
->bridgeType
== BRIDGE_NT1004
) {
2038 value
[0] = 20; // PCM Threshold 1
2039 value
[1] = 12; // PCM Threshold 2
2040 value
[2] = 255; // Distorsion Threshold inter
2041 value
[3] = 255; // Distorsion Threshold intra
2042 value
[4] = 43; // Max Distorsion inter
2043 value
[5] = 43; // Max Distorsion intra
2046 value
[0] = 20; // PCM Threshold 1
2047 value
[1] = 12; // PCM Threshold 2
2048 value
[2] = 255; // Distorsion Threshold d7-d0
2049 value
[3] = 0; // Distorsion Threshold d11-d8
2050 value
[4] = 43; // Max Distorsion d7-d0
2051 value
[5] = 0; // Max Distorsion d8
2054 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2057 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2059 USB_DIR_OUT
| USB_TYPE_VENDOR
|
2060 USB_RECIP_ENDPOINT
, 0,
2061 (__u16
) USBVISION_PCM_THR1
, value
, 6, HZ
);
2064 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2065 "reconnect or reload driver.\n", proc
, rc
);
2075 * usbvision_set_input()
2077 * Set the input (saa711x, ...) size x y and other misc input params
2078 * I've no idea if this parameters are right
2081 int usbvision_set_input(struct usb_usbvision
*usbvision
)
2083 static const char proc
[] = "usbvision_set_input: ";
2085 unsigned char value
[8];
2086 unsigned char dvi_yuv_value
;
2088 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2091 /* Set input format expected from decoder*/
2092 if (usbvision_device_data
[usbvision
->DevModel
].Vin_Reg1_override
) {
2093 value
[0] = usbvision_device_data
[usbvision
->DevModel
].Vin_Reg1
;
2094 } else if(usbvision_device_data
[usbvision
->DevModel
].Codec
== CODEC_SAA7113
) {
2095 /* SAA7113 uses 8 bit output */
2096 value
[0] = USBVISION_8_422_SYNC
;
2098 /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
2099 * as that is how saa7111 is configured */
2100 value
[0] = USBVISION_16_422_SYNC
;
2101 /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
2104 rc
= usbvision_write_reg(usbvision
, USBVISION_VIN_REG1
, value
[0]);
2106 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2107 "reconnect or reload driver.\n", proc
, rc
);
2112 if (usbvision
->tvnormId
& V4L2_STD_PAL
) {
2114 value
[1] = 0x02; //0x02C0 -> 704 Input video line length
2116 value
[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2118 value
[5] = 0x00; //0x0060 -> 96 Input video h offset
2120 value
[7] = 0x00; //0x0016 -> 22 Input video v offset
2121 } else if (usbvision
->tvnormId
& V4L2_STD_SECAM
) {
2123 value
[1] = 0x02; //0x02C0 -> 704 Input video line length
2125 value
[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2127 value
[5] = 0x00; //0x0001 -> 01 Input video h offset
2129 value
[7] = 0x00; //0x0001 -> 01 Input video v offset
2130 } else { /* V4L2_STD_NTSC */
2132 value
[1] = 0x02; //0x02D0 -> 720 Input video line length
2134 value
[3] = 0x00; //0x00F0 -> 240 Input video number of lines
2136 value
[5] = 0x00; //0x0050 -> 80 Input video h offset
2138 value
[7] = 0x00; //0x0010 -> 16 Input video v offset
2141 if (usbvision_device_data
[usbvision
->DevModel
].X_Offset
>= 0) {
2142 value
[4]=usbvision_device_data
[usbvision
->DevModel
].X_Offset
& 0xff;
2143 value
[5]=(usbvision_device_data
[usbvision
->DevModel
].X_Offset
& 0x0300) >> 8;
2146 if (adjust_X_Offset
!= -1) {
2147 value
[4] = adjust_X_Offset
& 0xff;
2148 value
[5] = (adjust_X_Offset
& 0x0300) >> 8;
2151 if (usbvision_device_data
[usbvision
->DevModel
].Y_Offset
>= 0) {
2152 value
[6]=usbvision_device_data
[usbvision
->DevModel
].Y_Offset
& 0xff;
2153 value
[7]=(usbvision_device_data
[usbvision
->DevModel
].Y_Offset
& 0x0300) >> 8;
2156 if (adjust_Y_Offset
!= -1) {
2157 value
[6] = adjust_Y_Offset
& 0xff;
2158 value
[7] = (adjust_Y_Offset
& 0x0300) >> 8;
2161 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2162 USBVISION_OP_CODE
, /* USBVISION specific code */
2163 USB_DIR_OUT
| USB_TYPE_VENDOR
| USB_RECIP_ENDPOINT
, 0,
2164 (__u16
) USBVISION_LXSIZE_I
, value
, 8, HZ
);
2166 printk(KERN_ERR
"%sERROR=%d. USBVISION stopped - "
2167 "reconnect or reload driver.\n", proc
, rc
);
2172 dvi_yuv_value
= 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
2174 if(usbvision_device_data
[usbvision
->DevModel
].Dvi_yuv_override
){
2175 dvi_yuv_value
= usbvision_device_data
[usbvision
->DevModel
].Dvi_yuv
;
2177 else if(usbvision_device_data
[usbvision
->DevModel
].Codec
== CODEC_SAA7113
) {
2178 /* This changes as the fine sync control changes. Further investigation necessary */
2179 dvi_yuv_value
= 0x06;
2182 return (usbvision_write_reg(usbvision
, USBVISION_DVI_YUV
, dvi_yuv_value
));
2187 * usbvision_set_dram_settings()
2189 * Set the buffer address needed by the usbvision dram to operate
2190 * This values has been taken with usbsnoop.
2194 static int usbvision_set_dram_settings(struct usb_usbvision
*usbvision
)
2197 unsigned char value
[8];
2199 if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
2208 // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
2209 // FDL: 0x00000-0x0E099 = 57498 Words
2210 // VDW: 0x0E3FF-0x3FFFF
2222 /* These are the values of the address of the video buffer,
2223 * they have to be loaded into the USBVISION_DRM_PRM1-8
2225 * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
2226 * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
2227 * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
2228 * Only used in compressed mode
2229 * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
2230 * Only used in compressed mode
2231 * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
2232 * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
2235 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2238 rc
= usb_control_msg(usbvision
->dev
, usb_sndctrlpipe(usbvision
->dev
, 1),
2239 USBVISION_OP_CODE
, /* USBVISION specific code */
2240 USB_DIR_OUT
| USB_TYPE_VENDOR
|
2241 USB_RECIP_ENDPOINT
, 0,
2242 (__u16
) USBVISION_DRM_PRM1
, value
, 8, HZ
);
2245 dev_err(&usbvision
->dev
->dev
, "%sERROR=%d\n", __func__
, rc
);
2249 /* Restart the video buffer logic */
2250 if ((rc
= usbvision_write_reg(usbvision
, USBVISION_DRM_CONT
, USBVISION_RES_UR
|
2251 USBVISION_RES_FDL
| USBVISION_RES_VDW
)) < 0)
2253 rc
= usbvision_write_reg(usbvision
, USBVISION_DRM_CONT
, 0x00);
2261 * Power on the device, enables suspend-resume logic
2262 * & reset the isoc End-Point
2266 int usbvision_power_on(struct usb_usbvision
*usbvision
)
2270 PDEBUG(DBG_FUNC
, "");
2272 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
, USBVISION_SSPND_EN
);
2273 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2274 USBVISION_SSPND_EN
| USBVISION_RES2
);
2276 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2277 USBVISION_SSPND_EN
| USBVISION_PWR_VID
);
2278 errCode
= usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2279 USBVISION_SSPND_EN
| USBVISION_PWR_VID
| USBVISION_RES2
);
2281 usbvision
->power
= 1;
2283 PDEBUG(DBG_FUNC
, "%s: errCode %d", (errCode
<0)?"ERROR":"power is on", errCode
);
2289 * usbvision timer stuff
2292 // to call usbvision_power_off from task queue
2293 static void call_usbvision_power_off(struct work_struct
*work
)
2295 struct usb_usbvision
*usbvision
= container_of(work
, struct usb_usbvision
, powerOffWork
);
2297 PDEBUG(DBG_FUNC
, "");
2298 if(mutex_lock_interruptible(&usbvision
->lock
)) {
2303 if(usbvision
->user
== 0) {
2304 usbvision_i2c_unregister(usbvision
);
2306 usbvision_power_off(usbvision
);
2307 usbvision
->initialized
= 0;
2309 mutex_unlock(&usbvision
->lock
);
2312 static void usbvision_powerOffTimer(unsigned long data
)
2314 struct usb_usbvision
*usbvision
= (void *) data
;
2316 PDEBUG(DBG_FUNC
, "");
2317 del_timer(&usbvision
->powerOffTimer
);
2318 INIT_WORK(&usbvision
->powerOffWork
, call_usbvision_power_off
);
2319 (void) schedule_work(&usbvision
->powerOffWork
);
2322 void usbvision_init_powerOffTimer(struct usb_usbvision
*usbvision
)
2324 init_timer(&usbvision
->powerOffTimer
);
2325 usbvision
->powerOffTimer
.data
= (long) usbvision
;
2326 usbvision
->powerOffTimer
.function
= usbvision_powerOffTimer
;
2329 void usbvision_set_powerOffTimer(struct usb_usbvision
*usbvision
)
2331 mod_timer(&usbvision
->powerOffTimer
, jiffies
+ USBVISION_POWEROFF_TIME
);
2334 void usbvision_reset_powerOffTimer(struct usb_usbvision
*usbvision
)
2336 if (timer_pending(&usbvision
->powerOffTimer
)) {
2337 del_timer(&usbvision
->powerOffTimer
);
2342 * usbvision_begin_streaming()
2343 * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
2344 * idea about the rest
2346 int usbvision_begin_streaming(struct usb_usbvision
*usbvision
)
2350 if (usbvision
->isocMode
== ISOC_MODE_COMPRESS
) {
2351 usbvision_init_compression(usbvision
);
2353 errCode
= usbvision_write_reg(usbvision
, USBVISION_VIN_REG2
, USBVISION_NOHVALID
|
2354 usbvision
->Vin_Reg2_Preset
);
2359 * usbvision_restart_isoc()
2360 * Not sure yet if touching here PWR_REG make loose the config
2363 int usbvision_restart_isoc(struct usb_usbvision
*usbvision
)
2369 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2370 USBVISION_SSPND_EN
| USBVISION_PWR_VID
)) < 0)
2374 usbvision_write_reg(usbvision
, USBVISION_PWR_REG
,
2375 USBVISION_SSPND_EN
| USBVISION_PWR_VID
|
2376 USBVISION_RES2
)) < 0)
2380 usbvision_write_reg(usbvision
, USBVISION_VIN_REG2
,
2381 USBVISION_KEEP_BLANK
| USBVISION_NOHVALID
|
2382 usbvision
->Vin_Reg2_Preset
)) < 0) return ret
;
2384 /* TODO: schedule timeout */
2385 while ((usbvision_read_reg(usbvision
, USBVISION_STATUS_REG
) & 0x01) != 1);
2390 int usbvision_audio_off(struct usb_usbvision
*usbvision
)
2392 if (usbvision_write_reg(usbvision
, USBVISION_IOPIN_REG
, USBVISION_AUDIO_MUTE
) < 0) {
2393 printk(KERN_ERR
"usbvision_audio_off: can't wirte reg\n");
2396 usbvision
->AudioMute
= 0;
2397 usbvision
->AudioChannel
= USBVISION_AUDIO_MUTE
;
2401 int usbvision_set_audio(struct usb_usbvision
*usbvision
, int AudioChannel
)
2403 if (!usbvision
->AudioMute
) {
2404 if (usbvision_write_reg(usbvision
, USBVISION_IOPIN_REG
, AudioChannel
) < 0) {
2405 printk(KERN_ERR
"usbvision_set_audio: can't write iopin register for audio switching\n");
2409 usbvision
->AudioChannel
= AudioChannel
;
2413 int usbvision_setup(struct usb_usbvision
*usbvision
,int format
)
2415 usbvision_set_video_format(usbvision
, format
);
2416 usbvision_set_dram_settings(usbvision
);
2417 usbvision_set_compress_params(usbvision
);
2418 usbvision_set_input(usbvision
);
2419 usbvision_set_output(usbvision
, MAX_USB_WIDTH
, MAX_USB_HEIGHT
);
2420 usbvision_restart_isoc(usbvision
);
2423 return USBVISION_IS_OPERATIONAL(usbvision
);
2426 int usbvision_set_alternate(struct usb_usbvision
*dev
)
2428 int errCode
, prev_alt
= dev
->ifaceAlt
;
2432 for(i
=0;i
< dev
->num_alt
; i
++)
2433 if(dev
->alt_max_pkt_size
[i
]>dev
->alt_max_pkt_size
[dev
->ifaceAlt
])
2436 if (dev
->ifaceAlt
!= prev_alt
) {
2437 dev
->isocPacketSize
= dev
->alt_max_pkt_size
[dev
->ifaceAlt
];
2438 PDEBUG(DBG_FUNC
,"setting alternate %d with wMaxPacketSize=%u", dev
->ifaceAlt
,dev
->isocPacketSize
);
2439 errCode
= usb_set_interface(dev
->dev
, dev
->iface
, dev
->ifaceAlt
);
2441 dev_err(&dev
->dev
->dev
,
2442 "cannot change alternate number to %d (error=%i)\n",
2443 dev
->ifaceAlt
, errCode
);
2448 PDEBUG(DBG_ISOC
, "ISO Packet Length:%d", dev
->isocPacketSize
);
2454 * usbvision_init_isoc()
2457 int usbvision_init_isoc(struct usb_usbvision
*usbvision
)
2459 struct usb_device
*dev
= usbvision
->dev
;
2460 int bufIdx
, errCode
, regValue
;
2463 if (!USBVISION_IS_OPERATIONAL(usbvision
))
2466 usbvision
->curFrame
= NULL
;
2467 scratch_reset(usbvision
);
2469 /* Alternate interface 1 is is the biggest frame size */
2470 errCode
= usbvision_set_alternate(usbvision
);
2472 usbvision
->last_error
= errCode
;
2475 sb_size
= USBVISION_URB_FRAMES
* usbvision
->isocPacketSize
;
2477 regValue
= (16 - usbvision_read_reg(usbvision
,
2478 USBVISION_ALTER_REG
)) & 0x0F;
2480 usbvision
->usb_bandwidth
= regValue
>> 1;
2481 PDEBUG(DBG_ISOC
, "USB Bandwidth Usage: %dMbit/Sec",
2482 usbvision
->usb_bandwidth
);
2486 /* We double buffer the Iso lists */
2488 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2492 urb
= usb_alloc_urb(USBVISION_URB_FRAMES
, GFP_KERNEL
);
2494 dev_err(&usbvision
->dev
->dev
,
2495 "%s: usb_alloc_urb() failed\n", __func__
);
2498 usbvision
->sbuf
[bufIdx
].urb
= urb
;
2499 usbvision
->sbuf
[bufIdx
].data
=
2500 usb_buffer_alloc(usbvision
->dev
,
2503 &urb
->transfer_dma
);
2505 urb
->context
= usbvision
;
2506 urb
->pipe
= usb_rcvisocpipe(dev
, usbvision
->video_endp
);
2507 urb
->transfer_flags
= URB_ISO_ASAP
;
2509 urb
->transfer_buffer
= usbvision
->sbuf
[bufIdx
].data
;
2510 urb
->complete
= usbvision_isocIrq
;
2511 urb
->number_of_packets
= USBVISION_URB_FRAMES
;
2512 urb
->transfer_buffer_length
=
2513 usbvision
->isocPacketSize
* USBVISION_URB_FRAMES
;
2514 for (j
= k
= 0; j
< USBVISION_URB_FRAMES
; j
++,
2515 k
+= usbvision
->isocPacketSize
) {
2516 urb
->iso_frame_desc
[j
].offset
= k
;
2517 urb
->iso_frame_desc
[j
].length
=
2518 usbvision
->isocPacketSize
;
2522 /* Submit all URBs */
2523 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2524 errCode
= usb_submit_urb(usbvision
->sbuf
[bufIdx
].urb
,
2527 dev_err(&usbvision
->dev
->dev
,
2528 "%s: usb_submit_urb(%d) failed: error %d\n",
2529 __func__
, bufIdx
, errCode
);
2533 usbvision
->streaming
= Stream_Idle
;
2534 PDEBUG(DBG_ISOC
, "%s: streaming=1 usbvision->video_endp=$%02x",
2536 usbvision
->video_endp
);
2541 * usbvision_stop_isoc()
2543 * This procedure stops streaming and deallocates URBs. Then it
2544 * activates zero-bandwidth alt. setting of the video interface.
2547 void usbvision_stop_isoc(struct usb_usbvision
*usbvision
)
2549 int bufIdx
, errCode
, regValue
;
2550 int sb_size
= USBVISION_URB_FRAMES
* usbvision
->isocPacketSize
;
2552 if ((usbvision
->streaming
== Stream_Off
) || (usbvision
->dev
== NULL
))
2555 /* Unschedule all of the iso td's */
2556 for (bufIdx
= 0; bufIdx
< USBVISION_NUMSBUF
; bufIdx
++) {
2557 usb_kill_urb(usbvision
->sbuf
[bufIdx
].urb
);
2558 if (usbvision
->sbuf
[bufIdx
].data
){
2559 usb_buffer_free(usbvision
->dev
,
2561 usbvision
->sbuf
[bufIdx
].data
,
2562 usbvision
->sbuf
[bufIdx
].urb
->transfer_dma
);
2564 usb_free_urb(usbvision
->sbuf
[bufIdx
].urb
);
2565 usbvision
->sbuf
[bufIdx
].urb
= NULL
;
2568 PDEBUG(DBG_ISOC
, "%s: streaming=Stream_Off\n", __func__
);
2569 usbvision
->streaming
= Stream_Off
;
2571 if (!usbvision
->remove_pending
) {
2573 /* Set packet size to 0 */
2574 usbvision
->ifaceAlt
=0;
2575 errCode
= usb_set_interface(usbvision
->dev
, usbvision
->iface
,
2576 usbvision
->ifaceAlt
);
2578 dev_err(&usbvision
->dev
->dev
,
2579 "%s: usb_set_interface() failed: error %d\n",
2581 usbvision
->last_error
= errCode
;
2583 regValue
= (16-usbvision_read_reg(usbvision
, USBVISION_ALTER_REG
)) & 0x0F;
2584 usbvision
->isocPacketSize
=
2585 (regValue
== 0) ? 0 : (regValue
* 64) - 1;
2586 PDEBUG(DBG_ISOC
, "ISO Packet Length:%d",
2587 usbvision
->isocPacketSize
);
2589 usbvision
->usb_bandwidth
= regValue
>> 1;
2590 PDEBUG(DBG_ISOC
, "USB Bandwidth Usage: %dMbit/Sec",
2591 usbvision
->usb_bandwidth
);
2595 int usbvision_muxsel(struct usb_usbvision
*usbvision
, int channel
)
2597 /* inputs #0 and #3 are constant for every SAA711x. */
2598 /* inputs #1 and #2 are variable for SAA7111 and SAA7113 */
2599 int mode
[4]= {SAA7115_COMPOSITE0
, 0, 0, SAA7115_COMPOSITE3
};
2600 int audio
[]= {1, 0, 0, 0};
2601 struct v4l2_routing route
;
2602 //channel 0 is TV with audiochannel 1 (tuner mono)
2603 //channel 1 is Composite with audio channel 0 (line in)
2604 //channel 2 is S-Video with audio channel 0 (line in)
2605 //channel 3 is additional video inputs to the device with audio channel 0 (line in)
2607 RESTRICT_TO_RANGE(channel
, 0, usbvision
->video_inputs
);
2608 usbvision
->ctl_input
= channel
;
2610 // set the new channel
2611 // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
2612 // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
2614 switch (usbvision_device_data
[usbvision
->DevModel
].Codec
) {
2616 mode
[1] = SAA7115_COMPOSITE2
;
2617 if (SwitchSVideoInput
) {
2618 /* To handle problems with S-Video Input for
2619 * some devices. Use SwitchSVideoInput
2620 * parameter when loading the module.*/
2621 mode
[2] = SAA7115_COMPOSITE1
;
2624 mode
[2] = SAA7115_SVIDEO1
;
2629 /* modes for saa7111 */
2630 mode
[1] = SAA7115_COMPOSITE1
;
2631 mode
[2] = SAA7115_SVIDEO1
;
2634 route
.input
= mode
[channel
];
2636 call_all(usbvision
, video
, s_routing
, &route
);
2637 usbvision_set_audio(usbvision
, audio
[channel
]);
2642 * Overrides for Emacs so that we follow Linus's tabbing style.
2643 * ---------------------------------------------------------------------------