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