gma500: begin the config based split
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / easycap / easycap_low.c
blob0385735ac6df8a56d0a0a7f306941afdce47f6fb
1 /*****************************************************************************
2 * *
3 * *
4 * easycap_low.c *
5 * *
6 * *
7 *****************************************************************************/
8 /*
10 * Copyright (C) 2010 R.M. Thomas <rmthomas@sciolus.org>
13 * This is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * The software is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this software; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 /*****************************************************************************/
30 * ACKNOWLEGEMENTS AND REFERENCES
31 * ------------------------------
32 * This driver makes use of register information contained in the Syntek
33 * Semicon DC-1125 driver hosted at
34 * http://sourceforge.net/projects/syntekdriver/.
35 * Particularly useful has been a patch to the latter driver provided by
36 * Ivor Hewitt in January 2009. The NTSC implementation is taken from the
37 * work of Ben Trask.
39 /****************************************************************************/
41 #include "easycap.h"
43 #define GET(X, Y, Z) do { \
44 int __rc; \
45 *(Z) = (u16)0; \
46 __rc = regget(X, Y, Z, sizeof(u8)); \
47 if (0 > __rc) { \
48 JOT(8, ":-(%i\n", __LINE__); return __rc; \
49 } \
50 } while (0)
52 #define SET(X, Y, Z) do { \
53 int __rc; \
54 __rc = regset(X, Y, Z); \
55 if (0 > __rc) { \
56 JOT(8, ":-(%i\n", __LINE__); return __rc; \
57 } \
58 } while (0)
60 /*--------------------------------------------------------------------------*/
61 static const struct stk1160config {
62 int reg;
63 int set;
64 } stk1160configPAL[256] = {
65 {0x000, 0x0098},
66 {0x002, 0x0093},
68 {0x001, 0x0003},
69 {0x003, 0x0080},
70 {0x00D, 0x0000},
71 {0x00F, 0x0002},
72 {0x018, 0x0010},
73 {0x019, 0x0000},
74 {0x01A, 0x0014},
75 {0x01B, 0x000E},
76 {0x01C, 0x0046},
78 {0x100, 0x0033},
79 {0x103, 0x0000},
80 {0x104, 0x0000},
81 {0x105, 0x0000},
82 {0x106, 0x0000},
84 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
86 * RESOLUTION 640x480
88 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
89 {0x110, 0x0008},
90 {0x111, 0x0000},
91 {0x112, 0x0020},
92 {0x113, 0x0000},
93 {0x114, 0x0508},
94 {0x115, 0x0005},
95 {0x116, 0x0110},
96 {0x117, 0x0001},
97 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
99 {0x202, 0x000F},
100 {0x203, 0x004A},
101 {0x2FF, 0x0000},
103 {0xFFF, 0xFFFF}
105 /*--------------------------------------------------------------------------*/
106 static const struct stk1160config stk1160configNTSC[256] = {
107 {0x000, 0x0098},
108 {0x002, 0x0093},
110 {0x001, 0x0003},
111 {0x003, 0x0080},
112 {0x00D, 0x0000},
113 {0x00F, 0x0002},
114 {0x018, 0x0010},
115 {0x019, 0x0000},
116 {0x01A, 0x0014},
117 {0x01B, 0x000E},
118 {0x01C, 0x0046},
120 {0x100, 0x0033},
121 {0x103, 0x0000},
122 {0x104, 0x0000},
123 {0x105, 0x0000},
124 {0x106, 0x0000},
126 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
128 * RESOLUTION 640x480
130 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
131 {0x110, 0x0008},
132 {0x111, 0x0000},
133 {0x112, 0x0003},
134 {0x113, 0x0000},
135 {0x114, 0x0508},
136 {0x115, 0x0005},
137 {0x116, 0x00F3},
138 {0x117, 0x0000},
139 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
141 {0x202, 0x000F},
142 {0x203, 0x004A},
143 {0x2FF, 0x0000},
145 {0xFFF, 0xFFFF}
147 /*--------------------------------------------------------------------------*/
148 static const struct saa7113config {
149 int reg;
150 int set;
151 } saa7113configPAL[256] = {
152 {0x01, 0x08},
153 {0x02, 0x80},
154 {0x03, 0x33},
155 {0x04, 0x00},
156 {0x05, 0x00},
157 {0x06, 0xE9},
158 {0x07, 0x0D},
159 {0x08, 0x38},
160 {0x09, 0x00},
161 {0x0A, SAA_0A_DEFAULT},
162 {0x0B, SAA_0B_DEFAULT},
163 {0x0C, SAA_0C_DEFAULT},
164 {0x0D, SAA_0D_DEFAULT},
165 {0x0E, 0x01},
166 {0x0F, 0x36},
167 {0x10, 0x00},
168 {0x11, 0x0C},
169 {0x12, 0xE7},
170 {0x13, 0x00},
171 {0x15, 0x00},
172 {0x16, 0x00},
173 {0x40, 0x02},
174 {0x41, 0xFF},
175 {0x42, 0xFF},
176 {0x43, 0xFF},
177 {0x44, 0xFF},
178 {0x45, 0xFF},
179 {0x46, 0xFF},
180 {0x47, 0xFF},
181 {0x48, 0xFF},
182 {0x49, 0xFF},
183 {0x4A, 0xFF},
184 {0x4B, 0xFF},
185 {0x4C, 0xFF},
186 {0x4D, 0xFF},
187 {0x4E, 0xFF},
188 {0x4F, 0xFF},
189 {0x50, 0xFF},
190 {0x51, 0xFF},
191 {0x52, 0xFF},
192 {0x53, 0xFF},
193 {0x54, 0xFF},
194 {0x55, 0xFF},
195 {0x56, 0xFF},
196 {0x57, 0xFF},
197 {0x58, 0x40},
198 {0x59, 0x54},
199 {0x5A, 0x07},
200 {0x5B, 0x83},
202 {0xFF, 0xFF}
204 /*--------------------------------------------------------------------------*/
205 static const struct saa7113config saa7113configNTSC[256] = {
206 {0x01, 0x08},
207 {0x02, 0x80},
208 {0x03, 0x33},
209 {0x04, 0x00},
210 {0x05, 0x00},
211 {0x06, 0xE9},
212 {0x07, 0x0D},
213 {0x08, 0x78},
214 {0x09, 0x00},
215 {0x0A, SAA_0A_DEFAULT},
216 {0x0B, SAA_0B_DEFAULT},
217 {0x0C, SAA_0C_DEFAULT},
218 {0x0D, SAA_0D_DEFAULT},
219 {0x0E, 0x01},
220 {0x0F, 0x36},
221 {0x10, 0x00},
222 {0x11, 0x0C},
223 {0x12, 0xE7},
224 {0x13, 0x00},
225 {0x15, 0x00},
226 {0x16, 0x00},
227 {0x40, 0x82},
228 {0x41, 0xFF},
229 {0x42, 0xFF},
230 {0x43, 0xFF},
231 {0x44, 0xFF},
232 {0x45, 0xFF},
233 {0x46, 0xFF},
234 {0x47, 0xFF},
235 {0x48, 0xFF},
236 {0x49, 0xFF},
237 {0x4A, 0xFF},
238 {0x4B, 0xFF},
239 {0x4C, 0xFF},
240 {0x4D, 0xFF},
241 {0x4E, 0xFF},
242 {0x4F, 0xFF},
243 {0x50, 0xFF},
244 {0x51, 0xFF},
245 {0x52, 0xFF},
246 {0x53, 0xFF},
247 {0x54, 0xFF},
248 {0x55, 0xFF},
249 {0x56, 0xFF},
250 {0x57, 0xFF},
251 {0x58, 0x40},
252 {0x59, 0x54},
253 {0x5A, 0x0A},
254 {0x5B, 0x83},
256 {0xFF, 0xFF}
259 static int regget(struct usb_device *pusb_device,
260 u16 index, void *reg, int reg_size)
262 int rc;
264 if (!pusb_device)
265 return -ENODEV;
267 rc = usb_control_msg(pusb_device, usb_rcvctrlpipe(pusb_device, 0),
268 0x00,
269 (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
270 0x00,
271 index, reg, reg_size, 50000);
273 return rc;
276 static int regset(struct usb_device *pusb_device, u16 index, u16 value)
278 int rc;
280 if (!pusb_device)
281 return -ENODEV;
283 rc = usb_control_msg(pusb_device, usb_sndctrlpipe(pusb_device, 0),
284 0x01,
285 (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
286 value, index, NULL, 0, 500);
288 if (rc < 0)
289 return rc;
291 if (easycap_readback) {
292 u16 igot = 0;
293 rc = regget(pusb_device, index, &igot, sizeof(igot));
294 igot = 0xFF & igot;
295 switch (index) {
296 case 0x000:
297 case 0x500:
298 case 0x502:
299 case 0x503:
300 case 0x504:
301 case 0x506:
302 case 0x507:
303 break;
305 case 0x204:
306 case 0x205:
307 case 0x350:
308 case 0x351:
309 if (igot)
310 JOT(8, "unexpected 0x%02X "
311 "for STK register 0x%03X\n",
312 igot, index);
313 break;
315 default:
316 if ((0xFF & value) != igot)
317 JOT(8, "unexpected 0x%02X != 0x%02X "
318 "for STK register 0x%03X\n",
319 igot, value, index);
320 break;
324 return rc;
326 /*--------------------------------------------------------------------------*/
328 * FUNCTION wait_i2c() RETURNS 0 ON SUCCESS
330 /*--------------------------------------------------------------------------*/
331 static int wait_i2c(struct usb_device *p)
333 u16 get0;
334 u8 igot;
335 const int max = 2;
336 int k;
338 if (!p)
339 return -ENODEV;
341 for (k = 0; k < max; k++) {
342 GET(p, 0x0201, &igot); get0 = igot;
343 switch (get0) {
344 case 0x04:
345 case 0x01:
346 return 0;
347 case 0x00:
348 msleep(20);
349 continue;
350 default:
351 return get0 - 1;
354 return -1;
357 /****************************************************************************/
358 int confirm_resolution(struct usb_device *p)
360 u8 get0, get1, get2, get3, get4, get5, get6, get7;
362 if (!p)
363 return -ENODEV;
364 GET(p, 0x0110, &get0);
365 GET(p, 0x0111, &get1);
366 GET(p, 0x0112, &get2);
367 GET(p, 0x0113, &get3);
368 GET(p, 0x0114, &get4);
369 GET(p, 0x0115, &get5);
370 GET(p, 0x0116, &get6);
371 GET(p, 0x0117, &get7);
372 JOT(8, "0x%03X, 0x%03X, "
373 "0x%03X, 0x%03X, "
374 "0x%03X, 0x%03X, "
375 "0x%03X, 0x%03X\n",
376 get0, get1, get2, get3, get4, get5, get6, get7);
377 JOT(8, "....cf PAL_720x526: "
378 "0x%03X, 0x%03X, "
379 "0x%03X, 0x%03X, "
380 "0x%03X, 0x%03X, "
381 "0x%03X, 0x%03X\n",
382 0x000, 0x000, 0x001, 0x000, 0x5A0, 0x005, 0x121, 0x001);
383 JOT(8, "....cf PAL_704x526: "
384 "0x%03X, 0x%03X, "
385 "0x%03X, 0x%03X, "
386 "0x%03X, 0x%03X, "
387 "0x%03X, 0x%03X\n",
388 0x004, 0x000, 0x001, 0x000, 0x584, 0x005, 0x121, 0x001);
389 JOT(8, "....cf VGA_640x480: "
390 "0x%03X, 0x%03X, "
391 "0x%03X, 0x%03X, "
392 "0x%03X, 0x%03X, "
393 "0x%03X, 0x%03X\n",
394 0x008, 0x000, 0x020, 0x000, 0x508, 0x005, 0x110, 0x001);
395 return 0;
397 /****************************************************************************/
398 int confirm_stream(struct usb_device *p)
400 u16 get2;
401 u8 igot;
403 if (!p)
404 return -ENODEV;
405 GET(p, 0x0100, &igot); get2 = 0x80 & igot;
406 if (0x80 == get2)
407 JOT(8, "confirm_stream: OK\n");
408 else
409 JOT(8, "confirm_stream: STUCK\n");
410 return 0;
412 /****************************************************************************/
413 int setup_stk(struct usb_device *p, bool ntsc)
415 int i;
416 const struct stk1160config *cfg;
417 if (!p)
418 return -ENODEV;
419 cfg = (ntsc) ? stk1160configNTSC : stk1160configPAL;
420 for (i = 0; cfg[i].reg != 0xFFF; i++)
421 SET(p, cfg[i].reg, cfg[i].set);
423 write_300(p);
425 return 0;
427 /****************************************************************************/
428 int setup_saa(struct usb_device *p, bool ntsc)
430 int i, ir;
431 const struct saa7113config *cfg;
432 if (!p)
433 return -ENODEV;
434 cfg = (ntsc) ? saa7113configNTSC : saa7113configPAL;
435 for (i = 0; cfg[i].reg != 0xFF; i++)
436 ir = write_saa(p, cfg[i].reg, cfg[i].set);
437 return 0;
439 /****************************************************************************/
440 int write_000(struct usb_device *p, u16 set2, u16 set0)
442 u8 igot0, igot2;
444 if (!p)
445 return -ENODEV;
446 GET(p, 0x0002, &igot2);
447 GET(p, 0x0000, &igot0);
448 SET(p, 0x0002, set2);
449 SET(p, 0x0000, set0);
450 return 0;
452 /****************************************************************************/
453 int write_saa(struct usb_device *p, u16 reg0, u16 set0)
455 if (!p)
456 return -ENODEV;
457 SET(p, 0x200, 0x00);
458 SET(p, 0x204, reg0);
459 SET(p, 0x205, set0);
460 SET(p, 0x200, 0x01);
461 return wait_i2c(p);
463 /****************************************************************************/
464 /*--------------------------------------------------------------------------*/
466 * REGISTER 500: SETTING VALUE TO 0x008B READS FROM VT1612A (?)
467 * REGISTER 500: SETTING VALUE TO 0x008C WRITES TO VT1612A
468 * REGISTER 502: LEAST SIGNIFICANT BYTE OF VALUE TO SET
469 * REGISTER 503: MOST SIGNIFICANT BYTE OF VALUE TO SET
470 * REGISTER 504: TARGET ADDRESS ON VT1612A
472 /*--------------------------------------------------------------------------*/
474 write_vt(struct usb_device *p, u16 reg0, u16 set0)
476 u8 igot;
477 u16 got502, got503;
478 u16 set502, set503;
480 if (!p)
481 return -ENODEV;
482 SET(p, 0x0504, reg0);
483 SET(p, 0x0500, 0x008B);
485 GET(p, 0x0502, &igot); got502 = (0xFF & igot);
486 GET(p, 0x0503, &igot); got503 = (0xFF & igot);
488 JOT(16, "write_vt(., 0x%04X, 0x%04X): was 0x%04X\n",
489 reg0, set0, ((got503 << 8) | got502));
491 set502 = (0x00FF & set0);
492 set503 = ((0xFF00 & set0) >> 8);
494 SET(p, 0x0504, reg0);
495 SET(p, 0x0502, set502);
496 SET(p, 0x0503, set503);
497 SET(p, 0x0500, 0x008C);
499 return 0;
501 /****************************************************************************/
502 /*--------------------------------------------------------------------------*/
504 * REGISTER 500: SETTING VALUE TO 0x008B READS FROM VT1612A (?)
505 * REGISTER 500: SETTING VALUE TO 0x008C WRITES TO VT1612A
506 * REGISTER 502: LEAST SIGNIFICANT BYTE OF VALUE TO GET
507 * REGISTER 503: MOST SIGNIFICANT BYTE OF VALUE TO GET
508 * REGISTER 504: TARGET ADDRESS ON VT1612A
510 /*--------------------------------------------------------------------------*/
511 int read_vt(struct usb_device *p, u16 reg0)
513 u8 igot;
514 u16 got502, got503;
516 if (!p)
517 return -ENODEV;
518 SET(p, 0x0504, reg0);
519 SET(p, 0x0500, 0x008B);
521 GET(p, 0x0502, &igot); got502 = (0xFF & igot);
522 GET(p, 0x0503, &igot); got503 = (0xFF & igot);
524 JOT(16, "read_vt(., 0x%04X): has 0x%04X\n",
525 reg0, ((got503 << 8) | got502));
527 return (got503 << 8) | got502;
529 /****************************************************************************/
530 /*--------------------------------------------------------------------------*/
532 * THESE APPEAR TO HAVE NO EFFECT ON EITHER VIDEO OR AUDIO.
534 /*--------------------------------------------------------------------------*/
535 int write_300(struct usb_device *p)
537 if (!p)
538 return -ENODEV;
539 SET(p, 0x300, 0x0012);
540 SET(p, 0x350, 0x002D);
541 SET(p, 0x351, 0x0001);
542 SET(p, 0x352, 0x0000);
543 SET(p, 0x353, 0x0000);
544 SET(p, 0x300, 0x0080);
545 return 0;
547 /****************************************************************************/
548 /*--------------------------------------------------------------------------*/
550 * NOTE: THE FOLLOWING IS NOT CHECKED:
551 * REGISTER 0x0F, WHICH IS INVOLVED IN CHROMINANCE AUTOMATIC GAIN CONTROL.
553 /*--------------------------------------------------------------------------*/
554 int check_saa(struct usb_device *p, bool ntsc)
556 int i, ir, rc = 0;
557 struct saa7113config const *cfg;
558 if (!p)
559 return -ENODEV;
561 cfg = (ntsc) ? saa7113configNTSC : saa7113configPAL;
562 for (i = 0; cfg[i].reg != 0xFF; i++) {
563 if (0x0F == cfg[i].reg)
564 continue;
565 ir = read_saa(p, cfg[i].reg);
566 if (ir != cfg[i].set) {
567 SAY("SAA register 0x%02X has 0x%02X, expected 0x%02X\n",
568 cfg[i].reg, ir, cfg[i].set);
569 rc--;
573 return (rc < -8) ? rc : 0;
575 /****************************************************************************/
576 int merit_saa(struct usb_device *p)
578 int rc;
580 if (!p)
581 return -ENODEV;
582 rc = read_saa(p, 0x1F);
583 return ((0 > rc) || (0x02 & rc)) ? 1 : 0;
585 /****************************************************************************/
586 int ready_saa(struct usb_device *p)
588 int j, rc, rate;
589 const int max = 5, marktime = PATIENCE/5;
590 /*--------------------------------------------------------------------------*/
592 * RETURNS 0 FOR INTERLACED 50 Hz
593 * 1 FOR NON-INTERLACED 50 Hz
594 * 2 FOR INTERLACED 60 Hz
595 * 3 FOR NON-INTERLACED 60 Hz
597 /*--------------------------------------------------------------------------*/
598 if (!p)
599 return -ENODEV;
600 j = 0;
601 while (max > j) {
602 rc = read_saa(p, 0x1F);
603 if (0 <= rc) {
604 if (0 == (0x40 & rc))
605 break;
606 if (1 == (0x01 & rc))
607 break;
609 msleep(marktime);
610 j++;
612 if (max == j)
613 return -1;
614 else {
615 if (0x20 & rc) {
616 rate = 2;
617 JOT(8, "hardware detects 60 Hz\n");
618 } else {
619 rate = 0;
620 JOT(8, "hardware detects 50 Hz\n");
622 if (0x80 & rc)
623 JOT(8, "hardware detects interlacing\n");
624 else {
625 rate++;
626 JOT(8, "hardware detects no interlacing\n");
629 return 0;
631 /****************************************************************************/
632 /*--------------------------------------------------------------------------*/
634 * NOTE: THE FOLLOWING ARE NOT CHECKED:
635 * REGISTERS 0x000, 0x002: FUNCTIONALITY IS NOT KNOWN
636 * REGISTER 0x100: ACCEPT ALSO (0x80 | stk1160config....[.].set)
638 /*--------------------------------------------------------------------------*/
639 int check_stk(struct usb_device *p, bool ntsc)
641 int i, ir;
642 const struct stk1160config *cfg;
644 if (!p)
645 return -ENODEV;
646 cfg = (ntsc) ? stk1160configNTSC : stk1160configPAL;
648 for (i = 0; 0xFFF != cfg[i].reg; i++) {
649 if (0x000 == cfg[i].reg || 0x002 == cfg[i].reg)
650 continue;
653 ir = read_stk(p, cfg[i].reg);
654 if (0x100 == cfg[i].reg) {
655 if ((ir != (0xFF & cfg[i].set)) &&
656 (ir != (0x80 | (0xFF & cfg[i].set))) &&
657 (0xFFFF != cfg[i].set)) {
658 SAY("STK reg[0x%03X]=0x%02X expected 0x%02X\n",
659 cfg[i].reg, ir, cfg[i].set);
661 continue;
663 if ((ir != (0xFF & cfg[i].set)) && (0xFFFF != cfg[i].set))
664 SAY("STK register 0x%03X has 0x%02X,expected 0x%02X\n",
665 cfg[i].reg, ir, cfg[i].set);
667 return 0;
669 /****************************************************************************/
670 int read_saa(struct usb_device *p, u16 reg0)
672 u8 igot;
674 if (!p)
675 return -ENODEV;
676 SET(p, 0x208, reg0);
677 SET(p, 0x200, 0x20);
678 if (0 != wait_i2c(p))
679 return -1;
680 igot = 0;
681 GET(p, 0x0209, &igot);
682 return igot;
684 /****************************************************************************/
685 int read_stk(struct usb_device *p, u32 reg0)
687 u8 igot;
689 if (!p)
690 return -ENODEV;
691 igot = 0;
692 GET(p, reg0, &igot);
693 return igot;
695 /****************************************************************************/
696 /*--------------------------------------------------------------------------*/
698 * HARDWARE USERSPACE INPUT NUMBER PHYSICAL INPUT DRIVER input VALUE
700 * CVBS+S-VIDEO 0 or 1 CVBS 1
701 * FOUR-CVBS 0 or 1 CVBS1 1
702 * FOUR-CVBS 2 CVBS2 2
703 * FOUR-CVBS 3 CVBS3 3
704 * FOUR-CVBS 4 CVBS4 4
705 * CVBS+S-VIDEO 5 S-VIDEO 5
707 * WHEN 5==input THE ARGUMENT mode MUST ALSO BE SUPPLIED:
709 * mode 7 => GAIN TO BE SET EXPLICITLY USING REGISTER 0x05 (UNTESTED)
710 * mode 9 => USE AUTOMATIC GAIN CONTROL (DEFAULT)
713 /*---------------------------------------------------------------------------*/
715 select_input(struct usb_device *p, int input, int mode)
717 int ir;
719 if (!p)
720 return -ENODEV;
721 stop_100(p);
722 switch (input) {
723 case 0:
724 case 1: {
725 if (0 != write_saa(p, 0x02, 0x80))
726 SAY("ERROR: failed to set SAA register 0x02 "
727 "for input %i\n", input);
729 SET(p, 0x0000, 0x0098);
730 SET(p, 0x0002, 0x0078);
731 break;
733 case 2: {
734 if (0 != write_saa(p, 0x02, 0x80))
735 SAY("ERROR: failed to set SAA register 0x02 "
736 "for input %i\n", input);
738 SET(p, 0x0000, 0x0090);
739 SET(p, 0x0002, 0x0078);
740 break;
742 case 3: {
743 if (0 != write_saa(p, 0x02, 0x80))
744 SAY("ERROR: failed to set SAA register 0x02 "
745 " for input %i\n", input);
747 SET(p, 0x0000, 0x0088);
748 SET(p, 0x0002, 0x0078);
749 break;
751 case 4: {
752 if (0 != write_saa(p, 0x02, 0x80)) {
753 SAY("ERROR: failed to set SAA register 0x02 "
754 "for input %i\n", input);
756 SET(p, 0x0000, 0x0080);
757 SET(p, 0x0002, 0x0078);
758 break;
760 case 5: {
761 if (9 != mode)
762 mode = 7;
763 switch (mode) {
764 case 7: {
765 if (0 != write_saa(p, 0x02, 0x87))
766 SAY("ERROR: failed to set SAA register 0x02 "
767 "for input %i\n", input);
769 if (0 != write_saa(p, 0x05, 0xFF))
770 SAY("ERROR: failed to set SAA register 0x05 "
771 "for input %i\n", input);
773 break;
775 case 9: {
776 if (0 != write_saa(p, 0x02, 0x89))
777 SAY("ERROR: failed to set SAA register 0x02 "
778 "for input %i\n", input);
780 if (0 != write_saa(p, 0x05, 0x00))
781 SAY("ERROR: failed to set SAA register 0x05 "
782 "for input %i\n", input);
784 break;
786 default:
787 SAY("MISTAKE: bad mode: %i\n", mode);
788 return -1;
791 if (0 != write_saa(p, 0x04, 0x00))
792 SAY("ERROR: failed to set SAA register 0x04 "
793 "for input %i\n", input);
795 if (0 != write_saa(p, 0x09, 0x80))
796 SAY("ERROR: failed to set SAA register 0x09 "
797 "for input %i\n", input);
799 SET(p, 0x0002, 0x0093);
800 break;
802 default:
803 SAY("ERROR: bad input: %i\n", input);
804 return -1;
807 ir = read_stk(p, 0x00);
808 JOT(8, "STK register 0x00 has 0x%02X\n", ir);
809 ir = read_saa(p, 0x02);
810 JOT(8, "SAA register 0x02 has 0x%02X\n", ir);
812 start_100(p);
814 return 0;
816 /****************************************************************************/
817 int set_resolution(struct usb_device *p,
818 u16 set0, u16 set1, u16 set2, u16 set3)
820 u16 u0x0111, u0x0113, u0x0115, u0x0117;
822 if (!p)
823 return -ENODEV;
824 u0x0111 = ((0xFF00 & set0) >> 8);
825 u0x0113 = ((0xFF00 & set1) >> 8);
826 u0x0115 = ((0xFF00 & set2) >> 8);
827 u0x0117 = ((0xFF00 & set3) >> 8);
829 SET(p, 0x0110, (0x00FF & set0));
830 SET(p, 0x0111, u0x0111);
831 SET(p, 0x0112, (0x00FF & set1));
832 SET(p, 0x0113, u0x0113);
833 SET(p, 0x0114, (0x00FF & set2));
834 SET(p, 0x0115, u0x0115);
835 SET(p, 0x0116, (0x00FF & set3));
836 SET(p, 0x0117, u0x0117);
838 return 0;
840 /****************************************************************************/
841 int start_100(struct usb_device *p)
843 u16 get116, get117, get0;
844 u8 igot116, igot117, igot;
846 if (!p)
847 return -ENODEV;
848 GET(p, 0x0116, &igot116);
849 get116 = igot116;
850 GET(p, 0x0117, &igot117);
851 get117 = igot117;
852 SET(p, 0x0116, 0x0000);
853 SET(p, 0x0117, 0x0000);
855 GET(p, 0x0100, &igot);
856 get0 = igot;
857 SET(p, 0x0100, (0x80 | get0));
859 SET(p, 0x0116, get116);
860 SET(p, 0x0117, get117);
862 return 0;
864 /****************************************************************************/
865 int stop_100(struct usb_device *p)
867 u16 get0;
868 u8 igot;
870 if (!p)
871 return -ENODEV;
872 GET(p, 0x0100, &igot);
873 get0 = igot;
874 SET(p, 0x0100, (0x7F & get0));
875 return 0;
877 /****************************************************************************/
878 /****************************************************************************/
879 /*****************************************************************************/
880 int wakeup_device(struct usb_device *pusb_device)
882 if (!pusb_device)
883 return -ENODEV;
884 return usb_control_msg(pusb_device, usb_sndctrlpipe(pusb_device, 0),
885 USB_REQ_SET_FEATURE,
886 USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE,
887 USB_DEVICE_REMOTE_WAKEUP,
888 0, NULL, 0, 50000);
890 /*****************************************************************************/
892 audio_setup(struct easycap *peasycap)
894 struct usb_device *pusb_device;
895 u8 buffer[1];
896 int rc, id1, id2;
897 /*---------------------------------------------------------------------------*/
899 * IMPORTANT:
900 * THE MESSAGE OF TYPE (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE)
901 * CAUSES MUTING IF THE VALUE 0x0100 IS SENT.
902 * TO ENABLE AUDIO THE VALUE 0x0200 MUST BE SENT.
904 /*---------------------------------------------------------------------------*/
905 const u8 request = 0x01;
906 const u8 requesttype = USB_DIR_OUT |
907 USB_TYPE_CLASS |
908 USB_RECIP_INTERFACE;
909 const u16 value_unmute = 0x0200;
910 const u16 index = 0x0301;
911 const u16 length = 1;
913 if (!peasycap)
914 return -EFAULT;
916 pusb_device = peasycap->pusb_device;
917 if (!pusb_device)
918 return -ENODEV;
920 JOM(8, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
921 requesttype, request,
922 (0x00FF & value_unmute),
923 (0xFF00 & value_unmute) >> 8,
924 (0x00FF & index),
925 (0xFF00 & index) >> 8,
926 (0x00FF & length),
927 (0xFF00 & length) >> 8);
929 buffer[0] = 0x01;
931 rc = usb_control_msg(pusb_device, usb_sndctrlpipe(pusb_device, 0),
932 request, requesttype, value_unmute,
933 index, &buffer[0], length, 50000);
935 JOT(8, "0x%02X=buffer\n", buffer[0]);
936 if (rc != (int)length) {
937 switch (rc) {
938 case -EPIPE:
939 SAY("usb_control_msg returned -EPIPE\n");
940 break;
941 default:
942 SAY("ERROR: usb_control_msg returned %i\n", rc);
943 break;
946 /*--------------------------------------------------------------------------*/
948 * REGISTER 500: SETTING VALUE TO 0x0094 RESETS AUDIO CONFIGURATION ???
949 * REGISTER 506: ANALOGUE AUDIO ATTENTUATOR ???
950 * FOR THE CVBS+S-VIDEO HARDWARE:
951 * SETTING VALUE TO 0x0000 GIVES QUIET SOUND.
952 * THE UPPER BYTE SEEMS TO HAVE NO EFFECT.
953 * FOR THE FOUR-CVBS HARDWARE:
954 * SETTING VALUE TO 0x0000 SEEMS TO HAVE NO EFFECT.
955 * REGISTER 507: ANALOGUE AUDIO PREAMPLIFIER ON/OFF ???
956 * FOR THE CVBS-S-VIDEO HARDWARE:
957 * SETTING VALUE TO 0x0001 GIVES VERY LOUD, DISTORTED SOUND.
958 * THE UPPER BYTE SEEMS TO HAVE NO EFFECT.
960 /*--------------------------------------------------------------------------*/
961 SET(pusb_device, 0x0500, 0x0094);
962 SET(pusb_device, 0x0500, 0x008C);
963 SET(pusb_device, 0x0506, 0x0001);
964 SET(pusb_device, 0x0507, 0x0000);
965 id1 = read_vt(pusb_device, 0x007C);
966 id2 = read_vt(pusb_device, 0x007E);
967 SAM("0x%04X:0x%04X is audio vendor id\n", id1, id2);
968 /*---------------------------------------------------------------------------*/
970 * SELECT AUDIO SOURCE "LINE IN" AND SET THE AUDIO GAIN.
972 /*---------------------------------------------------------------------------*/
973 if (0 != audio_gainset(pusb_device, peasycap->gain))
974 SAY("ERROR: audio_gainset() failed\n");
975 check_vt(pusb_device);
976 return 0;
978 /*****************************************************************************/
979 int check_vt(struct usb_device *pusb_device)
981 int igot;
983 if (!pusb_device)
984 return -ENODEV;
985 igot = read_vt(pusb_device, 0x0002);
986 if (0 > igot)
987 SAY("ERROR: failed to read VT1612A register 0x02\n");
988 if (0x8000 & igot)
989 SAY("register 0x%02X muted\n", 0x02);
991 igot = read_vt(pusb_device, 0x000E);
992 if (0 > igot)
993 SAY("ERROR: failed to read VT1612A register 0x0E\n");
994 if (0x8000 & igot)
995 SAY("register 0x%02X muted\n", 0x0E);
997 igot = read_vt(pusb_device, 0x0010);
998 if (0 > igot)
999 SAY("ERROR: failed to read VT1612A register 0x10\n");
1000 if (0x8000 & igot)
1001 SAY("register 0x%02X muted\n", 0x10);
1003 igot = read_vt(pusb_device, 0x0012);
1004 if (0 > igot)
1005 SAY("ERROR: failed to read VT1612A register 0x12\n");
1006 if (0x8000 & igot)
1007 SAY("register 0x%02X muted\n", 0x12);
1009 igot = read_vt(pusb_device, 0x0014);
1010 if (0 > igot)
1011 SAY("ERROR: failed to read VT1612A register 0x14\n");
1012 if (0x8000 & igot)
1013 SAY("register 0x%02X muted\n", 0x14);
1015 igot = read_vt(pusb_device, 0x0016);
1016 if (0 > igot)
1017 SAY("ERROR: failed to read VT1612A register 0x16\n");
1018 if (0x8000 & igot)
1019 SAY("register 0x%02X muted\n", 0x16);
1021 igot = read_vt(pusb_device, 0x0018);
1022 if (0 > igot)
1023 SAY("ERROR: failed to read VT1612A register 0x18\n");
1024 if (0x8000 & igot)
1025 SAY("register 0x%02X muted\n", 0x18);
1027 igot = read_vt(pusb_device, 0x001C);
1028 if (0 > igot)
1029 SAY("ERROR: failed to read VT1612A register 0x1C\n");
1030 if (0x8000 & igot)
1031 SAY("register 0x%02X muted\n", 0x1C);
1033 return 0;
1035 /*****************************************************************************/
1036 /*---------------------------------------------------------------------------*/
1037 /* NOTE: THIS DOES INCREASE THE VOLUME DRAMATICALLY:
1038 * audio_gainset(pusb_device, 0x000F);
1040 * loud dB register 0x10 dB register 0x1C dB total
1041 * 0 -34.5 0 -34.5
1042 * .. .... . ....
1043 * 15 10.5 0 10.5
1044 * 16 12.0 0 12.0
1045 * 17 12.0 1.5 13.5
1046 * .. .... .... ....
1047 * 31 12.0 22.5 34.5
1049 /*---------------------------------------------------------------------------*/
1050 int audio_gainset(struct usb_device *pusb_device, s8 loud)
1052 int igot;
1053 u8 tmp;
1054 u16 mute;
1056 if (!pusb_device)
1057 return -ENODEV;
1058 if (0 > loud)
1059 loud = 0;
1060 if (31 < loud)
1061 loud = 31;
1063 write_vt(pusb_device, 0x0002, 0x8000);
1064 /*---------------------------------------------------------------------------*/
1065 igot = read_vt(pusb_device, 0x000E);
1066 if (0 > igot) {
1067 SAY("ERROR: failed to read VT1612A register 0x0E\n");
1068 mute = 0x0000;
1069 } else
1070 mute = 0x8000 & ((unsigned int)igot);
1071 mute = 0;
1073 if (16 > loud)
1074 tmp = 0x01 | (0x001F & (((u8)(15 - loud)) << 1));
1075 else
1076 tmp = 0;
1078 JOT(8, "0x%04X=(mute|tmp) for VT1612A register 0x0E\n", mute | tmp);
1079 write_vt(pusb_device, 0x000E, (mute | tmp));
1080 /*---------------------------------------------------------------------------*/
1081 igot = read_vt(pusb_device, 0x0010);
1082 if (0 > igot) {
1083 SAY("ERROR: failed to read VT1612A register 0x10\n");
1084 mute = 0x0000;
1085 } else
1086 mute = 0x8000 & ((unsigned int)igot);
1087 mute = 0;
1089 JOT(8, "0x%04X=(mute|tmp|(tmp<<8)) for VT1612A register 0x10,...0x18\n",
1090 mute | tmp | (tmp << 8));
1091 write_vt(pusb_device, 0x0010, (mute | tmp | (tmp << 8)));
1092 write_vt(pusb_device, 0x0012, (mute | tmp | (tmp << 8)));
1093 write_vt(pusb_device, 0x0014, (mute | tmp | (tmp << 8)));
1094 write_vt(pusb_device, 0x0016, (mute | tmp | (tmp << 8)));
1095 write_vt(pusb_device, 0x0018, (mute | tmp | (tmp << 8)));
1096 /*---------------------------------------------------------------------------*/
1097 igot = read_vt(pusb_device, 0x001C);
1098 if (0 > igot) {
1099 SAY("ERROR: failed to read VT1612A register 0x1C\n");
1100 mute = 0x0000;
1101 } else
1102 mute = 0x8000 & ((unsigned int)igot);
1103 mute = 0;
1105 if (16 <= loud)
1106 tmp = 0x000F & (u8)(loud - 16);
1107 else
1108 tmp = 0;
1110 JOT(8, "0x%04X=(mute|tmp|(tmp<<8)) for VT1612A register 0x1C\n",
1111 mute | tmp | (tmp << 8));
1112 write_vt(pusb_device, 0x001C, (mute | tmp | (tmp << 8)));
1113 write_vt(pusb_device, 0x001A, 0x0404);
1114 write_vt(pusb_device, 0x0002, 0x0000);
1115 return 0;
1117 /*****************************************************************************/
1118 int audio_gainget(struct usb_device *pusb_device)
1120 int igot;
1122 if (!pusb_device)
1123 return -ENODEV;
1124 igot = read_vt(pusb_device, 0x001C);
1125 if (0 > igot)
1126 SAY("ERROR: failed to read VT1612A register 0x1C\n");
1127 return igot;
1129 /*****************************************************************************/