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