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