V4L/DVB (7501): soc-camera: use a spinlock for videobuffer queue
[deliverable/linux.git] / drivers / media / video / usbvision / usbvision-core.c
CommitLineData
781aa1d1 1/*
483dfdb6 2 * usbvision-core.c - driver for NT100x USB video capture devices
7ca659e3 3 *
781aa1d1
MCC
4 *
5 * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
483dfdb6 6 * Dwaine Garden <dwainegarden@rogers.com>
781aa1d1
MCC
7 *
8 * This module is part of usbvision driver project.
483dfdb6 9 * Updates to driver completed by Dwaine P. Garden
781aa1d1
MCC
10 *
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.
15 *
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.
20 *
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.
781aa1d1
MCC
24 */
25
26#include <linux/kernel.h>
781aa1d1
MCC
27#include <linux/list.h>
28#include <linux/timer.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/utsname.h>
32#include <linux/highmem.h>
781aa1d1
MCC
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>
781aa1d1
MCC
38#include <asm/io.h>
39#include <linux/videodev2.h>
40#include <linux/video_decoder.h>
41#include <linux/i2c.h>
781aa1d1 42
18d8a454
DG
43#include <media/saa7115.h>
44#include <media/v4l2-common.h>
781aa1d1
MCC
45#include <media/tuner.h>
46#include <media/audiochip.h>
47
a1ed551c 48#include <linux/workqueue.h>
781aa1d1
MCC
49
50#ifdef CONFIG_KMOD
51#include <linux/kmod.h>
52#endif
53
54#include "usbvision.h"
781aa1d1 55
ff699e6b 56static unsigned int core_debug;
483dfdb6
TM
57module_param(core_debug,int,0644);
58MODULE_PARM_DESC(core_debug,"enable debug messages [core]");
781aa1d1 59
ff699e6b 60static unsigned int force_testpattern;
483dfdb6
TM
61module_param(force_testpattern,int,0644);
62MODULE_PARM_DESC(force_testpattern,"enable test pattern display [core]");
63
ff699e6b 64static int adjustCompression = 1; /* Set the compression to be adaptive */
483dfdb6
TM
65module_param(adjustCompression, int, 0444);
66MODULE_PARM_DESC(adjustCompression, " Set the ADPCM compression for the device. Default: 1 (On)");
67
ff699e6b
DSL
68/* To help people with Black and White output with using s-video input.
69 * Some cables and input device are wired differently. */
70static int SwitchSVideoInput;
483dfdb6
TM
71module_param(SwitchSVideoInput, int, 0444);
72MODULE_PARM_DESC(SwitchSVideoInput, " Set the S-Video input. Some cables and input device are wired differently. Default: 0 (Off)");
781aa1d1 73
c6243d9c
TM
74static unsigned int adjust_X_Offset = -1;
75module_param(adjust_X_Offset, int, 0644);
76MODULE_PARM_DESC(adjust_X_Offset, "adjust X offset display [core]");
77
78static unsigned int adjust_Y_Offset = -1;
79module_param(adjust_Y_Offset, int, 0644);
80MODULE_PARM_DESC(adjust_Y_Offset, "adjust Y offset display [core]");
81
82
483dfdb6 83#define ENABLE_HEXDUMP 0 /* Enable if you need it */
781aa1d1 84
781aa1d1
MCC
85
86#ifdef USBVISION_DEBUG
87 #define PDEBUG(level, fmt, args...) \
483dfdb6 88 if (core_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
781aa1d1
MCC
89#else
90 #define PDEBUG(level, fmt, args...) do {} while(0)
91#endif
92
483dfdb6
TM
93#define DBG_HEADER 1<<0
94#define DBG_IRQ 1<<1
95#define DBG_ISOC 1<<2
96#define DBG_PARSE 1<<3
97#define DBG_SCRATCH 1<<4
c876a346 98#define DBG_FUNC 1<<5
781aa1d1
MCC
99
100static const int max_imgwidth = MAX_FRAME_WIDTH;
101static const int max_imgheight = MAX_FRAME_HEIGHT;
102static const int min_imgwidth = MIN_FRAME_WIDTH;
103static const int min_imgheight = MIN_FRAME_HEIGHT;
104
483dfdb6 105/* The value of 'scratch_buf_size' affects quality of the picture
781aa1d1
MCC
106 * in many ways. Shorter buffers may cause loss of data when client
107 * is too slow. Larger buffers are memory-consuming and take longer
108 * to work with. This setting can be adjusted, but the default value
109 * should be OK for most desktop users.
110 */
111#define DEFAULT_SCRATCH_BUF_SIZE (0x20000) // 128kB memory scratch buffer
112static const int scratch_buf_size = DEFAULT_SCRATCH_BUF_SIZE;
113
781aa1d1 114// Function prototypes
781aa1d1
MCC
115static int usbvision_request_intra (struct usb_usbvision *usbvision);
116static int usbvision_unrequest_intra (struct usb_usbvision *usbvision);
117static int usbvision_adjust_compression (struct usb_usbvision *usbvision);
118static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision);
781aa1d1
MCC
119
120/*******************************/
121/* Memory management functions */
122/*******************************/
123
124/*
125 * Here we want the physical address of the memory.
126 * This is used when initializing the contents of the area.
127 */
128
0a0ceade 129static void *usbvision_rvmalloc(unsigned long size)
781aa1d1
MCC
130{
131 void *mem;
132 unsigned long adr;
133
134 size = PAGE_ALIGN(size);
135 mem = vmalloc_32(size);
136 if (!mem)
137 return NULL;
138
139 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
483dfdb6
TM
140 adr = (unsigned long) mem;
141 while (size > 0) {
142 SetPageReserved(vmalloc_to_page((void *)adr));
143 adr += PAGE_SIZE;
144 size -= PAGE_SIZE;
145 }
146
147 return mem;
781aa1d1
MCC
148}
149
4a06b538 150static void usbvision_rvfree(void *mem, unsigned long size)
781aa1d1 151{
483dfdb6
TM
152 unsigned long adr;
153
154 if (!mem)
155 return;
781aa1d1 156
483dfdb6 157 size = PAGE_ALIGN(size);
781aa1d1 158
483dfdb6
TM
159 adr = (unsigned long) mem;
160 while ((long) size > 0) {
161 ClearPageReserved(vmalloc_to_page((void *)adr));
162 adr += PAGE_SIZE;
163 size -= PAGE_SIZE;
164 }
781aa1d1 165
483dfdb6
TM
166 vfree(mem);
167}
781aa1d1
MCC
168
169
170
171#if ENABLE_HEXDUMP
172static void usbvision_hexdump(const unsigned char *data, int len)
173{
483dfdb6
TM
174 char tmp[80];
175 int i, k;
176
177 for (i = k = 0; len > 0; i++, len--) {
178 if (i > 0 && (i % 16 == 0)) {
179 printk("%s\n", tmp);
180 k = 0;
181 }
182 k += sprintf(&tmp[k], "%02x ", data[i]);
183 }
184 if (k > 0)
185 printk("%s\n", tmp);
781aa1d1
MCC
186}
187#endif
188
483dfdb6
TM
189/********************************
190 * scratch ring buffer handling
191 ********************************/
0a0ceade 192static int scratch_len(struct usb_usbvision *usbvision) /*This returns the amount of data actually in the buffer */
781aa1d1
MCC
193{
194 int len = usbvision->scratch_write_ptr - usbvision->scratch_read_ptr;
195 if (len < 0) {
196 len += scratch_buf_size;
197 }
198 PDEBUG(DBG_SCRATCH, "scratch_len() = %d\n", len);
199
200 return len;
201}
202
203
204/* This returns the free space left in the buffer */
0a0ceade 205static int scratch_free(struct usb_usbvision *usbvision)
781aa1d1
MCC
206{
207 int free = usbvision->scratch_read_ptr - usbvision->scratch_write_ptr;
208 if (free <= 0) {
209 free += scratch_buf_size;
210 }
211 if (free) {
212 free -= 1; /* at least one byte in the buffer must */
213 /* left blank, otherwise there is no chance to differ between full and empty */
214 }
215 PDEBUG(DBG_SCRATCH, "return %d\n", free);
216
217 return free;
218}
219
220
781aa1d1 221/* This puts data into the buffer */
0a0ceade
AB
222static int scratch_put(struct usb_usbvision *usbvision, unsigned char *data,
223 int len)
781aa1d1
MCC
224{
225 int len_part;
226
227 if (usbvision->scratch_write_ptr + len < scratch_buf_size) {
228 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len);
229 usbvision->scratch_write_ptr += len;
230 }
231 else {
232 len_part = scratch_buf_size - usbvision->scratch_write_ptr;
233 memcpy(usbvision->scratch + usbvision->scratch_write_ptr, data, len_part);
234 if (len == len_part) {
235 usbvision->scratch_write_ptr = 0; /* just set write_ptr to zero */
236 }
237 else {
238 memcpy(usbvision->scratch, data + len_part, len - len_part);
239 usbvision->scratch_write_ptr = len - len_part;
240 }
241 }
242
243 PDEBUG(DBG_SCRATCH, "len=%d, new write_ptr=%d\n", len, usbvision->scratch_write_ptr);
244
245 return len;
246}
247
248/* This marks the write_ptr as position of new frame header */
0a0ceade 249static void scratch_mark_header(struct usb_usbvision *usbvision)
781aa1d1
MCC
250{
251 PDEBUG(DBG_SCRATCH, "header at write_ptr=%d\n", usbvision->scratch_headermarker_write_ptr);
252
253 usbvision->scratch_headermarker[usbvision->scratch_headermarker_write_ptr] =
254 usbvision->scratch_write_ptr;
255 usbvision->scratch_headermarker_write_ptr += 1;
256 usbvision->scratch_headermarker_write_ptr %= USBVISION_NUM_HEADERMARKER;
257}
258
259/* This gets data from the buffer at the given "ptr" position */
0a0ceade
AB
260static int scratch_get_extra(struct usb_usbvision *usbvision,
261 unsigned char *data, int *ptr, int len)
781aa1d1
MCC
262{
263 int len_part;
264 if (*ptr + len < scratch_buf_size) {
265 memcpy(data, usbvision->scratch + *ptr, len);
266 *ptr += len;
267 }
268 else {
269 len_part = scratch_buf_size - *ptr;
270 memcpy(data, usbvision->scratch + *ptr, len_part);
271 if (len == len_part) {
272 *ptr = 0; /* just set the y_ptr to zero */
273 }
274 else {
275 memcpy(data + len_part, usbvision->scratch, len - len_part);
276 *ptr = len - len_part;
277 }
278 }
279
280 PDEBUG(DBG_SCRATCH, "len=%d, new ptr=%d\n", len, *ptr);
281
282 return len;
283}
284
285
286/* This sets the scratch extra read pointer */
0a0ceade
AB
287static void scratch_set_extra_ptr(struct usb_usbvision *usbvision, int *ptr,
288 int len)
781aa1d1
MCC
289{
290 *ptr = (usbvision->scratch_read_ptr + len)%scratch_buf_size;
291
292 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
293}
294
295
296/*This increments the scratch extra read pointer */
0a0ceade 297static void scratch_inc_extra_ptr(int *ptr, int len)
781aa1d1
MCC
298{
299 *ptr = (*ptr + len) % scratch_buf_size;
300
301 PDEBUG(DBG_SCRATCH, "ptr=%d\n", *ptr);
302}
303
304
305/* This gets data from the buffer */
0a0ceade
AB
306static int scratch_get(struct usb_usbvision *usbvision, unsigned char *data,
307 int len)
781aa1d1
MCC
308{
309 int len_part;
310 if (usbvision->scratch_read_ptr + len < scratch_buf_size) {
311 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len);
312 usbvision->scratch_read_ptr += len;
313 }
314 else {
315 len_part = scratch_buf_size - usbvision->scratch_read_ptr;
316 memcpy(data, usbvision->scratch + usbvision->scratch_read_ptr, len_part);
317 if (len == len_part) {
318 usbvision->scratch_read_ptr = 0; /* just set the read_ptr to zero */
319 }
320 else {
321 memcpy(data + len_part, usbvision->scratch, len - len_part);
322 usbvision->scratch_read_ptr = len - len_part;
323 }
324 }
325
326 PDEBUG(DBG_SCRATCH, "len=%d, new read_ptr=%d\n", len, usbvision->scratch_read_ptr);
327
328 return len;
329}
330
331
332/* This sets read pointer to next header and returns it */
0a0ceade
AB
333static int scratch_get_header(struct usb_usbvision *usbvision,
334 struct usbvision_frame_header *header)
781aa1d1
MCC
335{
336 int errCode = 0;
337
338 PDEBUG(DBG_SCRATCH, "from read_ptr=%d", usbvision->scratch_headermarker_read_ptr);
339
340 while (usbvision->scratch_headermarker_write_ptr -
341 usbvision->scratch_headermarker_read_ptr != 0) {
342 usbvision->scratch_read_ptr =
343 usbvision->scratch_headermarker[usbvision->scratch_headermarker_read_ptr];
344 usbvision->scratch_headermarker_read_ptr += 1;
345 usbvision->scratch_headermarker_read_ptr %= USBVISION_NUM_HEADERMARKER;
346 scratch_get(usbvision, (unsigned char *)header, USBVISION_HEADER_LENGTH);
347 if ((header->magic_1 == USBVISION_MAGIC_1)
348 && (header->magic_2 == USBVISION_MAGIC_2)
349 && (header->headerLength == USBVISION_HEADER_LENGTH)) {
350 errCode = USBVISION_HEADER_LENGTH;
351 header->frameWidth = header->frameWidthLo + (header->frameWidthHi << 8);
352 header->frameHeight = header->frameHeightLo + (header->frameHeightHi << 8);
353 break;
354 }
355 }
356
357 return errCode;
358}
359
360
361/*This removes len bytes of old data from the buffer */
0a0ceade 362static void scratch_rm_old(struct usb_usbvision *usbvision, int len)
781aa1d1
MCC
363{
364
365 usbvision->scratch_read_ptr += len;
366 usbvision->scratch_read_ptr %= scratch_buf_size;
367 PDEBUG(DBG_SCRATCH, "read_ptr is now %d\n", usbvision->scratch_read_ptr);
368}
369
370
371/*This resets the buffer - kills all data in it too */
0a0ceade 372static void scratch_reset(struct usb_usbvision *usbvision)
781aa1d1
MCC
373{
374 PDEBUG(DBG_SCRATCH, "\n");
375
376 usbvision->scratch_read_ptr = 0;
377 usbvision->scratch_write_ptr = 0;
378 usbvision->scratch_headermarker_read_ptr = 0;
379 usbvision->scratch_headermarker_write_ptr = 0;
380 usbvision->isocstate = IsocState_NoFrame;
381}
382
483dfdb6 383int usbvision_scratch_alloc(struct usb_usbvision *usbvision)
781aa1d1 384{
38284ba3 385 usbvision->scratch = vmalloc_32(scratch_buf_size);
483dfdb6
TM
386 scratch_reset(usbvision);
387 if(usbvision->scratch == NULL) {
388 err("%s: unable to allocate %d bytes for scratch",
389 __FUNCTION__, scratch_buf_size);
390 return -ENOMEM;
781aa1d1 391 }
483dfdb6 392 return 0;
781aa1d1
MCC
393}
394
483dfdb6 395void usbvision_scratch_free(struct usb_usbvision *usbvision)
781aa1d1 396{
483dfdb6
TM
397 if (usbvision->scratch != NULL) {
398 vfree(usbvision->scratch);
399 usbvision->scratch = NULL;
781aa1d1
MCC
400 }
401}
402
781aa1d1
MCC
403/*
404 * usbvision_testpattern()
405 *
406 * Procedure forms a test pattern (yellow grid on blue background).
407 *
408 * Parameters:
409 * fullframe: if TRUE then entire frame is filled, otherwise the procedure
18d8a454 410 * continues from the current scanline.
781aa1d1 411 * pmode 0: fill the frame with solid blue color (like on VCR or TV)
18d8a454 412 * 1: Draw a colored grid
781aa1d1
MCC
413 *
414 */
0a0ceade
AB
415static void usbvision_testpattern(struct usb_usbvision *usbvision,
416 int fullframe, int pmode)
781aa1d1
MCC
417{
418 static const char proc[] = "usbvision_testpattern";
419 struct usbvision_frame *frame;
420 unsigned char *f;
421 int num_cell = 0;
422 int scan_length = 0;
ff699e6b 423 static int num_pass;
781aa1d1
MCC
424
425 if (usbvision == NULL) {
426 printk(KERN_ERR "%s: usbvision == NULL\n", proc);
427 return;
428 }
f2242ee5
TM
429 if (usbvision->curFrame == NULL) {
430 printk(KERN_ERR "%s: usbvision->curFrame is NULL.\n", proc);
781aa1d1
MCC
431 return;
432 }
433
434 /* Grab the current frame */
f2242ee5 435 frame = usbvision->curFrame;
781aa1d1
MCC
436
437 /* Optionally start at the beginning */
438 if (fullframe) {
439 frame->curline = 0;
440 frame->scanlength = 0;
441 }
442
443 /* Form every scan line */
444 for (; frame->curline < frame->frmheight; frame->curline++) {
445 int i;
446
447 f = frame->data + (usbvision->curwidth * 3 * frame->curline);
448 for (i = 0; i < usbvision->curwidth; i++) {
449 unsigned char cb = 0x80;
450 unsigned char cg = 0;
451 unsigned char cr = 0;
452
453 if (pmode == 1) {
454 if (frame->curline % 32 == 0)
455 cb = 0, cg = cr = 0xFF;
456 else if (i % 32 == 0) {
457 if (frame->curline % 32 == 1)
458 num_cell++;
459 cb = 0, cg = cr = 0xFF;
460 } else {
461 cb =
462 ((num_cell * 7) +
463 num_pass) & 0xFF;
464 cg =
465 ((num_cell * 5) +
466 num_pass * 2) & 0xFF;
467 cr =
468 ((num_cell * 3) +
469 num_pass * 3) & 0xFF;
470 }
471 } else {
472 /* Just the blue screen */
473 }
474
475 *f++ = cb;
476 *f++ = cg;
477 *f++ = cr;
478 scan_length += 3;
479 }
480 }
481
482 frame->grabstate = FrameState_Done;
483 frame->scanlength += scan_length;
484 ++num_pass;
485
781aa1d1
MCC
486}
487
488/*
483dfdb6
TM
489 * usbvision_decompress_alloc()
490 *
491 * allocates intermediate buffer for decompression
781aa1d1 492 */
483dfdb6
TM
493int usbvision_decompress_alloc(struct usb_usbvision *usbvision)
494{
495 int IFB_size = MAX_FRAME_WIDTH * MAX_FRAME_HEIGHT * 3 / 2;
38284ba3 496 usbvision->IntraFrameBuffer = vmalloc_32(IFB_size);
483dfdb6
TM
497 if (usbvision->IntraFrameBuffer == NULL) {
498 err("%s: unable to allocate %d for compr. frame buffer", __FUNCTION__, IFB_size);
499 return -ENOMEM;
500 }
501 return 0;
502}
503
504/*
505 * usbvision_decompress_free()
506 *
507 * frees intermediate buffer for decompression
508 */
509void usbvision_decompress_free(struct usb_usbvision *usbvision)
510{
511 if (usbvision->IntraFrameBuffer != NULL) {
512 vfree(usbvision->IntraFrameBuffer);
513 usbvision->IntraFrameBuffer = NULL;
514 }
515}
516
517/************************************************************
518 * Here comes the data parsing stuff that is run as interrupt
519 ************************************************************/
781aa1d1
MCC
520/*
521 * usbvision_find_header()
522 *
523 * Locate one of supported header markers in the scratch buffer.
524 */
525static enum ParseState usbvision_find_header(struct usb_usbvision *usbvision)
526{
527 struct usbvision_frame *frame;
528 int foundHeader = 0;
529
483dfdb6 530 frame = usbvision->curFrame;
781aa1d1
MCC
531
532 while (scratch_get_header(usbvision, &frame->isocHeader) == USBVISION_HEADER_LENGTH) {
533 // found header in scratch
534 PDEBUG(DBG_HEADER, "found header: 0x%02x%02x %d %d %d %d %#x 0x%02x %u %u",
535 frame->isocHeader.magic_2,
536 frame->isocHeader.magic_1,
537 frame->isocHeader.headerLength,
538 frame->isocHeader.frameNum,
539 frame->isocHeader.framePhase,
540 frame->isocHeader.frameLatency,
541 frame->isocHeader.dataFormat,
542 frame->isocHeader.formatParam,
543 frame->isocHeader.frameWidth,
544 frame->isocHeader.frameHeight);
545
546 if (usbvision->requestIntra) {
547 if (frame->isocHeader.formatParam & 0x80) {
548 foundHeader = 1;
549 usbvision->lastIsocFrameNum = -1; // do not check for lost frames this time
550 usbvision_unrequest_intra(usbvision);
551 break;
552 }
553 }
554 else {
555 foundHeader = 1;
556 break;
557 }
558 }
559
560 if (foundHeader) {
561 frame->frmwidth = frame->isocHeader.frameWidth * usbvision->stretch_width;
562 frame->frmheight = frame->isocHeader.frameHeight * usbvision->stretch_height;
563 frame->v4l2_linesize = (frame->frmwidth * frame->v4l2_format.depth)>> 3;
781aa1d1
MCC
564 }
565 else { // no header found
566 PDEBUG(DBG_HEADER, "skipping scratch data, no header");
567 scratch_reset(usbvision);
568 return ParseState_EndParse;
569 }
570
571 // found header
572 if (frame->isocHeader.dataFormat==ISOC_MODE_COMPRESS) {
573 //check isocHeader.frameNum for lost frames
574 if (usbvision->lastIsocFrameNum >= 0) {
575 if (((usbvision->lastIsocFrameNum + 1) % 32) != frame->isocHeader.frameNum) {
576 // unexpected frame drop: need to request new intra frame
577 PDEBUG(DBG_HEADER, "Lost frame before %d on USB", frame->isocHeader.frameNum);
578 usbvision_request_intra(usbvision);
579 return ParseState_NextFrame;
580 }
581 }
582 usbvision->lastIsocFrameNum = frame->isocHeader.frameNum;
583 }
584 usbvision->header_count++;
585 frame->scanstate = ScanState_Lines;
586 frame->curline = 0;
587
483dfdb6 588 if (force_testpattern) {
781aa1d1
MCC
589 usbvision_testpattern(usbvision, 1, 1);
590 return ParseState_NextFrame;
591 }
592 return ParseState_Continue;
593}
594
595static enum ParseState usbvision_parse_lines_422(struct usb_usbvision *usbvision,
596 long *pcopylen)
597{
598 volatile struct usbvision_frame *frame;
599 unsigned char *f;
600 int len;
601 int i;
602 unsigned char yuyv[4]={180, 128, 10, 128}; // YUV components
603 unsigned char rv, gv, bv; // RGB components
604 int clipmask_index, bytes_per_pixel;
781aa1d1
MCC
605 int stretch_bytes, clipmask_add;
606
483dfdb6
TM
607 frame = usbvision->curFrame;
608 f = frame->data + (frame->v4l2_linesize * frame->curline);
781aa1d1
MCC
609
610 /* Make sure there's enough data for the entire line */
611 len = (frame->isocHeader.frameWidth * 2)+5;
612 if (scratch_len(usbvision) < len) {
613 PDEBUG(DBG_PARSE, "out of data in line %d, need %u.\n", frame->curline, len);
614 return ParseState_Out;
615 }
616
617 if ((frame->curline + 1) >= frame->frmheight) {
618 return ParseState_NextFrame;
619 }
620
621 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
622 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
623 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
624 clipmask_add = usbvision->stretch_width;
625
626 for (i = 0; i < frame->frmwidth; i+=(2 * usbvision->stretch_width)) {
627
628 scratch_get(usbvision, &yuyv[0], 4);
629
483dfdb6 630 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
631 *f++ = yuyv[0]; // Y
632 *f++ = yuyv[3]; // U
633 }
634 else {
635
636 YUV_TO_RGB_BY_THE_BOOK(yuyv[0], yuyv[1], yuyv[3], rv, gv, bv);
637 switch (frame->v4l2_format.format) {
6200bbaa
TM
638 case V4L2_PIX_FMT_RGB565:
639 *f++ = (0x1F & rv) |
640 (0xE0 & (gv << 5));
641 *f++ = (0x07 & (gv >> 3)) |
642 (0xF8 & bv);
643 break;
644 case V4L2_PIX_FMT_RGB24:
645 *f++ = rv;
646 *f++ = gv;
647 *f++ = bv;
648 break;
649 case V4L2_PIX_FMT_RGB32:
650 *f++ = rv;
651 *f++ = gv;
652 *f++ = bv;
653 f++;
654 break;
655 case V4L2_PIX_FMT_RGB555:
656 *f++ = (0x1F & rv) |
657 (0xE0 & (gv << 5));
658 *f++ = (0x03 & (gv >> 3)) |
659 (0x7C & (bv << 2));
660 break;
781aa1d1
MCC
661 }
662 }
663 clipmask_index += clipmask_add;
664 f += stretch_bytes;
665
483dfdb6 666 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
667 *f++ = yuyv[2]; // Y
668 *f++ = yuyv[1]; // V
669 }
670 else {
671
672 YUV_TO_RGB_BY_THE_BOOK(yuyv[2], yuyv[1], yuyv[3], rv, gv, bv);
673 switch (frame->v4l2_format.format) {
6200bbaa
TM
674 case V4L2_PIX_FMT_RGB565:
675 *f++ = (0x1F & rv) |
676 (0xE0 & (gv << 5));
677 *f++ = (0x07 & (gv >> 3)) |
678 (0xF8 & bv);
679 break;
680 case V4L2_PIX_FMT_RGB24:
681 *f++ = rv;
682 *f++ = gv;
683 *f++ = bv;
684 break;
685 case V4L2_PIX_FMT_RGB32:
686 *f++ = rv;
687 *f++ = gv;
688 *f++ = bv;
689 f++;
690 break;
691 case V4L2_PIX_FMT_RGB555:
692 *f++ = (0x1F & rv) |
693 (0xE0 & (gv << 5));
694 *f++ = (0x03 & (gv >> 3)) |
695 (0x7C & (bv << 2));
696 break;
781aa1d1
MCC
697 }
698 }
699 clipmask_index += clipmask_add;
700 f += stretch_bytes;
701 }
702
703 frame->curline += usbvision->stretch_height;
704 *pcopylen += frame->v4l2_linesize * usbvision->stretch_height;
705
706 if (frame->curline >= frame->frmheight) {
707 return ParseState_NextFrame;
708 }
709 else {
710 return ParseState_Continue;
711 }
712}
713
483dfdb6 714/* The decompression routine */
781aa1d1
MCC
715static int usbvision_decompress(struct usb_usbvision *usbvision,unsigned char *Compressed,
716 unsigned char *Decompressed, int *StartPos,
717 int *BlockTypeStartPos, int Len)
718{
719 int RestPixel, Idx, MaxPos, Pos, ExtraPos, BlockLen, BlockTypePos, BlockTypeLen;
720 unsigned char BlockByte, BlockCode, BlockType, BlockTypeByte, Integrator;
721
722 Integrator = 0;
723 Pos = *StartPos;
724 BlockTypePos = *BlockTypeStartPos;
725 MaxPos = 396; //Pos + Len;
726 ExtraPos = Pos;
727 BlockLen = 0;
728 BlockByte = 0;
729 BlockCode = 0;
730 BlockType = 0;
731 BlockTypeByte = 0;
732 BlockTypeLen = 0;
733 RestPixel = Len;
734
735 for (Idx = 0; Idx < Len; Idx++) {
736
737 if (BlockLen == 0) {
738 if (BlockTypeLen==0) {
739 BlockTypeByte = Compressed[BlockTypePos];
740 BlockTypePos++;
741 BlockTypeLen = 4;
742 }
743 BlockType = (BlockTypeByte & 0xC0) >> 6;
744
745 //statistic:
746 usbvision->ComprBlockTypes[BlockType]++;
747
748 Pos = ExtraPos;
749 if (BlockType == 0) {
750 if(RestPixel >= 24) {
751 Idx += 23;
752 RestPixel -= 24;
753 Integrator = Decompressed[Idx];
754 } else {
755 Idx += RestPixel - 1;
756 RestPixel = 0;
757 }
758 } else {
759 BlockCode = Compressed[Pos];
760 Pos++;
761 if (RestPixel >= 24) {
762 BlockLen = 24;
763 } else {
764 BlockLen = RestPixel;
765 }
766 RestPixel -= BlockLen;
767 ExtraPos = Pos + (BlockLen / 4);
768 }
769 BlockTypeByte <<= 2;
770 BlockTypeLen -= 1;
771 }
772 if (BlockLen > 0) {
773 if ((BlockLen%4) == 0) {
774 BlockByte = Compressed[Pos];
775 Pos++;
776 }
777 if (BlockType == 1) { //inter Block
778 Integrator = Decompressed[Idx];
779 }
780 switch (BlockByte & 0xC0) {
781 case 0x03<<6:
782 Integrator += Compressed[ExtraPos];
783 ExtraPos++;
784 break;
785 case 0x02<<6:
786 Integrator += BlockCode;
787 break;
788 case 0x00:
789 Integrator -= BlockCode;
790 break;
791 }
792 Decompressed[Idx] = Integrator;
793 BlockByte <<= 2;
794 BlockLen -= 1;
795 }
796 }
797 *StartPos = ExtraPos;
798 *BlockTypeStartPos = BlockTypePos;
799 return Idx;
800}
801
802
803/*
804 * usbvision_parse_compress()
805 *
806 * Parse compressed frame from the scratch buffer, put
807 * decoded RGB value into the current frame buffer and add the written
808 * number of bytes (RGB) to the *pcopylen.
809 *
810 */
811static enum ParseState usbvision_parse_compress(struct usb_usbvision *usbvision,
812 long *pcopylen)
813{
814#define USBVISION_STRIP_MAGIC 0x5A
815#define USBVISION_STRIP_LEN_MAX 400
816#define USBVISION_STRIP_HEADER_LEN 3
817
818 struct usbvision_frame *frame;
819 unsigned char *f,*u = NULL ,*v = NULL;
820 unsigned char StripData[USBVISION_STRIP_LEN_MAX];
821 unsigned char StripHeader[USBVISION_STRIP_HEADER_LEN];
822 int Idx, IdxEnd, StripLen, StripPtr, StartBlockPos, BlockPos, BlockTypePos;
823 int clipmask_index, bytes_per_pixel, rc;
781aa1d1
MCC
824 int imageSize;
825 unsigned char rv, gv, bv;
826 static unsigned char *Y, *U, *V;
827
483dfdb6
TM
828 frame = usbvision->curFrame;
829 imageSize = frame->frmwidth * frame->frmheight;
830 if ( (frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) ||
831 (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) ) { // this is a planar format
832 //... v4l2_linesize not used here.
833 f = frame->data + (frame->width * frame->curline);
834 } else
835 f = frame->data + (frame->v4l2_linesize * frame->curline);
836
837 if (frame->v4l2_format.format == V4L2_PIX_FMT_YUYV){ //initialise u and v pointers
838 // get base of u and b planes add halfoffset
839
840 u = frame->data
841 + imageSize
842 + (frame->frmwidth >>1) * frame->curline ;
843 v = u + (imageSize >>1 );
844
845 } else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420){
846
847 v = frame->data + imageSize + ((frame->curline* (frame->width))>>2) ;
848 u = v + (imageSize >>2) ;
781aa1d1
MCC
849 }
850
851 if (frame->curline == 0) {
852 usbvision_adjust_compression(usbvision);
853 }
854
855 if (scratch_len(usbvision) < USBVISION_STRIP_HEADER_LEN) {
856 return ParseState_Out;
857 }
858
859 //get strip header without changing the scratch_read_ptr
860 scratch_set_extra_ptr(usbvision, &StripPtr, 0);
861 scratch_get_extra(usbvision, &StripHeader[0], &StripPtr,
862 USBVISION_STRIP_HEADER_LEN);
863
864 if (StripHeader[0] != USBVISION_STRIP_MAGIC) {
865 // wrong strip magic
866 usbvision->stripMagicErrors++;
867 return ParseState_NextFrame;
868 }
869
870 if (frame->curline != (int)StripHeader[2]) {
871 //line number missmatch error
872 usbvision->stripLineNumberErrors++;
873 }
874
875 StripLen = 2 * (unsigned int)StripHeader[1];
876 if (StripLen > USBVISION_STRIP_LEN_MAX) {
877 // strip overrun
878 // I think this never happens
879 usbvision_request_intra(usbvision);
880 }
881
882 if (scratch_len(usbvision) < StripLen) {
883 //there is not enough data for the strip
884 return ParseState_Out;
885 }
886
887 if (usbvision->IntraFrameBuffer) {
888 Y = usbvision->IntraFrameBuffer + frame->frmwidth * frame->curline;
889 U = usbvision->IntraFrameBuffer + imageSize + (frame->frmwidth / 2) * (frame->curline / 2);
890 V = usbvision->IntraFrameBuffer + imageSize / 4 * 5 + (frame->frmwidth / 2) * (frame->curline / 2);
891 }
892 else {
893 return ParseState_NextFrame;
894 }
895
18d8a454 896 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
781aa1d1
MCC
897 clipmask_index = frame->curline * MAX_FRAME_WIDTH;
898
899 scratch_get(usbvision, StripData, StripLen);
900
901 IdxEnd = frame->frmwidth;
902 BlockTypePos = USBVISION_STRIP_HEADER_LEN;
903 StartBlockPos = BlockTypePos + (IdxEnd - 1) / 96 + (IdxEnd / 2 - 1) / 96 + 2;
904 BlockPos = StartBlockPos;
905
906 usbvision->BlockPos = BlockPos;
907
908 if ((rc = usbvision_decompress(usbvision, StripData, Y, &BlockPos, &BlockTypePos, IdxEnd)) != IdxEnd) {
909 //return ParseState_Continue;
910 }
911 if (StripLen > usbvision->maxStripLen) {
912 usbvision->maxStripLen = StripLen;
913 }
914
915 if (frame->curline%2) {
916 if ((rc = usbvision_decompress(usbvision, StripData, V, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
917 //return ParseState_Continue;
918 }
919 }
920 else {
921 if ((rc = usbvision_decompress(usbvision, StripData, U, &BlockPos, &BlockTypePos, IdxEnd/2)) != IdxEnd/2) {
922 //return ParseState_Continue;
923 }
924 }
925
926 if (BlockPos > usbvision->comprBlockPos) {
927 usbvision->comprBlockPos = BlockPos;
928 }
929 if (BlockPos > StripLen) {
930 usbvision->stripLenErrors++;
931 }
18d8a454 932
781aa1d1 933 for (Idx = 0; Idx < IdxEnd; Idx++) {
483dfdb6 934 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
935 *f++ = Y[Idx];
936 *f++ = Idx & 0x01 ? U[Idx/2] : V[Idx/2];
937 }
938 else if(frame->v4l2_format.format == V4L2_PIX_FMT_YUV422P) {
939 *f++ = Y[Idx];
940 if ( Idx & 0x01)
941 *u++ = U[Idx>>1] ;
942 else
943 *v++ = V[Idx>>1];
944 }
945 else if (frame->v4l2_format.format == V4L2_PIX_FMT_YVU420) {
946 *f++ = Y [Idx];
18d8a454
DG
947 if ( !(( Idx & 0x01 ) | ( frame->curline & 0x01 )) ){
948
781aa1d1
MCC
949/* only need do this for 1 in 4 pixels */
950/* intraframe buffer is YUV420 format */
951
952 *u++ = U[Idx >>1];
953 *v++ = V[Idx >>1];
954 }
18d8a454 955
781aa1d1
MCC
956 }
957 else {
958 YUV_TO_RGB_BY_THE_BOOK(Y[Idx], U[Idx/2], V[Idx/2], rv, gv, bv);
959 switch (frame->v4l2_format.format) {
960 case V4L2_PIX_FMT_GREY:
961 *f++ = Y[Idx];
962 break;
963 case V4L2_PIX_FMT_RGB555:
6200bbaa
TM
964 *f++ = (0x1F & rv) |
965 (0xE0 & (gv << 5));
966 *f++ = (0x03 & (gv >> 3)) |
967 (0x7C & (bv << 2));
781aa1d1
MCC
968 break;
969 case V4L2_PIX_FMT_RGB565:
6200bbaa
TM
970 *f++ = (0x1F & rv) |
971 (0xE0 & (gv << 5));
972 *f++ = (0x07 & (gv >> 3)) |
973 (0xF8 & bv);
781aa1d1
MCC
974 break;
975 case V4L2_PIX_FMT_RGB24:
781aa1d1 976 *f++ = rv;
6200bbaa
TM
977 *f++ = gv;
978 *f++ = bv;
781aa1d1
MCC
979 break;
980 case V4L2_PIX_FMT_RGB32:
781aa1d1 981 *f++ = rv;
6200bbaa
TM
982 *f++ = gv;
983 *f++ = bv;
781aa1d1
MCC
984 f++;
985 break;
986 }
987 }
988 clipmask_index++;
989 }
18d8a454
DG
990 /* Deal with non-integer no. of bytes for YUV420P */
991 if (frame->v4l2_format.format != V4L2_PIX_FMT_YVU420 )
992 *pcopylen += frame->v4l2_linesize;
993 else
994 *pcopylen += frame->curline & 0x01 ? frame->v4l2_linesize : frame->v4l2_linesize << 1;
995
781aa1d1
MCC
996 frame->curline += 1;
997
998 if (frame->curline >= frame->frmheight) {
999 return ParseState_NextFrame;
1000 }
1001 else {
1002 return ParseState_Continue;
1003 }
1004
1005}
1006
1007
1008/*
1009 * usbvision_parse_lines_420()
1010 *
1011 * Parse two lines from the scratch buffer, put
1012 * decoded RGB value into the current frame buffer and add the written
1013 * number of bytes (RGB) to the *pcopylen.
1014 *
1015 */
1016static enum ParseState usbvision_parse_lines_420(struct usb_usbvision *usbvision,
1017 long *pcopylen)
1018{
1019 struct usbvision_frame *frame;
1020 unsigned char *f_even = NULL, *f_odd = NULL;
1021 unsigned int pixel_per_line, block;
1022 int pixel, block_split;
1023 int y_ptr, u_ptr, v_ptr, y_odd_offset;
1024 const int y_block_size = 128;
1025 const int uv_block_size = 64;
1026 const int sub_block_size = 32;
1027 const int y_step[] = { 0, 0, 0, 2 }, y_step_size = 4;
1028 const int uv_step[]= { 0, 0, 0, 4 }, uv_step_size = 4;
1029 unsigned char y[2], u, v; /* YUV components */
1030 int y_, u_, v_, vb, uvg, ur;
1031 int r_, g_, b_; /* RGB components */
1032 unsigned char g;
1033 int clipmask_even_index, clipmask_odd_index, bytes_per_pixel;
1034 int clipmask_add, stretch_bytes;
781aa1d1 1035
483dfdb6
TM
1036 frame = usbvision->curFrame;
1037 f_even = frame->data + (frame->v4l2_linesize * frame->curline);
1038 f_odd = f_even + frame->v4l2_linesize * usbvision->stretch_height;
781aa1d1
MCC
1039
1040 /* Make sure there's enough data for the entire line */
1041 /* In this mode usbvision transfer 3 bytes for every 2 pixels */
1042 /* I need two lines to decode the color */
18d8a454 1043 bytes_per_pixel = frame->v4l2_format.bytes_per_pixel;
781aa1d1
MCC
1044 stretch_bytes = (usbvision->stretch_width - 1) * bytes_per_pixel;
1045 clipmask_even_index = frame->curline * MAX_FRAME_WIDTH;
1046 clipmask_odd_index = clipmask_even_index + MAX_FRAME_WIDTH;
1047 clipmask_add = usbvision->stretch_width;
1048 pixel_per_line = frame->isocHeader.frameWidth;
1049
1050 if (scratch_len(usbvision) < (int)pixel_per_line * 3) {
1051 //printk(KERN_DEBUG "out of data, need %d\n", len);
1052 return ParseState_Out;
1053 }
1054
1055 if ((frame->curline + 1) >= frame->frmheight) {
1056 return ParseState_NextFrame;
1057 }
1058
1059 block_split = (pixel_per_line%y_block_size) ? 1 : 0; //are some blocks splitted into different lines?
1060
1061 y_odd_offset = (pixel_per_line / y_block_size) * (y_block_size + uv_block_size)
1062 + block_split * uv_block_size;
1063
1064 scratch_set_extra_ptr(usbvision, &y_ptr, y_odd_offset);
1065 scratch_set_extra_ptr(usbvision, &u_ptr, y_block_size);
1066 scratch_set_extra_ptr(usbvision, &v_ptr, y_odd_offset
1067 + (4 - block_split) * sub_block_size);
1068
1069 for (block = 0; block < (pixel_per_line / sub_block_size);
1070 block++) {
1071
1072
1073 for (pixel = 0; pixel < sub_block_size; pixel +=2) {
1074 scratch_get(usbvision, &y[0], 2);
1075 scratch_get_extra(usbvision, &u, &u_ptr, 1);
1076 scratch_get_extra(usbvision, &v, &v_ptr, 1);
1077
1078 //I don't use the YUV_TO_RGB macro for better performance
1079 v_ = v - 128;
1080 u_ = u - 128;
1081 vb = 132252 * v_;
1082 uvg= -53281 * u_ - 25625 * v_;
1083 ur = 104595 * u_;
1084
483dfdb6 1085 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
1086 *f_even++ = y[0];
1087 *f_even++ = v;
1088 }
1089 else {
1090 y_ = 76284 * (y[0] - 16);
1091
1092 b_ = (y_ + vb) >> 16;
1093 g_ = (y_ + uvg)>> 16;
1094 r_ = (y_ + ur) >> 16;
1095
1096 switch (frame->v4l2_format.format) {
6200bbaa
TM
1097 case V4L2_PIX_FMT_RGB565:
1098 g = LIMIT_RGB(g_);
1099 *f_even++ =
1100 (0x1F & LIMIT_RGB(r_)) |
1101 (0xE0 & (g << 5));
1102 *f_even++ =
1103 (0x07 & (g >> 3)) |
1104 (0xF8 & LIMIT_RGB(b_));
1105 break;
1106 case V4L2_PIX_FMT_RGB24:
1107 *f_even++ = LIMIT_RGB(r_);
1108 *f_even++ = LIMIT_RGB(g_);
1109 *f_even++ = LIMIT_RGB(b_);
1110 break;
1111 case V4L2_PIX_FMT_RGB32:
1112 *f_even++ = LIMIT_RGB(r_);
1113 *f_even++ = LIMIT_RGB(g_);
1114 *f_even++ = LIMIT_RGB(b_);
1115 f_even++;
1116 break;
1117 case V4L2_PIX_FMT_RGB555:
1118 g = LIMIT_RGB(g_);
1119 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1120 (0xE0 & (g << 5));
1121 *f_even++ = (0x03 & (g >> 3)) |
1122 (0x7C & (LIMIT_RGB(b_) << 2));
1123 break;
781aa1d1
MCC
1124 }
1125 }
1126 clipmask_even_index += clipmask_add;
1127 f_even += stretch_bytes;
1128
483dfdb6 1129 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
1130 *f_even++ = y[1];
1131 *f_even++ = u;
1132 }
1133 else {
1134 y_ = 76284 * (y[1] - 16);
1135
1136 b_ = (y_ + vb) >> 16;
1137 g_ = (y_ + uvg)>> 16;
1138 r_ = (y_ + ur) >> 16;
1139
1140 switch (frame->v4l2_format.format) {
6200bbaa
TM
1141 case V4L2_PIX_FMT_RGB565:
1142 g = LIMIT_RGB(g_);
1143 *f_even++ =
1144 (0x1F & LIMIT_RGB(r_)) |
1145 (0xE0 & (g << 5));
1146 *f_even++ =
1147 (0x07 & (g >> 3)) |
1148 (0xF8 & LIMIT_RGB(b_));
1149 break;
1150 case V4L2_PIX_FMT_RGB24:
1151 *f_even++ = LIMIT_RGB(r_);
1152 *f_even++ = LIMIT_RGB(g_);
1153 *f_even++ = LIMIT_RGB(b_);
1154 break;
1155 case V4L2_PIX_FMT_RGB32:
1156 *f_even++ = LIMIT_RGB(r_);
1157 *f_even++ = LIMIT_RGB(g_);
1158 *f_even++ = LIMIT_RGB(b_);
1159 f_even++;
1160 break;
1161 case V4L2_PIX_FMT_RGB555:
1162 g = LIMIT_RGB(g_);
1163 *f_even++ = (0x1F & LIMIT_RGB(r_)) |
1164 (0xE0 & (g << 5));
1165 *f_even++ = (0x03 & (g >> 3)) |
1166 (0x7C & (LIMIT_RGB(b_) << 2));
1167 break;
781aa1d1
MCC
1168 }
1169 }
1170 clipmask_even_index += clipmask_add;
1171 f_even += stretch_bytes;
1172
1173 scratch_get_extra(usbvision, &y[0], &y_ptr, 2);
1174
483dfdb6 1175 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
1176 *f_odd++ = y[0];
1177 *f_odd++ = v;
1178 }
1179 else {
1180 y_ = 76284 * (y[0] - 16);
1181
1182 b_ = (y_ + vb) >> 16;
1183 g_ = (y_ + uvg)>> 16;
1184 r_ = (y_ + ur) >> 16;
1185
1186 switch (frame->v4l2_format.format) {
6200bbaa
TM
1187 case V4L2_PIX_FMT_RGB565:
1188 g = LIMIT_RGB(g_);
1189 *f_odd++ =
1190 (0x1F & LIMIT_RGB(r_)) |
1191 (0xE0 & (g << 5));
1192 *f_odd++ =
1193 (0x07 & (g >> 3)) |
1194 (0xF8 & LIMIT_RGB(b_));
1195 break;
1196 case V4L2_PIX_FMT_RGB24:
1197 *f_odd++ = LIMIT_RGB(r_);
1198 *f_odd++ = LIMIT_RGB(g_);
1199 *f_odd++ = LIMIT_RGB(b_);
1200 break;
1201 case V4L2_PIX_FMT_RGB32:
1202 *f_odd++ = LIMIT_RGB(r_);
1203 *f_odd++ = LIMIT_RGB(g_);
1204 *f_odd++ = LIMIT_RGB(b_);
1205 f_odd++;
1206 break;
1207 case V4L2_PIX_FMT_RGB555:
1208 g = LIMIT_RGB(g_);
1209 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1210 (0xE0 & (g << 5));
1211 *f_odd++ = (0x03 & (g >> 3)) |
1212 (0x7C & (LIMIT_RGB(b_) << 2));
1213 break;
781aa1d1
MCC
1214 }
1215 }
1216 clipmask_odd_index += clipmask_add;
1217 f_odd += stretch_bytes;
1218
483dfdb6 1219 if(frame->v4l2_format.format == V4L2_PIX_FMT_YUYV) {
781aa1d1
MCC
1220 *f_odd++ = y[1];
1221 *f_odd++ = u;
1222 }
1223 else {
1224 y_ = 76284 * (y[1] - 16);
1225
1226 b_ = (y_ + vb) >> 16;
1227 g_ = (y_ + uvg)>> 16;
1228 r_ = (y_ + ur) >> 16;
1229
1230 switch (frame->v4l2_format.format) {
6200bbaa
TM
1231 case V4L2_PIX_FMT_RGB565:
1232 g = LIMIT_RGB(g_);
1233 *f_odd++ =
1234 (0x1F & LIMIT_RGB(r_)) |
1235 (0xE0 & (g << 5));
1236 *f_odd++ =
1237 (0x07 & (g >> 3)) |
1238 (0xF8 & LIMIT_RGB(b_));
1239 break;
1240 case V4L2_PIX_FMT_RGB24:
1241 *f_odd++ = LIMIT_RGB(r_);
1242 *f_odd++ = LIMIT_RGB(g_);
1243 *f_odd++ = LIMIT_RGB(b_);
1244 break;
1245 case V4L2_PIX_FMT_RGB32:
1246 *f_odd++ = LIMIT_RGB(r_);
1247 *f_odd++ = LIMIT_RGB(g_);
1248 *f_odd++ = LIMIT_RGB(b_);
1249 f_odd++;
1250 break;
1251 case V4L2_PIX_FMT_RGB555:
1252 g = LIMIT_RGB(g_);
1253 *f_odd++ = (0x1F & LIMIT_RGB(r_)) |
1254 (0xE0 & (g << 5));
1255 *f_odd++ = (0x03 & (g >> 3)) |
1256 (0x7C & (LIMIT_RGB(b_) << 2));
1257 break;
781aa1d1
MCC
1258 }
1259 }
1260 clipmask_odd_index += clipmask_add;
1261 f_odd += stretch_bytes;
1262 }
1263
1264 scratch_rm_old(usbvision,y_step[block % y_step_size] * sub_block_size);
1265 scratch_inc_extra_ptr(&y_ptr, y_step[(block + 2 * block_split) % y_step_size]
1266 * sub_block_size);
1267 scratch_inc_extra_ptr(&u_ptr, uv_step[block % uv_step_size]
1268 * sub_block_size);
1269 scratch_inc_extra_ptr(&v_ptr, uv_step[(block + 2 * block_split) % uv_step_size]
1270 * sub_block_size);
1271 }
1272
1273 scratch_rm_old(usbvision, pixel_per_line * 3 / 2
1274 + block_split * sub_block_size);
1275
1276 frame->curline += 2 * usbvision->stretch_height;
1277 *pcopylen += frame->v4l2_linesize * 2 * usbvision->stretch_height;
1278
1279 if (frame->curline >= frame->frmheight)
1280 return ParseState_NextFrame;
1281 else
1282 return ParseState_Continue;
1283}
1284
1285/*
1286 * usbvision_parse_data()
1287 *
1288 * Generic routine to parse the scratch buffer. It employs either
1289 * usbvision_find_header() or usbvision_parse_lines() to do most
1290 * of work.
1291 *
1292 */
1293static void usbvision_parse_data(struct usb_usbvision *usbvision)
1294{
1295 struct usbvision_frame *frame;
1296 enum ParseState newstate;
1297 long copylen = 0;
f2242ee5 1298 unsigned long lock_flags;
781aa1d1 1299
483dfdb6 1300 frame = usbvision->curFrame;
781aa1d1
MCC
1301
1302 PDEBUG(DBG_PARSE, "parsing len=%d\n", scratch_len(usbvision));
1303
781aa1d1
MCC
1304 while (1) {
1305
1306 newstate = ParseState_Out;
1307 if (scratch_len(usbvision)) {
1308 if (frame->scanstate == ScanState_Scanning) {
1309 newstate = usbvision_find_header(usbvision);
1310 }
1311 else if (frame->scanstate == ScanState_Lines) {
1312 if (usbvision->isocMode == ISOC_MODE_YUV420) {
1313 newstate = usbvision_parse_lines_420(usbvision, &copylen);
1314 }
1315 else if (usbvision->isocMode == ISOC_MODE_YUV422) {
1316 newstate = usbvision_parse_lines_422(usbvision, &copylen);
1317 }
1318 else if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
1319 newstate = usbvision_parse_compress(usbvision, &copylen);
1320 }
1321
1322 }
1323 }
1324 if (newstate == ParseState_Continue) {
1325 continue;
1326 }
1327 else if ((newstate == ParseState_NextFrame) || (newstate == ParseState_Out)) {
1328 break;
1329 }
1330 else {
1331 return; /* ParseState_EndParse */
1332 }
1333 }
1334
1335 if (newstate == ParseState_NextFrame) {
1336 frame->grabstate = FrameState_Done;
1337 do_gettimeofday(&(frame->timestamp));
1338 frame->sequence = usbvision->frame_num;
781aa1d1 1339
483dfdb6
TM
1340 spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
1341 list_move_tail(&(frame->frame), &usbvision->outqueue);
1342 usbvision->curFrame = NULL;
1343 spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
1344
1345 usbvision->frame_num++;
781aa1d1
MCC
1346
1347 /* This will cause the process to request another frame. */
f2242ee5
TM
1348 if (waitqueue_active(&usbvision->wait_frame)) {
1349 PDEBUG(DBG_PARSE, "Wake up !");
1350 wake_up_interruptible(&usbvision->wait_frame);
781aa1d1
MCC
1351 }
1352 }
f2242ee5
TM
1353 else
1354 frame->grabstate = FrameState_Grabbing;
1355
781aa1d1
MCC
1356
1357 /* Update the frame's uncompressed length. */
1358 frame->scanlength += copylen;
1359}
1360
1361
1362/*
1363 * Make all of the blocks of data contiguous
1364 */
1365static int usbvision_compress_isochronous(struct usb_usbvision *usbvision,
f2242ee5 1366 struct urb *urb)
781aa1d1
MCC
1367{
1368 unsigned char *packet_data;
1369 int i, totlen = 0;
1370
1371 for (i = 0; i < urb->number_of_packets; i++) {
1372 int packet_len = urb->iso_frame_desc[i].actual_length;
1373 int packet_stat = urb->iso_frame_desc[i].status;
1374
1375 packet_data = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
1376
1377 /* Detect and ignore errored packets */
1378 if (packet_stat) { // packet_stat != 0 ?????????????
1379 PDEBUG(DBG_ISOC, "data error: [%d] len=%d, status=%X", i, packet_len, packet_stat);
1380 usbvision->isocErrCount++;
1381 continue;
1382 }
1383
1384 /* Detect and ignore empty packets */
1385 if (packet_len < 0) {
1386 PDEBUG(DBG_ISOC, "error packet [%d]", i);
1387 usbvision->isocSkipCount++;
1388 continue;
1389 }
1390 else if (packet_len == 0) { /* Frame end ????? */
1391 PDEBUG(DBG_ISOC, "null packet [%d]", i);
1392 usbvision->isocstate=IsocState_NoFrame;
1393 usbvision->isocSkipCount++;
1394 continue;
1395 }
1396 else if (packet_len > usbvision->isocPacketSize) {
1397 PDEBUG(DBG_ISOC, "packet[%d] > isocPacketSize", i);
1398 usbvision->isocSkipCount++;
1399 continue;
1400 }
1401
1402 PDEBUG(DBG_ISOC, "packet ok [%d] len=%d", i, packet_len);
1403
1404 if (usbvision->isocstate==IsocState_NoFrame) { //new frame begins
1405 usbvision->isocstate=IsocState_InFrame;
1406 scratch_mark_header(usbvision);
1407 usbvision_measure_bandwidth(usbvision);
1408 PDEBUG(DBG_ISOC, "packet with header");
1409 }
1410
1411 /*
1412 * If usbvision continues to feed us with data but there is no
1413 * consumption (if, for example, V4L client fell asleep) we
1414 * may overflow the buffer. We have to move old data over to
1415 * free room for new data. This is bad for old data. If we
1416 * just drop new data then it's bad for new data... choose
1417 * your favorite evil here.
1418 */
1419 if (scratch_free(usbvision) < packet_len) {
1420
1421 usbvision->scratch_ovf_count++;
1422 PDEBUG(DBG_ISOC, "scratch buf overflow! scr_len: %d, n: %d",
1423 scratch_len(usbvision), packet_len);
1424 scratch_rm_old(usbvision, packet_len - scratch_free(usbvision));
1425 }
1426
1427 /* Now we know that there is enough room in scratch buffer */
1428 scratch_put(usbvision, packet_data, packet_len);
1429 totlen += packet_len;
1430 usbvision->isocDataCount += packet_len;
1431 usbvision->isocPacketCount++;
1432 }
1433#if ENABLE_HEXDUMP
1434 if (totlen > 0) {
ff699e6b 1435 static int foo;
781aa1d1
MCC
1436 if (foo < 1) {
1437 printk(KERN_DEBUG "+%d.\n", usbvision->scratchlen);
1438 usbvision_hexdump(data0, (totlen > 64) ? 64 : totlen);
1439 ++foo;
1440 }
1441 }
1442#endif
1443 return totlen;
1444}
1445
a1ed551c 1446static void usbvision_isocIrq(struct urb *urb)
781aa1d1 1447{
f2242ee5
TM
1448 int errCode = 0;
1449 int len;
1450 struct usb_usbvision *usbvision = urb->context;
1451 int i;
1452 unsigned long startTime = jiffies;
1453 struct usbvision_frame **f;
781aa1d1 1454
f2242ee5
TM
1455 /* We don't want to do anything if we are about to be removed! */
1456 if (!USBVISION_IS_OPERATIONAL(usbvision))
1457 return;
781aa1d1 1458
fe818d1d
TM
1459 /* any urb with wrong status is ignored without acknowledgement */
1460 if (urb->status == -ENOENT) {
1461 return;
1462 }
1463
f2242ee5 1464 f = &usbvision->curFrame;
781aa1d1 1465
f2242ee5
TM
1466 /* Manage streaming interruption */
1467 if (usbvision->streaming == Stream_Interrupt) {
5f7fb877 1468 usbvision->streaming = Stream_Idle;
f2242ee5
TM
1469 if ((*f)) {
1470 (*f)->grabstate = FrameState_Ready;
1471 (*f)->scanstate = ScanState_Scanning;
1472 }
1473 PDEBUG(DBG_IRQ, "stream interrupted");
1474 wake_up_interruptible(&usbvision->wait_stream);
1475 }
781aa1d1 1476
f2242ee5
TM
1477 /* Copy the data received into our scratch buffer */
1478 len = usbvision_compress_isochronous(usbvision, urb);
781aa1d1 1479
f2242ee5
TM
1480 usbvision->isocUrbCount++;
1481 usbvision->urb_length = len;
781aa1d1 1482
f2242ee5
TM
1483 if (usbvision->streaming == Stream_On) {
1484
1485 /* If we collected enough data let's parse! */
fe818d1d
TM
1486 if ((scratch_len(usbvision) > USBVISION_HEADER_LENGTH) &&
1487 (!list_empty(&(usbvision->inqueue))) ) {
1488 if (!(*f)) {
1489 (*f) = list_entry(usbvision->inqueue.next,
1490 struct usbvision_frame,
1491 frame);
f2242ee5 1492 }
fe818d1d
TM
1493 usbvision_parse_data(usbvision);
1494 }
1495 else {
1496 /*If we don't have a frame
1497 we're current working on, complain */
1498 PDEBUG(DBG_IRQ,
1499 "received data, but no one needs it");
1500 scratch_reset(usbvision);
781aa1d1
MCC
1501 }
1502 }
40bad678
TM
1503 else {
1504 PDEBUG(DBG_IRQ, "received data, but no one needs it");
1505 scratch_reset(usbvision);
1506 }
f2242ee5 1507
781aa1d1 1508 usbvision->timeInIrq += jiffies - startTime;
f2242ee5
TM
1509
1510 for (i = 0; i < USBVISION_URB_FRAMES; i++) {
1511 urb->iso_frame_desc[i].status = 0;
1512 urb->iso_frame_desc[i].actual_length = 0;
1513 }
1514
1515 urb->status = 0;
1516 urb->dev = usbvision->dev;
1517 errCode = usb_submit_urb (urb, GFP_ATOMIC);
1518
fe818d1d
TM
1519 if(errCode) {
1520 err("%s: usb_submit_urb failed: error %d",
1521 __FUNCTION__, errCode);
1522 }
f2242ee5 1523
781aa1d1
MCC
1524 return;
1525}
1526
1527/*************************************/
1528/* Low level usbvision access functions */
1529/*************************************/
1530
1531/*
1532 * usbvision_read_reg()
1533 *
1534 * return < 0 -> Error
1535 * >= 0 -> Data
1536 */
1537
483dfdb6 1538int usbvision_read_reg(struct usb_usbvision *usbvision, unsigned char reg)
781aa1d1
MCC
1539{
1540 int errCode = 0;
1541 unsigned char buffer[1];
1542
1543 if (!USBVISION_IS_OPERATIONAL(usbvision))
1544 return -1;
1545
1546 errCode = usb_control_msg(usbvision->dev, usb_rcvctrlpipe(usbvision->dev, 1),
1547 USBVISION_OP_CODE,
1548 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1549 0, (__u16) reg, buffer, 1, HZ);
1550
1551 if (errCode < 0) {
1552 err("%s: failed: error %d", __FUNCTION__, errCode);
1553 return errCode;
1554 }
1555 return buffer[0];
1556}
1557
1558/*
1559 * usbvision_write_reg()
1560 *
1561 * return 1 -> Reg written
1562 * 0 -> usbvision is not yet ready
1563 * -1 -> Something went wrong
1564 */
1565
483dfdb6 1566int usbvision_write_reg(struct usb_usbvision *usbvision, unsigned char reg,
781aa1d1
MCC
1567 unsigned char value)
1568{
1569 int errCode = 0;
1570
1571 if (!USBVISION_IS_OPERATIONAL(usbvision))
1572 return 0;
1573
1574 errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1575 USBVISION_OP_CODE,
1576 USB_DIR_OUT | USB_TYPE_VENDOR |
1577 USB_RECIP_ENDPOINT, 0, (__u16) reg, &value, 1, HZ);
1578
1579 if (errCode < 0) {
1580 err("%s: failed: error %d", __FUNCTION__, errCode);
1581 }
1582 return errCode;
1583}
1584
1585
a1ed551c 1586static void usbvision_ctrlUrb_complete(struct urb *urb)
781aa1d1
MCC
1587{
1588 struct usb_usbvision *usbvision = (struct usb_usbvision *)urb->context;
1589
1590 PDEBUG(DBG_IRQ, "");
1591 usbvision->ctrlUrbBusy = 0;
1592 if (waitqueue_active(&usbvision->ctrlUrb_wq)) {
1593 wake_up_interruptible(&usbvision->ctrlUrb_wq);
1594 }
1595}
1596
1597
1598static int usbvision_write_reg_irq(struct usb_usbvision *usbvision,int address,
1599 unsigned char *data, int len)
1600{
1601 int errCode = 0;
1602
1603 PDEBUG(DBG_IRQ, "");
1604 if (len > 8) {
1605 return -EFAULT;
1606 }
781aa1d1 1607 if (usbvision->ctrlUrbBusy) {
781aa1d1
MCC
1608 return -EBUSY;
1609 }
1610 usbvision->ctrlUrbBusy = 1;
781aa1d1
MCC
1611
1612 usbvision->ctrlUrbSetup.bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
1613 usbvision->ctrlUrbSetup.bRequest = USBVISION_OP_CODE;
1614 usbvision->ctrlUrbSetup.wValue = 0;
1615 usbvision->ctrlUrbSetup.wIndex = cpu_to_le16(address);
1616 usbvision->ctrlUrbSetup.wLength = cpu_to_le16(len);
1617 usb_fill_control_urb (usbvision->ctrlUrb, usbvision->dev,
1618 usb_sndctrlpipe(usbvision->dev, 1),
1619 (unsigned char *)&usbvision->ctrlUrbSetup,
1620 (void *)usbvision->ctrlUrbBuffer, len,
1621 usbvision_ctrlUrb_complete,
1622 (void *)usbvision);
1623
1624 memcpy(usbvision->ctrlUrbBuffer, data, len);
1625
1626 errCode = usb_submit_urb(usbvision->ctrlUrb, GFP_ATOMIC);
1627 if (errCode < 0) {
1628 // error in usb_submit_urb()
1629 usbvision->ctrlUrbBusy = 0;
1630 }
1631 PDEBUG(DBG_IRQ, "submit %d byte: error %d", len, errCode);
1632 return errCode;
1633}
1634
1635
781aa1d1
MCC
1636static int usbvision_init_compression(struct usb_usbvision *usbvision)
1637{
1638 int errCode = 0;
1639
1640 usbvision->lastIsocFrameNum = -1;
1641 usbvision->isocDataCount = 0;
1642 usbvision->isocPacketCount = 0;
1643 usbvision->isocSkipCount = 0;
1644 usbvision->comprLevel = 50;
1645 usbvision->lastComprLevel = -1;
1646 usbvision->isocUrbCount = 0;
1647 usbvision->requestIntra = 1;
1648 usbvision->isocMeasureBandwidthCount = 0;
1649
1650 return errCode;
1651}
1652
1653/* this function measures the used bandwidth since last call
1654 * return: 0 : no error
1655 * sets usedBandwidth to 1-100 : 1-100% of full bandwidth resp. to isocPacketSize
1656 */
1657static int usbvision_measure_bandwidth (struct usb_usbvision *usbvision)
1658{
1659 int errCode = 0;
1660
1661 if (usbvision->isocMeasureBandwidthCount < 2) { // this gives an average bandwidth of 3 frames
1662 usbvision->isocMeasureBandwidthCount++;
1663 return errCode;
1664 }
1665 if ((usbvision->isocPacketSize > 0) && (usbvision->isocPacketCount > 0)) {
1666 usbvision->usedBandwidth = usbvision->isocDataCount /
1667 (usbvision->isocPacketCount + usbvision->isocSkipCount) *
1668 100 / usbvision->isocPacketSize;
1669 }
1670 usbvision->isocMeasureBandwidthCount = 0;
1671 usbvision->isocDataCount = 0;
1672 usbvision->isocPacketCount = 0;
1673 usbvision->isocSkipCount = 0;
1674 return errCode;
1675}
1676
1677static int usbvision_adjust_compression (struct usb_usbvision *usbvision)
1678{
1679 int errCode = 0;
1680 unsigned char buffer[6];
1681
1682 PDEBUG(DBG_IRQ, "");
1683 if ((adjustCompression) && (usbvision->usedBandwidth > 0)) {
1684 usbvision->comprLevel += (usbvision->usedBandwidth - 90) / 2;
1685 RESTRICT_TO_RANGE(usbvision->comprLevel, 0, 100);
1686 if (usbvision->comprLevel != usbvision->lastComprLevel) {
1687 int distorsion;
1688 if (usbvision->bridgeType == BRIDGE_NT1004 || usbvision->bridgeType == BRIDGE_NT1005) {
1689 buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM Threshold 1
1690 buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM Threshold 2
1691 distorsion = 7 + 248 * usbvision->comprLevel / 100;
1692 buffer[2] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (inter)
1693 buffer[3] = (unsigned char)(distorsion & 0xFF); // Average distorsion Threshold (intra)
1694 distorsion = 1 + 42 * usbvision->comprLevel / 100;
1695 buffer[4] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (inter)
1696 buffer[5] = (unsigned char)(distorsion & 0xFF); // Maximum distorsion Threshold (intra)
1697 }
1698 else { //BRIDGE_NT1003
1699 buffer[0] = (unsigned char)(4 + 16 * usbvision->comprLevel / 100); // PCM threshold 1
1700 buffer[1] = (unsigned char)(4 + 8 * usbvision->comprLevel / 100); // PCM threshold 2
1701 distorsion = 2 + 253 * usbvision->comprLevel / 100;
1702 buffer[2] = (unsigned char)(distorsion & 0xFF); // distorsion threshold bit0-7
1703 buffer[3] = 0; //(unsigned char)((distorsion >> 8) & 0x0F); // distorsion threshold bit 8-11
1704 distorsion = 0 + 43 * usbvision->comprLevel / 100;
1705 buffer[4] = (unsigned char)(distorsion & 0xFF); // maximum distorsion bit0-7
1706 buffer[5] = 0; //(unsigned char)((distorsion >> 8) & 0x01); // maximum distorsion bit 8
1707 }
1708 errCode = usbvision_write_reg_irq(usbvision, USBVISION_PCM_THR1, buffer, 6);
1709 if (errCode == 0){
1710 PDEBUG(DBG_IRQ, "new compr params %#02x %#02x %#02x %#02x %#02x %#02x", buffer[0],
1711 buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
1712 usbvision->lastComprLevel = usbvision->comprLevel;
1713 }
1714 }
1715 }
1716 return errCode;
1717}
1718
1719static int usbvision_request_intra (struct usb_usbvision *usbvision)
1720{
1721 int errCode = 0;
1722 unsigned char buffer[1];
1723
1724 PDEBUG(DBG_IRQ, "");
1725 usbvision->requestIntra = 1;
1726 buffer[0] = 1;
1727 usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
1728 return errCode;
1729}
1730
1731static int usbvision_unrequest_intra (struct usb_usbvision *usbvision)
1732{
1733 int errCode = 0;
1734 unsigned char buffer[1];
1735
1736 PDEBUG(DBG_IRQ, "");
1737 usbvision->requestIntra = 0;
1738 buffer[0] = 0;
1739 usbvision_write_reg_irq(usbvision, USBVISION_FORCE_INTRA, buffer, 1);
1740 return errCode;
1741}
1742
483dfdb6
TM
1743/*******************************
1744 * usbvision utility functions
1745 *******************************/
781aa1d1 1746
483dfdb6 1747int usbvision_power_off(struct usb_usbvision *usbvision)
781aa1d1
MCC
1748{
1749 int errCode = 0;
1750
1751 PDEBUG(DBG_FUNC, "");
1752
1753 errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
1754 if (errCode == 1) {
1755 usbvision->power = 0;
1756 }
1757 PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode!=1)?"ERROR":"power is off", errCode);
1758 return errCode;
1759}
1760
781aa1d1
MCC
1761/*
1762 * usbvision_set_video_format()
1763 *
1764 */
1765static int usbvision_set_video_format(struct usb_usbvision *usbvision, int format)
1766{
1767 static const char proc[] = "usbvision_set_video_format";
1768 int rc;
1769 unsigned char value[2];
1770
1771 if (!USBVISION_IS_OPERATIONAL(usbvision))
1772 return 0;
1773
1774 PDEBUG(DBG_FUNC, "isocMode %#02x", format);
1775
1776 if ((format != ISOC_MODE_YUV422)
1777 && (format != ISOC_MODE_YUV420)
1778 && (format != ISOC_MODE_COMPRESS)) {
1779 printk(KERN_ERR "usbvision: unknown video format %02x, using default YUV420",
1780 format);
1781 format = ISOC_MODE_YUV420;
1782 }
1783 value[0] = 0x0A; //TODO: See the effect of the filter
8c7189d1 1784 value[1] = format; // Sets the VO_MODE register which follows FILT_CONT
781aa1d1
MCC
1785 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1786 USBVISION_OP_CODE,
1787 USB_DIR_OUT | USB_TYPE_VENDOR |
1788 USB_RECIP_ENDPOINT, 0,
1789 (__u16) USBVISION_FILT_CONT, value, 2, HZ);
1790
1791 if (rc < 0) {
1792 printk(KERN_ERR "%s: ERROR=%d. USBVISION stopped - "
1793 "reconnect or reload driver.\n", proc, rc);
1794 }
1795 usbvision->isocMode = format;
1796 return rc;
1797}
1798
1799/*
1800 * usbvision_set_output()
1801 *
1802 */
1803
483dfdb6
TM
1804int usbvision_set_output(struct usb_usbvision *usbvision, int width,
1805 int height)
781aa1d1
MCC
1806{
1807 int errCode = 0;
1808 int UsbWidth, UsbHeight;
1809 unsigned int frameRate=0, frameDrop=0;
1810 unsigned char value[4];
1811
1812 if (!USBVISION_IS_OPERATIONAL(usbvision)) {
1813 return 0;
1814 }
1815
1816 if (width > MAX_USB_WIDTH) {
1817 UsbWidth = width / 2;
1818 usbvision->stretch_width = 2;
1819 }
1820 else {
1821 UsbWidth = width;
1822 usbvision->stretch_width = 1;
1823 }
1824
1825 if (height > MAX_USB_HEIGHT) {
1826 UsbHeight = height / 2;
1827 usbvision->stretch_height = 2;
1828 }
1829 else {
1830 UsbHeight = height;
1831 usbvision->stretch_height = 1;
1832 }
1833
1834 RESTRICT_TO_RANGE(UsbWidth, MIN_FRAME_WIDTH, MAX_USB_WIDTH);
1835 UsbWidth &= ~(MIN_FRAME_WIDTH-1);
1836 RESTRICT_TO_RANGE(UsbHeight, MIN_FRAME_HEIGHT, MAX_USB_HEIGHT);
1837 UsbHeight &= ~(1);
1838
1839 PDEBUG(DBG_FUNC, "usb %dx%d; screen %dx%d; stretch %dx%d",
1840 UsbWidth, UsbHeight, width, height,
1841 usbvision->stretch_width, usbvision->stretch_height);
1842
1843 /* I'll not rewrite the same values */
1844 if ((UsbWidth != usbvision->curwidth) || (UsbHeight != usbvision->curheight)) {
1845 value[0] = UsbWidth & 0xff; //LSB
1846 value[1] = (UsbWidth >> 8) & 0x03; //MSB
1847 value[2] = UsbHeight & 0xff; //LSB
1848 value[3] = (UsbHeight >> 8) & 0x03; //MSB
1849
1850 errCode = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
1851 USBVISION_OP_CODE,
1852 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT,
1853 0, (__u16) USBVISION_LXSIZE_O, value, 4, HZ);
1854
1855 if (errCode < 0) {
1856 err("%s failed: error %d", __FUNCTION__, errCode);
1857 return errCode;
1858 }
1859 usbvision->curwidth = usbvision->stretch_width * UsbWidth;
1860 usbvision->curheight = usbvision->stretch_height * UsbHeight;
1861 }
1862
1863 if (usbvision->isocMode == ISOC_MODE_YUV422) {
1864 frameRate = (usbvision->isocPacketSize * 1000) / (UsbWidth * UsbHeight * 2);
1865 }
1866 else if (usbvision->isocMode == ISOC_MODE_YUV420) {
1867 frameRate = (usbvision->isocPacketSize * 1000) / ((UsbWidth * UsbHeight * 12) / 8);
1868 }
1869 else {
1870 frameRate = FRAMERATE_MAX;
1871 }
1872
c5f48367 1873 if (usbvision->tvnormId & V4L2_STD_625_50) {
18d8a454 1874 frameDrop = frameRate * 32 / 25 - 1;
781aa1d1 1875 }
c5f48367 1876 else if (usbvision->tvnormId & V4L2_STD_525_60) {
781aa1d1
MCC
1877 frameDrop = frameRate * 32 / 30 - 1;
1878 }
1879
1880 RESTRICT_TO_RANGE(frameDrop, FRAMERATE_MIN, FRAMERATE_MAX);
1881
1882 PDEBUG(DBG_FUNC, "frameRate %d fps, frameDrop %d", frameRate, frameDrop);
1883
1884 frameDrop = FRAMERATE_MAX; // We can allow the maximum here, because dropping is controlled
1885
1886 /* frameDrop = 7; => framePhase = 1, 5, 9, 13, 17, 21, 25, 0, 4, 8, ...
1887 => frameSkip = 4;
1888 => frameRate = (7 + 1) * 25 / 32 = 200 / 32 = 6.25;
1889
1890 frameDrop = 9; => framePhase = 1, 5, 8, 11, 14, 17, 21, 24, 27, 1, 4, 8, ...
1891 => frameSkip = 4, 3, 3, 3, 3, 4, 3, 3, 3, 3, 4, ...
1892 => frameRate = (9 + 1) * 25 / 32 = 250 / 32 = 7.8125;
1893 */
1894 errCode = usbvision_write_reg(usbvision, USBVISION_FRM_RATE, frameDrop);
1895 return errCode;
1896}
1897
1898
483dfdb6
TM
1899/*
1900 * usbvision_frames_alloc
6f78e186 1901 * allocate the required frames
483dfdb6 1902 */
6f78e186 1903int usbvision_frames_alloc(struct usb_usbvision *usbvision, int number_of_frames)
483dfdb6
TM
1904{
1905 int i;
1906
6f78e186
TM
1907 /*needs to be page aligned cause the buffers can be mapped individually! */
1908 usbvision->max_frame_size = PAGE_ALIGN(usbvision->curwidth *
1909 usbvision->curheight *
1910 usbvision->palette.bytes_per_pixel);
483dfdb6 1911
6f78e186
TM
1912 /* Try to do my best to allocate the frames the user want in the remaining memory */
1913 usbvision->num_frames = number_of_frames;
1914 while (usbvision->num_frames > 0) {
1915 usbvision->fbuf_size = usbvision->num_frames * usbvision->max_frame_size;
1916 if((usbvision->fbuf = usbvision_rvmalloc(usbvision->fbuf_size))) {
1917 break;
1918 }
1919 usbvision->num_frames--;
483dfdb6 1920 }
6f78e186 1921
483dfdb6
TM
1922 spin_lock_init(&usbvision->queue_lock);
1923 init_waitqueue_head(&usbvision->wait_frame);
1924 init_waitqueue_head(&usbvision->wait_stream);
1925
1926 /* Allocate all buffers */
6f78e186 1927 for (i = 0; i < usbvision->num_frames; i++) {
483dfdb6
TM
1928 usbvision->frame[i].index = i;
1929 usbvision->frame[i].grabstate = FrameState_Unused;
1930 usbvision->frame[i].data = usbvision->fbuf +
1931 i * usbvision->max_frame_size;
1932 /*
1933 * Set default sizes for read operation.
1934 */
1935 usbvision->stretch_width = 1;
1936 usbvision->stretch_height = 1;
1937 usbvision->frame[i].width = usbvision->curwidth;
1938 usbvision->frame[i].height = usbvision->curheight;
1939 usbvision->frame[i].bytes_read = 0;
1940 }
6f78e186
TM
1941 PDEBUG(DBG_FUNC, "allocated %d frames (%d bytes per frame)",usbvision->num_frames,usbvision->max_frame_size);
1942 return usbvision->num_frames;
483dfdb6
TM
1943}
1944
1945/*
1946 * usbvision_frames_free
1947 * frees memory allocated for the frames
1948 */
1949void usbvision_frames_free(struct usb_usbvision *usbvision)
1950{
1951 /* Have to free all that memory */
6f78e186
TM
1952 PDEBUG(DBG_FUNC, "free %d frames",usbvision->num_frames);
1953
483dfdb6
TM
1954 if (usbvision->fbuf != NULL) {
1955 usbvision_rvfree(usbvision->fbuf, usbvision->fbuf_size);
1956 usbvision->fbuf = NULL;
6f78e186
TM
1957
1958 usbvision->num_frames = 0;
483dfdb6
TM
1959 }
1960}
f2242ee5
TM
1961/*
1962 * usbvision_empty_framequeues()
1963 * prepare queues for incoming and outgoing frames
1964 */
483dfdb6 1965void usbvision_empty_framequeues(struct usb_usbvision *usbvision)
f2242ee5
TM
1966{
1967 u32 i;
1968
1969 INIT_LIST_HEAD(&(usbvision->inqueue));
1970 INIT_LIST_HEAD(&(usbvision->outqueue));
1971
1972 for (i = 0; i < USBVISION_NUMFRAMES; i++) {
1973 usbvision->frame[i].grabstate = FrameState_Unused;
1974 usbvision->frame[i].bytes_read = 0;
1975 }
1976}
1977
1978/*
1979 * usbvision_stream_interrupt()
1980 * stops streaming
1981 */
483dfdb6 1982int usbvision_stream_interrupt(struct usb_usbvision *usbvision)
f2242ee5
TM
1983{
1984 int ret = 0;
1985
1986 /* stop reading from the device */
1987
1988 usbvision->streaming = Stream_Interrupt;
1989 ret = wait_event_timeout(usbvision->wait_stream,
5f7fb877 1990 (usbvision->streaming == Stream_Idle),
f2242ee5
TM
1991 msecs_to_jiffies(USBVISION_NUMSBUF*USBVISION_URB_FRAMES));
1992 return ret;
1993}
1994
781aa1d1
MCC
1995/*
1996 * usbvision_set_compress_params()
1997 *
1998 */
1999
2000static int usbvision_set_compress_params(struct usb_usbvision *usbvision)
2001{
2002 static const char proc[] = "usbvision_set_compresion_params: ";
2003 int rc;
2004 unsigned char value[6];
2005
2006 value[0] = 0x0F; // Intra-Compression cycle
2007 value[1] = 0x01; // Reg.45 one line per strip
2008 value[2] = 0x00; // Reg.46 Force intra mode on all new frames
2009 value[3] = 0x00; // Reg.47 FORCE_UP <- 0 normal operation (not force)
2010 value[4] = 0xA2; // Reg.48 BUF_THR I'm not sure if this does something in not compressed mode.
18d8a454 2011 value[5] = 0x00; // Reg.49 DVI_YUV This has nothing to do with compression
781aa1d1
MCC
2012
2013 //catched values for NT1004
2014 // value[0] = 0xFF; // Never apply intra mode automatically
2015 // value[1] = 0xF1; // Use full frame height for virtual strip width; One line per strip
2016 // value[2] = 0x01; // Force intra mode on all new frames
2017 // value[3] = 0x00; // Strip size 400 Bytes; do not force up
2018 // value[4] = 0xA2; //
2019 if (!USBVISION_IS_OPERATIONAL(usbvision))
2020 return 0;
2021
2022 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2023 USBVISION_OP_CODE,
2024 USB_DIR_OUT | USB_TYPE_VENDOR |
2025 USB_RECIP_ENDPOINT, 0,
2026 (__u16) USBVISION_INTRA_CYC, value, 5, HZ);
2027
2028 if (rc < 0) {
2029 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2030 "reconnect or reload driver.\n", proc, rc);
2031 return rc;
2032 }
2033
2034 if (usbvision->bridgeType == BRIDGE_NT1004) {
2035 value[0] = 20; // PCM Threshold 1
2036 value[1] = 12; // PCM Threshold 2
2037 value[2] = 255; // Distorsion Threshold inter
2038 value[3] = 255; // Distorsion Threshold intra
2039 value[4] = 43; // Max Distorsion inter
2040 value[5] = 43; // Max Distorsion intra
2041 }
2042 else {
2043 value[0] = 20; // PCM Threshold 1
2044 value[1] = 12; // PCM Threshold 2
2045 value[2] = 255; // Distorsion Threshold d7-d0
2046 value[3] = 0; // Distorsion Threshold d11-d8
2047 value[4] = 43; // Max Distorsion d7-d0
2048 value[5] = 0; // Max Distorsion d8
2049 }
2050
2051 if (!USBVISION_IS_OPERATIONAL(usbvision))
2052 return 0;
2053
2054 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2055 USBVISION_OP_CODE,
2056 USB_DIR_OUT | USB_TYPE_VENDOR |
2057 USB_RECIP_ENDPOINT, 0,
2058 (__u16) USBVISION_PCM_THR1, value, 6, HZ);
2059
2060 if (rc < 0) {
2061 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2062 "reconnect or reload driver.\n", proc, rc);
2063 return rc;
2064 }
2065
2066
2067 return rc;
2068}
2069
2070
2071/*
2072 * usbvision_set_input()
2073 *
2074 * Set the input (saa711x, ...) size x y and other misc input params
2075 * I've no idea if this parameters are right
2076 *
2077 */
483dfdb6 2078int usbvision_set_input(struct usb_usbvision *usbvision)
781aa1d1
MCC
2079{
2080 static const char proc[] = "usbvision_set_input: ";
2081 int rc;
2082 unsigned char value[8];
2083 unsigned char dvi_yuv_value;
2084
2085 if (!USBVISION_IS_OPERATIONAL(usbvision))
2086 return 0;
2087
2088 /* Set input format expected from decoder*/
c682b3a7
TP
2089 if (usbvision_device_data[usbvision->DevModel].Vin_Reg1_override) {
2090 value[0] = usbvision_device_data[usbvision->DevModel].Vin_Reg1;
781aa1d1
MCC
2091 } else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
2092 /* SAA7113 uses 8 bit output */
2093 value[0] = USBVISION_8_422_SYNC;
2094 } else {
2095 /* I'm sure only about d2-d0 [010] 16 bit 4:2:2 usin sync pulses
2096 * as that is how saa7111 is configured */
2097 value[0] = USBVISION_16_422_SYNC;
2098 /* | USBVISION_VSNC_POL | USBVISION_VCLK_POL);*/
2099 }
2100
2101 rc = usbvision_write_reg(usbvision, USBVISION_VIN_REG1, value[0]);
2102 if (rc < 0) {
2103 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2104 "reconnect or reload driver.\n", proc, rc);
2105 return rc;
2106 }
2107
2108
c5f48367 2109 if (usbvision->tvnormId & V4L2_STD_PAL) {
781aa1d1
MCC
2110 value[0] = 0xC0;
2111 value[1] = 0x02; //0x02C0 -> 704 Input video line length
2112 value[2] = 0x20;
2113 value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2114 value[4] = 0x60;
2115 value[5] = 0x00; //0x0060 -> 96 Input video h offset
2116 value[6] = 0x16;
2117 value[7] = 0x00; //0x0016 -> 22 Input video v offset
c5f48367 2118 } else if (usbvision->tvnormId & V4L2_STD_SECAM) {
781aa1d1
MCC
2119 value[0] = 0xC0;
2120 value[1] = 0x02; //0x02C0 -> 704 Input video line length
2121 value[2] = 0x20;
2122 value[3] = 0x01; //0x0120 -> 288 Input video n. of lines
2123 value[4] = 0x01;
2124 value[5] = 0x00; //0x0001 -> 01 Input video h offset
2125 value[6] = 0x01;
2126 value[7] = 0x00; //0x0001 -> 01 Input video v offset
2127 } else { /* V4L2_STD_NTSC */
2128 value[0] = 0xD0;
2129 value[1] = 0x02; //0x02D0 -> 720 Input video line length
2130 value[2] = 0xF0;
2131 value[3] = 0x00; //0x00F0 -> 240 Input video number of lines
2132 value[4] = 0x50;
2133 value[5] = 0x00; //0x0050 -> 80 Input video h offset
2134 value[6] = 0x10;
2135 value[7] = 0x00; //0x0010 -> 16 Input video v offset
2136 }
2137
2138 if (usbvision_device_data[usbvision->DevModel].X_Offset >= 0) {
2139 value[4]=usbvision_device_data[usbvision->DevModel].X_Offset & 0xff;
2140 value[5]=(usbvision_device_data[usbvision->DevModel].X_Offset & 0x0300) >> 8;
2141 }
2142
c6243d9c
TM
2143 if (adjust_X_Offset != -1) {
2144 value[4] = adjust_X_Offset & 0xff;
2145 value[5] = (adjust_X_Offset & 0x0300) >> 8;
2146 }
2147
781aa1d1
MCC
2148 if (usbvision_device_data[usbvision->DevModel].Y_Offset >= 0) {
2149 value[6]=usbvision_device_data[usbvision->DevModel].Y_Offset & 0xff;
2150 value[7]=(usbvision_device_data[usbvision->DevModel].Y_Offset & 0x0300) >> 8;
2151 }
2152
c6243d9c
TM
2153 if (adjust_Y_Offset != -1) {
2154 value[6] = adjust_Y_Offset & 0xff;
2155 value[7] = (adjust_Y_Offset & 0x0300) >> 8;
2156 }
2157
781aa1d1
MCC
2158 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2159 USBVISION_OP_CODE, /* USBVISION specific code */
2160 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT, 0,
2161 (__u16) USBVISION_LXSIZE_I, value, 8, HZ);
2162 if (rc < 0) {
2163 printk(KERN_ERR "%sERROR=%d. USBVISION stopped - "
2164 "reconnect or reload driver.\n", proc, rc);
2165 return rc;
2166 }
2167
2168
2169 dvi_yuv_value = 0x00; /* U comes after V, Ya comes after U/V, Yb comes after Yb */
2170
c682b3a7
TP
2171 if(usbvision_device_data[usbvision->DevModel].Dvi_yuv_override){
2172 dvi_yuv_value = usbvision_device_data[usbvision->DevModel].Dvi_yuv;
781aa1d1
MCC
2173 }
2174 else if(usbvision_device_data[usbvision->DevModel].Codec == CODEC_SAA7113) {
18d8a454 2175 /* This changes as the fine sync control changes. Further investigation necessary */
781aa1d1
MCC
2176 dvi_yuv_value = 0x06;
2177 }
2178
2179 return (usbvision_write_reg(usbvision, USBVISION_DVI_YUV, dvi_yuv_value));
2180}
2181
2182
2183/*
2184 * usbvision_set_dram_settings()
2185 *
2186 * Set the buffer address needed by the usbvision dram to operate
2187 * This values has been taken with usbsnoop.
2188 *
2189 */
2190
2191static int usbvision_set_dram_settings(struct usb_usbvision *usbvision)
2192{
2193 int rc;
2194 unsigned char value[8];
2195
2196 if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
2197 value[0] = 0x42;
2198 value[1] = 0x71;
2199 value[2] = 0xff;
2200 value[3] = 0x00;
2201 value[4] = 0x98;
2202 value[5] = 0xe0;
2203 value[6] = 0x71;
2204 value[7] = 0xff;
2205 // UR: 0x0E200-0x3FFFF = 204288 Words (1 Word = 2 Byte)
2206 // FDL: 0x00000-0x0E099 = 57498 Words
2207 // VDW: 0x0E3FF-0x3FFFF
2208 }
2209 else {
2210 value[0] = 0x42;
2211 value[1] = 0x00;
2212 value[2] = 0xff;
2213 value[3] = 0x00;
2214 value[4] = 0x00;
2215 value[5] = 0x00;
2216 value[6] = 0x00;
2217 value[7] = 0xff;
2218 }
2219 /* These are the values of the address of the video buffer,
2220 * they have to be loaded into the USBVISION_DRM_PRM1-8
2221 *
2222 * Start address of video output buffer for read: drm_prm1-2 -> 0x00000
2223 * End address of video output buffer for read: drm_prm1-3 -> 0x1ffff
2224 * Start address of video frame delay buffer: drm_prm1-4 -> 0x20000
2225 * Only used in compressed mode
2226 * End address of video frame delay buffer: drm_prm1-5-6 -> 0x3ffff
2227 * Only used in compressed mode
2228 * Start address of video output buffer for write: drm_prm1-7 -> 0x00000
2229 * End address of video output buffer for write: drm_prm1-8 -> 0x1ffff
2230 */
2231
2232 if (!USBVISION_IS_OPERATIONAL(usbvision))
2233 return 0;
2234
2235 rc = usb_control_msg(usbvision->dev, usb_sndctrlpipe(usbvision->dev, 1),
2236 USBVISION_OP_CODE, /* USBVISION specific code */
2237 USB_DIR_OUT | USB_TYPE_VENDOR |
2238 USB_RECIP_ENDPOINT, 0,
2239 (__u16) USBVISION_DRM_PRM1, value, 8, HZ);
2240
2241 if (rc < 0) {
2242 err("%sERROR=%d", __FUNCTION__, rc);
2243 return rc;
2244 }
2245
2246 /* Restart the video buffer logic */
2247 if ((rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, USBVISION_RES_UR |
2248 USBVISION_RES_FDL | USBVISION_RES_VDW)) < 0)
2249 return rc;
2250 rc = usbvision_write_reg(usbvision, USBVISION_DRM_CONT, 0x00);
2251
2252 return rc;
2253}
2254
2255/*
2256 * ()
2257 *
2258 * Power on the device, enables suspend-resume logic
2259 * & reset the isoc End-Point
2260 *
2261 */
2262
483dfdb6 2263int usbvision_power_on(struct usb_usbvision *usbvision)
781aa1d1
MCC
2264{
2265 int errCode = 0;
2266
2267 PDEBUG(DBG_FUNC, "");
2268
2269 usbvision_write_reg(usbvision, USBVISION_PWR_REG, USBVISION_SSPND_EN);
2270 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2271 USBVISION_SSPND_EN | USBVISION_RES2);
38284ba3 2272
781aa1d1
MCC
2273 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2274 USBVISION_SSPND_EN | USBVISION_PWR_VID);
2275 errCode = usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2276 USBVISION_SSPND_EN | USBVISION_PWR_VID | USBVISION_RES2);
2277 if (errCode == 1) {
2278 usbvision->power = 1;
2279 }
2280 PDEBUG(DBG_FUNC, "%s: errCode %d", (errCode<0)?"ERROR":"power is on", errCode);
2281 return errCode;
2282}
2283
2284
483dfdb6
TM
2285/*
2286 * usbvision timer stuff
2287 */
2288
2289// to call usbvision_power_off from task queue
67952e8c 2290static void call_usbvision_power_off(struct work_struct *work)
483dfdb6 2291{
1de1bf06 2292 struct usb_usbvision *usbvision = container_of(work, struct usb_usbvision, powerOffWork);
483dfdb6
TM
2293
2294 PDEBUG(DBG_FUNC, "");
289d4d2f
MK
2295 if(mutex_lock_interruptible(&usbvision->lock)) {
2296 return;
2297 }
2298
2299
483dfdb6 2300 if(usbvision->user == 0) {
1ff16c20 2301 usbvision_i2c_unregister(usbvision);
67952e8c 2302
483dfdb6
TM
2303 usbvision_power_off(usbvision);
2304 usbvision->initialized = 0;
2305 }
289d4d2f 2306 mutex_unlock(&usbvision->lock);
483dfdb6
TM
2307}
2308
781aa1d1
MCC
2309static void usbvision_powerOffTimer(unsigned long data)
2310{
2311 struct usb_usbvision *usbvision = (void *) data;
2312
2313 PDEBUG(DBG_FUNC, "");
2314 del_timer(&usbvision->powerOffTimer);
67952e8c 2315 INIT_WORK(&usbvision->powerOffWork, call_usbvision_power_off);
781aa1d1 2316 (void) schedule_work(&usbvision->powerOffWork);
18d8a454 2317
781aa1d1
MCC
2318}
2319
483dfdb6
TM
2320void usbvision_init_powerOffTimer(struct usb_usbvision *usbvision)
2321{
2322 init_timer(&usbvision->powerOffTimer);
2323 usbvision->powerOffTimer.data = (long) usbvision;
2324 usbvision->powerOffTimer.function = usbvision_powerOffTimer;
2325}
2326
2327void usbvision_set_powerOffTimer(struct usb_usbvision *usbvision)
2328{
2329 mod_timer(&usbvision->powerOffTimer, jiffies + USBVISION_POWEROFF_TIME);
2330}
2331
2332void usbvision_reset_powerOffTimer(struct usb_usbvision *usbvision)
2333{
2334 if (timer_pending(&usbvision->powerOffTimer)) {
2335 del_timer(&usbvision->powerOffTimer);
2336 }
2337}
781aa1d1
MCC
2338
2339/*
2340 * usbvision_begin_streaming()
2341 * Sure you have to put bit 7 to 0, if not incoming frames are droped, but no
2342 * idea about the rest
2343 */
483dfdb6 2344int usbvision_begin_streaming(struct usb_usbvision *usbvision)
781aa1d1
MCC
2345{
2346 int errCode = 0;
2347
2348 if (usbvision->isocMode == ISOC_MODE_COMPRESS) {
2349 usbvision_init_compression(usbvision);
2350 }
2351 errCode = usbvision_write_reg(usbvision, USBVISION_VIN_REG2, USBVISION_NOHVALID |
2352 usbvision->Vin_Reg2_Preset);
2353 return errCode;
2354}
2355
2356/*
2357 * usbvision_restart_isoc()
2358 * Not sure yet if touching here PWR_REG make loose the config
2359 */
2360
483dfdb6 2361int usbvision_restart_isoc(struct usb_usbvision *usbvision)
781aa1d1
MCC
2362{
2363 int ret;
2364
2365 if (
2366 (ret =
2367 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2368 USBVISION_SSPND_EN | USBVISION_PWR_VID)) < 0)
2369 return ret;
2370 if (
2371 (ret =
2372 usbvision_write_reg(usbvision, USBVISION_PWR_REG,
2373 USBVISION_SSPND_EN | USBVISION_PWR_VID |
2374 USBVISION_RES2)) < 0)
2375 return ret;
2376 if (
2377 (ret =
2378 usbvision_write_reg(usbvision, USBVISION_VIN_REG2,
2379 USBVISION_KEEP_BLANK | USBVISION_NOHVALID |
2380 usbvision->Vin_Reg2_Preset)) < 0) return ret;
2381
2382 /* TODO: schedule timeout */
70bdd9c8 2383 while ((usbvision_read_reg(usbvision, USBVISION_STATUS_REG) & 0x01) != 1);
781aa1d1
MCC
2384
2385 return 0;
2386}
2387
483dfdb6 2388int usbvision_audio_off(struct usb_usbvision *usbvision)
781aa1d1
MCC
2389{
2390 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, USBVISION_AUDIO_MUTE) < 0) {
2391 printk(KERN_ERR "usbvision_audio_off: can't wirte reg\n");
2392 return -1;
2393 }
781aa1d1
MCC
2394 usbvision->AudioMute = 0;
2395 usbvision->AudioChannel = USBVISION_AUDIO_MUTE;
2396 return 0;
2397}
2398
483dfdb6 2399int usbvision_set_audio(struct usb_usbvision *usbvision, int AudioChannel)
781aa1d1
MCC
2400{
2401 if (!usbvision->AudioMute) {
2402 if (usbvision_write_reg(usbvision, USBVISION_IOPIN_REG, AudioChannel) < 0) {
2403 printk(KERN_ERR "usbvision_set_audio: can't write iopin register for audio switching\n");
2404 return -1;
2405 }
2406 }
781aa1d1
MCC
2407 usbvision->AudioChannel = AudioChannel;
2408 return 0;
2409}
2410
483dfdb6 2411int usbvision_setup(struct usb_usbvision *usbvision,int format)
781aa1d1 2412{
483dfdb6 2413 usbvision_set_video_format(usbvision, format);
781aa1d1
MCC
2414 usbvision_set_dram_settings(usbvision);
2415 usbvision_set_compress_params(usbvision);
2416 usbvision_set_input(usbvision);
2417 usbvision_set_output(usbvision, MAX_USB_WIDTH, MAX_USB_HEIGHT);
2418 usbvision_restart_isoc(usbvision);
18d8a454 2419
781aa1d1
MCC
2420 /* cosas del PCM */
2421 return USBVISION_IS_OPERATIONAL(usbvision);
2422}
2423
2a9f8b5d
TM
2424int usbvision_set_alternate(struct usb_usbvision *dev)
2425{
2426 int errCode, prev_alt = dev->ifaceAlt;
2427 int i;
2428
2429 dev->ifaceAlt=0;
2430 for(i=0;i< dev->num_alt; i++)
2431 if(dev->alt_max_pkt_size[i]>dev->alt_max_pkt_size[dev->ifaceAlt])
2432 dev->ifaceAlt=i;
2433
2434 if (dev->ifaceAlt != prev_alt) {
2435 dev->isocPacketSize = dev->alt_max_pkt_size[dev->ifaceAlt];
2436 PDEBUG(DBG_FUNC,"setting alternate %d with wMaxPacketSize=%u", dev->ifaceAlt,dev->isocPacketSize);
2437 errCode = usb_set_interface(dev->dev, dev->iface, dev->ifaceAlt);
2438 if (errCode < 0) {
2439 err ("cannot change alternate number to %d (error=%i)",
2440 dev->ifaceAlt, errCode);
2441 return errCode;
2442 }
2443 }
2444
2445 PDEBUG(DBG_ISOC, "ISO Packet Length:%d", dev->isocPacketSize);
2446
2447 return 0;
2448}
2449
483dfdb6
TM
2450/*
2451 * usbvision_init_isoc()
2452 *
2453 */
2454int usbvision_init_isoc(struct usb_usbvision *usbvision)
2455{
2456 struct usb_device *dev = usbvision->dev;
2457 int bufIdx, errCode, regValue;
fe818d1d 2458 int sb_size;
483dfdb6
TM
2459
2460 if (!USBVISION_IS_OPERATIONAL(usbvision))
2461 return -EFAULT;
2462
2463 usbvision->curFrame = NULL;
2464 scratch_reset(usbvision);
2465
2466 /* Alternate interface 1 is is the biggest frame size */
2a9f8b5d 2467 errCode = usbvision_set_alternate(usbvision);
781aa1d1
MCC
2468 if (errCode < 0) {
2469 usbvision->last_error = errCode;
2470 return -EBUSY;
2471 }
fe818d1d 2472 sb_size = USBVISION_URB_FRAMES * usbvision->isocPacketSize;
781aa1d1 2473
fe818d1d
TM
2474 regValue = (16 - usbvision_read_reg(usbvision,
2475 USBVISION_ALTER_REG)) & 0x0F;
781aa1d1
MCC
2476
2477 usbvision->usb_bandwidth = regValue >> 1;
fe818d1d
TM
2478 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2479 usbvision->usb_bandwidth);
781aa1d1
MCC
2480
2481
2482
2483 /* We double buffer the Iso lists */
2484
2485 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
2486 int j, k;
2487 struct urb *urb;
2488
a1ed551c 2489 urb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
781aa1d1
MCC
2490 if (urb == NULL) {
2491 err("%s: usb_alloc_urb() failed", __FUNCTION__);
2492 return -ENOMEM;
2493 }
2494 usbvision->sbuf[bufIdx].urb = urb;
fe818d1d
TM
2495 usbvision->sbuf[bufIdx].data =
2496 usb_buffer_alloc(usbvision->dev,
2497 sb_size,
2498 GFP_KERNEL,
2499 &urb->transfer_dma);
781aa1d1
MCC
2500 urb->dev = dev;
2501 urb->context = usbvision;
2502 urb->pipe = usb_rcvisocpipe(dev, usbvision->video_endp);
781aa1d1
MCC
2503 urb->transfer_flags = URB_ISO_ASAP;
2504 urb->interval = 1;
781aa1d1
MCC
2505 urb->transfer_buffer = usbvision->sbuf[bufIdx].data;
2506 urb->complete = usbvision_isocIrq;
2507 urb->number_of_packets = USBVISION_URB_FRAMES;
2508 urb->transfer_buffer_length =
2509 usbvision->isocPacketSize * USBVISION_URB_FRAMES;
2510 for (j = k = 0; j < USBVISION_URB_FRAMES; j++,
2511 k += usbvision->isocPacketSize) {
2512 urb->iso_frame_desc[j].offset = k;
fe818d1d
TM
2513 urb->iso_frame_desc[j].length =
2514 usbvision->isocPacketSize;
781aa1d1
MCC
2515 }
2516 }
2517
2518
2519 /* Submit all URBs */
2520 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
fe818d1d
TM
2521 errCode = usb_submit_urb(usbvision->sbuf[bufIdx].urb,
2522 GFP_KERNEL);
781aa1d1 2523 if (errCode) {
fe818d1d
TM
2524 err("%s: usb_submit_urb(%d) failed: error %d",
2525 __FUNCTION__, bufIdx, errCode);
781aa1d1
MCC
2526 }
2527 }
2528
5f7fb877 2529 usbvision->streaming = Stream_Idle;
fe818d1d
TM
2530 PDEBUG(DBG_ISOC, "%s: streaming=1 usbvision->video_endp=$%02x",
2531 __FUNCTION__,
2532 usbvision->video_endp);
781aa1d1
MCC
2533 return 0;
2534}
2535
2536/*
2537 * usbvision_stop_isoc()
2538 *
2539 * This procedure stops streaming and deallocates URBs. Then it
2540 * activates zero-bandwidth alt. setting of the video interface.
2541 *
2542 */
483dfdb6 2543void usbvision_stop_isoc(struct usb_usbvision *usbvision)
781aa1d1
MCC
2544{
2545 int bufIdx, errCode, regValue;
fe818d1d 2546 int sb_size = USBVISION_URB_FRAMES * usbvision->isocPacketSize;
781aa1d1 2547
5f7fb877 2548 if ((usbvision->streaming == Stream_Off) || (usbvision->dev == NULL))
781aa1d1
MCC
2549 return;
2550
2551 /* Unschedule all of the iso td's */
2552 for (bufIdx = 0; bufIdx < USBVISION_NUMSBUF; bufIdx++) {
2553 usb_kill_urb(usbvision->sbuf[bufIdx].urb);
38284ba3
TM
2554 if (usbvision->sbuf[bufIdx].data){
2555 usb_buffer_free(usbvision->dev,
2556 sb_size,
2557 usbvision->sbuf[bufIdx].data,
2558 usbvision->sbuf[bufIdx].urb->transfer_dma);
2559 }
781aa1d1
MCC
2560 usb_free_urb(usbvision->sbuf[bufIdx].urb);
2561 usbvision->sbuf[bufIdx].urb = NULL;
18d8a454 2562 }
781aa1d1
MCC
2563
2564
f2242ee5
TM
2565 PDEBUG(DBG_ISOC, "%s: streaming=Stream_Off\n", __FUNCTION__);
2566 usbvision->streaming = Stream_Off;
781aa1d1
MCC
2567
2568 if (!usbvision->remove_pending) {
2569
2570 /* Set packet size to 0 */
2a9f8b5d 2571 usbvision->ifaceAlt=0;
781aa1d1 2572 errCode = usb_set_interface(usbvision->dev, usbvision->iface,
2a9f8b5d 2573 usbvision->ifaceAlt);
781aa1d1 2574 if (errCode < 0) {
fe818d1d
TM
2575 err("%s: usb_set_interface() failed: error %d",
2576 __FUNCTION__, errCode);
781aa1d1
MCC
2577 usbvision->last_error = errCode;
2578 }
fe818d1d
TM
2579 regValue = (16-usbvision_read_reg(usbvision, USBVISION_ALTER_REG)) & 0x0F;
2580 usbvision->isocPacketSize =
2581 (regValue == 0) ? 0 : (regValue * 64) - 1;
2582 PDEBUG(DBG_ISOC, "ISO Packet Length:%d",
2583 usbvision->isocPacketSize);
781aa1d1
MCC
2584
2585 usbvision->usb_bandwidth = regValue >> 1;
fe818d1d
TM
2586 PDEBUG(DBG_ISOC, "USB Bandwidth Usage: %dMbit/Sec",
2587 usbvision->usb_bandwidth);
781aa1d1
MCC
2588 }
2589}
2590
483dfdb6 2591int usbvision_muxsel(struct usb_usbvision *usbvision, int channel)
781aa1d1 2592{
66a17879
TM
2593 /* inputs #0 and #3 are constant for every SAA711x. */
2594 /* inputs #1 and #2 are variable for SAA7111 and SAA7113 */
2595 int mode[4]= {SAA7115_COMPOSITE0, 0, 0, SAA7115_COMPOSITE3};
18d8a454 2596 int audio[]= {1, 0, 0, 0};
781aa1d1
MCC
2597 struct v4l2_routing route;
2598 //channel 0 is TV with audiochannel 1 (tuner mono)
2599 //channel 1 is Composite with audio channel 0 (line in)
2600 //channel 2 is S-Video with audio channel 0 (line in)
2601 //channel 3 is additional video inputs to the device with audio channel 0 (line in)
2602
2603 RESTRICT_TO_RANGE(channel, 0, usbvision->video_inputs);
f2242ee5 2604 usbvision->ctl_input = channel;
781aa1d1
MCC
2605
2606 // set the new channel
2607 // Regular USB TV Tuners -> channel: 0 = Television, 1 = Composite, 2 = S-Video
18d8a454 2608 // Four video input devices -> channel: 0 = Chan White, 1 = Chan Green, 2 = Chan Yellow, 3 = Chan Red
781aa1d1
MCC
2609
2610 switch (usbvision_device_data[usbvision->DevModel].Codec) {
2611 case CODEC_SAA7113:
66a17879
TM
2612 mode[1] = SAA7115_COMPOSITE2;
2613 if (SwitchSVideoInput) {
2614 /* To handle problems with S-Video Input for
2615 * some devices. Use SwitchSVideoInput
2616 * parameter when loading the module.*/
2617 mode[2] = SAA7115_COMPOSITE1;
781aa1d1
MCC
2618 }
2619 else {
66a17879 2620 mode[2] = SAA7115_SVIDEO1;
781aa1d1
MCC
2621 }
2622 break;
2623 case CODEC_SAA7111:
781aa1d1 2624 default:
66a17879
TM
2625 /* modes for saa7111 */
2626 mode[1] = SAA7115_COMPOSITE1;
2627 mode[2] = SAA7115_SVIDEO1;
2628 break;
781aa1d1
MCC
2629 }
2630 route.input = mode[channel];
66a17879 2631 route.output = 0;
781aa1d1 2632 call_i2c_clients(usbvision, VIDIOC_INT_S_VIDEO_ROUTING,&route);
781aa1d1
MCC
2633 usbvision_set_audio(usbvision, audio[channel]);
2634 return 0;
2635}
2636
781aa1d1
MCC
2637/*
2638 * Overrides for Emacs so that we follow Linus's tabbing style.
2639 * ---------------------------------------------------------------------------
2640 * Local variables:
2641 * c-basic-offset: 8
2642 * End:
2643 */
This page took 0.61452 seconds and 5 git commands to generate.