aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/media/video/gspca/spca506.c
blob: 143203c1fd9fbf92772f26714321e31ce8097561 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
 * SPCA506 chip based cameras function
 * M Xhaard 15/04/2004 based on different work Mark Taylor and others
 * and my own snoopy file on a pv-321c donate by a german compagny
 *                "Firma Frank Gmbh" from  Saarbruecken
 *
 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define MODULE_NAME "spca506"

#include "gspca.h"

#define DRIVER_VERSION_NUMBER	KERNEL_VERSION(2, 1, 7)
static const char version[] = "2.1.7";

MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
MODULE_DESCRIPTION("GSPCA/SPCA506 USB Camera Driver");
MODULE_LICENSE("GPL");

/* specific webcam descriptor */
struct sd {
	struct gspca_dev gspca_dev;	/* !! must be the first item */

	int buflen;
	__u8 tmpbuf[640 * 480 * 3];	/* YYUV per line */
	__u8 tmpbuf2[640 * 480 * 2];	/* YUYV */

	unsigned char brightness;
	unsigned char contrast;
	unsigned char colors;
	unsigned char hue;
	char norme;
	char channel;
};

/* V4L2 controls supported by the driver */
static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val);
static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val);

static struct ctrl sd_ctrls[] = {
#define SD_BRIGHTNESS 0
	{
	    {
		.id      = V4L2_CID_BRIGHTNESS,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Brightness",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
		.default_value = 0x80,
	    },
	    .set = sd_setbrightness,
	    .get = sd_getbrightness,
	},
#define SD_CONTRAST 1
	{
	    {
		.id      = V4L2_CID_CONTRAST,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Contrast",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
		.default_value = 0x47,
	    },
	    .set = sd_setcontrast,
	    .get = sd_getcontrast,
	},
#define SD_COLOR 2
	{
	    {
		.id      = V4L2_CID_SATURATION,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Saturation",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
		.default_value = 0x40,
	    },
	    .set = sd_setcolors,
	    .get = sd_getcolors,
	},
#define SD_HUE 3
	{
	    {
		.id      = V4L2_CID_HUE,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Hue",
		.minimum = 0,
		.maximum = 0xff,
		.step    = 1,
		.default_value = 0,
	    },
	    .set = sd_sethue,
	    .get = sd_gethue,
	},
};

static struct v4l2_pix_format vga_mode[] = {
	{160, 120, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 160 * 2,
		.sizeimage = 160 * 120 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 5},
	{176, 144, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 176 * 2,
		.sizeimage = 176 * 144 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 4},
	{320, 240, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 320 * 2,
		.sizeimage = 320 * 240 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 2},
	{352, 288, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 352 * 2,
		.sizeimage = 352 * 288 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 1},
	{640, 480, V4L2_PIX_FMT_YUYV, V4L2_FIELD_NONE,
		.bytesperline = 640 * 2,
		.sizeimage = 640 * 480 * 2,
		.colorspace = V4L2_COLORSPACE_SRGB,
		.priv = 0},
};

#define SPCA50X_OFFSET_DATA 10

#define SAA7113_bright 0x0a	/* defaults 0x80 */
#define SAA7113_contrast 0x0b	/* defaults 0x47 */
#define SAA7113_saturation 0x0c	/* defaults 0x40 */
#define SAA7113_hue 0x0d	/* defaults 0x00 */
#define SAA7113_I2C_BASE_WRITE 0x4a

/* read 'len' bytes to gspca_dev->usb_buf */
static void reg_r(struct gspca_dev *gspca_dev,
		  __u16 req,
		  __u16 index,
		  __u16 length)
{
	usb_control_msg(gspca_dev->dev,
			usb_rcvctrlpipe(gspca_dev->dev, 0),
			req,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			0,		/* value */
			index, gspca_dev->usb_buf, length,
			500);
}

static void reg_w(struct usb_device *dev,
		  __u16 req,
		  __u16 value,
		  __u16 index)
{
	usb_control_msg(dev,
			usb_sndctrlpipe(dev, 0),
			req,
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			value, index,
			NULL, 0, 500);
}

static void spca506_Initi2c(struct gspca_dev *gspca_dev)
{
	reg_w(gspca_dev->dev, 0x07, SAA7113_I2C_BASE_WRITE, 0x0004);
}

static void spca506_WriteI2c(struct gspca_dev *gspca_dev, __u16 valeur,
			     __u16 reg)
{
	int retry = 60;

	reg_w(gspca_dev->dev, 0x07, reg, 0x0001);
	reg_w(gspca_dev->dev, 0x07, valeur, 0x0000);
	while (retry--) {
		reg_r(gspca_dev, 0x07, 0x0003, 2);
		if ((gspca_dev->usb_buf[0] | gspca_dev->usb_buf[1]) == 0x00)
			break;
	}
}

static int spca506_ReadI2c(struct gspca_dev *gspca_dev, __u16 reg)
{
	int retry = 60;

	reg_w(gspca_dev->dev, 0x07, SAA7113_I2C_BASE_WRITE, 0x0004);
	reg_w(gspca_dev->dev, 0x07, reg, 0x0001);
	reg_w(gspca_dev->dev, 0x07, 0x01, 0x0002);
	while (--retry) {
		reg_r(gspca_dev, 0x07, 0x0003, 2);
		if ((gspca_dev->usb_buf[0] | gspca_dev->usb_buf[1]) == 0x00)
			break;
	}
	if (retry == 0)
		return -1;
	reg_r(gspca_dev, 0x07, 0x0000, 1);
	return gspca_dev->usb_buf[0];
}

static void spca506_SetNormeInput(struct gspca_dev *gspca_dev,
				 __u16 norme,
				 __u16 channel)
{
	struct sd *sd = (struct sd *) gspca_dev;
/* fixme: check if channel == 0..3 and 6..9 (8 values) */
	__u8 setbit0 = 0x00;
	__u8 setbit1 = 0x00;
	__u8 videomask = 0x00;

	PDEBUG(D_STREAM, "** Open Set Norme **");
	spca506_Initi2c(gspca_dev);
	/* NTSC bit0 -> 1(525 l) PAL SECAM bit0 -> 0 (625 l) */
	/* Composite channel bit1 -> 1 S-video bit 1 -> 0 */
	/* and exclude SAA7113 reserved channel set default 0 otherwise */
	if (norme & V4L2_STD_NTSC)
		setbit0 = 0x01;
	if (channel == 4 || channel == 5 || channel > 9)
		channel = 0;
	if (channel < 4)
		setbit1 = 0x02;
	videomask = (0x48 | setbit0 | setbit1);
	reg_w(gspca_dev->dev, 0x08, videomask, 0x0000);
	spca506_WriteI2c(gspca_dev, (0xc0 | (channel & 0x0F)), 0x02);

	if (norme & V4L2_STD_NTSC)
		spca506_WriteI2c(gspca_dev, 0x33, 0x0e);
					/* Chrominance Control NTSC N */
	else if (norme & V4L2_STD_SECAM)
		spca506_WriteI2c(gspca_dev, 0x53, 0x0e);
					/* Chrominance Control SECAM */
	else
		spca506_WriteI2c(gspca_dev, 0x03, 0x0e);
					/* Chrominance Control PAL BGHIV */

	sd->norme = norme;
	sd->channel = channel;
	PDEBUG(D_STREAM, "Set Video Byte to 0x%2x", videomask);
	PDEBUG(D_STREAM, "Set Norme: %08x Channel %d", norme, channel);
}

static void spca506_GetNormeInput(struct gspca_dev *gspca_dev,
				  __u16 *norme, __u16 *channel)
{
	struct sd *sd = (struct sd *) gspca_dev;

	/* Read the register is not so good value change so
	   we use your own copy in spca50x struct */
	*norme = sd->norme;
	*channel = sd->channel;
	PDEBUG(D_STREAM, "Get Norme: %d Channel %d", *norme, *channel);
}

static void spca506_Setsize(struct gspca_dev *gspca_dev, __u16 code,
			    __u16 xmult, __u16 ymult)
{
	struct usb_device *dev = gspca_dev->dev;

	PDEBUG(D_STREAM, "** SetSize **");
	reg_w(dev, 0x04, (0x18 | (code & 0x07)), 0x0000);
	/* Soft snap 0x40 Hard 0x41 */
	reg_w(dev, 0x04, 0x41, 0x0001);
	reg_w(dev, 0x04, 0x00, 0x0002);
	/* reserved */
	reg_w(dev, 0x04, 0x00, 0x0003);

	/* reserved */
	reg_w(dev, 0x04, 0x00, 0x0004);
	/* reserved */
	reg_w(dev, 0x04, 0x01, 0x0005);
	/* reserced */
	reg_w(dev, 0x04, xmult, 0x0006);
	/* reserved */
	reg_w(dev, 0x04, ymult, 0x0007);
	/* compression 1 */
	reg_w(dev, 0x04, 0x00, 0x0008);
	/* T=64 -> 2 */
	reg_w(dev, 0x04, 0x00, 0x0009);
	/* threshold2D */
	reg_w(dev, 0x04, 0x21, 0x000a);
	/* quantization */
	reg_w(dev, 0x04, 0x00, 0x000b);
}

/* this function is called at probe time */
static int sd_config(struct gspca_dev *gspca_dev,
			const struct usb_device_id *id)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct cam *cam;

	cam = &gspca_dev->cam;
	cam->dev_name = (char *) id->driver_info;
	cam->epaddr = 0x01;
	cam->cam_mode = vga_mode;
	cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
	sd->brightness = sd_ctrls[SD_BRIGHTNESS].qctrl.default_value;
	sd->contrast = sd_ctrls[SD_CONTRAST].qctrl.default_value;
	sd->colors = sd_ctrls[SD_COLOR].qctrl.default_value;
	sd->hue = sd_ctrls[SD_HUE].qctrl.default_value;
	return 0;
}

/* this function is called at open time */
static int sd_open(struct gspca_dev *gspca_dev)
{
	struct usb_device *dev = gspca_dev->dev;

	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0xFF, 0x0003);
	reg_w(dev, 0x03, 0x00, 0x0000);
	reg_w(dev, 0x03, 0x1c, 0x0001);
	reg_w(dev, 0x03, 0x18, 0x0001);
	/* Init on PAL and composite input0 */
	spca506_SetNormeInput(gspca_dev, 0, 0);
	reg_w(dev, 0x03, 0x1c, 0x0001);
	reg_w(dev, 0x03, 0x18, 0x0001);
	reg_w(dev, 0x05, 0x00, 0x0000);
	reg_w(dev, 0x05, 0xef, 0x0001);
	reg_w(dev, 0x05, 0x00, 0x00c1);
	reg_w(dev, 0x05, 0x00, 0x00c2);
	reg_w(dev, 0x06, 0x18, 0x0002);
	reg_w(dev, 0x06, 0xf5, 0x0011);
	reg_w(dev, 0x06, 0x02, 0x0012);
	reg_w(dev, 0x06, 0xfb, 0x0013);
	reg_w(dev, 0x06, 0x00, 0x0014);
	reg_w(dev, 0x06, 0xa4, 0x0051);
	reg_w(dev, 0x06, 0x40, 0x0052);
	reg_w(dev, 0x06, 0x71, 0x0053);
	reg_w(dev, 0x06, 0x40, 0x0054);
	/************************************************/
	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0x00, 0x0003);
	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0xFF, 0x0003);
	reg_w(dev, 0x02, 0x00, 0x0000);
	reg_w(dev, 0x03, 0x60, 0x0000);
	reg_w(dev, 0x03, 0x18, 0x0001);
	/* for a better reading mx :)	  */
	/*sdca506_WriteI2c(value,register) */
	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, 0x08, 0x01);
	spca506_WriteI2c(gspca_dev, 0xc0, 0x02);
						/* input composite video */
	spca506_WriteI2c(gspca_dev, 0x33, 0x03);
	spca506_WriteI2c(gspca_dev, 0x00, 0x04);
	spca506_WriteI2c(gspca_dev, 0x00, 0x05);
	spca506_WriteI2c(gspca_dev, 0x0d, 0x06);
	spca506_WriteI2c(gspca_dev, 0xf0, 0x07);
	spca506_WriteI2c(gspca_dev, 0x98, 0x08);
	spca506_WriteI2c(gspca_dev, 0x03, 0x09);
	spca506_WriteI2c(gspca_dev, 0x80, 0x0a);
	spca506_WriteI2c(gspca_dev, 0x47, 0x0b);
	spca506_WriteI2c(gspca_dev, 0x48, 0x0c);
	spca506_WriteI2c(gspca_dev, 0x00, 0x0d);
	spca506_WriteI2c(gspca_dev, 0x03, 0x0e);	/* Chroma Pal adjust */
	spca506_WriteI2c(gspca_dev, 0x2a, 0x0f);
	spca506_WriteI2c(gspca_dev, 0x00, 0x10);
	spca506_WriteI2c(gspca_dev, 0x0c, 0x11);
	spca506_WriteI2c(gspca_dev, 0xb8, 0x12);
	spca506_WriteI2c(gspca_dev, 0x01, 0x13);
	spca506_WriteI2c(gspca_dev, 0x00, 0x14);
	spca506_WriteI2c(gspca_dev, 0x00, 0x15);
	spca506_WriteI2c(gspca_dev, 0x00, 0x16);
	spca506_WriteI2c(gspca_dev, 0x00, 0x17);
	spca506_WriteI2c(gspca_dev, 0x00, 0x18);
	spca506_WriteI2c(gspca_dev, 0x00, 0x19);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1a);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1b);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1c);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1d);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1e);
	spca506_WriteI2c(gspca_dev, 0xa1, 0x1f);
	spca506_WriteI2c(gspca_dev, 0x02, 0x40);
	spca506_WriteI2c(gspca_dev, 0xff, 0x41);
	spca506_WriteI2c(gspca_dev, 0xff, 0x42);
	spca506_WriteI2c(gspca_dev, 0xff, 0x43);
	spca506_WriteI2c(gspca_dev, 0xff, 0x44);
	spca506_WriteI2c(gspca_dev, 0xff, 0x45);
	spca506_WriteI2c(gspca_dev, 0xff, 0x46);
	spca506_WriteI2c(gspca_dev, 0xff, 0x47);
	spca506_WriteI2c(gspca_dev, 0xff, 0x48);
	spca506_WriteI2c(gspca_dev, 0xff, 0x49);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4a);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4b);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4c);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4d);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4e);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4f);
	spca506_WriteI2c(gspca_dev, 0xff, 0x50);
	spca506_WriteI2c(gspca_dev, 0xff, 0x51);
	spca506_WriteI2c(gspca_dev, 0xff, 0x52);
	spca506_WriteI2c(gspca_dev, 0xff, 0x53);
	spca506_WriteI2c(gspca_dev, 0xff, 0x54);
	spca506_WriteI2c(gspca_dev, 0xff, 0x55);
	spca506_WriteI2c(gspca_dev, 0xff, 0x56);
	spca506_WriteI2c(gspca_dev, 0xff, 0x57);
	spca506_WriteI2c(gspca_dev, 0x00, 0x58);
	spca506_WriteI2c(gspca_dev, 0x54, 0x59);
	spca506_WriteI2c(gspca_dev, 0x07, 0x5a);
	spca506_WriteI2c(gspca_dev, 0x83, 0x5b);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5c);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5d);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5e);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5f);
	spca506_WriteI2c(gspca_dev, 0x00, 0x60);
	spca506_WriteI2c(gspca_dev, 0x05, 0x61);
	spca506_WriteI2c(gspca_dev, 0x9f, 0x62);
	PDEBUG(D_STREAM, "** Close Init *");
	return 0;
}

static void sd_start(struct gspca_dev *gspca_dev)
{
	struct usb_device *dev = gspca_dev->dev;
	__u16 norme;
	__u16 channel;

	/**************************************/
	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0x00, 0x0003);
	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0xFF, 0x0003);
	reg_w(dev, 0x02, 0x00, 0x0000);
	reg_w(dev, 0x03, 0x60, 0x0000);
	reg_w(dev, 0x03, 0x18, 0x0001);

	/*sdca506_WriteI2c(value,register) */
	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, 0x08, 0x01);	/* Increment Delay */
/*	spca506_WriteI2c(gspca_dev, 0xc0, 0x02); * Analog Input Control 1 */
	spca506_WriteI2c(gspca_dev, 0x33, 0x03);
						/* Analog Input Control 2 */
	spca506_WriteI2c(gspca_dev, 0x00, 0x04);
						/* Analog Input Control 3 */
	spca506_WriteI2c(gspca_dev, 0x00, 0x05);
						/* Analog Input Control 4 */
	spca506_WriteI2c(gspca_dev, 0x0d, 0x06);
					/* Horizontal Sync Start 0xe9-0x0d */
	spca506_WriteI2c(gspca_dev, 0xf0, 0x07);
					/* Horizontal Sync Stop  0x0d-0xf0 */

	spca506_WriteI2c(gspca_dev, 0x98, 0x08);	/* Sync Control */
/*		Defaults value			*/
	spca506_WriteI2c(gspca_dev, 0x03, 0x09);	/* Luminance Control */
	spca506_WriteI2c(gspca_dev, 0x80, 0x0a);
						/* Luminance Brightness */
	spca506_WriteI2c(gspca_dev, 0x47, 0x0b);	/* Luminance Contrast */
	spca506_WriteI2c(gspca_dev, 0x48, 0x0c);
						/* Chrominance Saturation */
	spca506_WriteI2c(gspca_dev, 0x00, 0x0d);
						/* Chrominance Hue Control */
	spca506_WriteI2c(gspca_dev, 0x2a, 0x0f);
						/* Chrominance Gain Control */
	/**************************************/
	spca506_WriteI2c(gspca_dev, 0x00, 0x10);
						/* Format/Delay Control */
	spca506_WriteI2c(gspca_dev, 0x0c, 0x11);	/* Output Control 1 */
	spca506_WriteI2c(gspca_dev, 0xb8, 0x12);	/* Output Control 2 */
	spca506_WriteI2c(gspca_dev, 0x01, 0x13);	/* Output Control 3 */
	spca506_WriteI2c(gspca_dev, 0x00, 0x14);	/* reserved */
	spca506_WriteI2c(gspca_dev, 0x00, 0x15);	/* VGATE START */
	spca506_WriteI2c(gspca_dev, 0x00, 0x16);	/* VGATE STOP */
	spca506_WriteI2c(gspca_dev, 0x00, 0x17);    /* VGATE Control (MSB) */
	spca506_WriteI2c(gspca_dev, 0x00, 0x18);
	spca506_WriteI2c(gspca_dev, 0x00, 0x19);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1a);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1b);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1c);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1d);
	spca506_WriteI2c(gspca_dev, 0x00, 0x1e);
	spca506_WriteI2c(gspca_dev, 0xa1, 0x1f);
	spca506_WriteI2c(gspca_dev, 0x02, 0x40);
	spca506_WriteI2c(gspca_dev, 0xff, 0x41);
	spca506_WriteI2c(gspca_dev, 0xff, 0x42);
	spca506_WriteI2c(gspca_dev, 0xff, 0x43);
	spca506_WriteI2c(gspca_dev, 0xff, 0x44);
	spca506_WriteI2c(gspca_dev, 0xff, 0x45);
	spca506_WriteI2c(gspca_dev, 0xff, 0x46);
	spca506_WriteI2c(gspca_dev, 0xff, 0x47);
	spca506_WriteI2c(gspca_dev, 0xff, 0x48);
	spca506_WriteI2c(gspca_dev, 0xff, 0x49);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4a);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4b);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4c);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4d);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4e);
	spca506_WriteI2c(gspca_dev, 0xff, 0x4f);
	spca506_WriteI2c(gspca_dev, 0xff, 0x50);
	spca506_WriteI2c(gspca_dev, 0xff, 0x51);
	spca506_WriteI2c(gspca_dev, 0xff, 0x52);
	spca506_WriteI2c(gspca_dev, 0xff, 0x53);
	spca506_WriteI2c(gspca_dev, 0xff, 0x54);
	spca506_WriteI2c(gspca_dev, 0xff, 0x55);
	spca506_WriteI2c(gspca_dev, 0xff, 0x56);
	spca506_WriteI2c(gspca_dev, 0xff, 0x57);
	spca506_WriteI2c(gspca_dev, 0x00, 0x58);
	spca506_WriteI2c(gspca_dev, 0x54, 0x59);
	spca506_WriteI2c(gspca_dev, 0x07, 0x5a);
	spca506_WriteI2c(gspca_dev, 0x83, 0x5b);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5c);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5d);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5e);
	spca506_WriteI2c(gspca_dev, 0x00, 0x5f);
	spca506_WriteI2c(gspca_dev, 0x00, 0x60);
	spca506_WriteI2c(gspca_dev, 0x05, 0x61);
	spca506_WriteI2c(gspca_dev, 0x9f, 0x62);
	/**************************************/
	reg_w(dev, 0x05, 0x00, 0x0003);
	reg_w(dev, 0x05, 0x00, 0x0004);
	reg_w(dev, 0x03, 0x10, 0x0001);
	reg_w(dev, 0x03, 0x78, 0x0000);
	switch (gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv) {
	case 0:
		spca506_Setsize(gspca_dev, 0, 0x10, 0x10);
		break;
	case 1:
		spca506_Setsize(gspca_dev, 1, 0x1a, 0x1a);
		break;
	case 2:
		spca506_Setsize(gspca_dev, 2, 0x1c, 0x1c);
		break;
	case 4:
		spca506_Setsize(gspca_dev, 4, 0x34, 0x34);
		break;
	default:
/*	case 5: */
		spca506_Setsize(gspca_dev, 5, 0x40, 0x40);
		break;
	}

	/* compress setting and size */
	/* set i2c luma */
	reg_w(dev, 0x02, 0x01, 0x0000);
	reg_w(dev, 0x03, 0x12, 0x0000);
	reg_r(gspca_dev, 0x04, 0x0001, 2);
	PDEBUG(D_STREAM, "webcam started");
	spca506_GetNormeInput(gspca_dev, &norme, &channel);
	spca506_SetNormeInput(gspca_dev, norme, channel);
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
	struct usb_device *dev = gspca_dev->dev;

	reg_w(dev, 0x02, 0x00, 0x0000);
	reg_w(dev, 0x03, 0x00, 0x0004);
	reg_w(dev, 0x03, 0x00, 0x0003);
}

static void sd_stop0(struct gspca_dev *gspca_dev)
{
}

static void sd_close(struct gspca_dev *gspca_dev)
{
}

/* convert YYUV per line to YUYV (YUV 4:2:2) */
static void yyuv_decode(unsigned char *out,
			unsigned char *in,
			int width,
			int height)
{
	unsigned char *Ui, *Vi, *yi, *yi1;
	unsigned char *out1;
	int i, j;

	yi = in;
	for (i = height / 2; --i >= 0; ) {
		out1 = out + width * 2;		/* next line */
		yi1 = yi + width;
		Ui = yi1 + width;
		Vi = Ui + width / 2;
		for (j = width / 2; --j >= 0; ) {
			*out++ = 128 + *yi++;
			*out++ = 128 + *Ui;
			*out++ = 128 + *yi++;
			*out++ = 128 + *Vi;

			*out1++ = 128 + *yi1++;
			*out1++ = 128 + *Ui++;
			*out1++ = 128 + *yi1++;
			*out1++ = 128 + *Vi++;
		}
		yi += width * 2;
		out = out1;
	}
}

static void sd_pkt_scan(struct gspca_dev *gspca_dev,
			struct gspca_frame *frame,	/* target */
			__u8 *data,			/* isoc packet */
			int len)			/* iso packet length */
{
	struct sd *sd = (struct sd *) gspca_dev;

	switch (data[0]) {
	case 0:				/* start of frame */
		if (gspca_dev->last_packet_type == FIRST_PACKET) {
			yyuv_decode(sd->tmpbuf2, sd->tmpbuf,
					gspca_dev->width,
					gspca_dev->height);
			frame = gspca_frame_add(gspca_dev,
						LAST_PACKET,
						frame,
						sd->tmpbuf2,
						gspca_dev->width
							* gspca_dev->height
							* 2);
		}
		gspca_frame_add(gspca_dev, FIRST_PACKET, frame,
				data, 0);
		data += SPCA50X_OFFSET_DATA;
		len -= SPCA50X_OFFSET_DATA;
		if (len > 0)
			memcpy(sd->tmpbuf, data, len);
		else
			len = 0;
		sd->buflen = len;
		return;
	case 0xff:			/* drop */
/*		gspca_dev->last_packet_type = DISCARD_PACKET; */
		return;
	}
	data += 1;
	len -= 1;
	memcpy(&sd->tmpbuf[sd->buflen], data, len);
	sd->buflen += len;
}

static void setbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, sd->brightness, SAA7113_bright);
	spca506_WriteI2c(gspca_dev, 0x01, 0x09);
}

static void getbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->brightness = spca506_ReadI2c(gspca_dev, SAA7113_bright);
}

static void setcontrast(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, sd->contrast, SAA7113_contrast);
	spca506_WriteI2c(gspca_dev, 0x01, 0x09);
}

static void getcontrast(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->contrast = spca506_ReadI2c(gspca_dev, SAA7113_contrast);
}

static void setcolors(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, sd->colors, SAA7113_saturation);
	spca506_WriteI2c(gspca_dev, 0x01, 0x09);
}

static void getcolors(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->colors = spca506_ReadI2c(gspca_dev, SAA7113_saturation);
}

static void sethue(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	spca506_Initi2c(gspca_dev);
	spca506_WriteI2c(gspca_dev, sd->hue, SAA7113_hue);
	spca506_WriteI2c(gspca_dev, 0x01, 0x09);
}

static void gethue(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->hue = spca506_ReadI2c(gspca_dev, SAA7113_hue);
}

static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->brightness = val;
	if (gspca_dev->streaming)
		setbrightness(gspca_dev);
	return 0;
}

static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	getbrightness(gspca_dev);
	*val = sd->brightness;
	return 0;
}

static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->contrast = val;
	if (gspca_dev->streaming)
		setcontrast(gspca_dev);
	return 0;
}

static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	getcontrast(gspca_dev);
	*val = sd->contrast;
	return 0;
}

static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->colors = val;
	if (gspca_dev->streaming)
		setcolors(gspca_dev);
	return 0;
}

static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	getcolors(gspca_dev);
	*val = sd->colors;
	return 0;
}

static int sd_sethue(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->hue = val;
	if (gspca_dev->streaming)
		sethue(gspca_dev);
	return 0;
}

static int sd_gethue(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	gethue(gspca_dev);
	*val = sd->hue;
	return 0;
}

/* sub-driver description */
static struct sd_desc sd_desc = {
	.name = MODULE_NAME,
	.ctrls = sd_ctrls,
	.nctrls = ARRAY_SIZE(sd_ctrls),
	.config = sd_config,
	.open = sd_open,
	.start = sd_start,
	.stopN = sd_stopN,
	.stop0 = sd_stop0,
	.close = sd_close,
	.pkt_scan = sd_pkt_scan,
};

/* -- module initialisation -- */
#define DVNM(name) .driver_info = (kernel_ulong_t) name
static __devinitdata struct usb_device_id device_table[] = {
	{USB_DEVICE(0x06e1, 0xa190), DVNM("ADS Instant VCD")},
/*	{USB_DEVICE(0x0733, 0x0430), DVNM("UsbGrabber PV321c")}, */
	{USB_DEVICE(0x0734, 0x043b), DVNM("3DeMon USB Capture aka")},
	{USB_DEVICE(0x99fa, 0x8988), DVNM("Grandtec V.cap")},
	{}
};
MODULE_DEVICE_TABLE(usb, device_table);

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
				THIS_MODULE);
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
	if (usb_register(&sd_driver) < 0)
		return -1;
	PDEBUG(D_PROBE, "v%s registered", version);
	return 0;
}
static void __exit sd_mod_exit(void)
{
	usb_deregister(&sd_driver);
	PDEBUG(D_PROBE, "deregistered");
}

module_init(sd_mod_init);
module_exit(sd_mod_exit);