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