]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/easycap/easycap_low.c
a345a1bc69d03ede29522e6dd51553cb17d43a0e
[karo-tx-linux.git] / drivers / staging / easycap / easycap_low.c
1 /*****************************************************************************
2 *                                                                            *
3 *                                                                            *
4 *  easycap_low.c                                                             *
5 *                                                                            *
6 *                                                                            *
7 *****************************************************************************/
8 /*
9  *
10  *  Copyright (C) 2010 R.M. Thomas  <rmthomas@sciolus.org>
11  *
12  *
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.
17  *
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.
22  *
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
26  *
27 */
28 /*****************************************************************************/
29 /*
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.
38 */
39 /****************************************************************************/
40
41 #include "easycap.h"
42
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)
51
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)
59
60 /*--------------------------------------------------------------------------*/
61 static const struct stk1160config {
62         int reg;
63         int set;
64 } stk1160configPAL[256] = {
65                 {0x000, 0x0098},
66                 {0x002, 0x0093},
67
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},
77
78                 {0x100, 0x0033},
79                 {0x103, 0x0000},
80                 {0x104, 0x0000},
81                 {0x105, 0x0000},
82                 {0x106, 0x0000},
83
84 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85 /*
86  *  RESOLUTION 640x480
87 */
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 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
98
99                 {0x202, 0x000F},
100                 {0x203, 0x004A},
101                 {0x2FF, 0x0000},
102
103                 {0xFFF, 0xFFFF}
104 };
105 /*--------------------------------------------------------------------------*/
106 static const struct stk1160config stk1160configNTSC[256] = {
107                 {0x000, 0x0098},
108                 {0x002, 0x0093},
109
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},
119
120                 {0x100, 0x0033},
121                 {0x103, 0x0000},
122                 {0x104, 0x0000},
123                 {0x105, 0x0000},
124                 {0x106, 0x0000},
125
126 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
127 /*
128  *  RESOLUTION 640x480
129 */
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 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
140
141                 {0x202, 0x000F},
142                 {0x203, 0x004A},
143                 {0x2FF, 0x0000},
144
145                 {0xFFF, 0xFFFF}
146 };
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},
201
202                 {0xFF, 0xFF}
203 };
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},
255
256                 {0xFF, 0xFF}
257 };
258
259 static int regget(struct usb_device *pusb_device,
260                 u16 index, void *reg, int reg_size)
261 {
262         int rc;
263
264         if (!pusb_device)
265                 return -ENODEV;
266
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);
272
273         return 0xFF & rc;
274 }
275
276 static int regset(struct usb_device *pusb_device, u16 index, u16 value)
277 {
278         int rc;
279
280         if (!pusb_device)
281                 return -ENODEV;
282
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);
287
288         if (rc < 0)
289                 return rc;
290
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;
304
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;
314
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;
321                 }
322         }
323
324         return rc;
325 }
326 /*****************************************************************************/
327
328 /****************************************************************************/
329 int
330 confirm_resolution(struct usb_device *p)
331 {
332 u8 get0, get1, get2, get3, get4, get5, get6, get7;
333
334 if (NULL == p)
335         return -ENODEV;
336 GET(p, 0x0110, &get0);
337 GET(p, 0x0111, &get1);
338 GET(p, 0x0112, &get2);
339 GET(p, 0x0113, &get3);
340 GET(p, 0x0114, &get4);
341 GET(p, 0x0115, &get5);
342 GET(p, 0x0116, &get6);
343 GET(p, 0x0117, &get7);
344 JOT(8,  "0x%03X, 0x%03X, "
345         "0x%03X, 0x%03X, "
346         "0x%03X, 0x%03X, "
347         "0x%03X, 0x%03X\n",
348         get0, get1, get2, get3, get4, get5, get6, get7);
349 JOT(8,  "....cf PAL_720x526: "
350         "0x%03X, 0x%03X, "
351         "0x%03X, 0x%03X, "
352         "0x%03X, 0x%03X, "
353         "0x%03X, 0x%03X\n",
354         0x000, 0x000, 0x001, 0x000, 0x5A0, 0x005, 0x121, 0x001);
355 JOT(8,  "....cf PAL_704x526: "
356         "0x%03X, 0x%03X, "
357         "0x%03X, 0x%03X, "
358         "0x%03X, 0x%03X, "
359         "0x%03X, 0x%03X\n",
360         0x004, 0x000, 0x001, 0x000, 0x584, 0x005, 0x121, 0x001);
361 JOT(8,  "....cf VGA_640x480: "
362         "0x%03X, 0x%03X, "
363         "0x%03X, 0x%03X, "
364         "0x%03X, 0x%03X, "
365         "0x%03X, 0x%03X\n",
366         0x008, 0x000, 0x020, 0x000, 0x508, 0x005, 0x110, 0x001);
367 return 0;
368 }
369 /****************************************************************************/
370 int
371 confirm_stream(struct usb_device *p)
372 {
373 u16 get2;
374 u8 igot;
375
376 if (NULL == p)
377         return -ENODEV;
378 GET(p, 0x0100, &igot);  get2 = 0x80 & igot;
379 if (0x80 == get2)
380         JOT(8, "confirm_stream:  OK\n");
381 else
382         JOT(8, "confirm_stream:  STUCK\n");
383 return 0;
384 }
385 /****************************************************************************/
386 int
387 setup_stk(struct usb_device *p, bool ntsc)
388 {
389 int i0;
390
391 if (NULL == p)
392         return -ENODEV;
393 i0 = 0;
394 if (true == ntsc) {
395         while (0xFFF != stk1160configNTSC[i0].reg) {
396                 SET(p, stk1160configNTSC[i0].reg, stk1160configNTSC[i0].set);
397                 i0++;
398         }
399 } else {
400         while (0xFFF != stk1160configPAL[i0].reg) {
401                 SET(p, stk1160configPAL[i0].reg, stk1160configPAL[i0].set);
402                 i0++;
403         }
404 }
405
406 write_300(p);
407
408 return 0;
409 }
410 /****************************************************************************/
411 int
412 setup_saa(struct usb_device *p, bool ntsc)
413 {
414 int i0, ir;
415
416 if (NULL == p)
417         return -ENODEV;
418 i0 = 0;
419 if (true == ntsc) {
420         while (0xFF != saa7113configNTSC[i0].reg) {
421                 ir = write_saa(p, saa7113configNTSC[i0].reg,
422                                         saa7113configNTSC[i0].set);
423                 i0++;
424         }
425 } else {
426         while (0xFF != saa7113configPAL[i0].reg) {
427                 ir = write_saa(p, saa7113configPAL[i0].reg,
428                                         saa7113configPAL[i0].set);
429                 i0++;
430         }
431 }
432 return 0;
433 }
434 /****************************************************************************/
435 int
436 write_000(struct usb_device *p, u16 set2, u16 set0)
437 {
438 u8 igot0, igot2;
439
440 if (NULL == p)
441         return -ENODEV;
442 GET(p, 0x0002, &igot2);
443 GET(p, 0x0000, &igot0);
444 SET(p, 0x0002, set2);
445 SET(p, 0x0000, set0);
446 return 0;
447 }
448 /****************************************************************************/
449 int
450 write_saa(struct usb_device *p, u16 reg0, u16 set0)
451 {
452 if (NULL == p)
453         return -ENODEV;
454 SET(p, 0x200, 0x00);
455 SET(p, 0x204, reg0);
456 SET(p, 0x205, set0);
457 SET(p, 0x200, 0x01);
458 return wait_i2c(p);
459 }
460 /****************************************************************************/
461 /*--------------------------------------------------------------------------*/
462 /*
463  *  REGISTER 500:  SETTING VALUE TO 0x008B READS FROM VT1612A (?)
464  *  REGISTER 500:  SETTING VALUE TO 0x008C WRITES TO  VT1612A
465  *  REGISTER 502:  LEAST SIGNIFICANT BYTE OF VALUE TO SET
466  *  REGISTER 503:  MOST SIGNIFICANT BYTE OF VALUE TO SET
467  *  REGISTER 504:  TARGET ADDRESS ON VT1612A
468  */
469 /*--------------------------------------------------------------------------*/
470 int
471 write_vt(struct usb_device *p, u16 reg0, u16 set0)
472 {
473 u8 igot;
474 u16 got502, got503;
475 u16 set502, set503;
476
477 if (NULL == p)
478         return -ENODEV;
479 SET(p, 0x0504, reg0);
480 SET(p, 0x0500, 0x008B);
481
482 GET(p, 0x0502, &igot);  got502 = (0xFF & igot);
483 GET(p, 0x0503, &igot);  got503 = (0xFF & igot);
484
485 JOT(16, "write_vt(., 0x%04X, 0x%04X): was 0x%04X\n",
486                                         reg0, set0, ((got503 << 8) | got502));
487
488 set502 =  (0x00FF & set0);
489 set503 = ((0xFF00 & set0) >> 8);
490
491 SET(p, 0x0504, reg0);
492 SET(p, 0x0502, set502);
493 SET(p, 0x0503, set503);
494 SET(p, 0x0500, 0x008C);
495
496 return 0;
497 }
498 /****************************************************************************/
499 /*--------------------------------------------------------------------------*/
500 /*
501  *  REGISTER 500:  SETTING VALUE TO 0x008B READS FROM VT1612A (?)
502  *  REGISTER 500:  SETTING VALUE TO 0x008C WRITES TO  VT1612A
503  *  REGISTER 502:  LEAST SIGNIFICANT BYTE OF VALUE TO GET
504  *  REGISTER 503:  MOST SIGNIFICANT BYTE OF VALUE TO GET
505  *  REGISTER 504:  TARGET ADDRESS ON VT1612A
506  */
507 /*--------------------------------------------------------------------------*/
508 int
509 read_vt(struct usb_device *p, u16 reg0)
510 {
511 u8 igot;
512 u16 got502, got503;
513
514 if (NULL == p)
515         return -ENODEV;
516 SET(p, 0x0504, reg0);
517 SET(p, 0x0500, 0x008B);
518
519 GET(p, 0x0502, &igot);  got502 = (0xFF & igot);
520 GET(p, 0x0503, &igot);  got503 = (0xFF & igot);
521
522 JOT(16, "read_vt(., 0x%04X): has 0x%04X\n", reg0, ((got503 << 8) | got502));
523
524 return (got503 << 8) | got502;
525 }
526 /****************************************************************************/
527 /*--------------------------------------------------------------------------*/
528 /*
529  *  THESE APPEAR TO HAVE NO EFFECT ON EITHER VIDEO OR AUDIO.
530  */
531 /*--------------------------------------------------------------------------*/
532 int
533 write_300(struct usb_device *p)
534 {
535 if (NULL == p)
536         return -ENODEV;
537 SET(p, 0x300, 0x0012);
538 SET(p, 0x350, 0x002D);
539 SET(p, 0x351, 0x0001);
540 SET(p, 0x352, 0x0000);
541 SET(p, 0x353, 0x0000);
542 SET(p, 0x300, 0x0080);
543 return 0;
544 }
545 /****************************************************************************/
546 /*--------------------------------------------------------------------------*/
547 /*
548  *  NOTE: THE FOLLOWING IS NOT CHECKED:
549  *  REGISTER 0x0F, WHICH IS INVOLVED IN CHROMINANCE AUTOMATIC GAIN CONTROL.
550  */
551 /*--------------------------------------------------------------------------*/
552 int
553 check_saa(struct usb_device *p, bool ntsc)
554 {
555 int i0, ir, rc;
556
557 if (NULL == p)
558         return -ENODEV;
559 i0 = 0;
560 rc = 0;
561 if (true == ntsc) {
562         while (0xFF != saa7113configNTSC[i0].reg) {
563                 if (0x0F == saa7113configNTSC[i0].reg) {
564                         i0++;
565                         continue;
566                 }
567
568                 ir = read_saa(p, saa7113configNTSC[i0].reg);
569                 if (ir != saa7113configNTSC[i0].set) {
570                         SAY("SAA register 0x%02X has 0x%02X, "
571                                                 "expected 0x%02X\n",
572                                                 saa7113configNTSC[i0].reg,
573                                                 ir, saa7113configNTSC[i0].set);
574                         rc--;
575                 }
576                 i0++;
577         }
578 } else {
579         while (0xFF != saa7113configPAL[i0].reg) {
580                 if (0x0F == saa7113configPAL[i0].reg) {
581                         i0++;
582                         continue;
583                 }
584
585                 ir = read_saa(p, saa7113configPAL[i0].reg);
586                 if (ir != saa7113configPAL[i0].set) {
587                         SAY("SAA register 0x%02X has 0x%02X, "
588                                                 "expected 0x%02X\n",
589                                                 saa7113configPAL[i0].reg,
590                                                 ir, saa7113configPAL[i0].set);
591                         rc--;
592                 }
593                 i0++;
594         }
595 }
596 if (-8 > rc)
597         return rc;
598 else
599         return 0;
600 }
601 /****************************************************************************/
602 int
603 merit_saa(struct usb_device *p)
604 {
605 int rc;
606
607 if (NULL == p)
608         return -ENODEV;
609 rc = read_saa(p, 0x1F);
610 if ((0 > rc) || (0x02 & rc))
611         return 1 ;
612 else
613         return 0;
614 }
615 /****************************************************************************/
616 int
617 ready_saa(struct usb_device *p)
618 {
619 int j, rc, rate;
620 const int max = 5, marktime = PATIENCE/5;
621 /*--------------------------------------------------------------------------*/
622 /*
623  *   RETURNS    0     FOR INTERLACED       50 Hz
624  *              1     FOR NON-INTERLACED   50 Hz
625  *              2     FOR INTERLACED       60 Hz
626  *              3     FOR NON-INTERLACED   60 Hz
627 */
628 /*--------------------------------------------------------------------------*/
629 if (NULL == p)
630         return -ENODEV;
631 j = 0;
632 while (max > j) {
633         rc = read_saa(p, 0x1F);
634         if (0 <= rc) {
635                 if (0 == (0x40 & rc))
636                         break;
637                 if (1 == (0x01 & rc))
638                         break;
639         }
640         msleep(marktime);
641         j++;
642 }
643 if (max == j)
644         return -1;
645 else {
646         if (0x20 & rc) {
647                 rate = 2;
648                 JOT(8, "hardware detects 60 Hz\n");
649         } else {
650                 rate = 0;
651                 JOT(8, "hardware detects 50 Hz\n");
652         }
653         if (0x80 & rc)
654                 JOT(8, "hardware detects interlacing\n");
655         else {
656                 rate++;
657                 JOT(8, "hardware detects no interlacing\n");
658         }
659 }
660 return 0;
661 }
662 /****************************************************************************/
663 /*--------------------------------------------------------------------------*/
664 /*
665  *  NOTE: THE FOLLOWING ARE NOT CHECKED:
666  *  REGISTERS 0x000, 0x002:  FUNCTIONALITY IS NOT KNOWN
667  *  REGISTER  0x100:  ACCEPT ALSO (0x80 | stk1160config....[.].set)
668  */
669 /*--------------------------------------------------------------------------*/
670 int
671 check_stk(struct usb_device *p, bool ntsc)
672 {
673 int i0, ir;
674
675 if (NULL == p)
676         return -ENODEV;
677 i0 = 0;
678 if (true == ntsc) {
679         while (0xFFF != stk1160configNTSC[i0].reg) {
680                 if (0x000 == stk1160configNTSC[i0].reg) {
681                         i0++; continue;
682                 }
683                 if (0x002 == stk1160configNTSC[i0].reg) {
684                         i0++; continue;
685                 }
686                 ir = read_stk(p, stk1160configNTSC[i0].reg);
687                 if (0x100 == stk1160configNTSC[i0].reg) {
688                         if ((ir != (0xFF & stk1160configNTSC[i0].set)) &&
689                                         (ir != (0x80 | (0xFF &
690                                         stk1160configNTSC[i0].set))) &&
691                                         (0xFFFF !=
692                                         stk1160configNTSC[i0].set)) {
693                                 SAY("STK register 0x%03X has 0x%02X, "
694                                                 "expected 0x%02X\n",
695                                                 stk1160configNTSC[i0].reg,
696                                                 ir, stk1160configNTSC[i0].set);
697                                 }
698                         i0++; continue;
699                         }
700                 if ((ir != (0xFF & stk1160configNTSC[i0].set)) &&
701                                 (0xFFFF != stk1160configNTSC[i0].set)) {
702                         SAY("STK register 0x%03X has 0x%02X, "
703                                                 "expected 0x%02X\n",
704                                                 stk1160configNTSC[i0].reg,
705                                                 ir, stk1160configNTSC[i0].set);
706                 }
707                 i0++;
708         }
709 } else {
710         while (0xFFF != stk1160configPAL[i0].reg) {
711                 if (0x000 == stk1160configPAL[i0].reg) {
712                         i0++; continue;
713                 }
714                 if (0x002 == stk1160configPAL[i0].reg) {
715                         i0++; continue;
716                 }
717                 ir = read_stk(p, stk1160configPAL[i0].reg);
718                 if (0x100 == stk1160configPAL[i0].reg) {
719                         if ((ir != (0xFF & stk1160configPAL[i0].set)) &&
720                                         (ir != (0x80 | (0xFF &
721                                         stk1160configPAL[i0].set))) &&
722                                         (0xFFFF !=
723                                         stk1160configPAL[i0].set)) {
724                                 SAY("STK register 0x%03X has 0x%02X, "
725                                                 "expected 0x%02X\n",
726                                                 stk1160configPAL[i0].reg,
727                                                 ir, stk1160configPAL[i0].set);
728                                 }
729                         i0++; continue;
730                         }
731                 if ((ir != (0xFF & stk1160configPAL[i0].set)) &&
732                                 (0xFFFF != stk1160configPAL[i0].set)) {
733                         SAY("STK register 0x%03X has 0x%02X, "
734                                                 "expected 0x%02X\n",
735                                                 stk1160configPAL[i0].reg,
736                                                 ir, stk1160configPAL[i0].set);
737                 }
738                 i0++;
739         }
740 }
741 return 0;
742 }
743 /****************************************************************************/
744 int
745 read_saa(struct usb_device *p, u16 reg0)
746 {
747 u8 igot;
748
749 if (NULL == p)
750         return -ENODEV;
751 SET(p, 0x208, reg0);
752 SET(p, 0x200, 0x20);
753 if (0 != wait_i2c(p))
754         return -1;
755 igot = 0;
756 GET(p, 0x0209, &igot);
757 return igot;
758 }
759 /****************************************************************************/
760 int
761 read_stk(struct usb_device *p, u32 reg0)
762 {
763 u8 igot;
764
765 if (NULL == p)
766         return -ENODEV;
767 igot = 0;
768 GET(p, reg0, &igot);
769 return igot;
770 }
771 /****************************************************************************/
772 /*--------------------------------------------------------------------------*/
773 /*
774  *    HARDWARE    USERSPACE INPUT NUMBER   PHYSICAL INPUT   DRIVER input VALUE
775  *
776  *  CVBS+S-VIDEO           0 or 1              CVBS                 1
777  *   FOUR-CVBS             0 or 1              CVBS1                1
778  *   FOUR-CVBS                2                CVBS2                2
779  *   FOUR-CVBS                3                CVBS3                3
780  *   FOUR-CVBS                4                CVBS4                4
781  *  CVBS+S-VIDEO              5               S-VIDEO               5
782  *
783  *  WHEN 5==input THE ARGUMENT mode MUST ALSO BE SUPPLIED:
784  *
785  *     mode  7   => GAIN TO BE SET EXPLICITLY USING REGISTER 0x05 (UNTESTED)
786  *     mode  9   => USE AUTOMATIC GAIN CONTROL (DEFAULT)
787  *
788 */
789 /*---------------------------------------------------------------------------*/
790 int
791 select_input(struct usb_device *p, int input, int mode)
792 {
793 int ir;
794
795 if (NULL == p)
796         return -ENODEV;
797 stop_100(p);
798 switch (input) {
799 case 0:
800 case 1: {
801         if (0 != write_saa(p, 0x02, 0x80)) {
802                 SAY("ERROR: failed to set SAA register 0x02 for input %i\n",
803                                                                         input);
804         }
805         SET(p, 0x0000, 0x0098);
806         SET(p, 0x0002, 0x0078);
807         break;
808 }
809 case 2: {
810         if (0 != write_saa(p, 0x02, 0x80)) {
811                 SAY("ERROR: failed to set SAA register 0x02 for input %i\n",
812                                                                         input);
813         }
814         SET(p, 0x0000, 0x0090);
815         SET(p, 0x0002, 0x0078);
816         break;
817 }
818 case 3: {
819         if (0 != write_saa(p, 0x02, 0x80)) {
820                 SAY("ERROR: failed to set SAA register 0x02 for input %i\n",
821                                                                         input);
822         }
823         SET(p, 0x0000, 0x0088);
824         SET(p, 0x0002, 0x0078);
825         break;
826 }
827 case 4: {
828         if (0 != write_saa(p, 0x02, 0x80)) {
829                 SAY("ERROR: failed to set SAA register 0x02 for input %i\n",
830                                                                         input);
831         }
832         SET(p, 0x0000, 0x0080);
833         SET(p, 0x0002, 0x0078);
834         break;
835 }
836 case 5: {
837         if (9 != mode)
838                 mode = 7;
839         switch (mode) {
840         case 7: {
841                 if (0 != write_saa(p, 0x02, 0x87)) {
842                         SAY("ERROR: failed to set SAA register 0x02 "
843                                                 "for input %i\n", input);
844                 }
845                 if (0 != write_saa(p, 0x05, 0xFF)) {
846                         SAY("ERROR: failed to set SAA register 0x05 "
847                                                 "for input %i\n", input);
848                 }
849                 break;
850         }
851         case 9: {
852                 if (0 != write_saa(p, 0x02, 0x89)) {
853                         SAY("ERROR: failed to set SAA register 0x02 "
854                                                 "for input %i\n", input);
855                 }
856                 if (0 != write_saa(p, 0x05, 0x00)) {
857                         SAY("ERROR: failed to set SAA register 0x05 "
858                                                 "for input %i\n", input);
859                 }
860         break;
861         }
862         default: {
863                 SAY("MISTAKE:  bad mode: %i\n", mode);
864                 return -1;
865         }
866         }
867         if (0 != write_saa(p, 0x04, 0x00)) {
868                 SAY("ERROR: failed to set SAA register 0x04 for input %i\n",
869                                                                         input);
870         }
871         if (0 != write_saa(p, 0x09, 0x80)) {
872                 SAY("ERROR: failed to set SAA register 0x09 for input %i\n",
873                                                                         input);
874         }
875         SET(p, 0x0002, 0x0093);
876         break;
877 }
878 default: {
879         SAY("ERROR:  bad input: %i\n", input);
880         return -1;
881 }
882 }
883 ir = read_stk(p, 0x00);
884 JOT(8, "STK register 0x00 has 0x%02X\n", ir);
885 ir = read_saa(p, 0x02);
886 JOT(8, "SAA register 0x02 has 0x%02X\n", ir);
887
888 start_100(p);
889
890 return 0;
891 }
892 /****************************************************************************/
893 int
894 set_resolution(struct usb_device *p,
895                                 u16 set0, u16 set1, u16 set2, u16 set3)
896 {
897 u16 u0x0111, u0x0113, u0x0115, u0x0117;
898
899 if (NULL == p)
900         return -ENODEV;
901 u0x0111 = ((0xFF00 & set0) >> 8);
902 u0x0113 = ((0xFF00 & set1) >> 8);
903 u0x0115 = ((0xFF00 & set2) >> 8);
904 u0x0117 = ((0xFF00 & set3) >> 8);
905
906 SET(p, 0x0110, (0x00FF & set0));
907 SET(p, 0x0111, u0x0111);
908 SET(p, 0x0112, (0x00FF & set1));
909 SET(p, 0x0113, u0x0113);
910 SET(p, 0x0114, (0x00FF & set2));
911 SET(p, 0x0115, u0x0115);
912 SET(p, 0x0116, (0x00FF & set3));
913 SET(p, 0x0117, u0x0117);
914
915 return 0;
916 }
917 /****************************************************************************/
918 int
919 start_100(struct usb_device *p)
920 {
921 u16 get116, get117, get0;
922 u8 igot116, igot117, igot;
923
924 if (NULL == p)
925         return -ENODEV;
926 GET(p, 0x0116, &igot116);
927 get116 = igot116;
928 GET(p, 0x0117, &igot117);
929 get117 = igot117;
930 SET(p, 0x0116, 0x0000);
931 SET(p, 0x0117, 0x0000);
932
933 GET(p, 0x0100, &igot);
934 get0 = igot;
935 SET(p, 0x0100, (0x80 | get0));
936
937 SET(p, 0x0116, get116);
938 SET(p, 0x0117, get117);
939
940 return 0;
941 }
942 /****************************************************************************/
943 int
944 stop_100(struct usb_device *p)
945 {
946 u16 get0;
947 u8 igot;
948
949 if (NULL == p)
950         return -ENODEV;
951 GET(p, 0x0100, &igot);
952 get0 = igot;
953 SET(p, 0x0100, (0x7F & get0));
954 return 0;
955 }
956 /****************************************************************************/
957 /*--------------------------------------------------------------------------*/
958 /*
959  *  FUNCTION wait_i2c() RETURNS 0 ON SUCCESS
960 */
961 /*--------------------------------------------------------------------------*/
962 int
963 wait_i2c(struct usb_device *p)
964 {
965 u16 get0;
966 u8 igot;
967 const int max = 2;
968 int k;
969
970 if (NULL == p)
971         return -ENODEV;
972 for (k = 0;  k < max;  k++) {
973         GET(p, 0x0201, &igot);  get0 = igot;
974         switch (get0) {
975         case 0x04:
976         case 0x01: {
977                 return 0;
978         }
979         case 0x00: {
980                 msleep(20);
981                 continue;
982         }
983         default: {
984                 return get0 - 1;
985         }
986         }
987 }
988 return -1;
989 }
990 /****************************************************************************/
991 /*****************************************************************************/
992 int
993 wakeup_device(struct usb_device *pusb_device)
994 {
995 if (!pusb_device)
996         return -ENODEV;
997 return usb_control_msg(pusb_device, usb_sndctrlpipe(pusb_device, 0),
998                 (u8)USB_REQ_SET_FEATURE,
999                 (u8)(USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE),
1000                 USB_DEVICE_REMOTE_WAKEUP,
1001                 (u16)0,
1002                 (void *) NULL,
1003                 (u16)0,
1004                 (int)50000);
1005 }
1006 /*****************************************************************************/
1007 int
1008 audio_setup(struct easycap *peasycap)
1009 {
1010 struct usb_device *pusb_device;
1011 unsigned char buffer[1];
1012 int rc, id1, id2;
1013 /*---------------------------------------------------------------------------*/
1014 /*
1015  *                                IMPORTANT:
1016  *  THE MESSAGE OF TYPE (USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE)
1017  *  CAUSES MUTING IF THE VALUE 0x0100 IS SENT.
1018  *  TO ENABLE AUDIO  THE VALUE 0x0200 MUST BE SENT.
1019  */
1020 /*---------------------------------------------------------------------------*/
1021 const u8 request = 0x01;
1022 const u8 requesttype =
1023                 (u8)(USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE);
1024 const u16 value_unmute = 0x0200;
1025 const u16 index = 0x0301;
1026 const u16 length = 1;
1027
1028 if (NULL == peasycap)
1029         return -EFAULT;
1030
1031 pusb_device = peasycap->pusb_device;
1032 if (NULL == pusb_device)
1033         return -ENODEV;
1034
1035 JOM(8, "%02X %02X %02X %02X %02X %02X %02X %02X\n",
1036                         requesttype, request,
1037                         (0x00FF & value_unmute),
1038                         (0xFF00 & value_unmute) >> 8,
1039                         (0x00FF & index),
1040                         (0xFF00 & index) >> 8,
1041                         (0x00FF & length),
1042                         (0xFF00 & length) >> 8);
1043
1044 buffer[0] = 0x01;
1045
1046 rc = usb_control_msg(pusb_device, usb_sndctrlpipe(pusb_device, 0),
1047                         (u8)request,
1048                         (u8)requesttype,
1049                         (u16)value_unmute,
1050                         (u16)index,
1051                         (void *)&buffer[0],
1052                         (u16)length,
1053                         (int)50000);
1054
1055 JOT(8, "0x%02X=buffer\n", *((u8 *) &buffer[0]));
1056 if (rc != (int)length) {
1057         switch (rc) {
1058         case -EPIPE: {
1059                 SAY("usb_control_msg returned -EPIPE\n");
1060                 break;
1061         }
1062         default: {
1063                 SAY("ERROR: usb_control_msg returned %i\n", rc);
1064                 break;
1065         }
1066         }
1067 }
1068 /*--------------------------------------------------------------------------*/
1069 /*
1070  *  REGISTER 500:  SETTING VALUE TO 0x0094 RESETS AUDIO CONFIGURATION ???
1071  *  REGISTER 506:  ANALOGUE AUDIO ATTENTUATOR ???
1072  *                 FOR THE CVBS+S-VIDEO HARDWARE:
1073  *                    SETTING VALUE TO 0x0000 GIVES QUIET SOUND.
1074  *                    THE UPPER BYTE SEEMS TO HAVE NO EFFECT.
1075  *                 FOR THE FOUR-CVBS HARDWARE:
1076  *                    SETTING VALUE TO 0x0000 SEEMS TO HAVE NO EFFECT.
1077  *  REGISTER 507:  ANALOGUE AUDIO PREAMPLIFIER ON/OFF ???
1078  *                 FOR THE CVBS-S-VIDEO HARDWARE:
1079  *                    SETTING VALUE TO 0x0001 GIVES VERY LOUD, DISTORTED SOUND.
1080  *                    THE UPPER BYTE SEEMS TO HAVE NO EFFECT.
1081  */
1082 /*--------------------------------------------------------------------------*/
1083 SET(pusb_device, 0x0500, 0x0094);
1084 SET(pusb_device, 0x0500, 0x008C);
1085 SET(pusb_device, 0x0506, 0x0001);
1086 SET(pusb_device, 0x0507, 0x0000);
1087 id1 = read_vt(pusb_device, 0x007C);
1088 id2 = read_vt(pusb_device, 0x007E);
1089 SAM("0x%04X:0x%04X is audio vendor id\n", id1, id2);
1090 /*---------------------------------------------------------------------------*/
1091 /*
1092  *  SELECT AUDIO SOURCE "LINE IN" AND SET THE AUDIO GAIN.
1093 */
1094 /*---------------------------------------------------------------------------*/
1095 if (0 != audio_gainset(pusb_device, peasycap->gain))
1096         SAY("ERROR: audio_gainset() failed\n");
1097 check_vt(pusb_device);
1098 return 0;
1099 }
1100 /*****************************************************************************/
1101 int
1102 check_vt(struct usb_device *pusb_device)
1103 {
1104 int igot;
1105
1106 if (!pusb_device)
1107         return -ENODEV;
1108 igot = read_vt(pusb_device, 0x0002);
1109 if (0 > igot)
1110         SAY("ERROR: failed to read VT1612A register 0x02\n");
1111 if (0x8000 & igot)
1112         SAY("register 0x%02X muted\n", 0x02);
1113
1114 igot = read_vt(pusb_device, 0x000E);
1115 if (0 > igot)
1116         SAY("ERROR: failed to read VT1612A register 0x0E\n");
1117 if (0x8000 & igot)
1118         SAY("register 0x%02X muted\n", 0x0E);
1119
1120 igot = read_vt(pusb_device, 0x0010);
1121 if (0 > igot)
1122         SAY("ERROR: failed to read VT1612A register 0x10\n");
1123 if (0x8000 & igot)
1124         SAY("register 0x%02X muted\n", 0x10);
1125
1126 igot = read_vt(pusb_device, 0x0012);
1127 if (0 > igot)
1128         SAY("ERROR: failed to read VT1612A register 0x12\n");
1129 if (0x8000 & igot)
1130         SAY("register 0x%02X muted\n", 0x12);
1131
1132 igot = read_vt(pusb_device, 0x0014);
1133 if (0 > igot)
1134         SAY("ERROR: failed to read VT1612A register 0x14\n");
1135 if (0x8000 & igot)
1136         SAY("register 0x%02X muted\n", 0x14);
1137
1138 igot = read_vt(pusb_device, 0x0016);
1139 if (0 > igot)
1140         SAY("ERROR: failed to read VT1612A register 0x16\n");
1141 if (0x8000 & igot)
1142         SAY("register 0x%02X muted\n", 0x16);
1143
1144 igot = read_vt(pusb_device, 0x0018);
1145 if (0 > igot)
1146         SAY("ERROR: failed to read VT1612A register 0x18\n");
1147 if (0x8000 & igot)
1148         SAY("register 0x%02X muted\n", 0x18);
1149
1150 igot = read_vt(pusb_device, 0x001C);
1151 if (0 > igot)
1152         SAY("ERROR: failed to read VT1612A register 0x1C\n");
1153 if (0x8000 & igot)
1154         SAY("register 0x%02X muted\n", 0x1C);
1155
1156 return 0;
1157 }
1158 /*****************************************************************************/
1159 /*---------------------------------------------------------------------------*/
1160 /*  NOTE:  THIS DOES INCREASE THE VOLUME DRAMATICALLY:
1161  *                      audio_gainset(pusb_device, 0x000F);
1162  *
1163  *       loud        dB  register 0x10      dB register 0x1C    dB total
1164  *         0               -34.5                   0             -34.5
1165  *        ..                ....                   .              ....
1166  *        15                10.5                   0              10.5
1167  *        16                12.0                   0              12.0
1168  *        17                12.0                   1.5            13.5
1169  *        ..                ....                  ....            ....
1170  *        31                12.0                  22.5            34.5
1171 */
1172 /*---------------------------------------------------------------------------*/
1173 int
1174 audio_gainset(struct usb_device *pusb_device, s8 loud)
1175 {
1176 int igot;
1177 u8 tmp;
1178 u16 mute;
1179
1180 if (NULL == pusb_device)
1181         return -ENODEV;
1182 if (0 > loud)
1183         loud = 0;
1184 if (31 < loud)
1185         loud = 31;
1186
1187 write_vt(pusb_device, 0x0002, 0x8000);
1188 /*---------------------------------------------------------------------------*/
1189 igot = read_vt(pusb_device, 0x000E);
1190 if (0 > igot) {
1191         SAY("ERROR: failed to read VT1612A register 0x0E\n");
1192         mute = 0x0000;
1193 } else
1194         mute = 0x8000 & ((unsigned int)igot);
1195 mute = 0;
1196
1197 if (16 > loud)
1198         tmp = 0x01 | (0x001F & (((u8)(15 - loud)) << 1));
1199 else
1200         tmp = 0;
1201
1202 JOT(8, "0x%04X=(mute|tmp) for VT1612A register 0x0E\n", mute | tmp);
1203 write_vt(pusb_device, 0x000E, (mute | tmp));
1204 /*---------------------------------------------------------------------------*/
1205 igot = read_vt(pusb_device, 0x0010);
1206 if (0 > igot) {
1207         SAY("ERROR: failed to read VT1612A register 0x10\n");
1208         mute = 0x0000;
1209 } else
1210         mute = 0x8000 & ((unsigned int)igot);
1211 mute = 0;
1212
1213 JOT(8, "0x%04X=(mute|tmp|(tmp<<8)) for VT1612A register 0x10,...0x18\n",
1214                                                 mute | tmp | (tmp << 8));
1215 write_vt(pusb_device, 0x0010, (mute | tmp | (tmp << 8)));
1216 write_vt(pusb_device, 0x0012, (mute | tmp | (tmp << 8)));
1217 write_vt(pusb_device, 0x0014, (mute | tmp | (tmp << 8)));
1218 write_vt(pusb_device, 0x0016, (mute | tmp | (tmp << 8)));
1219 write_vt(pusb_device, 0x0018, (mute | tmp | (tmp << 8)));
1220 /*---------------------------------------------------------------------------*/
1221 igot = read_vt(pusb_device, 0x001C);
1222 if (0 > igot) {
1223         SAY("ERROR: failed to read VT1612A register 0x1C\n");
1224         mute = 0x0000;
1225 } else
1226         mute = 0x8000 & ((unsigned int)igot);
1227 mute = 0;
1228
1229 if (16 <= loud)
1230         tmp = 0x000F & (u8)(loud - 16);
1231 else
1232         tmp = 0;
1233
1234 JOT(8, "0x%04X=(mute|tmp|(tmp<<8)) for VT1612A register 0x1C\n",
1235                                         mute | tmp | (tmp << 8));
1236 write_vt(pusb_device, 0x001C, (mute | tmp | (tmp << 8)));
1237 write_vt(pusb_device, 0x001A, 0x0404);
1238 write_vt(pusb_device, 0x0002, 0x0000);
1239 return 0;
1240 }
1241 /*****************************************************************************/
1242 int
1243 audio_gainget(struct usb_device *pusb_device)
1244 {
1245 int igot;
1246
1247 if (NULL == pusb_device)
1248         return -ENODEV;
1249 igot = read_vt(pusb_device, 0x001C);
1250 if (0 > igot)
1251         SAY("ERROR: failed to read VT1612A register 0x1C\n");
1252 return igot;
1253 }
1254 /*****************************************************************************/