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