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/videodev.h>
34 #include <linux/vmalloc.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/spinlock.h>
39 #include <linux/videodev2.h>
40 #include <linux/video_decoder.h>
41 #include <linux/i2c.h>
43 #include <media/saa7115.h>
44 #include <media/v4l2-common.h>
45 #include <media/tuner.h>
46 #include <media/audiochip.h>
48 #include <linux/workqueue.h>
51 #include <linux/kmod.h>
54 #include "usbvision.h"
56 static unsigned int core_debug;
57 module_param(core_debug,int,0644);
58 MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
60 static unsigned int force_testpattern;
61 module_param(force_testpattern,int,0644);
62 MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]");
64 static int adjustCompression = 1; /* Set the compression to be adaptive */
65 module_param(adjustCompression, int, 0444);
66 MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)");
68 /* To help people with Black and White output with using s-video input.
69 * Some cables and input device are wired differently. */
70 static int SwitchSVideoInput;
71 module_param(SwitchSVideoInput, int, 0444);
72 MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
74 static unsigned int adjust_X_Offset = -1;
75 module_param(adjust_X_Offset, int, 0644);
76 MODULE_PARM_DESC(adjust_X_Offset, "adjust X offset display [core]");
78 static unsigned int adjust_Y_Offset = -1;
79 module_param(adjust_Y_Offset, int, 0644);
80 MODULE_PARM_DESC(adjust_Y_Offset, "adjust Y offset display [core]");
83 #define ENABLE_HEXDUMP 0 /* Enable if you need it */
86 #ifdef USBVISION_DEBUG
87 #define PDEBUG(level, fmt, args...) { \
88 if (core_debug & (level)) \
89 info("[%s:%d] " fmt, __func__, __LINE__ , ## args); \
92 #define PDEBUG(level, fmt, args...) do {} while(0)
95 #define DBG_HEADER 1<<0
98 #define DBG_PARSE 1<<3
99 #define DBG_SCRATCH 1<<4
100 #define DBG_FUNC 1<<5
102 static const int max_imgwidth = MAX_FRAME_WIDTH;
103 static const int max_imgheight = MAX_FRAME_HEIGHT;
104 static const int min_imgwidth = MIN_FRAME_WIDTH;
105 static const int min_imgheight = MIN_FRAME_HEIGHT;
107 /* The value of 'scratch_buf_size' affects quality of the picture
108 * in many ways. Shorter buffers may cause loss of data when client
109 * is too slow. Larger buffers are memory-consuming and take longer
110 * to work with. This setting can be adjusted, but the default value
111 * should be OK for most desktop users.
113 #define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
114 static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
116 // Function prototypes
117 static int usbvision_request_intra (struct usb_usbvision *usbvision);
118 static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
119 static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
120 static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
122 /*******************************/
123 /* Memory management functions */
124 /*******************************/
127 * Here we want the physical address of the memory.
128 * This is used when initializing the contents of the area.
131 static void *usbvision_rvmalloc(unsigned long size)
136 size = PAGE_ALIGN(size);
137 mem = vmalloc_32(size);
141 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
142 adr = (unsigned long) mem;
144 SetPageReserved(vmalloc_to_page((void *)adr));
152 static void usbvision_rvfree(void *mem, unsigned long size)
159 size = PAGE_ALIGN(size);
161 adr = (unsigned long) mem;
162 while ((long) size > 0) {
163 ClearPageReserved(vmalloc_to_page((void *)adr));
173 static void usbvision_hexdump(const unsigned char *data, int len)
178 for (i = k = 0; len > 0; i++, len--) {
179 if (i > 0 && (i % 16 == 0)) {
183 k += sprintf(&tmp[k], "%02x ", data[i]);
190 /********************************
191 * scratch ring buffer handling
192 ********************************/
193 static int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */
195 int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
197 len += scratch_buf_size;
199 PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
205 /* This returns the free space left in the buffer */
206 static int scratch_free(struct usb_usbvision *usbvision)
208 int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
210 free += scratch_buf_size;
213 free -= 1; /* at least one byte in the buffer must */
214 /* left blank, otherwise there is no chance to differ between full and empty */
216 PDEBUG(DBG_SCRATCH, "return %d\n", free);
222 /* This puts data into the buffer */
223 static int scratch_put(struct usb_usbvision *usbvision, unsigned char *data,
228 if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
229 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
230 usbvision->scratch_write_ptr += len;
233 len_part = scratch_buf_size - usbvision->scratch_write_ptr;
234 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
235 if (len == len_part) {
236 usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
239 memcpy(usbvision->scratch, data + len_part, len - len_part);
240 usbvision->scratch_write_ptr = len - len_part;
244 PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
249 /* This marks the write_ptr as position of new frame header */
250 static void scratch_mark_header(struct usb_usbvision *usbvision)
252 PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
254 usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
255 usbvision->scratch_write_ptr;
256 usbvision->scratch_headermarker_write_ptr += 1;
257 usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
260 /* This gets data from the buffer at the given "ptr" position */
261 static int scratch_get_extra(struct usb_usbvision *usbvision,
262 unsigned char *data, int *ptr, int len)
265 if (*ptr + len < scratch_buf_size) {
266 memcpy(data, usbvision->scratch + *ptr, len);
270 len_part = scratch_buf_size - *ptr;
271 memcpy(data, usbvision->scratch + *ptr, len_part);
272 if (len == len_part) {
273 *ptr = 0; /* just set the y_ptr to zero */
276 memcpy(data + len_part, usbvision->scratch, len - len_part);
277 *ptr = len - len_part;
281 PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
287 /* This sets the scratch extra read pointer */
288 static void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr,
291 *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size;
293 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
297 /*This increments the scratch extra read pointer */
298 static void scratch_inc_extra_ptr(int *ptr, int len)
300 *ptr = (*ptr + len) % scratch_buf_size;
302 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
306 /* This gets data from the buffer */
307 static int scratch_get(struct usb_usbvision *usbvision, unsigned char *data,
311 if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
312 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
313 usbvision->scratch_read_ptr += len;
316 len_part = scratch_buf_size - usbvision->scratch_read_ptr;
317 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
318 if (len == len_part) {
319 usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
322 memcpy(data + len_part, usbvision->scratch, len - len_part);
323 usbvision->scratch_read_ptr = len - len_part;
327 PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
333 /* This sets read pointer to next header and returns it */
334 static int scratch_get_header(struct usb_usbvision *usbvision,
335 struct usbvision_frame_header *header)
339 PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
341 while (usbvision->scratch_headermarker_write_ptr -
342 usbvision->scratch_headermarker_read_ptr != 0) {
343 usbvision->scratch_read_ptr =
344 usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
345 usbvision->scratch_headermarker_read_ptr += 1;
346 usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
347 scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
348 if ((header->magic_1 == USBVISION_MAGIC_1)
349 && (header->magic_2 == USBVISION_MAGIC_2)
350 && (header->headerLength == USBVISION_HEADER_LENGTH)) {
351 errCode = USBVISION_HEADER_LENGTH;
352 header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8);
353 header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8);
362 /*This removes len bytes of old data from the buffer */
363 static void scratch_rm_old(struct usb_usbvision *usbvision, int len)
366 usbvision->scratch_read_ptr += len;
367 usbvision->scratch_read_ptr %= scratch_buf_size;
368 PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
372 /*This resets the buffer - kills all data in it too */
373 static void scratch_reset(struct usb_usbvision *usbvision)
375 PDEBUG(DBG_SCRATCH, "\n");
377 usbvision->scratch_read_ptr = 0;
378 usbvision->scratch_write_ptr = 0;
379 usbvision->scratch_headermarker_read_ptr = 0;
380 usbvision->scratch_headermarker_write_ptr = 0;
381 usbvision->isocstate = IsocState_NoFrame;
384 int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
386 usbvision->scratch = vmalloc_32(scratch_buf_size);
387 scratch_reset(usbvision);
388 if(usbvision->scratch == NULL) {
389 err("%s: unable to allocate %d bytes for scratch",
390 __func__, scratch_buf_size);
396 void usbvision_scratch_free(struct usb_usbvision *usbvision)
398 if (usbvision->scratch != NULL) {
399 vfree(usbvision->scratch);
400 usbvision->scratch = NULL;
405 * usbvision_testpattern()
407 * Procedure forms a test pattern (yellow grid on blue background).
410 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
411 * continues from the current scanline.
412 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
413 * 1: Draw a colored grid
416 static void usbvision_testpattern(struct usb_usbvision *usbvision,
417 int fullframe, int pmode)
419 static const char proc[] = "usbvision_testpattern";
420 struct usbvision_frame *frame;
426 if (usbvision == NULL) {
427 printk(KERN_ERR "%s: usbvision == NULL\n", proc);
430 if (usbvision->curFrame == NULL) {
431 printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc);
435 /* Grab the current frame */
436 frame = usbvision->curFrame;
438 /* Optionally start at the beginning */
441 frame->scanlength = 0;
444 /* Form every scan line */
445 for (; frame->curline < frame->frmheight; frame->curline++) {
448 f = frame->data + (usbvision->curwidth * 3 * frame->curline);
449 for (i = 0; i < usbvision->curwidth; i++) {
450 unsigned char cb = 0x80;
451 unsigned char cg = 0;
452 unsigned char cr = 0;
455 if (frame->curline % 32 == 0)
456 cb = 0, cg = cr = 0xFF;
457 else if (i % 32 == 0) {
458 if (frame->curline % 32 == 1)
460 cb = 0, cg = cr = 0xFF;
467 num_pass * 2) & 0xFF;
470 num_pass * 3) & 0xFF;
473 /* Just the blue screen */
483 frame->grabstate = FrameState_Done;
484 frame->scanlength += scan_length;
490 * usbvision_decompress_alloc()
492 * allocates intermediate buffer for decompression
494 int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
496 int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
497 usbvision->IntraFrameBuffer = vmalloc_32(IFB_size);
498 if (usbvision->IntraFrameBuffer == NULL) {
499 err("%s: unable to allocate %d for compr. frame buffer",
507 * usbvision_decompress_free()
509 * frees intermediate buffer for decompression
511 void usbvision_decompress_free(struct usb_usbvision *usbvision)
513 if (usbvision->IntraFrameBuffer != NULL) {
514 vfree(usbvision->IntraFrameBuffer);
515 usbvision->IntraFrameBuffer = NULL;
519 /************************************************************
520 * Here comes the data parsing stuff that is run as interrupt
521 ************************************************************/
523 * usbvision_find_header()
525 * Locate one of supported header markers in the scratch buffer.
527 static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
529 struct usbvision_frame *frame;
532 frame = usbvision->curFrame;
534 while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
535 // found header in scratch
536 PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
537 frame->isocHeader.magic_2,
538 frame->isocHeader.magic_1,
539 frame->isocHeader.headerLength,
540 frame->isocHeader.frameNum,
541 frame->isocHeader.framePhase,
542 frame->isocHeader.frameLatency,
543 frame->isocHeader.dataFormat,
544 frame->isocHeader.formatParam,
545 frame->isocHeader.frameWidth,
546 frame->isocHeader.frameHeight);
548 if (usbvision->requestIntra) {
549 if (frame->isocHeader.formatParam & 0x80) {
551 usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time
552 usbvision_unrequest_intra(usbvision);
563 frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
564 frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
565 frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
567 else { // no header found
568 PDEBUG(DBG_HEADER, "skipping scratch data, no header");
569 scratch_reset(usbvision);
570 return ParseState_EndParse;
574 if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) {
575 //check isocHeader.frameNum for lost frames
576 if (usbvision->lastIsocFrameNum >= 0) {
577 if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) {
578 // unexpected frame drop: need to request new intra frame
579 PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum);
580 usbvision_request_intra(usbvision);
581 return ParseState_NextFrame;
584 usbvision->lastIsocFrameNum = frame->isocHeader.frameNum;
586 usbvision->header_count++;
587 frame->scanstate = ScanState_Lines;
590 if (force_testpattern) {
591 usbvision_testpattern(usbvision, 1, 1);
592 return ParseState_NextFrame;
594 return ParseState_Continue;
597 static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision,
600 volatile struct usbvision_frame *frame;
604 unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
605 unsigned char rv, gv, bv; // RGB components
606 int clipmask_index, bytes_per_pixel;
607 int stretch_bytes, clipmask_add;
609 frame = usbvision->curFrame;
610 f = frame->data + (frame->v4l2_linesize * frame->curline);
612 /* Make sure there's enough data for the entire line */
613 len = (frame->isocHeader.frameWidth * 2)+5;
614 if (scratch_len(usbvision) < len) {
615 PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
616 return ParseState_Out;
619 if ((frame->curline + 1) >= frame->frmheight) {
620 return ParseState_NextFrame;
623 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
624 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
625 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
626 clipmask_add = usbvision->stretch_width;
628 for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) {
630 scratch_get(usbvision, &yuyv[0], 4);
632 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
638 YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
639 switch (frame->v4l2_format.format) {
640 case V4L2_PIX_FMT_RGB565:
643 *f++ = (0x07 & (gv >> 3)) |
646 case V4L2_PIX_FMT_RGB24:
651 case V4L2_PIX_FMT_RGB32:
657 case V4L2_PIX_FMT_RGB555:
660 *f++ = (0x03 & (gv >> 3)) |
665 clipmask_index += clipmask_add;
668 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
674 YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
675 switch (frame->v4l2_format.format) {
676 case V4L2_PIX_FMT_RGB565:
679 *f++ = (0x07 & (gv >> 3)) |
682 case V4L2_PIX_FMT_RGB24:
687 case V4L2_PIX_FMT_RGB32:
693 case V4L2_PIX_FMT_RGB555:
696 *f++ = (0x03 & (gv >> 3)) |
701 clipmask_index += clipmask_add;
705 frame->curline += usbvision->stretch_height;
706 *pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
708 if (frame->curline >= frame->frmheight) {
709 return ParseState_NextFrame;
712 return ParseState_Continue;
716 /* The decompression routine */
717 static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
718 unsigned char *Decompressed, int *StartPos,
719 int *BlockTypeStartPos, int Len)
721 int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen;
722 unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator;
726 BlockTypePos = *BlockTypeStartPos;
727 MaxPos = 396; //Pos + Len;
737 for (Idx = 0; Idx < Len; Idx++) {
740 if (BlockTypeLen==0) {
741 BlockTypeByte = Compressed[BlockTypePos];
745 BlockType = (BlockTypeByte & 0xC0) >> 6;
748 usbvision->ComprBlockTypes[BlockType]++;
751 if (BlockType == 0) {
752 if(RestPixel >= 24) {
755 Integrator = Decompressed[Idx];
757 Idx += RestPixel - 1;
761 BlockCode = Compressed[Pos];
763 if (RestPixel >= 24) {
766 BlockLen = RestPixel;
768 RestPixel -= BlockLen;
769 ExtraPos = Pos + (BlockLen / 4);
775 if ((BlockLen%4) == 0) {
776 BlockByte = Compressed[Pos];
779 if (BlockType == 1) { //inter Block
780 Integrator = Decompressed[Idx];
782 switch (BlockByte & 0xC0) {
784 Integrator += Compressed[ExtraPos];
788 Integrator += BlockCode;
791 Integrator -= BlockCode;
794 Decompressed[Idx] = Integrator;
799 *StartPos = ExtraPos;
800 *BlockTypeStartPos = BlockTypePos;
806 * usbvision_parse_compress()
808 * Parse compressed frame from the scratch buffer, put
809 * decoded RGB value into the current frame buffer and add the written
810 * number of bytes (RGB) to the *pcopylen.
813 static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
816 #define USBVISION_STRIP_MAGIC 0x5A
817 #define USBVISION_STRIP_LEN_MAX 400
818 #define USBVISION_STRIP_HEADER_LEN 3
820 struct usbvision_frame *frame;
821 unsigned char *f,*u = NULL ,*v = NULL;
822 unsigned char StripData[USBVISION_STRIP_LEN_MAX];
823 unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
824 int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
825 int clipmask_index, bytes_per_pixel, rc;
827 unsigned char rv, gv, bv;
828 static unsigned char *Y, *U, *V;
830 frame = usbvision->curFrame;
831 imageSize = frame->frmwidth * frame->frmheight;
832 if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
833 (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) { // this is a planar format
834 //... v4l2_linesize not used here.
835 f = frame->data + (frame->width * frame->curline);
837 f = frame->data + (frame->v4l2_linesize * frame->curline);
839 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
840 // get base of u and b planes add halfoffset
844 + (frame->frmwidth >>1) * frame->curline ;
845 v = u + (imageSize >>1 );
847 } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
849 v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
850 u = v + (imageSize >>2) ;
853 if (frame->curline == 0) {
854 usbvision_adjust_compression(usbvision);
857 if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) {
858 return ParseState_Out;
861 //get strip header without changing the scratch_read_ptr
862 scratch_set_extra_ptr(usbvision, &StripPtr, 0);
863 scratch_get_extra(usbvision, &StripHeader[0], &StripPtr,
864 USBVISION_STRIP_HEADER_LEN);
866 if (StripHeader[0] != USBVISION_STRIP_MAGIC) {
868 usbvision->stripMagicErrors++;
869 return ParseState_NextFrame;
872 if (frame->curline != (int)StripHeader[2]) {
873 //line number missmatch error
874 usbvision->stripLineNumberErrors++;
877 StripLen = 2 * (unsigned int)StripHeader[1];
878 if (StripLen > USBVISION_STRIP_LEN_MAX) {
880 // I think this never happens
881 usbvision_request_intra(usbvision);
884 if (scratch_len(usbvision) < StripLen) {
885 //there is not enough data for the strip
886 return ParseState_Out;
889 if (usbvision->IntraFrameBuffer) {
890 Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline;
891 U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2);
892 V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
895 return ParseState_NextFrame;
898 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
899 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
901 scratch_get(usbvision, StripData, StripLen);
903 IdxEnd = frame->frmwidth;
904 BlockTypePos = USBVISION_STRIP_HEADER_LEN;
905 StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2;
906 BlockPos = StartBlockPos;
908 usbvision->BlockPos = BlockPos;
910 if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) {
911 //return ParseState_Continue;
913 if (StripLen > usbvision->maxStripLen) {
914 usbvision->maxStripLen = StripLen;
917 if (frame->curline%2) {
918 if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
919 //return ParseState_Continue;
923 if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
924 //return ParseState_Continue;
928 if (BlockPos > usbvision->comprBlockPos) {
929 usbvision->comprBlockPos = BlockPos;
931 if (BlockPos > StripLen) {
932 usbvision->stripLenErrors++;
935 for (Idx = 0; Idx < IdxEnd; Idx++) {
936 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
938 *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
940 else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
947 else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
949 if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){
951 /* only need do this for 1 in 4 pixels */
952 /* intraframe buffer is YUV420 format */
960 YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
961 switch (frame->v4l2_format.format) {
962 case V4L2_PIX_FMT_GREY:
965 case V4L2_PIX_FMT_RGB555:
968 *f++ = (0x03 & (gv >> 3)) |
971 case V4L2_PIX_FMT_RGB565:
974 *f++ = (0x07 & (gv >> 3)) |
977 case V4L2_PIX_FMT_RGB24:
982 case V4L2_PIX_FMT_RGB32:
992 /* Deal with non-integer no. of bytes for YUV420P */
993 if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
994 *pcopylen += frame->v4l2_linesize;
996 *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
1000 if (frame->curline >= frame->frmheight) {
1001 return ParseState_NextFrame;
1004 return ParseState_Continue;
1011 * usbvision_parse_lines_420()
1013 * Parse two lines from the scratch buffer, put
1014 * decoded RGB value into the current frame buffer and add the written
1015 * number of bytes (RGB) to the *pcopylen.
1018 static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision,
1021 struct usbvision_frame *frame;
1022 unsigned char *f_even = NULL, *f_odd = NULL;
1023 unsigned int pixel_per_line, block;
1024 int pixel, block_split;
1025 int y_ptr, u_ptr, v_ptr, y_odd_offset;
1026 const int y_block_size = 128;
1027 const int uv_block_size = 64;
1028 const int sub_block_size = 32;
1029 const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
1030 const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4;
1031 unsigned char y[2], u, v; /* YUV components */
1032 int y_, u_, v_, vb, uvg, ur;
1033 int r_, g_, b_; /* RGB components */
1035 int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
1036 int clipmask_add, stretch_bytes;
1038 frame = usbvision->curFrame;
1039 f_even = frame->data + (frame->v4l2_linesize * frame->curline);
1040 f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height;
1042 /* Make sure there's enough data for the entire line */
1043 /* In this mode usbvision transfer 3 bytes for every 2 pixels */
1044 /* I need two lines to decode the color */
1045 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
1046 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
1047 clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
1048 clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH;
1049 clipmask_add = usbvision->stretch_width;
1050 pixel_per_line = frame->isocHeader.frameWidth;
1052 if (scratch_len(usbvision) < (int)pixel_per_line * 3) {
1053 //printk(KERN_DEBUG "out of data, need %d\n", len);
1054 return ParseState_Out;
1057 if ((frame->curline + 1) >= frame->frmheight) {
1058 return ParseState_NextFrame;
1061 block_split = (pixel_per_line%y_block_size) ? 1 : 0; //are some blocks splitted into different lines?
1063 y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
1064 + block_split * uv_block_size;
1066 scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
1067 scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
1068 scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
1069 + (4 - block_split) * sub_block_size);
1071 for (block = 0; block < (pixel_per_line / sub_block_size);
1075 for (pixel = 0; pixel < sub_block_size; pixel +=2) {
1076 scratch_get(usbvision, &y[0], 2);
1077 scratch_get_extra(usbvision, &u, &u_ptr, 1);
1078 scratch_get_extra(usbvision, &v, &v_ptr, 1);
1080 //I don't use the YUV_TO_RGB macro for better performance
1084 uvg= -53281 * u_ - 25625 * v_;
1087 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1092 y_ = 76284 * (y[0] - 16);
1094 b_ = (y_ + vb) >> 16;
1095 g_ = (y_ + uvg)>> 16;
1096 r_ = (y_ + ur) >> 16;
1098 switch (frame->v4l2_format.format) {
1099 case V4L2_PIX_FMT_RGB565:
1102 (0x1F & LIMIT_RGB(r_)) |
1106 (0xF8 & LIMIT_RGB(b_));
1108 case V4L2_PIX_FMT_RGB24:
1109 *f_even++ = LIMIT_RGB(r_);
1110 *f_even++ = LIMIT_RGB(g_);
1111 *f_even++ = LIMIT_RGB(b_);
1113 case V4L2_PIX_FMT_RGB32:
1114 *f_even++ = LIMIT_RGB(r_);
1115 *f_even++ = LIMIT_RGB(g_);
1116 *f_even++ = LIMIT_RGB(b_);
1119 case V4L2_PIX_FMT_RGB555:
1121 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1123 *f_even++ = (0x03 & (g >> 3)) |
1124 (0x7C & (LIMIT_RGB(b_) << 2));
1128 clipmask_even_index += clipmask_add;
1129 f_even += stretch_bytes;
1131 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1136 y_ = 76284 * (y[1] - 16);
1138 b_ = (y_ + vb) >> 16;
1139 g_ = (y_ + uvg)>> 16;
1140 r_ = (y_ + ur) >> 16;
1142 switch (frame->v4l2_format.format) {
1143 case V4L2_PIX_FMT_RGB565:
1146 (0x1F & LIMIT_RGB(r_)) |
1150 (0xF8 & LIMIT_RGB(b_));
1152 case V4L2_PIX_FMT_RGB24:
1153 *f_even++ = LIMIT_RGB(r_);
1154 *f_even++ = LIMIT_RGB(g_);
1155 *f_even++ = LIMIT_RGB(b_);
1157 case V4L2_PIX_FMT_RGB32:
1158 *f_even++ = LIMIT_RGB(r_);
1159 *f_even++ = LIMIT_RGB(g_);
1160 *f_even++ = LIMIT_RGB(b_);
1163 case V4L2_PIX_FMT_RGB555:
1165 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1167 *f_even++ = (0x03 & (g >> 3)) |
1168 (0x7C & (LIMIT_RGB(b_) << 2));
1172 clipmask_even_index += clipmask_add;
1173 f_even += stretch_bytes;
1175 scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
1177 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1182 y_ = 76284 * (y[0] - 16);
1184 b_ = (y_ + vb) >> 16;
1185 g_ = (y_ + uvg)>> 16;
1186 r_ = (y_ + ur) >> 16;
1188 switch (frame->v4l2_format.format) {
1189 case V4L2_PIX_FMT_RGB565:
1192 (0x1F & LIMIT_RGB(r_)) |
1196 (0xF8 & LIMIT_RGB(b_));
1198 case V4L2_PIX_FMT_RGB24:
1199 *f_odd++ = LIMIT_RGB(r_);
1200 *f_odd++ = LIMIT_RGB(g_);
1201 *f_odd++ = LIMIT_RGB(b_);
1203 case V4L2_PIX_FMT_RGB32:
1204 *f_odd++ = LIMIT_RGB(r_);
1205 *f_odd++ = LIMIT_RGB(g_);
1206 *f_odd++ = LIMIT_RGB(b_);
1209 case V4L2_PIX_FMT_RGB555:
1211 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1213 *f_odd++ = (0x03 & (g >> 3)) |
1214 (0x7C & (LIMIT_RGB(b_) << 2));
1218 clipmask_odd_index += clipmask_add;
1219 f_odd += stretch_bytes;
1221 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
1226 y_ = 76284 * (y[1] - 16);
1228 b_ = (y_ + vb) >> 16;
1229 g_ = (y_ + uvg)>> 16;
1230 r_ = (y_ + ur) >> 16;
1232 switch (frame->v4l2_format.format) {
1233 case V4L2_PIX_FMT_RGB565:
1236 (0x1F & LIMIT_RGB(r_)) |
1240 (0xF8 & LIMIT_RGB(b_));
1242 case V4L2_PIX_FMT_RGB24:
1243 *f_odd++ = LIMIT_RGB(r_);
1244 *f_odd++ = LIMIT_RGB(g_);
1245 *f_odd++ = LIMIT_RGB(b_);
1247 case V4L2_PIX_FMT_RGB32:
1248 *f_odd++ = LIMIT_RGB(r_);
1249 *f_odd++ = LIMIT_RGB(g_);
1250 *f_odd++ = LIMIT_RGB(b_);
1253 case V4L2_PIX_FMT_RGB555:
1255 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1257 *f_odd++ = (0x03 & (g >> 3)) |
1258 (0x7C & (LIMIT_RGB(b_) << 2));
1262 clipmask_odd_index += clipmask_add;
1263 f_odd += stretch_bytes;
1266 scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size);
1267 scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
1269 scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
1271 scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
1275 scratch_rm_old(usbvision, pixel_per_line * 3 / 2
1276 + block_split * sub_block_size);
1278 frame->curline += 2 * usbvision->stretch_height;
1279 *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height;
1281 if (frame->curline >= frame->frmheight)
1282 return ParseState_NextFrame;
1284 return ParseState_Continue;
1288 * usbvision_parse_data()
1290 * Generic routine to parse the scratch buffer. It employs either
1291 * usbvision_find_header() or usbvision_parse_lines() to do most
1295 static void usbvision_parse_data(struct usb_usbvision *usbvision)
1297 struct usbvision_frame *frame;
1298 enum ParseState newstate;
1300 unsigned long lock_flags;
1302 frame = usbvision->curFrame;
1304 PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
1308 newstate = ParseState_Out;
1309 if (scratch_len(usbvision)) {
1310 if (frame->scanstate == ScanState_Scanning) {
1311 newstate = usbvision_find_header(usbvision);
1313 else if (frame->scanstate == ScanState_Lines) {
1314 if (usbvision->isocMode == ISOC_MODE_YUV420) {
1315 newstate = usbvision_parse_lines_420(usbvision, ©len);
1317 else if (usbvision->isocMode == ISOC_MODE_YUV422) {
1318 newstate = usbvision_parse_lines_422(usbvision, ©len);
1320 else if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
1321 newstate = usbvision_parse_compress(usbvision, ©len);
1326 if (newstate == ParseState_Continue) {
1329 else if ((newstate == ParseState_NextFrame) || (newstate == ParseState_Out)) {
1333 return; /* ParseState_EndParse */
1337 if (newstate == ParseState_NextFrame) {
1338 frame->grabstate = FrameState_Done;
1339 do_gettimeofday(&(frame->timestamp));
1340 frame->sequence = usbvision->frame_num;
1342 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1343 list_move_tail(&(frame->frame), &usbvision->outqueue);
1344 usbvision->curFrame = NULL;
1345 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1347 usbvision->frame_num++;
1349 /* This will cause the process to request another frame. */
1350 if (waitqueue_active(&usbvision->wait_frame)) {
1351 PDEBUG(DBG_PARSE, "Wake up !");
1352 wake_up_interruptible(&usbvision->wait_frame);
1356 frame->grabstate = FrameState_Grabbing;
1359 /* Update the frame's uncompressed length. */
1360 frame->scanlength += copylen;
1365 * Make all of the blocks of data contiguous
1367 static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
1370 unsigned char *packet_data;
1373 for (i = 0; i < urb->number_of_packets; i++) {
1374 int packet_len = urb->iso_frame_desc[i].actual_length;
1375 int packet_stat = urb->iso_frame_desc[i].status;
1377 packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1379 /* Detect and ignore errored packets */
1380 if (packet_stat) { // packet_stat != 0 ?????????????
1381 PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
1382 usbvision->isocErrCount++;
1386 /* Detect and ignore empty packets */
1387 if (packet_len < 0) {
1388 PDEBUG(DBG_ISOC, "error packet [%d]", i);
1389 usbvision->isocSkipCount++;
1392 else if (packet_len == 0) { /* Frame end ????? */
1393 PDEBUG(DBG_ISOC, "null packet [%d]", i);
1394 usbvision->isocstate=IsocState_NoFrame;
1395 usbvision->isocSkipCount++;
1398 else if (packet_len > usbvision->isocPacketSize) {
1399 PDEBUG(DBG_ISOC, "packet[%d] > isocPacketSize", i);
1400 usbvision->isocSkipCount++;
1404 PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len);
1406 if (usbvision->isocstate==IsocState_NoFrame) { //new frame begins
1407 usbvision->isocstate=IsocState_InFrame;
1408 scratch_mark_header(usbvision);
1409 usbvision_measure_bandwidth(usbvision);
1410 PDEBUG(DBG_ISOC, "packet with header");
1414 * If usbvision continues to feed us with data but there is no
1415 * consumption (if, for example, V4L client fell asleep) we
1416 * may overflow the buffer. We have to move old data over to
1417 * free room for new data. This is bad for old data. If we
1418 * just drop new data then it's bad for new data... choose
1419 * your favorite evil here.
1421 if (scratch_free(usbvision) < packet_len) {
1423 usbvision->scratch_ovf_count++;
1424 PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d",
1425 scratch_len(usbvision), packet_len);
1426 scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
1429 /* Now we know that there is enough room in scratch buffer */
1430 scratch_put(usbvision, packet_data, packet_len);
1431 totlen += packet_len;
1432 usbvision->isocDataCount += packet_len;
1433 usbvision->isocPacketCount++;
1439 printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen);
1440 usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen);
1448 static void usbvision_isocIrq(struct urb *urb)
1452 struct usb_usbvision *usbvision = urb->context;
1454 unsigned long startTime = jiffies;
1455 struct usbvision_frame **f;
1457 /* We don't want to do anything if we are about to be removed! */
1458 if (!USBVISION_IS_OPERATIONAL(usbvision))
1461 /* any urb with wrong status is ignored without acknowledgement */
1462 if (urb->status == -ENOENT) {
1466 f = &usbvision->curFrame;
1468 /* Manage streaming interruption */
1469 if (usbvision->streaming == Stream_Interrupt) {
1470 usbvision->streaming = Stream_Idle;
1472 (*f)->grabstate = FrameState_Ready;
1473 (*f)->scanstate = ScanState_Scanning;
1475 PDEBUG(DBG_IRQ, "stream interrupted");
1476 wake_up_interruptible(&usbvision->wait_stream);
1479 /* Copy the data received into our scratch buffer */
1480 len = usbvision_compress_isochronous(usbvision, urb);
1482 usbvision->isocUrbCount++;
1483 usbvision->urb_length = len;
1485 if (usbvision->streaming == Stream_On) {
1487 /* If we collected enough data let's parse! */
1488 if ((scratch_len(usbvision) > USBVISION_HEADER_LENGTH) &&
1489 (!list_empty(&(usbvision->inqueue))) ) {
1491 (*f) = list_entry(usbvision->inqueue.next,
1492 struct usbvision_frame,
1495 usbvision_parse_data(usbvision);
1498 /*If we don't have a frame
1499 we're current working on, complain */
1501 "received data, but no one needs it");
1502 scratch_reset(usbvision);
1506 PDEBUG(DBG_IRQ, "received data, but no one needs it");
1507 scratch_reset(usbvision);
1510 usbvision->timeInIrq += jiffies - startTime;
1512 for (i = 0; i < USBVISION_URB_FRAMES; i++) {
1513 urb->iso_frame_desc[i].status = 0;
1514 urb->iso_frame_desc[i].actual_length = 0;
1518 urb->dev = usbvision->dev;
1519 errCode = usb_submit_urb (urb, GFP_ATOMIC);
1522 err("%s: usb_submit_urb failed: error %d",
1529 /*************************************/
1530 /* Low level usbvision access functions */
1531 /*************************************/
1534 * usbvision_read_reg()
1536 * return < 0 -> Error
1540 int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
1543 unsigned char buffer[1];
1545 if (!USBVISION_IS_OPERATIONAL(usbvision))
1548 errCode = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1),
1550 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1551 0, (__u16) reg, buffer, 1, HZ);
1554 err("%s: failed: error %d", __func__, errCode);
1561 * usbvision_write_reg()
1563 * return 1 -> Reg written
1564 * 0 -> usbvision is not yet ready
1565 * -1 -> Something went wrong
1568 int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
1569 unsigned char value)
1573 if (!USBVISION_IS_OPERATIONAL(usbvision))
1576 errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1578 USB_DIR_OUT | USB_TYPE_VENDOR |
1579 USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ);
1582 err("%s: failed: error %d", __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 err("%s failed: error %d", __func__, errCode);
1861 usbvision->curwidth = usbvision->stretch_width * UsbWidth;
1862 usbvision->curheight = usbvision->stretch_height * UsbHeight;
1865 if (usbvision->isocMode == ISOC_MODE_YUV422) {
1866 frameRate = (usbvision->isocPacketSize * 1000) / (UsbWidth * UsbHeight * 2);
1868 else if (usbvision->isocMode == ISOC_MODE_YUV420) {
1869 frameRate = (usbvision->isocPacketSize * 1000) / ((UsbWidth * UsbHeight * 12) / 8);
1872 frameRate = FRAMERATE_MAX;
1875 if (usbvision->tvnormId & V4L2_STD_625_50) {
1876 frameDrop = frameRate * 32 / 25 - 1;
1878 else if (usbvision->tvnormId & V4L2_STD_525_60) {
1879 frameDrop = frameRate * 32 / 30 - 1;
1882 RESTRICT_TO_RANGE(frameDrop, FRAMERATE_MIN, FRAMERATE_MAX);
1884 PDEBUG(DBG_FUNC, "frameRate %d fps, frameDrop %d", frameRate, frameDrop);
1886 frameDrop = FRAMERATE_MAX; // We can allow the maximum here, because dropping is controlled
1888 /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
1890 => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
1892 frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
1893 => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
1894 => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
1896 errCode = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frameDrop);
1902 * usbvision_frames_alloc
1903 * allocate the required frames
1905 int usbvision_frames_alloc(struct usb_usbvision *usbvision, int number_of_frames)
1909 /*needs to be page aligned cause the buffers can be mapped individually! */
1910 usbvision->max_frame_size = PAGE_ALIGN(usbvision->curwidth *
1911 usbvision->curheight *
1912 usbvision->palette.bytes_per_pixel);
1914 /* Try to do my best to allocate the frames the user want in the remaining memory */
1915 usbvision->num_frames = number_of_frames;
1916 while (usbvision->num_frames > 0) {
1917 usbvision->fbuf_size = usbvision->num_frames * usbvision->max_frame_size;
1918 if((usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size))) {
1921 usbvision->num_frames--;
1924 spin_lock_init(&usbvision->queue_lock);
1925 init_waitqueue_head(&usbvision->wait_frame);
1926 init_waitqueue_head(&usbvision->wait_stream);
1928 /* Allocate all buffers */
1929 for (i = 0; i < usbvision->num_frames; i++) {
1930 usbvision->frame[i].index = i;
1931 usbvision->frame[i].grabstate = FrameState_Unused;
1932 usbvision->frame[i].data = usbvision->fbuf +
1933 i * usbvision->max_frame_size;
1935 * Set default sizes for read operation.
1937 usbvision->stretch_width = 1;
1938 usbvision->stretch_height = 1;
1939 usbvision->frame[i].width = usbvision->curwidth;
1940 usbvision->frame[i].height = usbvision->curheight;
1941 usbvision->frame[i].bytes_read = 0;
1943 PDEBUG(DBG_FUNC, "allocated %d frames (%d bytes per frame)",usbvision->num_frames,usbvision->max_frame_size);
1944 return usbvision->num_frames;
1948 * usbvision_frames_free
1949 * frees memory allocated for the frames
1951 void usbvision_frames_free(struct usb_usbvision *usbvision)
1953 /* Have to free all that memory */
1954 PDEBUG(DBG_FUNC, "free %d frames",usbvision->num_frames);
1956 if (usbvision->fbuf != NULL) {
1957 usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
1958 usbvision->fbuf = NULL;
1960 usbvision->num_frames = 0;
1964 * usbvision_empty_framequeues()
1965 * prepare queues for incoming and outgoing frames
1967 void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
1971 INIT_LIST_HEAD(&(usbvision->inqueue));
1972 INIT_LIST_HEAD(&(usbvision->outqueue));
1974 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1975 usbvision->frame[i].grabstate = FrameState_Unused;
1976 usbvision->frame[i].bytes_read = 0;
1981 * usbvision_stream_interrupt()
1984 int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
1988 /* stop reading from the device */
1990 usbvision->streaming = Stream_Interrupt;
1991 ret = wait_event_timeout(usbvision->wait_stream,
1992 (usbvision->streaming == Stream_Idle),
1993 msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES));
1998 * usbvision_set_compress_params()
2002 static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
2004 static const char proc[] = "usbvision_set_compresion_params: ";
2006 unsigned char value[6];
2008 value[0] = 0x0F; // Intra-Compression cycle
2009 value[1] = 0x01; // Reg.45 one line per strip
2010 value[2] = 0x00; // Reg.46 Force intra mode on all new frames
2011 value[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
2012 value[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
2013 value[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
2015 //catched values for NT1004
2016 // value[0] = 0xFF; // Never apply intra mode automatically
2017 // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
2018 // value[2] = 0x01; // Force intra mode on all new frames
2019 // value[3] = 0x00; // Strip size 400 Bytes; do not force up
2020 // value[4] = 0xA2; //
2021 if (!USBVISION_IS_OPERATIONAL(usbvision))
2024 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2026 USB_DIR_OUT | USB_TYPE_VENDOR |
2027 USB_RECIP_ENDPOINT, 0,
2028 (__u16) USBVISION_INTRA_CYC, value, 5, HZ);
2031 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2032 "reconnect or reload driver.\n", proc, rc);
2036 if (usbvision->bridgeType == BRIDGE_NT1004) {
2037 value[0] = 20; // PCM Threshold 1
2038 value[1] = 12; // PCM Threshold 2
2039 value[2] = 255; // Distorsion Threshold inter
2040 value[3] = 255; // Distorsion Threshold intra
2041 value[4] = 43; // Max Distorsion inter
2042 value[5] = 43; // Max Distorsion intra
2045 value[0] = 20; // PCM Threshold 1
2046 value[1] = 12; // PCM Threshold 2
2047 value[2] = 255; // Distorsion Threshold d7-d0
2048 value[3] = 0; // Distorsion Threshold d11-d8
2049 value[4] = 43; // Max Distorsion d7-d0
2050 value[5] = 0; // Max Distorsion d8
2053 if (!USBVISION_IS_OPERATIONAL(usbvision))
2056 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2058 USB_DIR_OUT | USB_TYPE_VENDOR |
2059 USB_RECIP_ENDPOINT, 0,
2060 (__u16) USBVISION_PCM_THR1, value, 6, HZ);
2063 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2064 "reconnect or reload driver.\n", proc, rc);
2074 * usbvision_set_input()
2076 * Set the input (saa711x, ...) size x y and other misc input params
2077 * I've no idea if this parameters are right
2080 int usbvision_set_input(struct usb_usbvision *usbvision)
2082 static const char proc[] = "usbvision_set_input: ";
2084 unsigned char value[8];
2085 unsigned char dvi_yuv_value;
2087 if (!USBVISION_IS_OPERATIONAL(usbvision))
2090 /* Set input format expected from decoder*/
2091 if (usbvision_device_data[usbvision->DevModel].Vin_Reg1_override) {
2092 value[0] = usbvision_device_data[usbvision->DevModel].Vin_Reg1;
2093 } else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
2094 /* SAA7113 uses 8 bit output */
2095 value[0] = USBVISION_8_422_SYNC;
2097 /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
2098 * as that is how saa7111 is configured */
2099 value[0] = USBVISION_16_422_SYNC;
2100 /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
2103 rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]);
2105 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2106 "reconnect or reload driver.\n", proc, rc);
2111 if (usbvision->tvnormId & V4L2_STD_PAL) {
2113 value[1] = 0x02; //0x02C0 -> 704 Input video line length
2115 value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2117 value[5] = 0x00; //0x0060 -> 96 Input video h offset
2119 value[7] = 0x00; //0x0016 -> 22 Input video v offset
2120 } else if (usbvision->tvnormId & V4L2_STD_SECAM) {
2122 value[1] = 0x02; //0x02C0 -> 704 Input video line length
2124 value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2126 value[5] = 0x00; //0x0001 -> 01 Input video h offset
2128 value[7] = 0x00; //0x0001 -> 01 Input video v offset
2129 } else { /* V4L2_STD_NTSC */
2131 value[1] = 0x02; //0x02D0 -> 720 Input video line length
2133 value[3] = 0x00; //0x00F0 -> 240 Input video number of lines
2135 value[5] = 0x00; //0x0050 -> 80 Input video h offset
2137 value[7] = 0x00; //0x0010 -> 16 Input video v offset
2140 if (usbvision_device_data[usbvision->DevModel].X_Offset >= 0) {
2141 value[4]=usbvision_device_data[usbvision->DevModel].X_Offset & 0xff;
2142 value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8;
2145 if (adjust_X_Offset != -1) {
2146 value[4] = adjust_X_Offset & 0xff;
2147 value[5] = (adjust_X_Offset & 0x0300) >> 8;
2150 if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) {
2151 value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff;
2152 value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8;
2155 if (adjust_Y_Offset != -1) {
2156 value[6] = adjust_Y_Offset & 0xff;
2157 value[7] = (adjust_Y_Offset & 0x0300) >> 8;
2160 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2161 USBVISION_OP_CODE, /* USBVISION specific code */
2162 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
2163 (__u16) USBVISION_LXSIZE_I, value, 8, HZ);
2165 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2166 "reconnect or reload driver.\n", proc, rc);
2171 dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
2173 if(usbvision_device_data[usbvision->DevModel].Dvi_yuv_override){
2174 dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv;
2176 else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
2177 /* This changes as the fine sync control changes. Further investigation necessary */
2178 dvi_yuv_value = 0x06;
2181 return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value));
2186 * usbvision_set_dram_settings()
2188 * Set the buffer address needed by the usbvision dram to operate
2189 * This values has been taken with usbsnoop.
2193 static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
2196 unsigned char value[8];
2198 if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
2207 // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
2208 // FDL: 0x00000-0x0E099 = 57498 Words
2209 // VDW: 0x0E3FF-0x3FFFF
2221 /* These are the values of the address of the video buffer,
2222 * they have to be loaded into the USBVISION_DRM_PRM1-8
2224 * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
2225 * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
2226 * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
2227 * Only used in compressed mode
2228 * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
2229 * Only used in compressed mode
2230 * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
2231 * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
2234 if (!USBVISION_IS_OPERATIONAL(usbvision))
2237 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2238 USBVISION_OP_CODE, /* USBVISION specific code */
2239 USB_DIR_OUT | USB_TYPE_VENDOR |
2240 USB_RECIP_ENDPOINT, 0,
2241 (__u16) USBVISION_DRM_PRM1, value, 8, HZ);
2244 err("%sERROR=%d", __func__, rc);
2248 /* Restart the video buffer logic */
2249 if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR |
2250 USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0)
2252 rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00);
2260 * Power on the device, enables suspend-resume logic
2261 * & reset the isoc End-Point
2265 int usbvision_power_on(struct usb_usbvision *usbvision)
2269 PDEBUG(DBG_FUNC, "");
2271 usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
2272 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2273 USBVISION_SSPND_EN | USBVISION_RES2);
2275 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2276 USBVISION_SSPND_EN | USBVISION_PWR_VID);
2277 errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2278 USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2);
2280 usbvision->power = 1;
2282 PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode<0)?"ERROR":"power is on", errCode);
2288 * usbvision timer stuff
2291 // to call usbvision_power_off from task queue
2292 static void call_usbvision_power_off(struct work_struct *work)
2294 struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, powerOffWork);
2296 PDEBUG(DBG_FUNC, "");
2297 if(mutex_lock_interruptible(&usbvision->lock)) {
2302 if(usbvision->user == 0) {
2303 usbvision_i2c_unregister(usbvision);
2305 usbvision_power_off(usbvision);
2306 usbvision->initialized = 0;
2308 mutex_unlock(&usbvision->lock);
2311 static void usbvision_powerOffTimer(unsigned long data)
2313 struct usb_usbvision *usbvision = (void *) data;
2315 PDEBUG(DBG_FUNC, "");
2316 del_timer(&usbvision->powerOffTimer);
2317 INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off);
2318 (void) schedule_work(&usbvision->powerOffWork);
2321 void usbvision_init_powerOffTimer(struct usb_usbvision *usbvision)
2323 init_timer(&usbvision->powerOffTimer);
2324 usbvision->powerOffTimer.data = (long) usbvision;
2325 usbvision->powerOffTimer.function = usbvision_powerOffTimer;
2328 void usbvision_set_powerOffTimer(struct usb_usbvision *usbvision)
2330 mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
2333 void usbvision_reset_powerOffTimer(struct usb_usbvision *usbvision)
2335 if (timer_pending(&usbvision->powerOffTimer)) {
2336 del_timer(&usbvision->powerOffTimer);
2341 * usbvision_begin_streaming()
2342 * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
2343 * idea about the rest
2345 int usbvision_begin_streaming(struct usb_usbvision *usbvision)
2349 if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
2350 usbvision_init_compression(usbvision);
2352 errCode = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID |
2353 usbvision->Vin_Reg2_Preset);
2358 * usbvision_restart_isoc()
2359 * Not sure yet if touching here PWR_REG make loose the config
2362 int usbvision_restart_isoc(struct usb_usbvision *usbvision)
2368 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2369 USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0)
2373 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2374 USBVISION_SSPND_EN | USBVISION_PWR_VID |
2375 USBVISION_RES2)) < 0)
2379 usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
2380 USBVISION_KEEP_BLANK | USBVISION_NOHVALID |
2381 usbvision->Vin_Reg2_Preset)) < 0) return ret;
2383 /* TODO: schedule timeout */
2384 while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) & 0x01) != 1);
2389 int usbvision_audio_off(struct usb_usbvision *usbvision)
2391 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
2392 printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n");
2395 usbvision->AudioMute = 0;
2396 usbvision->AudioChannel = USBVISION_AUDIO_MUTE;
2400 int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
2402 if (!usbvision->AudioMute) {
2403 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) {
2404 printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
2408 usbvision->AudioChannel = AudioChannel;
2412 int usbvision_setup(struct usb_usbvision *usbvision,int format)
2414 usbvision_set_video_format(usbvision, format);
2415 usbvision_set_dram_settings(usbvision);
2416 usbvision_set_compress_params(usbvision);
2417 usbvision_set_input(usbvision);
2418 usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
2419 usbvision_restart_isoc(usbvision);
2422 return USBVISION_IS_OPERATIONAL(usbvision);
2425 int usbvision_set_alternate(struct usb_usbvision *dev)
2427 int errCode, prev_alt = dev->ifaceAlt;
2431 for(i=0;i< dev->num_alt; i++)
2432 if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->ifaceAlt])
2435 if (dev->ifaceAlt != prev_alt) {
2436 dev->isocPacketSize = dev->alt_max_pkt_size[dev->ifaceAlt];
2437 PDEBUG(DBG_FUNC,"setting alternate %d with wMaxPacketSize=%u", dev->ifaceAlt,dev->isocPacketSize);
2438 errCode = usb_set_interface(dev->dev, dev->iface, dev->ifaceAlt);
2440 err ("cannot change alternate number to %d (error=%i)",
2441 dev->ifaceAlt, errCode);
2446 PDEBUG(DBG_ISOC, "ISO Packet Length:%d", dev->isocPacketSize);
2452 * usbvision_init_isoc()
2455 int usbvision_init_isoc(struct usb_usbvision *usbvision)
2457 struct usb_device *dev = usbvision->dev;
2458 int bufIdx, errCode, regValue;
2461 if (!USBVISION_IS_OPERATIONAL(usbvision))
2464 usbvision->curFrame = NULL;
2465 scratch_reset(usbvision);
2467 /* Alternate interface 1 is is the biggest frame size */
2468 errCode = usbvision_set_alternate(usbvision);
2470 usbvision->last_error = errCode;
2473 sb_size = USBVISION_URB_FRAMES * usbvision->isocPacketSize;
2475 regValue = (16 - usbvision_read_reg(usbvision,
2476 USBVISION_ALTER_REG)) & 0x0F;
2478 usbvision->usb_bandwidth = regValue >> 1;
2479 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2480 usbvision->usb_bandwidth);
2484 /* We double buffer the Iso lists */
2486 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
2490 urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
2492 err("%s: usb_alloc_urb() failed", __func__);
2495 usbvision->sbuf[bufIdx].urb = urb;
2496 usbvision->sbuf[bufIdx].data =
2497 usb_buffer_alloc(usbvision->dev,
2500 &urb->transfer_dma);
2502 urb->context = usbvision;
2503 urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp);
2504 urb->transfer_flags = URB_ISO_ASAP;
2506 urb->transfer_buffer = usbvision->sbuf[bufIdx].data;
2507 urb->complete = usbvision_isocIrq;
2508 urb->number_of_packets = USBVISION_URB_FRAMES;
2509 urb->transfer_buffer_length =
2510 usbvision->isocPacketSize * USBVISION_URB_FRAMES;
2511 for (j = k = 0; j < USBVISION_URB_FRAMES; j++,
2512 k += usbvision->isocPacketSize) {
2513 urb->iso_frame_desc[j].offset = k;
2514 urb->iso_frame_desc[j].length =
2515 usbvision->isocPacketSize;
2519 /* Submit all URBs */
2520 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
2521 errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb,
2524 err("%s: usb_submit_urb(%d) failed: error %d",
2525 __func__, bufIdx, errCode);
2529 usbvision->streaming = Stream_Idle;
2530 PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x",
2532 usbvision->video_endp);
2537 * usbvision_stop_isoc()
2539 * This procedure stops streaming and deallocates URBs. Then it
2540 * activates zero-bandwidth alt. setting of the video interface.
2543 void usbvision_stop_isoc(struct usb_usbvision *usbvision)
2545 int bufIdx, errCode, regValue;
2546 int sb_size = USBVISION_URB_FRAMES * usbvision->isocPacketSize;
2548 if ((usbvision->streaming == Stream_Off) || (usbvision->dev == NULL))
2551 /* Unschedule all of the iso td's */
2552 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
2553 usb_kill_urb(usbvision->sbuf[bufIdx].urb);
2554 if (usbvision->sbuf[bufIdx].data){
2555 usb_buffer_free(usbvision->dev,
2557 usbvision->sbuf[bufIdx].data,
2558 usbvision->sbuf[bufIdx].urb->transfer_dma);
2560 usb_free_urb(usbvision->sbuf[bufIdx].urb);
2561 usbvision->sbuf[bufIdx].urb = NULL;
2564 PDEBUG(DBG_ISOC, "%s: streaming=Stream_Off\n", __func__);
2565 usbvision->streaming = Stream_Off;
2567 if (!usbvision->remove_pending) {
2569 /* Set packet size to 0 */
2570 usbvision->ifaceAlt=0;
2571 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
2572 usbvision->ifaceAlt);
2574 err("%s: usb_set_interface() failed: error %d",
2576 usbvision->last_error = errCode;
2578 regValue = (16-usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
2579 usbvision->isocPacketSize =
2580 (regValue == 0) ? 0 : (regValue * 64) - 1;
2581 PDEBUG(DBG_ISOC, "ISO Packet Length:%d",
2582 usbvision->isocPacketSize);
2584 usbvision->usb_bandwidth = regValue >> 1;
2585 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2586 usbvision->usb_bandwidth);
2590 int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
2592 /* inputs #0 and #3 are constant for every SAA711x. */
2593 /* inputs #1 and #2 are variable for SAA7111 and SAA7113 */
2594 int mode[4]= {SAA7115_COMPOSITE0, 0, 0, SAA7115_COMPOSITE3};
2595 int audio[]= {1, 0, 0, 0};
2596 struct v4l2_routing route;
2597 //channel 0 is TV with audiochannel 1 (tuner mono)
2598 //channel 1 is Composite with audio channel 0 (line in)
2599 //channel 2 is S-Video with audio channel 0 (line in)
2600 //channel 3 is additional video inputs to the device with audio channel 0 (line in)
2602 RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
2603 usbvision->ctl_input = channel;
2605 // set the new channel
2606 // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
2607 // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
2609 switch (usbvision_device_data[usbvision->DevModel].Codec) {
2611 mode[1] = SAA7115_COMPOSITE2;
2612 if (SwitchSVideoInput) {
2613 /* To handle problems with S-Video Input for
2614 * some devices. Use SwitchSVideoInput
2615 * parameter when loading the module.*/
2616 mode[2] = SAA7115_COMPOSITE1;
2619 mode[2] = SAA7115_SVIDEO1;
2624 /* modes for saa7111 */
2625 mode[1] = SAA7115_COMPOSITE1;
2626 mode[2] = SAA7115_SVIDEO1;
2629 route.input = mode[channel];
2631 call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
2632 usbvision_set_audio(usbvision, audio[channel]);
2637 * Overrides for Emacs so that we follow Linus's tabbing style.
2638 * ---------------------------------------------------------------------------