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