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#include <linux/module.h>
39#include <linux/init.h>
40#include <linux/vmalloc.h>
41#include <linux/slab.h>
42#include <linux/ctype.h>
43#include <linux/pagemap.h>
44#include <asm/processor.h>
45#include <linux/mm.h>
46#include <linux/device.h>
47
48#if defined (__i386__)
49 #include <asm/cpufeature.h>
50#endif
51
52#include "ov511.h"
53
54
55
56
57#define DRIVER_VERSION "v1.64 for Linux 2.5"
58#define EMAIL "mark@alpha.dyndns.org"
59#define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
60 & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
61 <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
62#define DRIVER_DESC "ov511 USB Camera Driver"
63
64#define OV511_I2C_RETRIES 3
65#define ENABLE_Y_QUANTABLE 1
66#define ENABLE_UV_QUANTABLE 1
67
68#define OV511_MAX_UNIT_VIDEO 16
69
70
71#define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
72
73#define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
74
75
76#define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
77
78#define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
79
80
81
82
83
84
85
86static int autobright = 1;
87static int autogain = 1;
88static int autoexp = 1;
89static int debug;
90static int snapshot;
91static int cams = 1;
92static int compress;
93static int testpat;
94static int dumppix;
95static int led = 1;
96static int dump_bridge;
97static int dump_sensor;
98static int printph;
99static int phy = 0x1f;
100static int phuv = 0x05;
101static int pvy = 0x06;
102static int pvuv = 0x06;
103static int qhy = 0x14;
104static int qhuv = 0x03;
105static int qvy = 0x04;
106static int qvuv = 0x04;
107static int lightfreq;
108static int bandingfilter;
109static int clockdiv = -1;
110static int packetsize = -1;
111static int framedrop = -1;
112static int fastset;
113static int force_palette;
114static int backlight;
115
116static unsigned long ov511_devused;
117static int unit_video[OV511_MAX_UNIT_VIDEO];
118static int remove_zeros;
119static int mirror;
120static int ov518_color;
121
122module_param(autobright, int, 0);
123MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
124module_param(autogain, int, 0);
125MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
126module_param(autoexp, int, 0);
127MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
128module_param(debug, int, 0);
129MODULE_PARM_DESC(debug,
130 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
131module_param(snapshot, int, 0);
132MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
133module_param(cams, int, 0);
134MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
135module_param(compress, int, 0);
136MODULE_PARM_DESC(compress, "Turn on compression");
137module_param(testpat, int, 0);
138MODULE_PARM_DESC(testpat,
139 "Replace image with vertical bar testpattern (only partially working)");
140module_param(dumppix, int, 0);
141MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
142module_param(led, int, 0);
143MODULE_PARM_DESC(led,
144 "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
145module_param(dump_bridge, int, 0);
146MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
147module_param(dump_sensor, int, 0);
148MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
149module_param(printph, int, 0);
150MODULE_PARM_DESC(printph, "Print frame start/end headers");
151module_param(phy, int, 0);
152MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
153module_param(phuv, int, 0);
154MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
155module_param(pvy, int, 0);
156MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
157module_param(pvuv, int, 0);
158MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
159module_param(qhy, int, 0);
160MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
161module_param(qhuv, int, 0);
162MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
163module_param(qvy, int, 0);
164MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
165module_param(qvuv, int, 0);
166MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
167module_param(lightfreq, int, 0);
168MODULE_PARM_DESC(lightfreq,
169 "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
170module_param(bandingfilter, int, 0);
171MODULE_PARM_DESC(bandingfilter,
172 "Enable banding filter (to reduce effects of fluorescent lighting)");
173module_param(clockdiv, int, 0);
174MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
175module_param(packetsize, int, 0);
176MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
177module_param(framedrop, int, 0);
178MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
179module_param(fastset, int, 0);
180MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
181module_param(force_palette, int, 0);
182MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
183module_param(backlight, int, 0);
184MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
185static unsigned int num_uv;
186module_param_array(unit_video, int, &num_uv, 0);
187MODULE_PARM_DESC(unit_video,
188 "Force use of specific minor number(s). 0 is not allowed.");
189module_param(remove_zeros, int, 0);
190MODULE_PARM_DESC(remove_zeros,
191 "Remove zero-padding from uncompressed incoming data");
192module_param(mirror, int, 0);
193MODULE_PARM_DESC(mirror, "Reverse image horizontally");
194module_param(ov518_color, int, 0);
195MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
196
197MODULE_AUTHOR(DRIVER_AUTHOR);
198MODULE_DESCRIPTION(DRIVER_DESC);
199MODULE_LICENSE("GPL");
200
201
202
203
204
205static struct usb_driver ov511_driver;
206
207
208
209static const int i2c_detect_tries = 5;
210
211static struct usb_device_id device_table [] = {
212 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
213 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
214 { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
215 { }
216};
217
218MODULE_DEVICE_TABLE (usb, device_table);
219
220static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
221static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
222static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
223static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
224
225
226
227
228
229
230static struct symbolic_list camlist[] = {
231 { 0, "Generic Camera (no ID)" },
232 { 1, "Mustek WCam 3X" },
233 { 3, "D-Link DSB-C300" },
234 { 4, "Generic OV511/OV7610" },
235 { 5, "Puretek PT-6007" },
236 { 6, "Lifeview USB Life TV (NTSC)" },
237 { 21, "Creative Labs WebCam 3" },
238 { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
239 { 36, "Koala-Cam" },
240 { 38, "Lifeview USB Life TV (PAL)" },
241 { 41, "Samsung Anycam MPC-M10" },
242 { 43, "Mtekvision Zeca MV402" },
243 { 46, "Suma eON" },
244 { 70, "Lifeview USB Life TV (PAL/SECAM)" },
245 { 100, "Lifeview RoboCam" },
246 { 102, "AverMedia InterCam Elite" },
247 { 112, "MediaForte MV300" },
248 { 134, "Ezonics EZCam II" },
249 { 192, "Webeye 2000B" },
250 { 253, "Alpha Vision Tech. AlphaCam SE" },
251 { -1, NULL }
252};
253
254
255static struct symbolic_list v4l1_plist[] = {
256 { VIDEO_PALETTE_GREY, "GREY" },
257 { VIDEO_PALETTE_HI240, "HI240" },
258 { VIDEO_PALETTE_RGB565, "RGB565" },
259 { VIDEO_PALETTE_RGB24, "RGB24" },
260 { VIDEO_PALETTE_RGB32, "RGB32" },
261 { VIDEO_PALETTE_RGB555, "RGB555" },
262 { VIDEO_PALETTE_YUV422, "YUV422" },
263 { VIDEO_PALETTE_YUYV, "YUYV" },
264 { VIDEO_PALETTE_UYVY, "UYVY" },
265 { VIDEO_PALETTE_YUV420, "YUV420" },
266 { VIDEO_PALETTE_YUV411, "YUV411" },
267 { VIDEO_PALETTE_RAW, "RAW" },
268 { VIDEO_PALETTE_YUV422P,"YUV422P" },
269 { VIDEO_PALETTE_YUV411P,"YUV411P" },
270 { VIDEO_PALETTE_YUV420P,"YUV420P" },
271 { VIDEO_PALETTE_YUV410P,"YUV410P" },
272 { -1, NULL }
273};
274
275static struct symbolic_list brglist[] = {
276 { BRG_OV511, "OV511" },
277 { BRG_OV511PLUS, "OV511+" },
278 { BRG_OV518, "OV518" },
279 { BRG_OV518PLUS, "OV518+" },
280 { -1, NULL }
281};
282
283static struct symbolic_list senlist[] = {
284 { SEN_OV76BE, "OV76BE" },
285 { SEN_OV7610, "OV7610" },
286 { SEN_OV7620, "OV7620" },
287 { SEN_OV7620AE, "OV7620AE" },
288 { SEN_OV6620, "OV6620" },
289 { SEN_OV6630, "OV6630" },
290 { SEN_OV6630AE, "OV6630AE" },
291 { SEN_OV6630AF, "OV6630AF" },
292 { SEN_OV8600, "OV8600" },
293 { SEN_KS0127, "KS0127" },
294 { SEN_KS0127B, "KS0127B" },
295 { SEN_SAA7111A, "SAA7111A" },
296 { -1, NULL }
297};
298
299
300static struct symbolic_list urb_errlist[] = {
301 { -ENOSR, "Buffer error (overrun)" },
302 { -EPIPE, "Stalled (device not responding)" },
303 { -EOVERFLOW, "Babble (device sends too much data)" },
304 { -EPROTO, "Bit-stuff error (bad cable?)" },
305 { -EILSEQ, "CRC/Timeout (bad cable?)" },
306 { -ETIME, "Device does not respond to token" },
307 { -ETIMEDOUT, "Device does not respond to command" },
308 { -1, NULL }
309};
310
311
312
313
314static void *
315rvmalloc(unsigned long size)
316{
317 void *mem;
318 unsigned long adr;
319
320 size = PAGE_ALIGN(size);
321 mem = vmalloc_32(size);
322 if (!mem)
323 return NULL;
324
325 memset(mem, 0, size);
326 adr = (unsigned long) mem;
327 while (size > 0) {
328 SetPageReserved(vmalloc_to_page((void *)adr));
329 adr += PAGE_SIZE;
330 size -= PAGE_SIZE;
331 }
332
333 return mem;
334}
335
336static void
337rvfree(void *mem, unsigned long size)
338{
339 unsigned long adr;
340
341 if (!mem)
342 return;
343
344 adr = (unsigned long) mem;
345 while ((long) size > 0) {
346 ClearPageReserved(vmalloc_to_page((void *)adr));
347 adr += PAGE_SIZE;
348 size -= PAGE_SIZE;
349 }
350 vfree(mem);
351}
352
353
354
355
356
357
358
359
360static int
361reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
362{
363 int rc;
364
365 PDEBUG(5, "0x%02X:0x%02X", reg, value);
366
367 mutex_lock(&ov->cbuf_lock);
368 ov->cbuf[0] = value;
369 rc = usb_control_msg(ov->dev,
370 usb_sndctrlpipe(ov->dev, 0),
371 (ov->bclass == BCL_OV518)?1:2 ,
372 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
373 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
374 mutex_unlock(&ov->cbuf_lock);
375
376 if (rc < 0)
377 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
378
379 return rc;
380}
381
382
383
384static int
385reg_r(struct usb_ov511 *ov, unsigned char reg)
386{
387 int rc;
388
389 mutex_lock(&ov->cbuf_lock);
390 rc = usb_control_msg(ov->dev,
391 usb_rcvctrlpipe(ov->dev, 0),
392 (ov->bclass == BCL_OV518)?1:3 ,
393 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
394 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
395
396 if (rc < 0) {
397 err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
398 } else {
399 rc = ov->cbuf[0];
400 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
401 }
402
403 mutex_unlock(&ov->cbuf_lock);
404
405 return rc;
406}
407
408
409
410
411
412
413
414static int
415reg_w_mask(struct usb_ov511 *ov,
416 unsigned char reg,
417 unsigned char value,
418 unsigned char mask)
419{
420 int ret;
421 unsigned char oldval, newval;
422
423 ret = reg_r(ov, reg);
424 if (ret < 0)
425 return ret;
426
427 oldval = (unsigned char) ret;
428 oldval &= (~mask);
429 value &= mask;
430 newval = oldval | value;
431
432 return (reg_w(ov, reg, newval));
433}
434
435
436
437
438
439static int
440ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
441{
442 int rc;
443
444 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
445
446 mutex_lock(&ov->cbuf_lock);
447
448 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
449
450 rc = usb_control_msg(ov->dev,
451 usb_sndctrlpipe(ov->dev, 0),
452 1 ,
453 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454 0, (__u16)reg, ov->cbuf, n, 1000);
455 mutex_unlock(&ov->cbuf_lock);
456
457 if (rc < 0)
458 err("reg write multiple: error %d: %s", rc,
459 symbolic(urb_errlist, rc));
460
461 return rc;
462}
463
464static int
465ov511_upload_quan_tables(struct usb_ov511 *ov)
466{
467 unsigned char *pYTable = yQuanTable511;
468 unsigned char *pUVTable = uvQuanTable511;
469 unsigned char val0, val1;
470 int i, rc, reg = R511_COMP_LUT_BEGIN;
471
472 PDEBUG(4, "Uploading quantization tables");
473
474 for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
475 if (ENABLE_Y_QUANTABLE) {
476 val0 = *pYTable++;
477 val1 = *pYTable++;
478 val0 &= 0x0f;
479 val1 &= 0x0f;
480 val0 |= val1 << 4;
481 rc = reg_w(ov, reg, val0);
482 if (rc < 0)
483 return rc;
484 }
485
486 if (ENABLE_UV_QUANTABLE) {
487 val0 = *pUVTable++;
488 val1 = *pUVTable++;
489 val0 &= 0x0f;
490 val1 &= 0x0f;
491 val0 |= val1 << 4;
492 rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
493 if (rc < 0)
494 return rc;
495 }
496
497 reg++;
498 }
499
500 return 0;
501}
502
503
504static int
505ov518_upload_quan_tables(struct usb_ov511 *ov)
506{
507 unsigned char *pYTable = yQuanTable518;
508 unsigned char *pUVTable = uvQuanTable518;
509 unsigned char val0, val1;
510 int i, rc, reg = R511_COMP_LUT_BEGIN;
511
512 PDEBUG(4, "Uploading quantization tables");
513
514 for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
515 if (ENABLE_Y_QUANTABLE) {
516 val0 = *pYTable++;
517 val1 = *pYTable++;
518 val0 &= 0x0f;
519 val1 &= 0x0f;
520 val0 |= val1 << 4;
521 rc = reg_w(ov, reg, val0);
522 if (rc < 0)
523 return rc;
524 }
525
526 if (ENABLE_UV_QUANTABLE) {
527 val0 = *pUVTable++;
528 val1 = *pUVTable++;
529 val0 &= 0x0f;
530 val1 &= 0x0f;
531 val0 |= val1 << 4;
532 rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
533 if (rc < 0)
534 return rc;
535 }
536
537 reg++;
538 }
539
540 return 0;
541}
542
543static int
544ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
545{
546 int rc;
547
548
549 if (ov->bclass == BCL_OV518)
550 reset_type &= 0xfe;
551
552 PDEBUG(4, "Reset: type=0x%02X", reset_type);
553
554 rc = reg_w(ov, R51x_SYS_RESET, reset_type);
555 rc = reg_w(ov, R51x_SYS_RESET, 0);
556
557 if (rc < 0)
558 err("reset: command failed");
559
560 return rc;
561}
562
563
564
565
566
567
568
569
570
571
572
573
574static int
575ov518_i2c_write_internal(struct usb_ov511 *ov,
576 unsigned char reg,
577 unsigned char value)
578{
579 int rc;
580
581 PDEBUG(5, "0x%02X:0x%02X", reg, value);
582
583
584 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
585 if (rc < 0)
586 return rc;
587
588
589 rc = reg_w(ov, R51x_I2C_DATA, value);
590 if (rc < 0)
591 return rc;
592
593
594 rc = reg_w(ov, R518_I2C_CTL, 0x01);
595 if (rc < 0)
596 return rc;
597
598 return 0;
599}
600
601
602static int
603ov511_i2c_write_internal(struct usb_ov511 *ov,
604 unsigned char reg,
605 unsigned char value)
606{
607 int rc, retries;
608
609 PDEBUG(5, "0x%02X:0x%02X", reg, value);
610
611
612 for (retries = OV511_I2C_RETRIES; ; ) {
613
614 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
615 if (rc < 0)
616 break;
617
618
619 rc = reg_w(ov, R51x_I2C_DATA, value);
620 if (rc < 0)
621 break;
622
623
624 rc = reg_w(ov, R511_I2C_CTL, 0x01);
625 if (rc < 0)
626 break;
627
628
629 do {
630 rc = reg_r(ov, R511_I2C_CTL);
631 } while (rc > 0 && ((rc&1) == 0));
632 if (rc < 0)
633 break;
634
635
636 if ((rc&2) == 0) {
637 rc = 0;
638 break;
639 }
640#if 0
641
642 reg_w(ov, R511_I2C_CTL, 0x10);
643#endif
644 if (--retries < 0) {
645 err("i2c write retries exhausted");
646 rc = -1;
647 break;
648 }
649 }
650
651 return rc;
652}
653
654
655
656
657
658
659static int
660ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
661{
662 int rc, value;
663
664
665 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
666 if (rc < 0)
667 return rc;
668
669
670 rc = reg_w(ov, R518_I2C_CTL, 0x03);
671 if (rc < 0)
672 return rc;
673
674
675 rc = reg_w(ov, R518_I2C_CTL, 0x05);
676 if (rc < 0)
677 return rc;
678
679 value = reg_r(ov, R51x_I2C_DATA);
680
681 PDEBUG(5, "0x%02X:0x%02X", reg, value);
682
683 return value;
684}
685
686
687
688static int
689ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
690{
691 int rc, value, retries;
692
693
694 for (retries = OV511_I2C_RETRIES; ; ) {
695
696 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
697 if (rc < 0)
698 return rc;
699
700
701 rc = reg_w(ov, R511_I2C_CTL, 0x03);
702 if (rc < 0)
703 return rc;
704
705
706 do {
707 rc = reg_r(ov, R511_I2C_CTL);
708 } while (rc > 0 && ((rc & 1) == 0));
709 if (rc < 0)
710 return rc;
711
712 if ((rc&2) == 0)
713 break;
714
715
716 reg_w(ov, R511_I2C_CTL, 0x10);
717
718 if (--retries < 0) {
719 err("i2c write retries exhausted");
720 return -1;
721 }
722 }
723
724
725 for (retries = OV511_I2C_RETRIES; ; ) {
726
727 rc = reg_w(ov, R511_I2C_CTL, 0x05);
728 if (rc < 0)
729 return rc;
730
731
732 do {
733 rc = reg_r(ov, R511_I2C_CTL);
734 } while (rc > 0 && ((rc&1) == 0));
735 if (rc < 0)
736 return rc;
737
738 if ((rc&2) == 0)
739 break;
740
741
742 rc = reg_w(ov, R511_I2C_CTL, 0x10);
743 if (rc < 0)
744 return rc;
745
746 if (--retries < 0) {
747 err("i2c read retries exhausted");
748 return -1;
749 }
750 }
751
752 value = reg_r(ov, R51x_I2C_DATA);
753
754 PDEBUG(5, "0x%02X:0x%02X", reg, value);
755
756
757 rc = reg_w(ov, R511_I2C_CTL, 0x05);
758 if (rc < 0)
759 return rc;
760
761 return value;
762}
763
764
765static int
766i2c_r(struct usb_ov511 *ov, unsigned char reg)
767{
768 int rc;
769
770 mutex_lock(&ov->i2c_lock);
771
772 if (ov->bclass == BCL_OV518)
773 rc = ov518_i2c_read_internal(ov, reg);
774 else
775 rc = ov511_i2c_read_internal(ov, reg);
776
777 mutex_unlock(&ov->i2c_lock);
778
779 return rc;
780}
781
782static int
783i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
784{
785 int rc;
786
787 mutex_lock(&ov->i2c_lock);
788
789 if (ov->bclass == BCL_OV518)
790 rc = ov518_i2c_write_internal(ov, reg, value);
791 else
792 rc = ov511_i2c_write_internal(ov, reg, value);
793
794 mutex_unlock(&ov->i2c_lock);
795
796 return rc;
797}
798
799
800static int
801ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
802 unsigned char reg,
803 unsigned char value,
804 unsigned char mask)
805{
806 int rc;
807 unsigned char oldval, newval;
808
809 if (mask == 0xff) {
810 newval = value;
811 } else {
812 if (ov->bclass == BCL_OV518)
813 rc = ov518_i2c_read_internal(ov, reg);
814 else
815 rc = ov511_i2c_read_internal(ov, reg);
816 if (rc < 0)
817 return rc;
818
819 oldval = (unsigned char) rc;
820 oldval &= (~mask);
821 value &= mask;
822 newval = oldval | value;
823 }
824
825 if (ov->bclass == BCL_OV518)
826 return (ov518_i2c_write_internal(ov, reg, newval));
827 else
828 return (ov511_i2c_write_internal(ov, reg, newval));
829}
830
831
832
833
834
835
836static int
837i2c_w_mask(struct usb_ov511 *ov,
838 unsigned char reg,
839 unsigned char value,
840 unsigned char mask)
841{
842 int rc;
843
844 mutex_lock(&ov->i2c_lock);
845 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
846 mutex_unlock(&ov->i2c_lock);
847
848 return rc;
849}
850
851
852
853
854
855
856static int
857i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
858{
859 int rc;
860
861 rc = reg_w(ov, R51x_I2C_W_SID, slave);
862 if (rc < 0)
863 return rc;
864
865 rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
866 if (rc < 0)
867 return rc;
868
869 return 0;
870}
871
872
873static int
874i2c_w_slave(struct usb_ov511 *ov,
875 unsigned char slave,
876 unsigned char reg,
877 unsigned char value,
878 unsigned char mask)
879{
880 int rc = 0;
881
882 mutex_lock(&ov->i2c_lock);
883
884
885 rc = i2c_set_slave_internal(ov, slave);
886 if (rc < 0)
887 goto out;
888
889 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
890
891out:
892
893 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
894 err("Couldn't restore primary I2C slave");
895
896 mutex_unlock(&ov->i2c_lock);
897 return rc;
898}
899
900
901static int
902i2c_r_slave(struct usb_ov511 *ov,
903 unsigned char slave,
904 unsigned char reg)
905{
906 int rc;
907
908 mutex_lock(&ov->i2c_lock);
909
910
911 rc = i2c_set_slave_internal(ov, slave);
912 if (rc < 0)
913 goto out;
914
915 if (ov->bclass == BCL_OV518)
916 rc = ov518_i2c_read_internal(ov, reg);
917 else
918 rc = ov511_i2c_read_internal(ov, reg);
919
920out:
921
922 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
923 err("Couldn't restore primary I2C slave");
924
925 mutex_unlock(&ov->i2c_lock);
926 return rc;
927}
928
929
930static int
931ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
932{
933 int rc;
934
935 mutex_lock(&ov->i2c_lock);
936
937 rc = i2c_set_slave_internal(ov, sid);
938 if (rc < 0)
939 goto out;
940
941
942 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
943out:
944 mutex_unlock(&ov->i2c_lock);
945 return rc;
946}
947
948static int
949write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
950{
951 int rc;
952
953 while (pRegvals->bus != OV511_DONE_BUS) {
954 if (pRegvals->bus == OV511_REG_BUS) {
955 if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
956 return rc;
957 } else if (pRegvals->bus == OV511_I2C_BUS) {
958 if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
959 return rc;
960 } else {
961 err("Bad regval array");
962 return -1;
963 }
964 pRegvals++;
965 }
966 return 0;
967}
968
969#ifdef OV511_DEBUG
970static void
971dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
972{
973 int i, rc;
974
975 for (i = reg1; i <= regn; i++) {
976 rc = i2c_r(ov, i);
977 dev_info(&ov->dev->dev, "Sensor[0x%02X] = 0x%02X\n", i, rc);
978 }
979}
980
981static void
982dump_i2c_regs(struct usb_ov511 *ov)
983{
984 dev_info(&ov->dev->dev, "I2C REGS\n");
985 dump_i2c_range(ov, 0x00, 0x7C);
986}
987
988static void
989dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
990{
991 int i, rc;
992
993 for (i = reg1; i <= regn; i++) {
994 rc = reg_r(ov, i);
995 dev_info(&ov->dev->dev, "OV511[0x%02X] = 0x%02X\n", i, rc);
996 }
997}
998
999static void
1000ov511_dump_regs(struct usb_ov511 *ov)
1001{
1002 dev_info(&ov->dev->dev, "CAMERA INTERFACE REGS\n");
1003 dump_reg_range(ov, 0x10, 0x1f);
1004 dev_info(&ov->dev->dev, "DRAM INTERFACE REGS\n");
1005 dump_reg_range(ov, 0x20, 0x23);
1006 dev_info(&ov->dev->dev, "ISO FIFO REGS\n");
1007 dump_reg_range(ov, 0x30, 0x31);
1008 dev_info(&ov->dev->dev, "PIO REGS\n");
1009 dump_reg_range(ov, 0x38, 0x39);
1010 dump_reg_range(ov, 0x3e, 0x3e);
1011 dev_info(&ov->dev->dev, "I2C REGS\n");
1012 dump_reg_range(ov, 0x40, 0x49);
1013 dev_info(&ov->dev->dev, "SYSTEM CONTROL REGS\n");
1014 dump_reg_range(ov, 0x50, 0x55);
1015 dump_reg_range(ov, 0x5e, 0x5f);
1016 dev_info(&ov->dev->dev, "OmniCE REGS\n");
1017 dump_reg_range(ov, 0x70, 0x79);
1018
1019
1020 dump_reg_range(ov, 0x80, 0x9f);
1021 dump_reg_range(ov, 0xa0, 0xbf);
1022
1023}
1024
1025static void
1026ov518_dump_regs(struct usb_ov511 *ov)
1027{
1028 dev_info(&ov->dev->dev, "VIDEO MODE REGS\n");
1029 dump_reg_range(ov, 0x20, 0x2f);
1030 dev_info(&ov->dev->dev, "DATA PUMP AND SNAPSHOT REGS\n");
1031 dump_reg_range(ov, 0x30, 0x3f);
1032 dev_info(&ov->dev->dev, "I2C REGS\n");
1033 dump_reg_range(ov, 0x40, 0x4f);
1034 dev_info(&ov->dev->dev, "SYSTEM CONTROL AND VENDOR REGS\n");
1035 dump_reg_range(ov, 0x50, 0x5f);
1036 dev_info(&ov->dev->dev, "60 - 6F\n");
1037 dump_reg_range(ov, 0x60, 0x6f);
1038 dev_info(&ov->dev->dev, "70 - 7F\n");
1039 dump_reg_range(ov, 0x70, 0x7f);
1040 dev_info(&ov->dev->dev, "Y QUANTIZATION TABLE\n");
1041 dump_reg_range(ov, 0x80, 0x8f);
1042 dev_info(&ov->dev->dev, "UV QUANTIZATION TABLE\n");
1043 dump_reg_range(ov, 0x90, 0x9f);
1044 dev_info(&ov->dev->dev, "A0 - BF\n");
1045 dump_reg_range(ov, 0xa0, 0xbf);
1046 dev_info(&ov->dev->dev, "CBR\n");
1047 dump_reg_range(ov, 0xc0, 0xcf);
1048}
1049#endif
1050
1051
1052
1053
1054
1055static inline int
1056ov51x_stop(struct usb_ov511 *ov)
1057{
1058 PDEBUG(4, "stopping");
1059 ov->stopped = 1;
1060 if (ov->bclass == BCL_OV518)
1061 return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
1062 else
1063 return (reg_w(ov, R51x_SYS_RESET, 0x3d));
1064}
1065
1066
1067
1068static inline int
1069ov51x_restart(struct usb_ov511 *ov)
1070{
1071 if (ov->stopped) {
1072 PDEBUG(4, "restarting");
1073 ov->stopped = 0;
1074
1075
1076 if (ov->bclass == BCL_OV518)
1077 reg_w(ov, 0x2f, 0x80);
1078
1079 return (reg_w(ov, R51x_SYS_RESET, 0x00));
1080 }
1081
1082 return 0;
1083}
1084
1085
1086static int
1087ov51x_wait_frames_inactive(struct usb_ov511 *ov)
1088{
1089 return wait_event_interruptible(ov->wq, ov->curframe < 0);
1090}
1091
1092
1093static void
1094ov51x_clear_snapshot(struct usb_ov511 *ov)
1095{
1096 if (ov->bclass == BCL_OV511) {
1097 reg_w(ov, R51x_SYS_SNAP, 0x00);
1098 reg_w(ov, R51x_SYS_SNAP, 0x02);
1099 reg_w(ov, R51x_SYS_SNAP, 0x00);
1100 } else if (ov->bclass == BCL_OV518) {
1101 dev_warn(&ov->dev->dev,
1102 "snapshot reset not supported yet on OV518(+)\n");
1103 } else {
1104 dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
1105 }
1106}
1107
1108#if 0
1109
1110
1111static int
1112ov51x_check_snapshot(struct usb_ov511 *ov)
1113{
1114 int ret, status = 0;
1115
1116 if (ov->bclass == BCL_OV511) {
1117 ret = reg_r(ov, R51x_SYS_SNAP);
1118 if (ret < 0) {
1119 dev_err(&ov->dev->dev,
1120 "Error checking snspshot status (%d)\n", ret);
1121 } else if (ret & 0x08) {
1122 status = 1;
1123 }
1124 } else if (ov->bclass == BCL_OV518) {
1125 dev_warn(&ov->dev->dev,
1126 "snapshot check not supported yet on OV518(+)\n");
1127 } else {
1128 dev_err(&ov->dev->dev, "clear snap: invalid bridge type\n");
1129 }
1130
1131 return status;
1132}
1133#endif
1134
1135
1136
1137
1138static int
1139init_ov_sensor(struct usb_ov511 *ov)
1140{
1141 int i, success;
1142
1143
1144 if (i2c_w(ov, 0x12, 0x80) < 0)
1145 return -EIO;
1146
1147
1148 msleep(150);
1149
1150 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
1151 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
1152 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
1153 success = 1;
1154 continue;
1155 }
1156
1157
1158 if (i2c_w(ov, 0x12, 0x80) < 0)
1159 return -EIO;
1160
1161 msleep(150);
1162
1163 if (i2c_r(ov, 0x00) < 0)
1164 return -EIO;
1165 }
1166
1167 if (!success)
1168 return -EIO;
1169
1170 PDEBUG(1, "I2C synced in %d attempt(s)", i);
1171
1172 return 0;
1173}
1174
1175static int
1176ov511_set_packet_size(struct usb_ov511 *ov, int size)
1177{
1178 int alt, mult;
1179
1180 if (ov51x_stop(ov) < 0)
1181 return -EIO;
1182
1183 mult = size >> 5;
1184
1185 if (ov->bridge == BRG_OV511) {
1186 if (size == 0)
1187 alt = OV511_ALT_SIZE_0;
1188 else if (size == 257)
1189 alt = OV511_ALT_SIZE_257;
1190 else if (size == 513)
1191 alt = OV511_ALT_SIZE_513;
1192 else if (size == 769)
1193 alt = OV511_ALT_SIZE_769;
1194 else if (size == 993)
1195 alt = OV511_ALT_SIZE_993;
1196 else {
1197 err("Set packet size: invalid size (%d)", size);
1198 return -EINVAL;
1199 }
1200 } else if (ov->bridge == BRG_OV511PLUS) {
1201 if (size == 0)
1202 alt = OV511PLUS_ALT_SIZE_0;
1203 else if (size == 33)
1204 alt = OV511PLUS_ALT_SIZE_33;
1205 else if (size == 129)
1206 alt = OV511PLUS_ALT_SIZE_129;
1207 else if (size == 257)
1208 alt = OV511PLUS_ALT_SIZE_257;
1209 else if (size == 385)
1210 alt = OV511PLUS_ALT_SIZE_385;
1211 else if (size == 513)
1212 alt = OV511PLUS_ALT_SIZE_513;
1213 else if (size == 769)
1214 alt = OV511PLUS_ALT_SIZE_769;
1215 else if (size == 961)
1216 alt = OV511PLUS_ALT_SIZE_961;
1217 else {
1218 err("Set packet size: invalid size (%d)", size);
1219 return -EINVAL;
1220 }
1221 } else {
1222 err("Set packet size: Invalid bridge type");
1223 return -EINVAL;
1224 }
1225
1226 PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
1227
1228 if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
1229 return -EIO;
1230
1231 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1232 err("Set packet size: set interface error");
1233 return -EBUSY;
1234 }
1235
1236 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1237 return -EIO;
1238
1239 ov->packet_size = size;
1240
1241 if (ov51x_restart(ov) < 0)
1242 return -EIO;
1243
1244 return 0;
1245}
1246
1247
1248
1249
1250static int
1251ov518_set_packet_size(struct usb_ov511 *ov, int size)
1252{
1253 int alt;
1254
1255 if (ov51x_stop(ov) < 0)
1256 return -EIO;
1257
1258 if (ov->bclass == BCL_OV518) {
1259 if (size == 0)
1260 alt = OV518_ALT_SIZE_0;
1261 else if (size == 128)
1262 alt = OV518_ALT_SIZE_128;
1263 else if (size == 256)
1264 alt = OV518_ALT_SIZE_256;
1265 else if (size == 384)
1266 alt = OV518_ALT_SIZE_384;
1267 else if (size == 512)
1268 alt = OV518_ALT_SIZE_512;
1269 else if (size == 640)
1270 alt = OV518_ALT_SIZE_640;
1271 else if (size == 768)
1272 alt = OV518_ALT_SIZE_768;
1273 else if (size == 896)
1274 alt = OV518_ALT_SIZE_896;
1275 else {
1276 err("Set packet size: invalid size (%d)", size);
1277 return -EINVAL;
1278 }
1279 } else {
1280 err("Set packet size: Invalid bridge type");
1281 return -EINVAL;
1282 }
1283
1284 PDEBUG(3, "%d, alt=%d", size, alt);
1285
1286 ov->packet_size = size;
1287 if (size > 0) {
1288
1289 ov518_reg_w32(ov, 0x30, size, 2);
1290
1291 if (ov->packet_numbering)
1292 ++ov->packet_size;
1293 }
1294
1295 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1296 err("Set packet size: set interface error");
1297 return -EBUSY;
1298 }
1299
1300
1301 if (reg_w(ov, 0x2f, 0x80) < 0)
1302 return -EIO;
1303
1304 if (ov51x_restart(ov) < 0)
1305 return -EIO;
1306
1307 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1308 return -EIO;
1309
1310 return 0;
1311}
1312
1313
1314static int
1315ov511_init_compression(struct usb_ov511 *ov)
1316{
1317 int rc = 0;
1318
1319 if (!ov->compress_inited) {
1320 reg_w(ov, 0x70, phy);
1321 reg_w(ov, 0x71, phuv);
1322 reg_w(ov, 0x72, pvy);
1323 reg_w(ov, 0x73, pvuv);
1324 reg_w(ov, 0x74, qhy);
1325 reg_w(ov, 0x75, qhuv);
1326 reg_w(ov, 0x76, qvy);
1327 reg_w(ov, 0x77, qvuv);
1328
1329 if (ov511_upload_quan_tables(ov) < 0) {
1330 err("Error uploading quantization tables");
1331 rc = -EIO;
1332 goto out;
1333 }
1334 }
1335
1336 ov->compress_inited = 1;
1337out:
1338 return rc;
1339}
1340
1341
1342static int
1343ov518_init_compression(struct usb_ov511 *ov)
1344{
1345 int rc = 0;
1346
1347 if (!ov->compress_inited) {
1348 if (ov518_upload_quan_tables(ov) < 0) {
1349 err("Error uploading quantization tables");
1350 rc = -EIO;
1351 goto out;
1352 }
1353 }
1354
1355 ov->compress_inited = 1;
1356out:
1357 return rc;
1358}
1359
1360
1361
1362
1363static int
1364sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
1365{
1366 int rc;
1367
1368 PDEBUG(3, "%d", val);
1369
1370 if (ov->stop_during_set)
1371 if (ov51x_stop(ov) < 0)
1372 return -EIO;
1373
1374 switch (ov->sensor) {
1375 case SEN_OV7610:
1376 case SEN_OV6620:
1377 {
1378 rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
1379 if (rc < 0)
1380 goto out;
1381 break;
1382 }
1383 case SEN_OV6630:
1384 {
1385 rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
1386 if (rc < 0)
1387 goto out;
1388 break;
1389 }
1390 case SEN_OV7620:
1391 {
1392 unsigned char ctab[] = {
1393 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1394 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1395 };
1396
1397
1398 rc = i2c_w(ov, 0x64, ctab[val>>12]);
1399 if (rc < 0)
1400 goto out;
1401 break;
1402 }
1403 case SEN_SAA7111A:
1404 {
1405 rc = i2c_w(ov, 0x0b, val >> 9);
1406 if (rc < 0)
1407 goto out;
1408 break;
1409 }
1410 default:
1411 {
1412 PDEBUG(3, "Unsupported with this sensor");
1413 rc = -EPERM;
1414 goto out;
1415 }
1416 }
1417
1418 rc = 0;
1419 ov->contrast = val;
1420out:
1421 if (ov51x_restart(ov) < 0)
1422 return -EIO;
1423
1424 return rc;
1425}
1426
1427
1428static int
1429sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
1430{
1431 int rc;
1432
1433 switch (ov->sensor) {
1434 case SEN_OV7610:
1435 case SEN_OV6620:
1436 rc = i2c_r(ov, OV7610_REG_CNT);
1437 if (rc < 0)
1438 return rc;
1439 else
1440 *val = rc << 8;
1441 break;
1442 case SEN_OV6630:
1443 rc = i2c_r(ov, OV7610_REG_CNT);
1444 if (rc < 0)
1445 return rc;
1446 else
1447 *val = rc << 12;
1448 break;
1449 case SEN_OV7620:
1450
1451 rc = i2c_r(ov, 0x64);
1452 if (rc < 0)
1453 return rc;
1454 else
1455 *val = (rc & 0xfe) << 8;
1456 break;
1457 case SEN_SAA7111A:
1458 *val = ov->contrast;
1459 break;
1460 default:
1461 PDEBUG(3, "Unsupported with this sensor");
1462 return -EPERM;
1463 }
1464
1465 PDEBUG(3, "%d", *val);
1466 ov->contrast = *val;
1467
1468 return 0;
1469}
1470
1471
1472
1473
1474static int
1475sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
1476{
1477 int rc;
1478
1479 PDEBUG(4, "%d", val);
1480
1481 if (ov->stop_during_set)
1482 if (ov51x_stop(ov) < 0)
1483 return -EIO;
1484
1485 switch (ov->sensor) {
1486 case SEN_OV7610:
1487 case SEN_OV76BE:
1488 case SEN_OV6620:
1489 case SEN_OV6630:
1490 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1491 if (rc < 0)
1492 goto out;
1493 break;
1494 case SEN_OV7620:
1495
1496 if (!ov->auto_brt) {
1497 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1498 if (rc < 0)
1499 goto out;
1500 }
1501 break;
1502 case SEN_SAA7111A:
1503 rc = i2c_w(ov, 0x0a, val >> 8);
1504 if (rc < 0)
1505 goto out;
1506 break;
1507 default:
1508 PDEBUG(3, "Unsupported with this sensor");
1509 rc = -EPERM;
1510 goto out;
1511 }
1512
1513 rc = 0;
1514 ov->brightness = val;
1515out:
1516 if (ov51x_restart(ov) < 0)
1517 return -EIO;
1518
1519 return rc;
1520}
1521
1522
1523static int
1524sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
1525{
1526 int rc;
1527
1528 switch (ov->sensor) {
1529 case SEN_OV7610:
1530 case SEN_OV76BE:
1531 case SEN_OV7620:
1532 case SEN_OV6620:
1533 case SEN_OV6630:
1534 rc = i2c_r(ov, OV7610_REG_BRT);
1535 if (rc < 0)
1536 return rc;
1537 else
1538 *val = rc << 8;
1539 break;
1540 case SEN_SAA7111A:
1541 *val = ov->brightness;
1542 break;
1543 default:
1544 PDEBUG(3, "Unsupported with this sensor");
1545 return -EPERM;
1546 }
1547
1548 PDEBUG(3, "%d", *val);
1549 ov->brightness = *val;
1550
1551 return 0;
1552}
1553
1554
1555
1556
1557static int
1558sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
1559{
1560 int rc;
1561
1562 PDEBUG(3, "%d", val);
1563
1564 if (ov->stop_during_set)
1565 if (ov51x_stop(ov) < 0)
1566 return -EIO;
1567
1568 switch (ov->sensor) {
1569 case SEN_OV7610:
1570 case SEN_OV76BE:
1571 case SEN_OV6620:
1572 case SEN_OV6630:
1573 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1574 if (rc < 0)
1575 goto out;
1576 break;
1577 case SEN_OV7620:
1578
1579
1580
1581
1582 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1583 if (rc < 0)
1584 goto out;
1585 break;
1586 case SEN_SAA7111A:
1587 rc = i2c_w(ov, 0x0c, val >> 9);
1588 if (rc < 0)
1589 goto out;
1590 break;
1591 default:
1592 PDEBUG(3, "Unsupported with this sensor");
1593 rc = -EPERM;
1594 goto out;
1595 }
1596
1597 rc = 0;
1598 ov->colour = val;
1599out:
1600 if (ov51x_restart(ov) < 0)
1601 return -EIO;
1602
1603 return rc;
1604}
1605
1606
1607static int
1608sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
1609{
1610 int rc;
1611
1612 switch (ov->sensor) {
1613 case SEN_OV7610:
1614 case SEN_OV76BE:
1615 case SEN_OV6620:
1616 case SEN_OV6630:
1617 rc = i2c_r(ov, OV7610_REG_SAT);
1618 if (rc < 0)
1619 return rc;
1620 else
1621 *val = rc << 8;
1622 break;
1623 case SEN_OV7620:
1624
1625
1626
1627
1628
1629
1630 rc = i2c_r(ov, OV7610_REG_SAT);
1631 if (rc < 0)
1632 return rc;
1633 else
1634 *val = rc << 8;
1635 break;
1636 case SEN_SAA7111A:
1637 *val = ov->colour;
1638 break;
1639 default:
1640 PDEBUG(3, "Unsupported with this sensor");
1641 return -EPERM;
1642 }
1643
1644 PDEBUG(3, "%d", *val);
1645 ov->colour = *val;
1646
1647 return 0;
1648}
1649
1650
1651
1652
1653static int
1654sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
1655{
1656 int rc;
1657
1658 PDEBUG(3, "%d", val);
1659
1660 if (ov->stop_during_set)
1661 if (ov51x_stop(ov) < 0)
1662 return -EIO;
1663
1664 switch (ov->sensor) {
1665 case SEN_OV7610:
1666 case SEN_OV6620:
1667 case SEN_OV6630:
1668 rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
1669 if (rc < 0)
1670 goto out;
1671
1672 rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
1673 if (rc < 0)
1674 goto out;
1675 break;
1676 case SEN_OV7620:
1677
1678#if 0
1679 rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
1680 if (rc < 0)
1681 goto out;
1682
1683 rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
1684 if (rc < 0)
1685 goto out;
1686#endif
1687 break;
1688 case SEN_SAA7111A:
1689 rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
1690 if (rc < 0)
1691 goto out;
1692 break;
1693 default:
1694 PDEBUG(3, "Unsupported with this sensor");
1695 rc = -EPERM;
1696 goto out;
1697 }
1698
1699 rc = 0;
1700 ov->hue = val;
1701out:
1702 if (ov51x_restart(ov) < 0)
1703 return -EIO;
1704
1705 return rc;
1706}
1707
1708
1709static int
1710sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
1711{
1712 int rc;
1713
1714 switch (ov->sensor) {
1715 case SEN_OV7610:
1716 case SEN_OV6620:
1717 case SEN_OV6630:
1718 rc = i2c_r(ov, OV7610_REG_BLUE);
1719 if (rc < 0)
1720 return rc;
1721 else
1722 *val = rc << 8;
1723 break;
1724 case SEN_OV7620:
1725 rc = i2c_r(ov, 0x7a);
1726 if (rc < 0)
1727 return rc;
1728 else
1729 *val = rc << 8;
1730 break;
1731 case SEN_SAA7111A:
1732 *val = ov->hue;
1733 break;
1734 default:
1735 PDEBUG(3, "Unsupported with this sensor");
1736 return -EPERM;
1737 }
1738
1739 PDEBUG(3, "%d", *val);
1740 ov->hue = *val;
1741
1742 return 0;
1743}
1744
1745
1746
1747static int
1748sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
1749{
1750 int rc;
1751
1752 PDEBUG(4, "sensor_set_picture");
1753
1754 ov->whiteness = p->whiteness;
1755
1756
1757
1758
1759 rc = sensor_set_contrast(ov, p->contrast);
1760 if (FATAL_ERROR(rc))
1761 return rc;
1762
1763 rc = sensor_set_brightness(ov, p->brightness);
1764 if (FATAL_ERROR(rc))
1765 return rc;
1766
1767 rc = sensor_set_saturation(ov, p->colour);
1768 if (FATAL_ERROR(rc))
1769 return rc;
1770
1771 rc = sensor_set_hue(ov, p->hue);
1772 if (FATAL_ERROR(rc))
1773 return rc;
1774
1775 return 0;
1776}
1777
1778static int
1779sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
1780{
1781 int rc;
1782
1783 PDEBUG(4, "sensor_get_picture");
1784
1785
1786
1787
1788 rc = sensor_get_contrast(ov, &(p->contrast));
1789 if (FATAL_ERROR(rc))
1790 return rc;
1791
1792 rc = sensor_get_brightness(ov, &(p->brightness));
1793 if (FATAL_ERROR(rc))
1794 return rc;
1795
1796 rc = sensor_get_saturation(ov, &(p->colour));
1797 if (FATAL_ERROR(rc))
1798 return rc;
1799
1800 rc = sensor_get_hue(ov, &(p->hue));
1801 if (FATAL_ERROR(rc))
1802 return rc;
1803
1804 p->whiteness = 105 << 8;
1805
1806 return 0;
1807}
1808
1809#if 0
1810
1811
1812
1813static inline int
1814sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
1815{
1816 int rc;
1817
1818 PDEBUG(3, "%d", val);
1819
1820 if (ov->stop_during_set)
1821 if (ov51x_stop(ov) < 0)
1822 return -EIO;
1823
1824 switch (ov->sensor) {
1825 case SEN_OV6620:
1826 case SEN_OV6630:
1827 case SEN_OV7610:
1828 case SEN_OV7620:
1829 case SEN_OV76BE:
1830 case SEN_OV8600:
1831 rc = i2c_w(ov, 0x10, val);
1832 if (rc < 0)
1833 goto out;
1834
1835 break;
1836 case SEN_KS0127:
1837 case SEN_KS0127B:
1838 case SEN_SAA7111A:
1839 PDEBUG(3, "Unsupported with this sensor");
1840 return -EPERM;
1841 default:
1842 err("Sensor not supported for set_exposure");
1843 return -EINVAL;
1844 }
1845
1846 rc = 0;
1847 ov->exposure = val;
1848out:
1849 if (ov51x_restart(ov) < 0)
1850 return -EIO;
1851
1852 return rc;
1853}
1854#endif
1855
1856
1857
1858static int
1859sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
1860{
1861 int rc;
1862
1863 switch (ov->sensor) {
1864 case SEN_OV7610:
1865 case SEN_OV6620:
1866 case SEN_OV6630:
1867 case SEN_OV7620:
1868 case SEN_OV76BE:
1869 case SEN_OV8600:
1870 rc = i2c_r(ov, 0x10);
1871 if (rc < 0)
1872 return rc;
1873 else
1874 *val = rc;
1875 break;
1876 case SEN_KS0127:
1877 case SEN_KS0127B:
1878 case SEN_SAA7111A:
1879 val = NULL;
1880 PDEBUG(3, "Unsupported with this sensor");
1881 return -EPERM;
1882 default:
1883 err("Sensor not supported for get_exposure");
1884 return -EINVAL;
1885 }
1886
1887 PDEBUG(3, "%d", *val);
1888 ov->exposure = *val;
1889
1890 return 0;
1891}
1892
1893
1894static void
1895ov51x_led_control(struct usb_ov511 *ov, int enable)
1896{
1897 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1898
1899 if (ov->bridge == BRG_OV511PLUS)
1900 reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
1901 else if (ov->bclass == BCL_OV518)
1902 reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
1903
1904 return;
1905}
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916static int
1917sensor_set_light_freq(struct usb_ov511 *ov, int freq)
1918{
1919 int sixty;
1920
1921 PDEBUG(4, "%d Hz", freq);
1922
1923 if (freq == 60)
1924 sixty = 1;
1925 else if (freq == 50)
1926 sixty = 0;
1927 else {
1928 err("Invalid light freq (%d Hz)", freq);
1929 return -EINVAL;
1930 }
1931
1932 switch (ov->sensor) {
1933 case SEN_OV7610:
1934 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1935 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1936 i2c_w_mask(ov, 0x13, 0x10, 0x10);
1937 i2c_w_mask(ov, 0x13, 0x00, 0x10);
1938 break;
1939 case SEN_OV7620:
1940 case SEN_OV76BE:
1941 case SEN_OV8600:
1942 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1943 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1944 i2c_w_mask(ov, 0x76, 0x01, 0x01);
1945 break;
1946 case SEN_OV6620:
1947 case SEN_OV6630:
1948 i2c_w(ov, 0x2b, sixty?0xa8:0x28);
1949 i2c_w(ov, 0x2a, sixty?0x84:0xa4);
1950 break;
1951 case SEN_KS0127:
1952 case SEN_KS0127B:
1953 case SEN_SAA7111A:
1954 PDEBUG(5, "Unsupported with this sensor");
1955 return -EPERM;
1956 default:
1957 err("Sensor not supported for set_light_freq");
1958 return -EINVAL;
1959 }
1960
1961 ov->lightfreq = freq;
1962
1963 return 0;
1964}
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975static int
1976sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
1977{
1978 int rc;
1979
1980 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1981
1982 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
1983 || ov->sensor == SEN_SAA7111A) {
1984 PDEBUG(5, "Unsupported with this sensor");
1985 return -EPERM;
1986 }
1987
1988 rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
1989 if (rc < 0)
1990 return rc;
1991
1992 ov->bandfilt = enable;
1993
1994 return 0;
1995}
1996
1997
1998
1999
2000
2001
2002
2003static int
2004sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
2005{
2006 int rc;
2007
2008 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2009
2010 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
2011 || ov->sensor == SEN_SAA7111A) {
2012 PDEBUG(5, "Unsupported with this sensor");
2013 return -EPERM;
2014 }
2015
2016 rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
2017 if (rc < 0)
2018 return rc;
2019
2020 ov->auto_brt = enable;
2021
2022 return 0;
2023}
2024
2025
2026
2027
2028
2029
2030
2031static int
2032sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
2033{
2034 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2035
2036 switch (ov->sensor) {
2037 case SEN_OV7610:
2038 i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
2039 break;
2040 case SEN_OV6620:
2041 case SEN_OV7620:
2042 case SEN_OV76BE:
2043 case SEN_OV8600:
2044 i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
2045 break;
2046 case SEN_OV6630:
2047 i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
2048 break;
2049 case SEN_KS0127:
2050 case SEN_KS0127B:
2051 case SEN_SAA7111A:
2052 PDEBUG(5, "Unsupported with this sensor");
2053 return -EPERM;
2054 default:
2055 err("Sensor not supported for set_auto_exposure");
2056 return -EINVAL;
2057 }
2058
2059 ov->auto_exp = enable;
2060
2061 return 0;
2062}
2063
2064
2065
2066
2067
2068
2069
2070
2071static int
2072sensor_set_backlight(struct usb_ov511 *ov, int enable)
2073{
2074 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2075
2076 switch (ov->sensor) {
2077 case SEN_OV7620:
2078 case SEN_OV8600:
2079 i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
2080 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2081 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2082 break;
2083 case SEN_OV6620:
2084 i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
2085 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2086 i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
2087 break;
2088 case SEN_OV6630:
2089 i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
2090 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2091 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2092 break;
2093 case SEN_OV7610:
2094 case SEN_OV76BE:
2095 case SEN_KS0127:
2096 case SEN_KS0127B:
2097 case SEN_SAA7111A:
2098 PDEBUG(5, "Unsupported with this sensor");
2099 return -EPERM;
2100 default:
2101 err("Sensor not supported for set_backlight");
2102 return -EINVAL;
2103 }
2104
2105 ov->backlight = enable;
2106
2107 return 0;
2108}
2109
2110static int
2111sensor_set_mirror(struct usb_ov511 *ov, int enable)
2112{
2113 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2114
2115 switch (ov->sensor) {
2116 case SEN_OV6620:
2117 case SEN_OV6630:
2118 case SEN_OV7610:
2119 case SEN_OV7620:
2120 case SEN_OV76BE:
2121 case SEN_OV8600:
2122 i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
2123 break;
2124 case SEN_KS0127:
2125 case SEN_KS0127B:
2126 case SEN_SAA7111A:
2127 PDEBUG(5, "Unsupported with this sensor");
2128 return -EPERM;
2129 default:
2130 err("Sensor not supported for set_mirror");
2131 return -EINVAL;
2132 }
2133
2134 ov->mirror = enable;
2135
2136 return 0;
2137}
2138
2139
2140
2141
2142static inline int
2143get_depth(int palette)
2144{
2145 switch (palette) {
2146 case VIDEO_PALETTE_GREY: return 8;
2147 case VIDEO_PALETTE_YUV420: return 12;
2148 case VIDEO_PALETTE_YUV420P: return 12;
2149 default: return 0;
2150 }
2151}
2152
2153
2154static inline long int
2155get_frame_length(struct ov511_frame *frame)
2156{
2157 if (!frame)
2158 return 0;
2159 else
2160 return ((frame->width * frame->height
2161 * get_depth(frame->format)) >> 3);
2162}
2163
2164static int
2165mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
2166 int mode, int sub_flag, int qvga)
2167{
2168 int clock;
2169
2170
2171
2172 switch (ov->sensor) {
2173 case SEN_OV7610:
2174 i2c_w(ov, 0x14, qvga?0x24:0x04);
2175
2176#if 0
2177 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2178 i2c_w(ov, 0x24, 0x10);
2179 i2c_w(ov, 0x25, qvga?0x40:0x8a);
2180 i2c_w(ov, 0x2f, qvga?0x30:0xb0);
2181 i2c_w(ov, 0x35, qvga?0x1c:0x9c);
2182#endif
2183 break;
2184 case SEN_OV7620:
2185
2186 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2187 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2188 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2189 i2c_w(ov, 0x25, qvga?0x30:0x60);
2190 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2191 i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
2192 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2193 break;
2194 case SEN_OV76BE:
2195
2196 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2197
2198#if 0
2199 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2200 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2201 i2c_w(ov, 0x25, qvga?0x30:0x60);
2202 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2203 i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
2204 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2205#endif
2206 break;
2207 case SEN_OV6620:
2208 i2c_w(ov, 0x14, qvga?0x24:0x04);
2209 break;
2210 case SEN_OV6630:
2211 i2c_w(ov, 0x14, qvga?0xa0:0x80);
2212 break;
2213 default:
2214 err("Invalid sensor");
2215 return -EINVAL;
2216 }
2217
2218
2219
2220 if (mode == VIDEO_PALETTE_GREY) {
2221 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2222
2223 i2c_w_mask(ov, 0x0e, 0x40, 0x40);
2224 }
2225
2226 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2227 && ov518_color) {
2228 i2c_w_mask(ov, 0x12, 0x00, 0x10);
2229 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2230 } else {
2231 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2232 }
2233 } else {
2234 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2235
2236 i2c_w_mask(ov, 0x0e, 0x00, 0x40);
2237 }
2238
2239
2240
2241
2242
2243
2244
2245 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2246 && ov518_color) {
2247 i2c_w_mask(ov, 0x12, 0x10, 0x10);
2248 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2249 } else {
2250 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2251 }
2252 }
2253
2254
2255
2256
2257
2258 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
2259 {
2260
2261
2262 i2c_w(ov, 0x2a, 0x04);
2263
2264 if (ov->compress) {
2265
2266 clock = 3;
2267 } else if (clockdiv == -1) {
2268 clock = 3;
2269 } else {
2270 clock = clockdiv;
2271 }
2272
2273 PDEBUG(4, "Setting clock divisor to %d", clock);
2274
2275 i2c_w(ov, 0x11, clock);
2276
2277 i2c_w(ov, 0x2a, 0x84);
2278
2279
2280
2281 i2c_w(ov, 0x2d, 0x85);
2282 }
2283 else
2284 {
2285 if (ov->compress) {
2286 clock = 1;
2287 } else if (clockdiv == -1) {
2288
2289 clock = ((sub_flag ? ov->subw * ov->subh
2290 : width * height)
2291 * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
2292 / 66000;
2293 } else {
2294 clock = clockdiv;
2295 }
2296
2297 PDEBUG(4, "Setting clock divisor to %d", clock);
2298
2299 i2c_w(ov, 0x11, clock);
2300 }
2301
2302
2303
2304 if (framedrop >= 0)
2305 i2c_w(ov, 0x16, framedrop);
2306
2307
2308 i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
2309
2310
2311 i2c_w_mask(ov, 0x12, 0x04, 0x04);
2312
2313
2314
2315
2316 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2317 if (width == 640 && height == 480)
2318 i2c_w(ov, 0x35, 0x9e);
2319 else
2320 i2c_w(ov, 0x35, 0x1e);
2321 }
2322
2323 return 0;
2324}
2325
2326static int
2327set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
2328 int sub_flag)
2329{
2330 int ret;
2331 int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
2332 int hoffset, voffset, hwscale = 0, vwscale = 0;
2333
2334
2335
2336 switch (ov->sensor) {
2337 case SEN_OV7610:
2338 case SEN_OV76BE:
2339 hwsbase = 0x38;
2340 hwebase = 0x3a;
2341 vwsbase = vwebase = 0x05;
2342 break;
2343 case SEN_OV6620:
2344 case SEN_OV6630:
2345 hwsbase = 0x38;
2346 hwebase = 0x3a;
2347 vwsbase = 0x05;
2348 vwebase = 0x06;
2349 break;
2350 case SEN_OV7620:
2351 hwsbase = 0x2f;
2352 hwebase = 0x2f;
2353 vwsbase = vwebase = 0x05;
2354 break;
2355 default:
2356 err("Invalid sensor");
2357 return -EINVAL;
2358 }
2359
2360 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
2361
2362 if ((width > 176 && height > 144)
2363 || ov->bclass == BCL_OV518) {
2364 ret = mode_init_ov_sensor_regs(ov, width, height,
2365 mode, sub_flag, 0);
2366 if (ret < 0)
2367 return ret;
2368 hwscale = 1;
2369 vwscale = 1;
2370 hwsize = 352;
2371 vwsize = 288;
2372 } else if (width > 176 || height > 144) {
2373 err("Illegal dimensions");
2374 return -EINVAL;
2375 } else {
2376 ret = mode_init_ov_sensor_regs(ov, width, height,
2377 mode, sub_flag, 1);
2378 if (ret < 0)
2379 return ret;
2380 hwsize = 176;
2381 vwsize = 144;
2382 }
2383 } else {
2384 if (width > 320 && height > 240) {
2385 ret = mode_init_ov_sensor_regs(ov, width, height,
2386 mode, sub_flag, 0);
2387 if (ret < 0)
2388 return ret;
2389 hwscale = 2;
2390 vwscale = 1;
2391 hwsize = 640;
2392 vwsize = 480;
2393 } else if (width > 320 || height > 240) {
2394 err("Illegal dimensions");
2395 return -EINVAL;
2396 } else {
2397 ret = mode_init_ov_sensor_regs(ov, width, height,
2398 mode, sub_flag, 1);
2399 if (ret < 0)
2400 return ret;
2401 hwscale = 1;
2402 hwsize = 320;
2403 vwsize = 240;
2404 }
2405 }
2406
2407
2408 hoffset = ((hwsize - width) / 2) >> hwscale;
2409 voffset = ((vwsize - height) / 2) >> vwscale;
2410
2411
2412 if (sub_flag) {
2413 i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
2414 i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
2415 i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
2416 i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
2417 } else {
2418 i2c_w(ov, 0x17, hwsbase + hoffset);
2419 i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
2420 i2c_w(ov, 0x19, vwsbase + voffset);
2421 i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
2422 }
2423
2424#ifdef OV511_DEBUG
2425 if (dump_sensor)
2426 dump_i2c_regs(ov);
2427#endif
2428
2429 return 0;
2430}
2431
2432
2433
2434
2435
2436static int
2437ov511_mode_init_regs(struct usb_ov511 *ov,
2438 int width, int height, int mode, int sub_flag)
2439{
2440 int hsegs, vsegs;
2441
2442 if (sub_flag) {
2443 width = ov->subw;
2444 height = ov->subh;
2445 }
2446
2447 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2448 width, height, mode, sub_flag);
2449
2450
2451
2452 if (ov->sensor == SEN_SAA7111A) {
2453 if (width == 320 && height == 240) {
2454
2455 } else if (width == 640 && height == 480) {
2456
2457
2458 width = 320;
2459 } else {
2460 err("SAA7111A only allows 320x240 or 640x480");
2461 return -EINVAL;
2462 }
2463 }
2464
2465
2466 if (width % 8 || height % 8) {
2467 err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
2468 return -EINVAL;
2469 }
2470
2471 if (width < ov->minwidth || height < ov->minheight) {
2472 err("Requested dimensions are too small");
2473 return -EINVAL;
2474 }
2475
2476 if (ov51x_stop(ov) < 0)
2477 return -EIO;
2478
2479 if (mode == VIDEO_PALETTE_GREY) {
2480 reg_w(ov, R511_CAM_UV_EN, 0x00);
2481 reg_w(ov, R511_SNAP_UV_EN, 0x00);
2482 reg_w(ov, R511_SNAP_OPTS, 0x01);
2483 } else {
2484 reg_w(ov, R511_CAM_UV_EN, 0x01);
2485 reg_w(ov, R511_SNAP_UV_EN, 0x01);
2486 reg_w(ov, R511_SNAP_OPTS, 0x03);
2487 }
2488
2489
2490
2491
2492 hsegs = (width >> 3) - 1;
2493 vsegs = (height >> 3) - 1;
2494
2495 reg_w(ov, R511_CAM_PXCNT, hsegs);
2496 reg_w(ov, R511_CAM_LNCNT, vsegs);
2497 reg_w(ov, R511_CAM_PXDIV, 0x00);
2498 reg_w(ov, R511_CAM_LNDIV, 0x00);
2499
2500
2501 reg_w(ov, R511_CAM_OPTS, 0x03);
2502
2503
2504 reg_w(ov, R511_SNAP_PXCNT, hsegs);
2505 reg_w(ov, R511_SNAP_LNCNT, vsegs);
2506 reg_w(ov, R511_SNAP_PXDIV, 0x00);
2507 reg_w(ov, R511_SNAP_LNDIV, 0x00);
2508
2509 if (ov->compress) {
2510
2511 reg_w(ov, R511_COMP_EN, 0x07);
2512 reg_w(ov, R511_COMP_LUT_EN, 0x03);
2513 ov51x_reset(ov, OV511_RESET_OMNICE);
2514 }
2515
2516 if (ov51x_restart(ov) < 0)
2517 return -EIO;
2518
2519 return 0;
2520}
2521
2522
2523
2524
2525
2526
2527
2528
2529static int
2530ov518_mode_init_regs(struct usb_ov511 *ov,
2531 int width, int height, int mode, int sub_flag)
2532{
2533 int hsegs, vsegs, hi_res;
2534
2535 if (sub_flag) {
2536 width = ov->subw;
2537 height = ov->subh;
2538 }
2539
2540 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2541 width, height, mode, sub_flag);
2542
2543 if (width % 16 || height % 8) {
2544 err("Invalid size (%d, %d)", width, height);
2545 return -EINVAL;
2546 }
2547
2548 if (width < ov->minwidth || height < ov->minheight) {
2549 err("Requested dimensions are too small");
2550 return -EINVAL;
2551 }
2552
2553 if (width >= 320 && height >= 240) {
2554 hi_res = 1;
2555 } else if (width >= 320 || height >= 240) {
2556 err("Invalid width/height combination (%d, %d)", width, height);
2557 return -EINVAL;
2558 } else {
2559 hi_res = 0;
2560 }
2561
2562 if (ov51x_stop(ov) < 0)
2563 return -EIO;
2564
2565
2566
2567 reg_w(ov, 0x2b, 0);
2568 reg_w(ov, 0x2c, 0);
2569 reg_w(ov, 0x2d, 0);
2570 reg_w(ov, 0x2e, 0);
2571 reg_w(ov, 0x3b, 0);
2572 reg_w(ov, 0x3c, 0);
2573 reg_w(ov, 0x3d, 0);
2574 reg_w(ov, 0x3e, 0);
2575
2576 if (ov->bridge == BRG_OV518 && ov518_color) {
2577
2578 i2c_w_mask(ov, 0x15, 0x00, 0x01);
2579
2580 if (mode == VIDEO_PALETTE_GREY) {
2581
2582 reg_w_mask(ov, 0x20, 0x00, 0x08);
2583
2584
2585 reg_w_mask(ov, 0x28, 0x00, 0xf0);
2586 reg_w_mask(ov, 0x38, 0x00, 0xf0);
2587 } else {
2588
2589 reg_w_mask(ov, 0x20, 0x08, 0x08);
2590
2591
2592 reg_w_mask(ov, 0x28, 0x80, 0xf0);
2593 reg_w_mask(ov, 0x38, 0x80, 0xf0);
2594 }
2595 } else {
2596 reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2597 reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2598 }
2599
2600 hsegs = width / 16;
2601 vsegs = height / 4;
2602
2603 reg_w(ov, 0x29, hsegs);
2604 reg_w(ov, 0x2a, vsegs);
2605
2606 reg_w(ov, 0x39, hsegs);
2607 reg_w(ov, 0x3a, vsegs);
2608
2609
2610 reg_w(ov, 0x2f, 0x80);
2611
2612
2613
2614
2615 reg_w(ov, 0x51, 0x02);
2616 reg_w(ov, 0x22, 0x18);
2617 reg_w(ov, 0x23, 0xff);
2618
2619 if (ov->bridge == BRG_OV518PLUS)
2620 reg_w(ov, 0x21, 0x19);
2621 else
2622 reg_w(ov, 0x71, 0x19);
2623
2624
2625
2626 i2c_w(ov, 0x54, 0x23);
2627
2628 reg_w(ov, 0x2f, 0x80);
2629
2630 if (ov->bridge == BRG_OV518PLUS) {
2631 reg_w(ov, 0x24, 0x94);
2632 reg_w(ov, 0x25, 0x90);
2633 ov518_reg_w32(ov, 0xc4, 400, 2);
2634 ov518_reg_w32(ov, 0xc6, 540, 2);
2635 ov518_reg_w32(ov, 0xc7, 540, 2);
2636 ov518_reg_w32(ov, 0xc8, 108, 2);
2637 ov518_reg_w32(ov, 0xca, 131098, 3);
2638 ov518_reg_w32(ov, 0xcb, 532, 2);
2639 ov518_reg_w32(ov, 0xcc, 2400, 2);
2640 ov518_reg_w32(ov, 0xcd, 32, 2);
2641 ov518_reg_w32(ov, 0xce, 608, 2);
2642 } else {
2643 reg_w(ov, 0x24, 0x9f);
2644 reg_w(ov, 0x25, 0x90);
2645 ov518_reg_w32(ov, 0xc4, 400, 2);
2646 ov518_reg_w32(ov, 0xc6, 500, 2);
2647 ov518_reg_w32(ov, 0xc7, 500, 2);
2648 ov518_reg_w32(ov, 0xc8, 142, 2);
2649 ov518_reg_w32(ov, 0xca, 131098, 3);
2650 ov518_reg_w32(ov, 0xcb, 532, 2);
2651 ov518_reg_w32(ov, 0xcc, 2000, 2);
2652 ov518_reg_w32(ov, 0xcd, 32, 2);
2653 ov518_reg_w32(ov, 0xce, 608, 2);
2654 }
2655
2656 reg_w(ov, 0x2f, 0x80);
2657
2658 if (ov51x_restart(ov) < 0)
2659 return -EIO;
2660
2661
2662 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
2663 return -EIO;
2664
2665 return 0;
2666}
2667
2668
2669static int
2670mode_init_regs(struct usb_ov511 *ov,
2671 int width, int height, int mode, int sub_flag)
2672{
2673 int rc = 0;
2674
2675 if (!ov || !ov->dev)
2676 return -EFAULT;
2677
2678 if (ov->bclass == BCL_OV518) {
2679 rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
2680 } else {
2681 rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
2682 }
2683
2684 if (FATAL_ERROR(rc))
2685 return rc;
2686
2687 switch (ov->sensor) {
2688 case SEN_OV7610:
2689 case SEN_OV7620:
2690 case SEN_OV76BE:
2691 case SEN_OV8600:
2692 case SEN_OV6620:
2693 case SEN_OV6630:
2694 rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
2695 break;
2696 case SEN_KS0127:
2697 case SEN_KS0127B:
2698 err("KS0127-series decoders not supported yet");
2699 rc = -EINVAL;
2700 break;
2701 case SEN_SAA7111A:
2702
2703
2704
2705 PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
2706 break;
2707 default:
2708 err("Unknown sensor");
2709 rc = -EINVAL;
2710 }
2711
2712 if (FATAL_ERROR(rc))
2713 return rc;
2714
2715
2716 rc = sensor_set_auto_brightness(ov, ov->auto_brt);
2717 if (FATAL_ERROR(rc))
2718 return rc;
2719
2720 rc = sensor_set_auto_exposure(ov, ov->auto_exp);
2721 if (FATAL_ERROR(rc))
2722 return rc;
2723
2724 rc = sensor_set_banding_filter(ov, bandingfilter);
2725 if (FATAL_ERROR(rc))
2726 return rc;
2727
2728 if (ov->lightfreq) {
2729 rc = sensor_set_light_freq(ov, lightfreq);
2730 if (FATAL_ERROR(rc))
2731 return rc;
2732 }
2733
2734 rc = sensor_set_backlight(ov, ov->backlight);
2735 if (FATAL_ERROR(rc))
2736 return rc;
2737
2738 rc = sensor_set_mirror(ov, ov->mirror);
2739 if (FATAL_ERROR(rc))
2740 return rc;
2741
2742 return 0;
2743}
2744
2745
2746
2747
2748static int
2749ov51x_set_default_params(struct usb_ov511 *ov)
2750{
2751 int i;
2752
2753
2754
2755 for (i = 0; i < OV511_NUMFRAMES; i++) {
2756 ov->frame[i].width = ov->maxwidth;
2757 ov->frame[i].height = ov->maxheight;
2758 ov->frame[i].bytes_read = 0;
2759 if (force_palette)
2760 ov->frame[i].format = force_palette;
2761 else
2762 ov->frame[i].format = VIDEO_PALETTE_YUV420;
2763
2764 ov->frame[i].depth = get_depth(ov->frame[i].format);
2765 }
2766
2767 PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
2768 symbolic(v4l1_plist, ov->frame[0].format));
2769
2770
2771 if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
2772 ov->frame[0].format, 0) < 0)
2773 return -EINVAL;
2774
2775 return 0;
2776}
2777
2778
2779
2780
2781
2782
2783
2784
2785static int
2786decoder_set_input(struct usb_ov511 *ov, int input)
2787{
2788 PDEBUG(4, "port %d", input);
2789
2790 switch (ov->sensor) {
2791 case SEN_SAA7111A:
2792 {
2793
2794 i2c_w_mask(ov, 0x02, input, 0x07);
2795
2796 i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
2797 break;
2798 }
2799 default:
2800 return -EINVAL;
2801 }
2802
2803 return 0;
2804}
2805
2806
2807static int
2808decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
2809{
2810 switch (ov->sensor) {
2811 case SEN_SAA7111A:
2812 {
2813 if (input < 0 || input > 7)
2814 return -EINVAL;
2815 else if (input < 4)
2816 sprintf(name, "CVBS-%d", input);
2817 else
2818 sprintf(name, "S-Video-%d", input - 4);
2819 break;
2820 }
2821 default:
2822 sprintf(name, "%s", "Camera");
2823 }
2824
2825 return 0;
2826}
2827
2828
2829static int
2830decoder_set_norm(struct usb_ov511 *ov, int norm)
2831{
2832 PDEBUG(4, "%d", norm);
2833
2834 switch (ov->sensor) {
2835 case SEN_SAA7111A:
2836 {
2837 int reg_8, reg_e;
2838
2839 if (norm == VIDEO_MODE_NTSC) {
2840 reg_8 = 0x40;
2841 reg_e = 0x00;
2842 } else if (norm == VIDEO_MODE_PAL) {
2843 reg_8 = 0x00;
2844 reg_e = 0x00;
2845 } else if (norm == VIDEO_MODE_AUTO) {
2846 reg_8 = 0x80;
2847 reg_e = 0x00;
2848 } else if (norm == VIDEO_MODE_SECAM) {
2849 reg_8 = 0x00;
2850 reg_e = 0x50;
2851 } else {
2852 return -EINVAL;
2853 }
2854
2855 i2c_w_mask(ov, 0x08, reg_8, 0xc0);
2856 i2c_w_mask(ov, 0x0e, reg_e, 0x70);
2857 break;
2858 }
2859 default:
2860 return -EINVAL;
2861 }
2862
2863 return 0;
2864}
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875static inline void
2876make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
2877{
2878 unsigned char *pOut1 = pOut;
2879 int x, y;
2880
2881 for (y = 0; y < 8; y++) {
2882 pOut1 = pOut;
2883 for (x = 0; x < 8; x++) {
2884 *pOut1++ = *pIn++;
2885 }
2886 pOut += w;
2887 }
2888}
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900static void
2901yuv400raw_to_yuv400p(struct ov511_frame *frame,
2902 unsigned char *pIn0, unsigned char *pOut0)
2903{
2904 int x, y;
2905 unsigned char *pIn, *pOut, *pOutLine;
2906
2907
2908 pIn = pIn0;
2909 pOutLine = pOut0;
2910 for (y = 0; y < frame->rawheight - 1; y += 8) {
2911 pOut = pOutLine;
2912 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2913 make_8x8(pIn, pOut, frame->rawwidth);
2914 pIn += 64;
2915 pOut += 8;
2916 }
2917 pOutLine += 8 * frame->rawwidth;
2918 }
2919}
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957static void
2958yuv420raw_to_yuv420p(struct ov511_frame *frame,
2959 unsigned char *pIn0, unsigned char *pOut0)
2960{
2961 int k, x, y;
2962 unsigned char *pIn, *pOut, *pOutLine;
2963 const unsigned int a = frame->rawwidth * frame->rawheight;
2964 const unsigned int w = frame->rawwidth / 2;
2965
2966
2967 pIn = pIn0;
2968 pOutLine = pOut0 + a;
2969 for (y = 0; y < frame->rawheight - 1; y += 16) {
2970 pOut = pOutLine;
2971 for (x = 0; x < frame->rawwidth - 1; x += 16) {
2972 make_8x8(pIn, pOut, w);
2973 make_8x8(pIn + 64, pOut + a/4, w);
2974 pIn += 384;
2975 pOut += 8;
2976 }
2977 pOutLine += 8 * w;
2978 }
2979
2980
2981 pIn = pIn0 + 128;
2982 pOutLine = pOut0;
2983 k = 0;
2984 for (y = 0; y < frame->rawheight - 1; y += 8) {
2985 pOut = pOutLine;
2986 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2987 make_8x8(pIn, pOut, frame->rawwidth);
2988 pIn += 64;
2989 pOut += 8;
2990 if ((++k) > 3) {
2991 k = 0;
2992 pIn += 128;
2993 }
2994 }
2995 pOutLine += 8 * frame->rawwidth;
2996 }
2997}
2998
2999
3000
3001
3002
3003
3004
3005static int
3006request_decompressor(struct usb_ov511 *ov)
3007{
3008 if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
3009 err("No decompressor available");
3010 } else {
3011 err("Unknown bridge");
3012 }
3013
3014 return -ENOSYS;
3015}
3016
3017static void
3018decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3019 unsigned char *pIn0, unsigned char *pOut0)
3020{
3021 if (!ov->decomp_ops)
3022 if (request_decompressor(ov))
3023 return;
3024
3025}
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037static void
3038deinterlace(struct ov511_frame *frame, int rawformat,
3039 unsigned char *pIn0, unsigned char *pOut0)
3040{
3041 const int fieldheight = frame->rawheight / 2;
3042 const int fieldpix = fieldheight * frame->rawwidth;
3043 const int w = frame->width;
3044 int x, y;
3045 unsigned char *pInEven, *pInOdd, *pOut;
3046
3047 PDEBUG(5, "fieldheight=%d", fieldheight);
3048
3049 if (frame->rawheight != frame->height) {
3050 err("invalid height");
3051 return;
3052 }
3053
3054 if ((frame->rawwidth * 2) != frame->width) {
3055 err("invalid width");
3056 return;
3057 }
3058
3059
3060 pInOdd = pIn0;
3061 pInEven = pInOdd + fieldpix;
3062 pOut = pOut0;
3063 for (y = 0; y < fieldheight; y++) {
3064 for (x = 0; x < frame->rawwidth; x++) {
3065 *pOut = *pInEven;
3066 *(pOut+1) = *pInEven++;
3067 *(pOut+w) = *pInOdd;
3068 *(pOut+w+1) = *pInOdd++;
3069 pOut += 2;
3070 }
3071 pOut += w;
3072 }
3073
3074 if (rawformat == RAWFMT_YUV420) {
3075
3076 pInOdd = pIn0 + fieldpix * 2;
3077 pInEven = pInOdd + fieldpix / 4;
3078 for (y = 0; y < fieldheight / 2; y++) {
3079 for (x = 0; x < frame->rawwidth / 2; x++) {
3080 *pOut = *pInEven;
3081 *(pOut+1) = *pInEven++;
3082 *(pOut+w/2) = *pInOdd;
3083 *(pOut+w/2+1) = *pInOdd++;
3084 pOut += 2;
3085 }
3086 pOut += w/2;
3087 }
3088
3089 pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
3090 pInEven = pInOdd + fieldpix / 4;
3091 for (y = 0; y < fieldheight / 2; y++) {
3092 for (x = 0; x < frame->rawwidth / 2; x++) {
3093 *pOut = *pInEven;
3094 *(pOut+1) = *pInEven++;
3095 *(pOut+w/2) = *pInOdd;
3096 *(pOut+w/2+1) = *pInOdd++;
3097 pOut += 2;
3098 }
3099 pOut += w/2;
3100 }
3101 }
3102}
3103
3104static void
3105ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
3106{
3107
3108 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3109 if (frame->compressed)
3110 decompress(ov, frame, frame->rawdata,
3111 frame->tempdata);
3112 else
3113 yuv400raw_to_yuv400p(frame, frame->rawdata,
3114 frame->tempdata);
3115
3116 deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
3117 frame->data);
3118 } else {
3119 if (frame->compressed)
3120 decompress(ov, frame, frame->rawdata,
3121 frame->data);
3122 else
3123 yuv400raw_to_yuv400p(frame, frame->rawdata,
3124 frame->data);
3125 }
3126}
3127
3128
3129static void
3130ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
3131{
3132
3133 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3134 if (frame->compressed)
3135 decompress(ov, frame, frame->rawdata, frame->tempdata);
3136 else
3137 yuv420raw_to_yuv420p(frame, frame->rawdata,
3138 frame->tempdata);
3139
3140 deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
3141 frame->data);
3142 } else {
3143 if (frame->compressed)
3144 decompress(ov, frame, frame->rawdata, frame->data);
3145 else
3146 yuv420raw_to_yuv420p(frame, frame->rawdata,
3147 frame->data);
3148 }
3149}
3150
3151
3152
3153
3154
3155
3156
3157static void
3158ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
3159{
3160 if (dumppix) {
3161 memset(frame->data, 0,
3162 MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3163 PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
3164 memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
3165 } else {
3166 switch (frame->format) {
3167 case VIDEO_PALETTE_GREY:
3168 ov51x_postprocess_grey(ov, frame);
3169 break;
3170 case VIDEO_PALETTE_YUV420:
3171 case VIDEO_PALETTE_YUV420P:
3172 ov51x_postprocess_yuv420(ov, frame);
3173 break;
3174 default:
3175 err("Cannot convert data to %s",
3176 symbolic(v4l1_plist, frame->format));
3177 }
3178 }
3179}
3180
3181
3182
3183
3184
3185
3186
3187static inline void
3188ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3189{
3190 int num, offset;
3191 int pnum = in[ov->packet_size - 1];
3192 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3193 struct ov511_frame *frame = &ov->frame[ov->curframe];
3194 struct timeval *ts;
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210 if (printph) {
3211 dev_info(&ov->dev->dev,
3212 "ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x\n",
3213 pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
3214 in[7], in[8], in[9], in[10], in[11]);
3215 }
3216
3217
3218 if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
3219 (~in[8] & 0x08))
3220 goto check_middle;
3221
3222
3223 if (in[8] & 0x80) {
3224 ts = (struct timeval *)(frame->data
3225 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3226 do_gettimeofday(ts);
3227
3228
3229 frame->rawwidth = ((int)(in[9]) + 1) * 8;
3230 frame->rawheight = ((int)(in[10]) + 1) * 8;
3231
3232 PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
3233 ov->curframe, pnum, frame->rawwidth, frame->rawheight,
3234 frame->bytes_recvd);
3235
3236
3237 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3238 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
3239 ov->maxheight);
3240
3241
3242 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3243
3244 if (frame->scanstate == STATE_LINES) {
3245 int nextf;
3246
3247 frame->grabstate = FRAME_DONE;
3248 wake_up_interruptible(&frame->wq);
3249
3250
3251
3252 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3253 if (ov->frame[nextf].grabstate == FRAME_READY
3254 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3255 ov->curframe = nextf;
3256 ov->frame[nextf].scanstate = STATE_SCANNING;
3257 } else {
3258 if (frame->grabstate == FRAME_DONE) {
3259 PDEBUG(4, "** Frame done **");
3260 } else {
3261 PDEBUG(4, "Frame not ready? state = %d",
3262 ov->frame[nextf].grabstate);
3263 }
3264
3265 ov->curframe = -1;
3266 }
3267 } else {
3268 PDEBUG(5, "Frame done, but not scanning");
3269 }
3270
3271
3272
3273 } else {
3274
3275 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3276
3277
3278
3279 if (in[8] & 0x02) {
3280 frame->snapshot = 1;
3281 PDEBUG(3, "snapshot detected");
3282 }
3283
3284 frame->scanstate = STATE_LINES;
3285 frame->bytes_recvd = 0;
3286 frame->compressed = in[8] & 0x40;
3287 }
3288
3289check_middle:
3290
3291 if (frame->scanstate != STATE_LINES) {
3292 PDEBUG(5, "Not in a frame; packet skipped");
3293 return;
3294 }
3295
3296
3297 if (frame->bytes_recvd == 0)
3298 offset = 9;
3299 else
3300 offset = 0;
3301
3302 num = n - offset - 1;
3303
3304
3305 if (dumppix == 2) {
3306 frame->bytes_recvd += n - 1;
3307 if (frame->bytes_recvd <= max_raw)
3308 memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
3309 in, n - 1);
3310 else
3311 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3312 frame->bytes_recvd - max_raw);
3313 } else if (!frame->compressed && !remove_zeros) {
3314 frame->bytes_recvd += num;
3315 if (frame->bytes_recvd <= max_raw)
3316 memcpy(frame->rawdata + frame->bytes_recvd - num,
3317 in + offset, num);
3318 else
3319 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3320 frame->bytes_recvd - max_raw);
3321 } else {
3322 int b, read = 0, allzero, copied = 0;
3323 if (offset) {
3324 frame->bytes_recvd += 32 - offset;
3325 memcpy(frame->rawdata, in + offset, 32 - offset);
3326 read += 32;
3327 }
3328
3329 while (read < n - 1) {
3330 allzero = 1;
3331 for (b = 0; b < 32; b++) {
3332 if (in[read + b]) {
3333 allzero = 0;
3334 break;
3335 }
3336 }
3337
3338 if (allzero) {
3339
3340 } else {
3341 if (frame->bytes_recvd + copied + 32 <= max_raw)
3342 {
3343 memcpy(frame->rawdata
3344 + frame->bytes_recvd + copied,
3345 in + read, 32);
3346 copied += 32;
3347 } else {
3348 PDEBUG(3, "Raw data buffer overrun!!");
3349 }
3350 }
3351 read += 32;
3352 }
3353
3354 frame->bytes_recvd += copied;
3355 }
3356}
3357
3358static inline void
3359ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3360{
3361 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3362 struct ov511_frame *frame = &ov->frame[ov->curframe];
3363 struct timeval *ts;
3364
3365
3366 if (ov->packet_numbering)
3367 --n;
3368
3369
3370
3371 if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
3372 if (printph) {
3373 dev_info(&ov->dev->dev,
3374 "ph: %2x %2x %2x %2x %2x %2x %2x %2x\n",
3375 in[0], in[1], in[2], in[3], in[4], in[5],
3376 in[6], in[7]);
3377 }
3378
3379 if (frame->scanstate == STATE_LINES) {
3380 PDEBUG(4, "Detected frame end/start");
3381 goto eof;
3382 } else {
3383
3384 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3385 goto sof;
3386 }
3387 } else {
3388 goto check_middle;
3389 }
3390
3391eof:
3392 ts = (struct timeval *)(frame->data
3393 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3394 do_gettimeofday(ts);
3395
3396 PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
3397 ov->curframe,
3398 (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
3399
3400
3401
3402 frame->rawwidth = frame->width;
3403 frame->rawheight = frame->height;
3404
3405
3406 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3407 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
3408
3409
3410 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3411
3412 if (frame->scanstate == STATE_LINES) {
3413 int nextf;
3414
3415 frame->grabstate = FRAME_DONE;
3416 wake_up_interruptible(&frame->wq);
3417
3418
3419
3420 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3421 if (ov->frame[nextf].grabstate == FRAME_READY
3422 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3423 ov->curframe = nextf;
3424 ov->frame[nextf].scanstate = STATE_SCANNING;
3425 frame = &ov->frame[nextf];
3426 } else {
3427 if (frame->grabstate == FRAME_DONE) {
3428 PDEBUG(4, "** Frame done **");
3429 } else {
3430 PDEBUG(4, "Frame not ready? state = %d",
3431 ov->frame[nextf].grabstate);
3432 }
3433
3434 ov->curframe = -1;
3435 PDEBUG(4, "SOF dropped (no active frame)");
3436 return;
3437 }
3438 }
3439sof:
3440 PDEBUG(4, "Starting capture on frame %d", frame->framenum);
3441
3442
3443#if 0
3444
3445
3446 if (in[8] & 0x02) {
3447 frame->snapshot = 1;
3448 PDEBUG(3, "snapshot detected");
3449 }
3450#endif
3451 frame->scanstate = STATE_LINES;
3452 frame->bytes_recvd = 0;
3453 frame->compressed = 1;
3454
3455check_middle:
3456
3457 if (frame->scanstate != STATE_LINES) {
3458 PDEBUG(4, "scanstate: no SOF yet");
3459 return;
3460 }
3461
3462
3463 if (dumppix == 2) {
3464 frame->bytes_recvd += n;
3465 if (frame->bytes_recvd <= max_raw)
3466 memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
3467 else
3468 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3469 frame->bytes_recvd - max_raw);
3470 } else {
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480 int b, read = 0, allzero, copied = 0;
3481
3482 while (read < n) {
3483 allzero = 1;
3484 for (b = 0; b < 8; b++) {
3485 if (in[read + b]) {
3486 allzero = 0;
3487 break;
3488 }
3489 }
3490
3491 if (allzero) {
3492
3493 } else {
3494 if (frame->bytes_recvd + copied + 8 <= max_raw)
3495 {
3496 memcpy(frame->rawdata
3497 + frame->bytes_recvd + copied,
3498 in + read, 8);
3499 copied += 8;
3500 } else {
3501 PDEBUG(3, "Raw data buffer overrun!!");
3502 }
3503 }
3504 read += 8;
3505 }
3506 frame->bytes_recvd += copied;
3507 }
3508}
3509
3510static void
3511ov51x_isoc_irq(struct urb *urb)
3512{
3513 int i;
3514 struct usb_ov511 *ov;
3515 struct ov511_sbuf *sbuf;
3516
3517 if (!urb->context) {
3518 PDEBUG(4, "no context");
3519 return;
3520 }
3521
3522 sbuf = urb->context;
3523 ov = sbuf->ov;
3524
3525 if (!ov || !ov->dev || !ov->user) {
3526 PDEBUG(4, "no device, or not open");
3527 return;
3528 }
3529
3530 if (!ov->streaming) {
3531 PDEBUG(4, "hmmm... not streaming, but got interrupt");
3532 return;
3533 }
3534
3535 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
3536 PDEBUG(4, "URB unlinked");
3537 return;
3538 }
3539
3540 if (urb->status != -EINPROGRESS && urb->status != 0) {
3541 err("ERROR: urb->status=%d: %s", urb->status,
3542 symbolic(urb_errlist, urb->status));
3543 }
3544
3545
3546 PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
3547 urb->number_of_packets);
3548 for (i = 0; i < urb->number_of_packets; i++) {
3549
3550 if (ov->curframe >= 0) {
3551 int n = urb->iso_frame_desc[i].actual_length;
3552 int st = urb->iso_frame_desc[i].status;
3553 unsigned char *cdata;
3554
3555 urb->iso_frame_desc[i].actual_length = 0;
3556 urb->iso_frame_desc[i].status = 0;
3557
3558 cdata = urb->transfer_buffer
3559 + urb->iso_frame_desc[i].offset;
3560
3561 if (!n) {
3562 PDEBUG(4, "Zero-length packet");
3563 continue;
3564 }
3565
3566 if (st)
3567 PDEBUG(2, "data error: [%d] len=%d, status=%d",
3568 i, n, st);
3569
3570 if (ov->bclass == BCL_OV511)
3571 ov511_move_data(ov, cdata, n);
3572 else if (ov->bclass == BCL_OV518)
3573 ov518_move_data(ov, cdata, n);
3574 else
3575 err("Unknown bridge device (%d)", ov->bridge);
3576
3577 } else if (waitqueue_active(&ov->wq)) {
3578 wake_up_interruptible(&ov->wq);
3579 }
3580 }
3581
3582
3583 urb->dev = ov->dev;
3584 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
3585 err("usb_submit_urb() ret %d", i);
3586
3587 return;
3588}
3589
3590
3591
3592
3593
3594
3595
3596static int
3597ov51x_init_isoc(struct usb_ov511 *ov)
3598{
3599 struct urb *urb;
3600 int fx, err, n, i, size;
3601
3602 PDEBUG(3, "*** Initializing capture ***");
3603
3604 ov->curframe = -1;
3605
3606 if (ov->bridge == BRG_OV511) {
3607 if (cams == 1)
3608 size = 993;
3609 else if (cams == 2)
3610 size = 513;
3611 else if (cams == 3 || cams == 4)
3612 size = 257;
3613 else {
3614 err("\"cams\" parameter too high!");
3615 return -1;
3616 }
3617 } else if (ov->bridge == BRG_OV511PLUS) {
3618 if (cams == 1)
3619 size = 961;
3620 else if (cams == 2)
3621 size = 513;
3622 else if (cams == 3 || cams == 4)
3623 size = 257;
3624 else if (cams >= 5 && cams <= 8)
3625 size = 129;
3626 else if (cams >= 9 && cams <= 31)
3627 size = 33;
3628 else {
3629 err("\"cams\" parameter too high!");
3630 return -1;
3631 }
3632 } else if (ov->bclass == BCL_OV518) {
3633 if (cams == 1)
3634 size = 896;
3635 else if (cams == 2)
3636 size = 512;
3637 else if (cams == 3 || cams == 4)
3638 size = 256;
3639 else if (cams >= 5 && cams <= 8)
3640 size = 128;
3641 else {
3642 err("\"cams\" parameter too high!");
3643 return -1;
3644 }
3645 } else {
3646 err("invalid bridge type");
3647 return -1;
3648 }
3649
3650
3651 if (ov->bclass == BCL_OV518) {
3652 if (packetsize == -1) {
3653 ov518_set_packet_size(ov, 640);
3654 } else {
3655 dev_info(&ov->dev->dev, "Forcing packet size to %d\n",
3656 packetsize);
3657 ov518_set_packet_size(ov, packetsize);
3658 }
3659 } else {
3660 if (packetsize == -1) {
3661 ov511_set_packet_size(ov, size);
3662 } else {
3663 dev_info(&ov->dev->dev, "Forcing packet size to %d\n",
3664 packetsize);
3665 ov511_set_packet_size(ov, packetsize);
3666 }
3667 }
3668
3669 for (n = 0; n < OV511_NUMSBUF; n++) {
3670 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
3671 if (!urb) {
3672 err("init isoc: usb_alloc_urb ret. NULL");
3673 for (i = 0; i < n; i++)
3674 usb_free_urb(ov->sbuf[i].urb);
3675 return -ENOMEM;
3676 }
3677 ov->sbuf[n].urb = urb;
3678 urb->dev = ov->dev;
3679 urb->context = &ov->sbuf[n];
3680 urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
3681 urb->transfer_flags = URB_ISO_ASAP;
3682 urb->transfer_buffer = ov->sbuf[n].data;
3683 urb->complete = ov51x_isoc_irq;
3684 urb->number_of_packets = FRAMES_PER_DESC;
3685 urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
3686 urb->interval = 1;
3687 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
3688 urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
3689 urb->iso_frame_desc[fx].length = ov->packet_size;
3690 }
3691 }
3692
3693 ov->streaming = 1;
3694
3695 for (n = 0; n < OV511_NUMSBUF; n++) {
3696 ov->sbuf[n].urb->dev = ov->dev;
3697 err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
3698 if (err) {
3699 err("init isoc: usb_submit_urb(%d) ret %d", n, err);
3700 return err;
3701 }
3702 }
3703
3704 return 0;
3705}
3706
3707static void
3708ov51x_unlink_isoc(struct usb_ov511 *ov)
3709{
3710 int n;
3711
3712
3713 for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
3714 if (ov->sbuf[n].urb) {
3715 usb_kill_urb(ov->sbuf[n].urb);
3716 usb_free_urb(ov->sbuf[n].urb);
3717 ov->sbuf[n].urb = NULL;
3718 }
3719 }
3720}
3721
3722static void
3723ov51x_stop_isoc(struct usb_ov511 *ov)
3724{
3725 if (!ov->streaming || !ov->dev)
3726 return;
3727
3728 PDEBUG(3, "*** Stopping capture ***");
3729
3730 if (ov->bclass == BCL_OV518)
3731 ov518_set_packet_size(ov, 0);
3732 else
3733 ov511_set_packet_size(ov, 0);
3734
3735 ov->streaming = 0;
3736
3737 ov51x_unlink_isoc(ov);
3738}
3739
3740static int
3741ov51x_new_frame(struct usb_ov511 *ov, int framenum)
3742{
3743 struct ov511_frame *frame;
3744 int newnum;
3745
3746 PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
3747
3748 if (!ov->dev)
3749 return -1;
3750
3751
3752
3753 if (ov->curframe == -1) {
3754 newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
3755 if (ov->frame[newnum].grabstate == FRAME_READY)
3756 framenum = newnum;
3757 } else
3758 return 0;
3759
3760 frame = &ov->frame[framenum];
3761
3762 PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
3763 frame->width, frame->height);
3764
3765 frame->grabstate = FRAME_GRABBING;
3766 frame->scanstate = STATE_SCANNING;
3767 frame->snapshot = 0;
3768
3769 ov->curframe = framenum;
3770
3771
3772 if (frame->width > ov->maxwidth)
3773 frame->width = ov->maxwidth;
3774
3775 frame->width &= ~7L;
3776
3777 if (frame->height > ov->maxheight)
3778 frame->height = ov->maxheight;
3779
3780 frame->height &= ~3L;
3781
3782 return 0;
3783}
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796static void
3797ov51x_do_dealloc(struct usb_ov511 *ov)
3798{
3799 int i;
3800 PDEBUG(4, "entered");
3801
3802 if (ov->fbuf) {
3803 rvfree(ov->fbuf, OV511_NUMFRAMES
3804 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3805 ov->fbuf = NULL;
3806 }
3807
3808 vfree(ov->rawfbuf);
3809 ov->rawfbuf = NULL;
3810
3811 vfree(ov->tempfbuf);
3812 ov->tempfbuf = NULL;
3813
3814 for (i = 0; i < OV511_NUMSBUF; i++) {
3815 kfree(ov->sbuf[i].data);
3816 ov->sbuf[i].data = NULL;
3817 }
3818
3819 for (i = 0; i < OV511_NUMFRAMES; i++) {
3820 ov->frame[i].data = NULL;
3821 ov->frame[i].rawdata = NULL;
3822 ov->frame[i].tempdata = NULL;
3823 if (ov->frame[i].compbuf) {
3824 free_page((unsigned long) ov->frame[i].compbuf);
3825 ov->frame[i].compbuf = NULL;
3826 }
3827 }
3828
3829 PDEBUG(4, "buffer memory deallocated");
3830 ov->buf_state = BUF_NOT_ALLOCATED;
3831 PDEBUG(4, "leaving");
3832}
3833
3834static int
3835ov51x_alloc(struct usb_ov511 *ov)
3836{
3837 int i;
3838 const int w = ov->maxwidth;
3839 const int h = ov->maxheight;
3840 const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
3841 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3842
3843 PDEBUG(4, "entered");
3844 mutex_lock(&ov->buf_lock);
3845
3846 if (ov->buf_state == BUF_ALLOCATED)
3847 goto out;
3848
3849 ov->fbuf = rvmalloc(data_bufsize);
3850 if (!ov->fbuf)
3851 goto error;
3852
3853 ov->rawfbuf = vmalloc(raw_bufsize);
3854 if (!ov->rawfbuf)
3855 goto error;
3856
3857 memset(ov->rawfbuf, 0, raw_bufsize);
3858
3859 ov->tempfbuf = vmalloc(raw_bufsize);
3860 if (!ov->tempfbuf)
3861 goto error;
3862
3863 memset(ov->tempfbuf, 0, raw_bufsize);
3864
3865 for (i = 0; i < OV511_NUMSBUF; i++) {
3866 ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
3867 MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
3868 if (!ov->sbuf[i].data)
3869 goto error;
3870
3871 PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
3872 }
3873
3874 for (i = 0; i < OV511_NUMFRAMES; i++) {
3875 ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
3876 ov->frame[i].rawdata = ov->rawfbuf
3877 + i * MAX_RAW_DATA_SIZE(w, h);
3878 ov->frame[i].tempdata = ov->tempfbuf
3879 + i * MAX_RAW_DATA_SIZE(w, h);
3880
3881 ov->frame[i].compbuf =
3882 (unsigned char *) __get_free_page(GFP_KERNEL);
3883 if (!ov->frame[i].compbuf)
3884 goto error;
3885
3886 PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
3887 }
3888
3889 ov->buf_state = BUF_ALLOCATED;
3890out:
3891 mutex_unlock(&ov->buf_lock);
3892 PDEBUG(4, "leaving");
3893 return 0;
3894error:
3895 ov51x_do_dealloc(ov);
3896 mutex_unlock(&ov->buf_lock);
3897 PDEBUG(4, "errored");
3898 return -ENOMEM;
3899}
3900
3901static void
3902ov51x_dealloc(struct usb_ov511 *ov)
3903{
3904 PDEBUG(4, "entered");
3905 mutex_lock(&ov->buf_lock);
3906 ov51x_do_dealloc(ov);
3907 mutex_unlock(&ov->buf_lock);
3908 PDEBUG(4, "leaving");
3909}
3910
3911
3912
3913
3914
3915
3916
3917static int
3918ov51x_v4l1_open(struct file *file)
3919{
3920 struct video_device *vdev = video_devdata(file);
3921 struct usb_ov511 *ov = video_get_drvdata(vdev);
3922 int err, i;
3923
3924 PDEBUG(4, "opening");
3925
3926 mutex_lock(&ov->lock);
3927
3928 err = -EBUSY;
3929 if (ov->user)
3930 goto out;
3931
3932 ov->sub_flag = 0;
3933
3934
3935 err = ov51x_set_default_params(ov);
3936 if (err < 0)
3937 goto out;
3938
3939
3940 for (i = 0; i < OV511_NUMFRAMES; i++) {
3941 ov->frame[i].grabstate = FRAME_UNUSED;
3942 ov->frame[i].bytes_read = 0;
3943 }
3944
3945
3946
3947 if (ov->compress && !ov->decomp_ops) {
3948 err = request_decompressor(ov);
3949 if (err && !dumppix)
3950 goto out;
3951 }
3952
3953 err = ov51x_alloc(ov);
3954 if (err < 0)
3955 goto out;
3956
3957 err = ov51x_init_isoc(ov);
3958 if (err) {
3959 ov51x_dealloc(ov);
3960 goto out;
3961 }
3962
3963 ov->user++;
3964 file->private_data = vdev;
3965
3966 if (ov->led_policy == LED_AUTO)
3967 ov51x_led_control(ov, 1);
3968
3969out:
3970 mutex_unlock(&ov->lock);
3971 return err;
3972}
3973
3974static int
3975ov51x_v4l1_close(struct file *file)
3976{
3977 struct video_device *vdev = file->private_data;
3978 struct usb_ov511 *ov = video_get_drvdata(vdev);
3979
3980 PDEBUG(4, "ov511_close");
3981
3982 mutex_lock(&ov->lock);
3983
3984 ov->user--;
3985 ov51x_stop_isoc(ov);
3986
3987 if (ov->led_policy == LED_AUTO)
3988 ov51x_led_control(ov, 0);
3989
3990 if (ov->dev)
3991 ov51x_dealloc(ov);
3992
3993 mutex_unlock(&ov->lock);
3994
3995
3996
3997 if (!ov->dev) {
3998 mutex_lock(&ov->cbuf_lock);
3999 kfree(ov->cbuf);
4000 ov->cbuf = NULL;
4001 mutex_unlock(&ov->cbuf_lock);
4002
4003 ov51x_dealloc(ov);
4004 kfree(ov);
4005 ov = NULL;
4006 }
4007
4008 file->private_data = NULL;
4009 return 0;
4010}
4011
4012
4013static long
4014ov51x_v4l1_ioctl_internal(struct file *file, unsigned int cmd, void *arg)
4015{
4016 struct video_device *vdev = file->private_data;
4017 struct usb_ov511 *ov = video_get_drvdata(vdev);
4018 PDEBUG(5, "IOCtl: 0x%X", cmd);
4019
4020 if (!ov->dev)
4021 return -EIO;
4022
4023 switch (cmd) {
4024 case VIDIOCGCAP:
4025 {
4026 struct video_capability *b = arg;
4027
4028 PDEBUG(4, "VIDIOCGCAP");
4029
4030 memset(b, 0, sizeof(struct video_capability));
4031 sprintf(b->name, "%s USB Camera",
4032 symbolic(brglist, ov->bridge));
4033 b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
4034 b->channels = ov->num_inputs;
4035 b->audios = 0;
4036 b->maxwidth = ov->maxwidth;
4037 b->maxheight = ov->maxheight;
4038 b->minwidth = ov->minwidth;
4039 b->minheight = ov->minheight;
4040
4041 return 0;
4042 }
4043 case VIDIOCGCHAN:
4044 {
4045 struct video_channel *v = arg;
4046
4047 PDEBUG(4, "VIDIOCGCHAN");
4048
4049 if ((unsigned)(v->channel) >= ov->num_inputs) {
4050 err("Invalid channel (%d)", v->channel);
4051 return -EINVAL;
4052 }
4053
4054 v->norm = ov->norm;
4055 v->type = VIDEO_TYPE_CAMERA;
4056 v->flags = 0;
4057
4058 v->tuners = 0;
4059 decoder_get_input_name(ov, v->channel, v->name);
4060
4061 return 0;
4062 }
4063 case VIDIOCSCHAN:
4064 {
4065 struct video_channel *v = arg;
4066 int err;
4067
4068 PDEBUG(4, "VIDIOCSCHAN");
4069
4070
4071 if (!ov->has_decoder) {
4072 if (v->channel == 0)
4073 return 0;
4074 else
4075 return -EINVAL;
4076 }
4077
4078 if (v->norm != VIDEO_MODE_PAL &&
4079 v->norm != VIDEO_MODE_NTSC &&
4080 v->norm != VIDEO_MODE_SECAM &&
4081 v->norm != VIDEO_MODE_AUTO) {
4082 err("Invalid norm (%d)", v->norm);
4083 return -EINVAL;
4084 }
4085
4086 if ((unsigned)(v->channel) >= ov->num_inputs) {
4087 err("Invalid channel (%d)", v->channel);
4088 return -EINVAL;
4089 }
4090
4091 err = decoder_set_input(ov, v->channel);
4092 if (err)
4093 return err;
4094
4095 err = decoder_set_norm(ov, v->norm);
4096 if (err)
4097 return err;
4098
4099 return 0;
4100 }
4101 case VIDIOCGPICT:
4102 {
4103 struct video_picture *p = arg;
4104
4105 PDEBUG(4, "VIDIOCGPICT");
4106
4107 memset(p, 0, sizeof(struct video_picture));
4108 if (sensor_get_picture(ov, p))
4109 return -EIO;
4110
4111
4112 p->depth = ov->frame[0].depth;
4113 p->palette = ov->frame[0].format;
4114
4115 return 0;
4116 }
4117 case VIDIOCSPICT:
4118 {
4119 struct video_picture *p = arg;
4120 int i, rc;
4121
4122 PDEBUG(4, "VIDIOCSPICT");
4123
4124 if (!get_depth(p->palette))
4125 return -EINVAL;
4126
4127 if (sensor_set_picture(ov, p))
4128 return -EIO;
4129
4130 if (force_palette && p->palette != force_palette) {
4131 dev_info(&ov->dev->dev, "Palette rejected (%s)\n",
4132 symbolic(v4l1_plist, p->palette));
4133 return -EINVAL;
4134 }
4135
4136
4137 if (p->palette != ov->frame[0].format) {
4138 PDEBUG(4, "Detected format change");
4139
4140 rc = ov51x_wait_frames_inactive(ov);
4141 if (rc)
4142 return rc;
4143
4144 mode_init_regs(ov, ov->frame[0].width,
4145 ov->frame[0].height, p->palette, ov->sub_flag);
4146 }
4147
4148 PDEBUG(4, "Setting depth=%d, palette=%s",
4149 p->depth, symbolic(v4l1_plist, p->palette));
4150
4151 for (i = 0; i < OV511_NUMFRAMES; i++) {
4152 ov->frame[i].depth = p->depth;
4153 ov->frame[i].format = p->palette;
4154 }
4155
4156 return 0;
4157 }
4158 case VIDIOCGCAPTURE:
4159 {
4160 int *vf = arg;
4161
4162 PDEBUG(4, "VIDIOCGCAPTURE");
4163
4164 ov->sub_flag = *vf;
4165 return 0;
4166 }
4167 case VIDIOCSCAPTURE:
4168 {
4169 struct video_capture *vc = arg;
4170
4171 PDEBUG(4, "VIDIOCSCAPTURE");
4172
4173 if (vc->flags)
4174 return -EINVAL;
4175 if (vc->decimation)
4176 return -EINVAL;
4177
4178 vc->x &= ~3L;
4179 vc->y &= ~1L;
4180 vc->y &= ~31L;
4181
4182 if (vc->width == 0)
4183 vc->width = 32;
4184
4185 vc->height /= 16;
4186 vc->height *= 16;
4187 if (vc->height == 0)
4188 vc->height = 16;
4189
4190 ov->subx = vc->x;
4191 ov->suby = vc->y;
4192 ov->subw = vc->width;
4193 ov->subh = vc->height;
4194
4195 return 0;
4196 }
4197 case VIDIOCSWIN:
4198 {
4199 struct video_window *vw = arg;
4200 int i, rc;
4201
4202 PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
4203
4204#if 0
4205 if (vw->flags)
4206 return -EINVAL;
4207 if (vw->clipcount)
4208 return -EINVAL;
4209 if (vw->height != ov->maxheight)
4210 return -EINVAL;
4211 if (vw->width != ov->maxwidth)
4212 return -EINVAL;
4213#endif
4214
4215 rc = ov51x_wait_frames_inactive(ov);
4216 if (rc)
4217 return rc;
4218
4219 rc = mode_init_regs(ov, vw->width, vw->height,
4220 ov->frame[0].format, ov->sub_flag);
4221 if (rc < 0)
4222 return rc;
4223
4224 for (i = 0; i < OV511_NUMFRAMES; i++) {
4225 ov->frame[i].width = vw->width;
4226 ov->frame[i].height = vw->height;
4227 }
4228
4229 return 0;
4230 }
4231 case VIDIOCGWIN:
4232 {
4233 struct video_window *vw = arg;
4234
4235 memset(vw, 0, sizeof(struct video_window));
4236 vw->x = 0;
4237 vw->y = 0;
4238 vw->width = ov->frame[0].width;
4239 vw->height = ov->frame[0].height;
4240 vw->flags = 30;
4241
4242 PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
4243
4244 return 0;
4245 }
4246 case VIDIOCGMBUF:
4247 {
4248 struct video_mbuf *vm = arg;
4249 int i;
4250
4251 PDEBUG(4, "VIDIOCGMBUF");
4252
4253 memset(vm, 0, sizeof(struct video_mbuf));
4254 vm->size = OV511_NUMFRAMES
4255 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4256 vm->frames = OV511_NUMFRAMES;
4257
4258 vm->offsets[0] = 0;
4259 for (i = 1; i < OV511_NUMFRAMES; i++) {
4260 vm->offsets[i] = vm->offsets[i-1]
4261 + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4262 }
4263
4264 return 0;
4265 }
4266 case VIDIOCMCAPTURE:
4267 {
4268 struct video_mmap *vm = arg;
4269 int rc, depth;
4270 unsigned int f = vm->frame;
4271
4272 PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
4273 vm->height, symbolic(v4l1_plist, vm->format));
4274
4275 depth = get_depth(vm->format);
4276 if (!depth) {
4277 PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
4278 symbolic(v4l1_plist, vm->format));
4279 return -EINVAL;
4280 }
4281
4282 if (f >= OV511_NUMFRAMES) {
4283 err("VIDIOCMCAPTURE: invalid frame (%d)", f);
4284 return -EINVAL;
4285 }
4286
4287 if (vm->width > ov->maxwidth
4288 || vm->height > ov->maxheight) {
4289 err("VIDIOCMCAPTURE: requested dimensions too big");
4290 return -EINVAL;
4291 }
4292
4293 if (ov->frame[f].grabstate == FRAME_GRABBING) {
4294 PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
4295 return -EBUSY;
4296 }
4297
4298 if (force_palette && (vm->format != force_palette)) {
4299 PDEBUG(2, "palette rejected (%s)",
4300 symbolic(v4l1_plist, vm->format));
4301 return -EINVAL;
4302 }
4303
4304 if ((ov->frame[f].width != vm->width) ||
4305 (ov->frame[f].height != vm->height) ||
4306 (ov->frame[f].format != vm->format) ||
4307 (ov->frame[f].sub_flag != ov->sub_flag) ||
4308 (ov->frame[f].depth != depth)) {
4309 PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
4310
4311 rc = ov51x_wait_frames_inactive(ov);
4312 if (rc)
4313 return rc;
4314
4315 rc = mode_init_regs(ov, vm->width, vm->height,
4316 vm->format, ov->sub_flag);
4317#if 0
4318 if (rc < 0) {
4319 PDEBUG(1, "Got error while initializing regs ");
4320 return ret;
4321 }
4322#endif
4323 ov->frame[f].width = vm->width;
4324 ov->frame[f].height = vm->height;
4325 ov->frame[f].format = vm->format;
4326 ov->frame[f].sub_flag = ov->sub_flag;
4327 ov->frame[f].depth = depth;
4328 }
4329
4330
4331 ov->frame[f].grabstate = FRAME_READY;
4332
4333 PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
4334
4335 return ov51x_new_frame(ov, f);
4336 }
4337 case VIDIOCSYNC:
4338 {
4339 unsigned int fnum = *((unsigned int *) arg);
4340 struct ov511_frame *frame;
4341 int rc;
4342
4343 if (fnum >= OV511_NUMFRAMES) {
4344 err("VIDIOCSYNC: invalid frame (%d)", fnum);
4345 return -EINVAL;
4346 }
4347
4348 frame = &ov->frame[fnum];
4349
4350 PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
4351 frame->grabstate);
4352
4353 switch (frame->grabstate) {
4354 case FRAME_UNUSED:
4355 return -EINVAL;
4356 case FRAME_READY:
4357 case FRAME_GRABBING:
4358 case FRAME_ERROR:
4359redo:
4360 if (!ov->dev)
4361 return -EIO;
4362
4363 rc = wait_event_interruptible(frame->wq,
4364 (frame->grabstate == FRAME_DONE)
4365 || (frame->grabstate == FRAME_ERROR));
4366
4367 if (rc)
4368 return rc;
4369
4370 if (frame->grabstate == FRAME_ERROR) {
4371 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4372 return rc;
4373 goto redo;
4374 }
4375
4376 case FRAME_DONE:
4377 if (ov->snap_enabled && !frame->snapshot) {
4378 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4379 return rc;
4380 goto redo;
4381 }
4382
4383 frame->grabstate = FRAME_UNUSED;
4384
4385
4386
4387 if ((ov->snap_enabled) && (frame->snapshot)) {
4388 frame->snapshot = 0;
4389 ov51x_clear_snapshot(ov);
4390 }
4391
4392
4393 ov51x_postprocess(ov, frame);
4394
4395 break;
4396 }
4397
4398 return 0;
4399 }
4400 case VIDIOCGFBUF:
4401 {
4402 struct video_buffer *vb = arg;
4403
4404 PDEBUG(4, "VIDIOCGFBUF");
4405
4406 memset(vb, 0, sizeof(struct video_buffer));
4407
4408 return 0;
4409 }
4410 case VIDIOCGUNIT:
4411 {
4412 struct video_unit *vu = arg;
4413
4414 PDEBUG(4, "VIDIOCGUNIT");
4415
4416 memset(vu, 0, sizeof(struct video_unit));
4417
4418 vu->video = ov->vdev->minor;
4419 vu->vbi = VIDEO_NO_UNIT;
4420 vu->radio = VIDEO_NO_UNIT;
4421 vu->audio = VIDEO_NO_UNIT;
4422 vu->teletext = VIDEO_NO_UNIT;
4423
4424 return 0;
4425 }
4426 case OV511IOC_WI2C:
4427 {
4428 struct ov511_i2c_struct *w = arg;
4429
4430 return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
4431 }
4432 case OV511IOC_RI2C:
4433 {
4434 struct ov511_i2c_struct *r = arg;
4435 int rc;
4436
4437 rc = i2c_r_slave(ov, r->slave, r->reg);
4438 if (rc < 0)
4439 return rc;
4440
4441 r->value = rc;
4442 return 0;
4443 }
4444 default:
4445 PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
4446 return -ENOIOCTLCMD;
4447 }
4448
4449 return 0;
4450}
4451
4452static long
4453ov51x_v4l1_ioctl(struct file *file,
4454 unsigned int cmd, unsigned long arg)
4455{
4456 struct video_device *vdev = file->private_data;
4457 struct usb_ov511 *ov = video_get_drvdata(vdev);
4458 int rc;
4459
4460 if (mutex_lock_interruptible(&ov->lock))
4461 return -EINTR;
4462
4463 rc = video_usercopy(file, cmd, arg, ov51x_v4l1_ioctl_internal);
4464
4465 mutex_unlock(&ov->lock);
4466 return rc;
4467}
4468
4469static ssize_t
4470ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4471{
4472 struct video_device *vdev = file->private_data;
4473 int noblock = file->f_flags&O_NONBLOCK;
4474 unsigned long count = cnt;
4475 struct usb_ov511 *ov = video_get_drvdata(vdev);
4476 int i, rc = 0, frmx = -1;
4477 struct ov511_frame *frame;
4478
4479 if (mutex_lock_interruptible(&ov->lock))
4480 return -EINTR;
4481
4482 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
4483
4484 if (!vdev || !buf) {
4485 rc = -EFAULT;
4486 goto error;
4487 }
4488
4489 if (!ov->dev) {
4490 rc = -EIO;
4491 goto error;
4492 }
4493
4494
4495
4496 if (ov->frame[0].grabstate >= FRAME_DONE)
4497 frmx = 0;
4498 else if (ov->frame[1].grabstate >= FRAME_DONE)
4499 frmx = 1;
4500
4501
4502 if (noblock && (frmx == -1)) {
4503 rc = -EAGAIN;
4504 goto error;
4505 }
4506
4507
4508
4509 if (frmx == -1) {
4510 if (ov->frame[0].grabstate == FRAME_GRABBING)
4511 frmx = 0;
4512 else if (ov->frame[1].grabstate == FRAME_GRABBING)
4513 frmx = 1;
4514 }
4515
4516
4517 if (frmx == -1) {
4518 if ((rc = ov51x_new_frame(ov, frmx = 0))) {
4519 err("read: ov51x_new_frame error");
4520 goto error;
4521 }
4522 }
4523
4524 frame = &ov->frame[frmx];
4525
4526restart:
4527 if (!ov->dev) {
4528 rc = -EIO;
4529 goto error;
4530 }
4531
4532
4533 PDEBUG(4, "Waiting image grabbing");
4534 rc = wait_event_interruptible(frame->wq,
4535 (frame->grabstate == FRAME_DONE)
4536 || (frame->grabstate == FRAME_ERROR));
4537
4538 if (rc)
4539 goto error;
4540
4541 PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
4542 PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
4543
4544 if (frame->grabstate == FRAME_ERROR) {
4545 frame->bytes_read = 0;
4546 err("** ick! ** Errored frame %d", ov->curframe);
4547 if (ov51x_new_frame(ov, frmx)) {
4548 err("read: ov51x_new_frame error");
4549 goto error;
4550 }
4551 goto restart;
4552 }
4553
4554
4555
4556 if (ov->snap_enabled)
4557 PDEBUG(4, "Waiting snapshot frame");
4558 if (ov->snap_enabled && !frame->snapshot) {
4559 frame->bytes_read = 0;
4560 if ((rc = ov51x_new_frame(ov, frmx))) {
4561 err("read: ov51x_new_frame error");
4562 goto error;
4563 }
4564 goto restart;
4565 }
4566
4567
4568 if (ov->snap_enabled && frame->snapshot) {
4569 frame->snapshot = 0;
4570 ov51x_clear_snapshot(ov);
4571 }
4572
4573
4574 ov51x_postprocess(ov, frame);
4575
4576 PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
4577 frame->bytes_read,
4578 get_frame_length(frame));
4579
4580
4581
4582
4583
4584
4585
4586 count = get_frame_length(frame);
4587
4588 PDEBUG(4, "Copy to user space: %ld bytes", count);
4589 if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
4590 PDEBUG(4, "Copy failed! %d bytes not copied", i);
4591 rc = -EFAULT;
4592 goto error;
4593 }
4594
4595 frame->bytes_read += count;
4596 PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
4597 count, frame->bytes_read);
4598
4599
4600 if (frame->bytes_read
4601 >= get_frame_length(frame)) {
4602 frame->bytes_read = 0;
4603
4604
4605
4606 ov->frame[frmx].grabstate = FRAME_UNUSED;
4607 if ((rc = ov51x_new_frame(ov, !frmx))) {
4608 err("ov51x_new_frame returned error");
4609 goto error;
4610 }
4611 }
4612
4613 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4614
4615 mutex_unlock(&ov->lock);
4616 return count;
4617
4618error:
4619 mutex_unlock(&ov->lock);
4620 return rc;
4621}
4622
4623static int
4624ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4625{
4626 struct video_device *vdev = file->private_data;
4627 unsigned long start = vma->vm_start;
4628 unsigned long size = vma->vm_end - vma->vm_start;
4629 struct usb_ov511 *ov = video_get_drvdata(vdev);
4630 unsigned long page, pos;
4631
4632 if (ov->dev == NULL)
4633 return -EIO;
4634
4635 PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
4636
4637 if (size > (((OV511_NUMFRAMES
4638 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
4639 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4640 return -EINVAL;
4641
4642 if (mutex_lock_interruptible(&ov->lock))
4643 return -EINTR;
4644
4645 pos = (unsigned long)ov->fbuf;
4646 while (size > 0) {
4647 page = vmalloc_to_pfn((void *)pos);
4648 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4649 mutex_unlock(&ov->lock);
4650 return -EAGAIN;
4651 }
4652 start += PAGE_SIZE;
4653 pos += PAGE_SIZE;
4654 if (size > PAGE_SIZE)
4655 size -= PAGE_SIZE;
4656 else
4657 size = 0;
4658 }
4659
4660 mutex_unlock(&ov->lock);
4661 return 0;
4662}
4663
4664static const struct v4l2_file_operations ov511_fops = {
4665 .owner = THIS_MODULE,
4666 .open = ov51x_v4l1_open,
4667 .release = ov51x_v4l1_close,
4668 .read = ov51x_v4l1_read,
4669 .mmap = ov51x_v4l1_mmap,
4670 .ioctl = ov51x_v4l1_ioctl,
4671};
4672
4673static struct video_device vdev_template = {
4674 .name = "OV511 USB Camera",
4675 .fops = &ov511_fops,
4676 .release = video_device_release,
4677 .minor = -1,
4678};
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689static int
4690ov7xx0_configure(struct usb_ov511 *ov)
4691{
4692 int i, success;
4693 int rc;
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706 static struct ov511_regvals aRegvalsNorm7610[] = {
4707 { OV511_I2C_BUS, 0x10, 0xff },
4708 { OV511_I2C_BUS, 0x16, 0x06 },
4709 { OV511_I2C_BUS, 0x28, 0x24 },
4710 { OV511_I2C_BUS, 0x2b, 0xac },
4711 { OV511_I2C_BUS, 0x12, 0x00 },
4712 { OV511_I2C_BUS, 0x38, 0x81 },
4713 { OV511_I2C_BUS, 0x28, 0x24 },
4714 { OV511_I2C_BUS, 0x0f, 0x85 },
4715 { OV511_I2C_BUS, 0x15, 0x01 },
4716 { OV511_I2C_BUS, 0x20, 0x1c },
4717 { OV511_I2C_BUS, 0x23, 0x2a },
4718 { OV511_I2C_BUS, 0x24, 0x10 },
4719 { OV511_I2C_BUS, 0x25, 0x8a },
4720 { OV511_I2C_BUS, 0x26, 0xa2 },
4721 { OV511_I2C_BUS, 0x27, 0xc2 },
4722 { OV511_I2C_BUS, 0x2a, 0x04 },
4723 { OV511_I2C_BUS, 0x2c, 0xfe },
4724 { OV511_I2C_BUS, 0x2d, 0x93 },
4725 { OV511_I2C_BUS, 0x30, 0x71 },
4726 { OV511_I2C_BUS, 0x31, 0x60 },
4727 { OV511_I2C_BUS, 0x32, 0x26 },
4728 { OV511_I2C_BUS, 0x33, 0x20 },
4729 { OV511_I2C_BUS, 0x34, 0x48 },
4730 { OV511_I2C_BUS, 0x12, 0x24 },
4731 { OV511_I2C_BUS, 0x11, 0x01 },
4732 { OV511_I2C_BUS, 0x0c, 0x24 },
4733 { OV511_I2C_BUS, 0x0d, 0x24 },
4734 { OV511_DONE_BUS, 0x0, 0x00 },
4735 };
4736
4737 static struct ov511_regvals aRegvalsNorm7620[] = {
4738 { OV511_I2C_BUS, 0x00, 0x00 },
4739 { OV511_I2C_BUS, 0x01, 0x80 },
4740 { OV511_I2C_BUS, 0x02, 0x80 },
4741 { OV511_I2C_BUS, 0x03, 0xc0 },
4742 { OV511_I2C_BUS, 0x06, 0x60 },
4743 { OV511_I2C_BUS, 0x07, 0x00 },
4744 { OV511_I2C_BUS, 0x0c, 0x24 },
4745 { OV511_I2C_BUS, 0x0c, 0x24 },
4746 { OV511_I2C_BUS, 0x0d, 0x24 },
4747 { OV511_I2C_BUS, 0x11, 0x01 },
4748 { OV511_I2C_BUS, 0x12, 0x24 },
4749 { OV511_I2C_BUS, 0x13, 0x01 },
4750 { OV511_I2C_BUS, 0x14, 0x84 },
4751 { OV511_I2C_BUS, 0x15, 0x01 },
4752 { OV511_I2C_BUS, 0x16, 0x03 },
4753 { OV511_I2C_BUS, 0x17, 0x2f },
4754 { OV511_I2C_BUS, 0x18, 0xcf },
4755 { OV511_I2C_BUS, 0x19, 0x06 },
4756 { OV511_I2C_BUS, 0x1a, 0xf5 },
4757 { OV511_I2C_BUS, 0x1b, 0x00 },
4758 { OV511_I2C_BUS, 0x20, 0x18 },
4759 { OV511_I2C_BUS, 0x21, 0x80 },
4760 { OV511_I2C_BUS, 0x22, 0x80 },
4761 { OV511_I2C_BUS, 0x23, 0x00 },
4762 { OV511_I2C_BUS, 0x26, 0xa2 },
4763 { OV511_I2C_BUS, 0x27, 0xea },
4764 { OV511_I2C_BUS, 0x28, 0x20 },
4765 { OV511_I2C_BUS, 0x29, 0x00 },
4766 { OV511_I2C_BUS, 0x2a, 0x10 },
4767 { OV511_I2C_BUS, 0x2b, 0x00 },
4768 { OV511_I2C_BUS, 0x2c, 0x88 },
4769 { OV511_I2C_BUS, 0x2d, 0x91 },
4770 { OV511_I2C_BUS, 0x2e, 0x80 },
4771 { OV511_I2C_BUS, 0x2f, 0x44 },
4772 { OV511_I2C_BUS, 0x60, 0x27 },
4773 { OV511_I2C_BUS, 0x61, 0x02 },
4774 { OV511_I2C_BUS, 0x62, 0x5f },
4775 { OV511_I2C_BUS, 0x63, 0xd5 },
4776 { OV511_I2C_BUS, 0x64, 0x57 },
4777 { OV511_I2C_BUS, 0x65, 0x83 },
4778 { OV511_I2C_BUS, 0x66, 0x55 },
4779 { OV511_I2C_BUS, 0x67, 0x92 },
4780 { OV511_I2C_BUS, 0x68, 0xcf },
4781 { OV511_I2C_BUS, 0x69, 0x76 },
4782 { OV511_I2C_BUS, 0x6a, 0x22 },
4783 { OV511_I2C_BUS, 0x6b, 0x00 },
4784 { OV511_I2C_BUS, 0x6c, 0x02 },
4785 { OV511_I2C_BUS, 0x6d, 0x44 },
4786 { OV511_I2C_BUS, 0x6e, 0x80 },
4787 { OV511_I2C_BUS, 0x6f, 0x1d },
4788 { OV511_I2C_BUS, 0x70, 0x8b },
4789 { OV511_I2C_BUS, 0x71, 0x00 },
4790 { OV511_I2C_BUS, 0x72, 0x14 },
4791 { OV511_I2C_BUS, 0x73, 0x54 },
4792 { OV511_I2C_BUS, 0x74, 0x00 },
4793 { OV511_I2C_BUS, 0x75, 0x8e },
4794 { OV511_I2C_BUS, 0x76, 0x00 },
4795 { OV511_I2C_BUS, 0x77, 0xff },
4796 { OV511_I2C_BUS, 0x78, 0x80 },
4797 { OV511_I2C_BUS, 0x79, 0x80 },
4798 { OV511_I2C_BUS, 0x7a, 0x80 },
4799 { OV511_I2C_BUS, 0x7b, 0xe2 },
4800 { OV511_I2C_BUS, 0x7c, 0x00 },
4801 { OV511_DONE_BUS, 0x0, 0x00 },
4802 };
4803
4804 PDEBUG(4, "starting configuration");
4805
4806
4807 ov->primary_i2c_slave = OV7xx0_SID;
4808 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
4809 return -1;
4810
4811 if (init_ov_sensor(ov) >= 0) {
4812 PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
4813 } else {
4814
4815 if (i2c_w(ov, 0x12, 0x80) < 0)
4816 return -1;
4817
4818
4819 msleep(150);
4820
4821 i = 0;
4822 success = 0;
4823 while (i <= i2c_detect_tries) {
4824 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
4825 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
4826 success = 1;
4827 break;
4828 } else {
4829 i++;
4830 }
4831 }
4832
4833
4834
4835 if ((i >= i2c_detect_tries) && (success == 0)) {
4836 err("Failed to read sensor ID. You might not have an");
4837 err("OV7610/20, or it may be not responding. Report");
4838 err("this to " EMAIL);
4839 err("This is only a warning. You can attempt to use");
4840 err("your camera anyway");
4841
4842
4843 } else {
4844 PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
4845 }
4846 }
4847
4848
4849 rc = i2c_r(ov, OV7610_REG_COM_I);
4850
4851 if (rc < 0) {
4852 err("Error detecting sensor type");
4853 return -1;
4854 } else if ((rc & 3) == 3) {
4855 dev_info(&ov->dev->dev, "Sensor is an OV7610\n");
4856 ov->sensor = SEN_OV7610;
4857 } else if ((rc & 3) == 1) {
4858
4859 if (i2c_r(ov, 0x15) & 1)
4860 dev_info(&ov->dev->dev, "Sensor is an OV7620AE\n");
4861 else
4862 dev_info(&ov->dev->dev, "Sensor is an OV76BE\n");
4863
4864
4865
4866
4867 if (ov->bridge == BRG_OV511PLUS) {
4868 dev_info(&ov->dev->dev,
4869 "Enabling 511+/7620AE workaround\n");
4870 ov->sensor = SEN_OV7620;
4871 } else {
4872 ov->sensor = SEN_OV76BE;
4873 }
4874 } else if ((rc & 3) == 0) {
4875 dev_info(&ov->dev->dev, "Sensor is an OV7620\n");
4876 ov->sensor = SEN_OV7620;
4877 } else {
4878 err("Unknown image sensor version: %d", rc & 3);
4879 return -1;
4880 }
4881
4882 if (ov->sensor == SEN_OV7620) {
4883 PDEBUG(4, "Writing 7620 registers");
4884 if (write_regvals(ov, aRegvalsNorm7620))
4885 return -1;
4886 } else {
4887 PDEBUG(4, "Writing 7610 registers");
4888 if (write_regvals(ov, aRegvalsNorm7610))
4889 return -1;
4890 }
4891
4892
4893 ov->maxwidth = 640;
4894 ov->maxheight = 480;
4895 ov->minwidth = 64;
4896 ov->minheight = 48;
4897
4898
4899 ov->brightness = 0x80 << 8;
4900 ov->contrast = 0x80 << 8;
4901 ov->colour = 0x80 << 8;
4902 ov->hue = 0x80 << 8;
4903
4904 return 0;
4905}
4906
4907
4908static int
4909ov6xx0_configure(struct usb_ov511 *ov)
4910{
4911 int rc;
4912
4913 static struct ov511_regvals aRegvalsNorm6x20[] = {
4914 { OV511_I2C_BUS, 0x12, 0x80 },
4915 { OV511_I2C_BUS, 0x11, 0x01 },
4916 { OV511_I2C_BUS, 0x03, 0x60 },
4917 { OV511_I2C_BUS, 0x05, 0x7f },
4918 { OV511_I2C_BUS, 0x07, 0xa8 },
4919
4920 { OV511_I2C_BUS, 0x0c, 0x24 },
4921 { OV511_I2C_BUS, 0x0d, 0x24 },
4922 { OV511_I2C_BUS, 0x0f, 0x15 },
4923 { OV511_I2C_BUS, 0x10, 0x75 },
4924 { OV511_I2C_BUS, 0x12, 0x24 },
4925 { OV511_I2C_BUS, 0x14, 0x04 },
4926
4927 { OV511_I2C_BUS, 0x16, 0x06 },
4928
4929 { OV511_I2C_BUS, 0x26, 0xb2 },
4930
4931 { OV511_I2C_BUS, 0x28, 0x05 },
4932 { OV511_I2C_BUS, 0x2a, 0x04 },
4933
4934 { OV511_I2C_BUS, 0x2d, 0x99 },
4935 { OV511_I2C_BUS, 0x33, 0xa0 },
4936 { OV511_I2C_BUS, 0x34, 0xd2 },
4937 { OV511_I2C_BUS, 0x38, 0x8b },
4938 { OV511_I2C_BUS, 0x39, 0x40 },
4939
4940 { OV511_I2C_BUS, 0x3c, 0x39 },
4941 { OV511_I2C_BUS, 0x3c, 0x3c },
4942 { OV511_I2C_BUS, 0x3c, 0x24 },
4943
4944 { OV511_I2C_BUS, 0x3d, 0x80 },
4945
4946
4947 { OV511_I2C_BUS, 0x4a, 0x80 },
4948 { OV511_I2C_BUS, 0x4b, 0x80 },
4949 { OV511_I2C_BUS, 0x4d, 0xd2 },
4950 { OV511_I2C_BUS, 0x4e, 0xc1 },
4951 { OV511_I2C_BUS, 0x4f, 0x04 },
4952
4953
4954 { OV511_DONE_BUS, 0x0, 0x00 },
4955 };
4956
4957 static struct ov511_regvals aRegvalsNorm6x30[] = {
4958 { OV511_I2C_BUS, 0x12, 0x80 },
4959 { OV511_I2C_BUS, 0x11, 0x00 },
4960 { OV511_I2C_BUS, 0x03, 0x60 },
4961 { OV511_I2C_BUS, 0x05, 0x7f },
4962 { OV511_I2C_BUS, 0x07, 0xa8 },
4963
4964 { OV511_I2C_BUS, 0x0c, 0x24 },
4965 { OV511_I2C_BUS, 0x0d, 0x24 },
4966 { OV511_I2C_BUS, 0x0e, 0x20 },
4967
4968 { OV511_I2C_BUS, 0x16, 0x03 },
4969
4970
4971 { OV511_I2C_BUS, 0x23, 0xc0 },
4972 { OV511_I2C_BUS, 0x25, 0x9a },
4973
4974
4975
4976
4977
4978
4979 { OV511_I2C_BUS, 0x2a, 0x04 },
4980
4981 { OV511_I2C_BUS, 0x2d, 0x99 },
4982
4983
4984
4985
4986
4987
4988
4989 { OV511_I2C_BUS, 0x3d, 0x80 },
4990
4991
4992
4993
4994
4995
4996 { OV511_I2C_BUS, 0x4d, 0x10 },
4997 { OV511_I2C_BUS, 0x4e, 0x40 },
4998
4999
5000 { OV511_I2C_BUS, 0x4f, 0x07 },
5001
5002 { OV511_I2C_BUS, 0x54, 0x23 },
5003 { OV511_I2C_BUS, 0x57, 0x81 },
5004 { OV511_I2C_BUS, 0x59, 0x01 },
5005 { OV511_I2C_BUS, 0x5a, 0x2c },
5006 { OV511_I2C_BUS, 0x5b, 0x0f },
5007
5008 { OV511_DONE_BUS, 0x0, 0x00 },
5009 };
5010
5011 PDEBUG(4, "starting sensor configuration");
5012
5013 if (init_ov_sensor(ov) < 0) {
5014 err("Failed to read sensor ID. You might not have an OV6xx0,");
5015 err("or it may be not responding. Report this to " EMAIL);
5016 return -1;
5017 } else {
5018 PDEBUG(1, "OV6xx0 sensor detected");
5019 }
5020
5021
5022 rc = i2c_r(ov, OV7610_REG_COM_I);
5023
5024 if (rc < 0) {
5025 err("Error detecting sensor type");
5026 return -1;
5027 }
5028
5029 if ((rc & 3) == 0) {
5030 ov->sensor = SEN_OV6630;
5031 dev_info(&ov->dev->dev, "Sensor is an OV6630\n");
5032 } else if ((rc & 3) == 1) {
5033 ov->sensor = SEN_OV6620;
5034 dev_info(&ov->dev->dev, "Sensor is an OV6620\n");
5035 } else if ((rc & 3) == 2) {
5036 ov->sensor = SEN_OV6630;
5037 dev_info(&ov->dev->dev, "Sensor is an OV6630AE\n");
5038 } else if ((rc & 3) == 3) {
5039 ov->sensor = SEN_OV6630;
5040 dev_info(&ov->dev->dev, "Sensor is an OV6630AF\n");
5041 }
5042
5043
5044 ov->maxwidth = 352;
5045 ov->maxheight = 288;
5046 ov->minwidth = 64;
5047 ov->minheight = 48;
5048
5049
5050 ov->brightness = 0x80 << 8;
5051 ov->contrast = 0x80 << 8;
5052 ov->colour = 0x80 << 8;
5053 ov->hue = 0x80 << 8;
5054
5055 if (ov->sensor == SEN_OV6620) {
5056 PDEBUG(4, "Writing 6x20 registers");
5057 if (write_regvals(ov, aRegvalsNorm6x20))
5058 return -1;
5059 } else {
5060 PDEBUG(4, "Writing 6x30 registers");
5061 if (write_regvals(ov, aRegvalsNorm6x30))
5062 return -1;
5063 }
5064
5065 return 0;
5066}
5067
5068
5069static int
5070ks0127_configure(struct usb_ov511 *ov)
5071{
5072 int rc;
5073
5074
5075#if 0
5076 if (ov51x_init_ks_sensor(ov) < 0) {
5077 err("Failed to initialize the KS0127");
5078 return -1;
5079 } else {
5080 PDEBUG(1, "KS012x(B) sensor detected");
5081 }
5082#endif
5083
5084
5085 rc = i2c_r(ov, 0x00);
5086 if (rc < 0) {
5087 err("Error detecting sensor type");
5088 return -1;
5089 } else if (rc & 0x08) {
5090 rc = i2c_r(ov, 0x3d);
5091 if (rc < 0) {
5092 err("Error detecting sensor type");
5093 return -1;
5094 } else if ((rc & 0x0f) == 0) {
5095 dev_info(&ov->dev->dev, "Sensor is a KS0127\n");
5096 ov->sensor = SEN_KS0127;
5097 } else if ((rc & 0x0f) == 9) {
5098 dev_info(&ov->dev->dev, "Sensor is a KS0127B Rev. A\n");
5099 ov->sensor = SEN_KS0127B;
5100 }
5101 } else {
5102 err("Error: Sensor is an unsupported KS0122");
5103 return -1;
5104 }
5105
5106
5107 ov->maxwidth = 640;
5108 ov->maxheight = 480;
5109 ov->minwidth = 64;
5110 ov->minheight = 48;
5111
5112
5113 ov->brightness = 0x80 << 8;
5114 ov->contrast = 0x80 << 8;
5115 ov->colour = 0x80 << 8;
5116 ov->hue = 0x80 << 8;
5117
5118
5119 err("This sensor is not supported yet.");
5120 return -1;
5121
5122 return 0;
5123}
5124
5125
5126static int
5127saa7111a_configure(struct usb_ov511 *ov)
5128{
5129 int rc;
5130
5131
5132
5133 static struct ov511_regvals aRegvalsNormSAA7111A[] = {
5134 { OV511_I2C_BUS, 0x06, 0xce },
5135 { OV511_I2C_BUS, 0x07, 0x00 },
5136 { OV511_I2C_BUS, 0x10, 0x44 },
5137 { OV511_I2C_BUS, 0x0e, 0x01 },
5138 { OV511_I2C_BUS, 0x00, 0x00 },
5139 { OV511_I2C_BUS, 0x01, 0x00 },
5140 { OV511_I2C_BUS, 0x03, 0x23 },
5141 { OV511_I2C_BUS, 0x04, 0x00 },
5142 { OV511_I2C_BUS, 0x05, 0x00 },
5143 { OV511_I2C_BUS, 0x08, 0xc8 },
5144 { OV511_I2C_BUS, 0x09, 0x01 },
5145 { OV511_I2C_BUS, 0x0a, 0x80 },
5146 { OV511_I2C_BUS, 0x0b, 0x40 },
5147 { OV511_I2C_BUS, 0x0c, 0x40 },
5148 { OV511_I2C_BUS, 0x0d, 0x00 },
5149 { OV511_I2C_BUS, 0x0f, 0x00 },
5150 { OV511_I2C_BUS, 0x11, 0x0c },
5151 { OV511_I2C_BUS, 0x12, 0x00 },
5152 { OV511_I2C_BUS, 0x13, 0x00 },
5153 { OV511_I2C_BUS, 0x14, 0x00 },
5154 { OV511_I2C_BUS, 0x15, 0x00 },
5155 { OV511_I2C_BUS, 0x16, 0x00 },
5156 { OV511_I2C_BUS, 0x17, 0x00 },
5157 { OV511_I2C_BUS, 0x02, 0xc0 },
5158 { OV511_DONE_BUS, 0x0, 0x00 },
5159 };
5160
5161
5162#if 0
5163 if (ov51x_init_saa_sensor(ov) < 0) {
5164 err("Failed to initialize the SAA7111A");
5165 return -1;
5166 } else {
5167 PDEBUG(1, "SAA7111A sensor detected");
5168 }
5169#endif
5170
5171
5172 if (ov->pal) {
5173 ov->maxwidth = 320;
5174 ov->maxheight = 240;
5175 } else {
5176 ov->maxwidth = 640;
5177 ov->maxheight = 480;
5178 }
5179
5180 ov->minwidth = 320;
5181 ov->minheight = 240;
5182
5183 ov->has_decoder = 1;
5184 ov->num_inputs = 8;
5185 ov->norm = VIDEO_MODE_AUTO;
5186 ov->stop_during_set = 0;
5187
5188
5189
5190 ov->brightness = 0x80 << 8;
5191 ov->contrast = 0x40 << 9;
5192 ov->colour = 0x40 << 9;
5193 ov->hue = 32768;
5194
5195 PDEBUG(4, "Writing SAA7111A registers");
5196 if (write_regvals(ov, aRegvalsNormSAA7111A))
5197 return -1;
5198
5199
5200
5201 rc = i2c_r(ov, 0x00);
5202
5203 if (rc < 0) {
5204 err("Error detecting sensor version");
5205 return -1;
5206 } else {
5207 dev_info(&ov->dev->dev,
5208 "Sensor is an SAA7111A (version 0x%x)\n", rc);
5209 ov->sensor = SEN_SAA7111A;
5210 }
5211
5212
5213
5214
5215 if (ov->bclass == BCL_OV511)
5216 reg_w(ov, 0x11, 0x00);
5217 else
5218 dev_warn(&ov->dev->dev,
5219 "SAA7111A not yet supported with OV518/OV518+\n");
5220
5221 return 0;
5222}
5223
5224
5225static int
5226ov511_configure(struct usb_ov511 *ov)
5227{
5228 static struct ov511_regvals aRegvalsInit511[] = {
5229 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5230 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5231 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5232 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5233 { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
5234 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5235 { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
5236 { OV511_DONE_BUS, 0x0, 0x00},
5237 };
5238
5239 static struct ov511_regvals aRegvalsNorm511[] = {
5240 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
5241 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5242 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5243 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5244 { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
5245 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5246 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5247 { OV511_DONE_BUS, 0x0, 0x00 },
5248 };
5249
5250 static struct ov511_regvals aRegvalsNorm511Plus[] = {
5251 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
5252 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5253 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5254 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5255 { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
5256 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5257 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5258 { OV511_DONE_BUS, 0x0, 0x00 },
5259 };
5260
5261 PDEBUG(4, "");
5262
5263 ov->customid = reg_r(ov, R511_SYS_CUST_ID);
5264 if (ov->customid < 0) {
5265 err("Unable to read camera bridge registers");
5266 goto error;
5267 }
5268
5269 PDEBUG (1, "CustomID = %d", ov->customid);
5270 ov->desc = symbolic(camlist, ov->customid);
5271 dev_info(&ov->dev->dev, "model: %s\n", ov->desc);
5272
5273 if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
5274 err("Camera type (%d) not recognized", ov->customid);
5275 err("Please notify " EMAIL " of the name,");
5276 err("manufacturer, model, and this number of your camera.");
5277 err("Also include the output of the detection process.");
5278 }
5279
5280 if (ov->customid == 70)
5281 ov->pal = 1;
5282
5283 if (write_regvals(ov, aRegvalsInit511))
5284 goto error;
5285
5286 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5287 ov51x_led_control(ov, 0);
5288
5289
5290
5291 if (ov->bridge == BRG_OV511) {
5292 if (write_regvals(ov, aRegvalsNorm511))
5293 goto error;
5294 } else if (ov->bridge == BRG_OV511PLUS) {
5295 if (write_regvals(ov, aRegvalsNorm511Plus))
5296 goto error;
5297 } else {
5298 err("Invalid bridge");
5299 }
5300
5301 if (ov511_init_compression(ov))
5302 goto error;
5303
5304 ov->packet_numbering = 1;
5305 ov511_set_packet_size(ov, 0);
5306
5307 ov->snap_enabled = snapshot;
5308
5309
5310 PDEBUG(3, "Testing for 0V7xx0");
5311 ov->primary_i2c_slave = OV7xx0_SID;
5312 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5313 goto error;
5314
5315 if (i2c_w(ov, 0x12, 0x80) < 0) {
5316
5317 PDEBUG(3, "Testing for 0V6xx0");
5318 ov->primary_i2c_slave = OV6xx0_SID;
5319 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5320 goto error;
5321
5322 if (i2c_w(ov, 0x12, 0x80) < 0) {
5323
5324 PDEBUG(3, "Testing for 0V8xx0");
5325 ov->primary_i2c_slave = OV8xx0_SID;
5326 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5327 goto error;
5328
5329 if (i2c_w(ov, 0x12, 0x80) < 0) {
5330
5331 PDEBUG(3, "Testing for SAA7111A");
5332 ov->primary_i2c_slave = SAA7111A_SID;
5333 if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
5334 goto error;
5335
5336 if (i2c_w(ov, 0x0d, 0x00) < 0) {
5337
5338 PDEBUG(3, "Testing for KS0127");
5339 ov->primary_i2c_slave = KS0127_SID;
5340 if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
5341 goto error;
5342
5343 if (i2c_w(ov, 0x10, 0x00) < 0) {
5344 err("Can't determine sensor slave IDs");
5345 goto error;
5346 } else {
5347 if (ks0127_configure(ov) < 0) {
5348 err("Failed to configure KS0127");
5349 goto error;
5350 }
5351 }
5352 } else {
5353 if (saa7111a_configure(ov) < 0) {
5354 err("Failed to configure SAA7111A");
5355 goto error;
5356 }
5357 }
5358 } else {
5359 err("Detected unsupported OV8xx0 sensor");
5360 goto error;
5361 }
5362 } else {
5363 if (ov6xx0_configure(ov) < 0) {
5364 err("Failed to configure OV6xx0");
5365 goto error;
5366 }
5367 }
5368 } else {
5369 if (ov7xx0_configure(ov) < 0) {
5370 err("Failed to configure OV7xx0");
5371 goto error;
5372 }
5373 }
5374
5375 return 0;
5376
5377error:
5378 err("OV511 Config failed");
5379
5380 return -EBUSY;
5381}
5382
5383
5384static int
5385ov518_configure(struct usb_ov511 *ov)
5386{
5387
5388 static struct ov511_regvals aRegvalsInit518[] = {
5389 { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
5390 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5391 { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
5392 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5393 { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
5394 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5395 { OV511_REG_BUS, 0x46, 0x00 },
5396 { OV511_REG_BUS, 0x5d, 0x03 },
5397 { OV511_DONE_BUS, 0x0, 0x00},
5398 };
5399
5400 static struct ov511_regvals aRegvalsNorm518[] = {
5401 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5402 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 },
5403 { OV511_REG_BUS, 0x31, 0x0f },
5404 { OV511_REG_BUS, 0x5d, 0x03 },
5405 { OV511_REG_BUS, 0x24, 0x9f },
5406 { OV511_REG_BUS, 0x25, 0x90 },
5407 { OV511_REG_BUS, 0x20, 0x00 },
5408 { OV511_REG_BUS, 0x51, 0x04 },
5409 { OV511_REG_BUS, 0x71, 0x19 },
5410 { OV511_DONE_BUS, 0x0, 0x00 },
5411 };
5412
5413 static struct ov511_regvals aRegvalsNorm518Plus[] = {
5414 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5415 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 },
5416 { OV511_REG_BUS, 0x31, 0x0f },
5417 { OV511_REG_BUS, 0x5d, 0x03 },
5418 { OV511_REG_BUS, 0x24, 0x9f },
5419 { OV511_REG_BUS, 0x25, 0x90 },
5420 { OV511_REG_BUS, 0x20, 0x60 },
5421 { OV511_REG_BUS, 0x51, 0x02 },
5422 { OV511_REG_BUS, 0x71, 0x19 },
5423 { OV511_REG_BUS, 0x40, 0xff },
5424 { OV511_REG_BUS, 0x41, 0x42 },
5425 { OV511_REG_BUS, 0x46, 0x00 },
5426 { OV511_REG_BUS, 0x33, 0x04 },
5427 { OV511_REG_BUS, 0x21, 0x19 },
5428 { OV511_REG_BUS, 0x3f, 0x10 },
5429 { OV511_DONE_BUS, 0x0, 0x00 },
5430 };
5431
5432 PDEBUG(4, "");
5433
5434
5435 dev_info(&ov->dev->dev, "Device revision %d\n",
5436 0x1F & reg_r(ov, R511_SYS_CUST_ID));
5437
5438
5439 ov->desc = symbolic(camlist, 0);
5440
5441 if (write_regvals(ov, aRegvalsInit518))
5442 goto error;
5443
5444
5445 if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
5446 goto error;
5447
5448
5449 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5450 ov51x_led_control(ov, 0);
5451 else
5452 ov51x_led_control(ov, 1);
5453
5454
5455
5456 if (!dumppix && !ov->compress) {
5457 ov->compress = 1;
5458 dev_warn(&ov->dev->dev,
5459 "Compression required with OV518...enabling\n");
5460 }
5461
5462 if (ov->bridge == BRG_OV518) {
5463 if (write_regvals(ov, aRegvalsNorm518))
5464 goto error;
5465 } else if (ov->bridge == BRG_OV518PLUS) {
5466 if (write_regvals(ov, aRegvalsNorm518Plus))
5467 goto error;
5468 } else {
5469 err("Invalid bridge");
5470 }
5471
5472 if (reg_w(ov, 0x2f, 0x80) < 0)
5473 goto error;
5474
5475 if (ov518_init_compression(ov))
5476 goto error;
5477
5478 if (ov->bridge == BRG_OV518)
5479 {
5480 struct usb_interface *ifp;
5481 struct usb_host_interface *alt;
5482 __u16 mxps = 0;
5483
5484 ifp = usb_ifnum_to_if(ov->dev, 0);
5485 if (ifp) {
5486 alt = usb_altnum_to_altsetting(ifp, 7);
5487 if (alt)
5488 mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
5489 }
5490
5491
5492 if (mxps == 897)
5493 ov->packet_numbering = 1;
5494 else
5495 ov->packet_numbering = 0;
5496 } else {
5497
5498 ov->packet_numbering = 1;
5499 }
5500
5501 ov518_set_packet_size(ov, 0);
5502
5503 ov->snap_enabled = snapshot;
5504
5505
5506 ov->primary_i2c_slave = OV7xx0_SID;
5507 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5508 goto error;
5509
5510
5511
5512
5513
5514 if (init_ov_sensor(ov) < 0) {
5515
5516 ov->primary_i2c_slave = OV6xx0_SID;
5517 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5518 goto error;
5519
5520 if (init_ov_sensor(ov) < 0) {
5521
5522 ov->primary_i2c_slave = OV8xx0_SID;
5523 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5524 goto error;
5525
5526 if (init_ov_sensor(ov) < 0) {
5527 err("Can't determine sensor slave IDs");
5528 goto error;
5529 } else {
5530 err("Detected unsupported OV8xx0 sensor");
5531 goto error;
5532 }
5533 } else {
5534 if (ov6xx0_configure(ov) < 0) {
5535 err("Failed to configure OV6xx0");
5536 goto error;
5537 }
5538 }
5539 } else {
5540 if (ov7xx0_configure(ov) < 0) {
5541 err("Failed to configure OV7xx0");
5542 goto error;
5543 }
5544 }
5545
5546 ov->maxwidth = 352;
5547 ov->maxheight = 288;
5548
5549
5550 ov->minwidth = 160;
5551 ov->minheight = 120;
5552
5553 return 0;
5554
5555error:
5556 err("OV518 Config failed");
5557
5558 return -EBUSY;
5559}
5560
5561
5562
5563
5564
5565static inline struct usb_ov511 *cd_to_ov(struct device *cd)
5566{
5567 struct video_device *vdev = to_video_device(cd);
5568 return video_get_drvdata(vdev);
5569}
5570
5571static ssize_t show_custom_id(struct device *cd,
5572 struct device_attribute *attr, char *buf)
5573{
5574 struct usb_ov511 *ov = cd_to_ov(cd);
5575 return sprintf(buf, "%d\n", ov->customid);
5576}
5577static DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
5578
5579static ssize_t show_model(struct device *cd,
5580 struct device_attribute *attr, char *buf)
5581{
5582 struct usb_ov511 *ov = cd_to_ov(cd);
5583 return sprintf(buf, "%s\n", ov->desc);
5584}
5585static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
5586
5587static ssize_t show_bridge(struct device *cd,
5588 struct device_attribute *attr, char *buf)
5589{
5590 struct usb_ov511 *ov = cd_to_ov(cd);
5591 return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
5592}
5593static DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
5594
5595static ssize_t show_sensor(struct device *cd,
5596 struct device_attribute *attr, char *buf)
5597{
5598 struct usb_ov511 *ov = cd_to_ov(cd);
5599 return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
5600}
5601static DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
5602
5603static ssize_t show_brightness(struct device *cd,
5604 struct device_attribute *attr, char *buf)
5605{
5606 struct usb_ov511 *ov = cd_to_ov(cd);
5607 unsigned short x;
5608
5609 if (!ov->dev)
5610 return -ENODEV;
5611 sensor_get_brightness(ov, &x);
5612 return sprintf(buf, "%d\n", x >> 8);
5613}
5614static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
5615
5616static ssize_t show_saturation(struct device *cd,
5617 struct device_attribute *attr, char *buf)
5618{
5619 struct usb_ov511 *ov = cd_to_ov(cd);
5620 unsigned short x;
5621
5622 if (!ov->dev)
5623 return -ENODEV;
5624 sensor_get_saturation(ov, &x);
5625 return sprintf(buf, "%d\n", x >> 8);
5626}
5627static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
5628
5629static ssize_t show_contrast(struct device *cd,
5630 struct device_attribute *attr, char *buf)
5631{
5632 struct usb_ov511 *ov = cd_to_ov(cd);
5633 unsigned short x;
5634
5635 if (!ov->dev)
5636 return -ENODEV;
5637 sensor_get_contrast(ov, &x);
5638 return sprintf(buf, "%d\n", x >> 8);
5639}
5640static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
5641
5642static ssize_t show_hue(struct device *cd,
5643 struct device_attribute *attr, char *buf)
5644{
5645 struct usb_ov511 *ov = cd_to_ov(cd);
5646 unsigned short x;
5647
5648 if (!ov->dev)
5649 return -ENODEV;
5650 sensor_get_hue(ov, &x);
5651 return sprintf(buf, "%d\n", x >> 8);
5652}
5653static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5654
5655static ssize_t show_exposure(struct device *cd,
5656 struct device_attribute *attr, char *buf)
5657{
5658 struct usb_ov511 *ov = cd_to_ov(cd);
5659 unsigned char exp = 0;
5660
5661 if (!ov->dev)
5662 return -ENODEV;
5663 sensor_get_exposure(ov, &exp);
5664 return sprintf(buf, "%d\n", exp);
5665}
5666static DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
5667
5668static int ov_create_sysfs(struct video_device *vdev)
5669{
5670 int rc;
5671
5672 rc = device_create_file(&vdev->dev, &dev_attr_custom_id);
5673 if (rc) goto err;
5674 rc = device_create_file(&vdev->dev, &dev_attr_model);
5675 if (rc) goto err_id;
5676 rc = device_create_file(&vdev->dev, &dev_attr_bridge);
5677 if (rc) goto err_model;
5678 rc = device_create_file(&vdev->dev, &dev_attr_sensor);
5679 if (rc) goto err_bridge;
5680 rc = device_create_file(&vdev->dev, &dev_attr_brightness);
5681 if (rc) goto err_sensor;
5682 rc = device_create_file(&vdev->dev, &dev_attr_saturation);
5683 if (rc) goto err_bright;
5684 rc = device_create_file(&vdev->dev, &dev_attr_contrast);
5685 if (rc) goto err_sat;
5686 rc = device_create_file(&vdev->dev, &dev_attr_hue);
5687 if (rc) goto err_contrast;
5688 rc = device_create_file(&vdev->dev, &dev_attr_exposure);
5689 if (rc) goto err_hue;
5690
5691 return 0;
5692
5693err_hue:
5694 device_remove_file(&vdev->dev, &dev_attr_hue);
5695err_contrast:
5696 device_remove_file(&vdev->dev, &dev_attr_contrast);
5697err_sat:
5698 device_remove_file(&vdev->dev, &dev_attr_saturation);
5699err_bright:
5700 device_remove_file(&vdev->dev, &dev_attr_brightness);
5701err_sensor:
5702 device_remove_file(&vdev->dev, &dev_attr_sensor);
5703err_bridge:
5704 device_remove_file(&vdev->dev, &dev_attr_bridge);
5705err_model:
5706 device_remove_file(&vdev->dev, &dev_attr_model);
5707err_id:
5708 device_remove_file(&vdev->dev, &dev_attr_custom_id);
5709err:
5710 return rc;
5711}
5712
5713
5714
5715
5716
5717static int
5718ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5719{
5720 struct usb_device *dev = interface_to_usbdev(intf);
5721 struct usb_interface_descriptor *idesc;
5722 struct usb_ov511 *ov;
5723 int i, rc, nr;
5724
5725 PDEBUG(1, "probing for device...");
5726
5727
5728 if (dev->descriptor.bNumConfigurations != 1)
5729 return -ENODEV;
5730
5731 idesc = &intf->cur_altsetting->desc;
5732
5733 if (idesc->bInterfaceClass != 0xFF)
5734 return -ENODEV;
5735 if (idesc->bInterfaceSubClass != 0x00)
5736 return -ENODEV;
5737
5738 if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5739 err("couldn't kmalloc ov struct");
5740 goto error_out;
5741 }
5742
5743 ov->dev = dev;
5744 ov->iface = idesc->bInterfaceNumber;
5745 ov->led_policy = led;
5746 ov->compress = compress;
5747 ov->lightfreq = lightfreq;
5748 ov->num_inputs = 1;
5749 ov->stop_during_set = !fastset;
5750 ov->backlight = backlight;
5751 ov->mirror = mirror;
5752 ov->auto_brt = autobright;
5753 ov->auto_gain = autogain;
5754 ov->auto_exp = autoexp;
5755
5756 switch (le16_to_cpu(dev->descriptor.idProduct)) {
5757 case PROD_OV511:
5758 ov->bridge = BRG_OV511;
5759 ov->bclass = BCL_OV511;
5760 break;
5761 case PROD_OV511PLUS:
5762 ov->bridge = BRG_OV511PLUS;
5763 ov->bclass = BCL_OV511;
5764 break;
5765 case PROD_OV518:
5766 ov->bridge = BRG_OV518;
5767 ov->bclass = BCL_OV518;
5768 break;
5769 case PROD_OV518PLUS:
5770 ov->bridge = BRG_OV518PLUS;
5771 ov->bclass = BCL_OV518;
5772 break;
5773 case PROD_ME2CAM:
5774 if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
5775 goto error;
5776 ov->bridge = BRG_OV511PLUS;
5777 ov->bclass = BCL_OV511;
5778 break;
5779 default:
5780 err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
5781 goto error;
5782 }
5783
5784 dev_info(&intf->dev, "USB %s video device found\n",
5785 symbolic(brglist, ov->bridge));
5786
5787 init_waitqueue_head(&ov->wq);
5788
5789 mutex_init(&ov->lock);
5790 mutex_init(&ov->buf_lock);
5791 mutex_init(&ov->i2c_lock);
5792 mutex_init(&ov->cbuf_lock);
5793
5794 ov->buf_state = BUF_NOT_ALLOCATED;
5795
5796 if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
5797 err("usb_make_path error");
5798 goto error;
5799 }
5800
5801
5802
5803 ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
5804 if (!ov->cbuf)
5805 goto error;
5806
5807 if (ov->bclass == BCL_OV518) {
5808 if (ov518_configure(ov) < 0)
5809 goto error;
5810 } else {
5811 if (ov511_configure(ov) < 0)
5812 goto error;
5813 }
5814
5815 for (i = 0; i < OV511_NUMFRAMES; i++) {
5816 ov->frame[i].framenum = i;
5817 init_waitqueue_head(&ov->frame[i].wq);
5818 }
5819
5820 for (i = 0; i < OV511_NUMSBUF; i++) {
5821 ov->sbuf[i].ov = ov;
5822 spin_lock_init(&ov->sbuf[i].lock);
5823 ov->sbuf[i].n = i;
5824 }
5825
5826
5827
5828 if (ov51x_set_default_params(ov) < 0)
5829 goto error;
5830
5831#ifdef OV511_DEBUG
5832 if (dump_bridge) {
5833 if (ov->bclass == BCL_OV511)
5834 ov511_dump_regs(ov);
5835 else
5836 ov518_dump_regs(ov);
5837 }
5838#endif
5839
5840 ov->vdev = video_device_alloc();
5841 if (!ov->vdev)
5842 goto error;
5843
5844 memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
5845 ov->vdev->parent = &intf->dev;
5846 video_set_drvdata(ov->vdev, ov);
5847
5848 mutex_lock(&ov->lock);
5849
5850
5851 nr = find_first_zero_bit(&ov511_devused, OV511_MAX_UNIT_VIDEO);
5852
5853
5854 if (unit_video[nr] != 0)
5855 rc = video_register_device(ov->vdev, VFL_TYPE_GRABBER,
5856 unit_video[nr]);
5857 else
5858 rc = video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1);
5859
5860 if (rc < 0) {
5861 err("video_register_device failed");
5862 mutex_unlock(&ov->lock);
5863 goto error;
5864 }
5865
5866
5867 ov511_devused |= 1 << nr;
5868 ov->nr = nr;
5869
5870 dev_info(&intf->dev, "Device at %s registered to minor %d\n",
5871 ov->usb_path, ov->vdev->minor);
5872
5873 usb_set_intfdata(intf, ov);
5874 if (ov_create_sysfs(ov->vdev)) {
5875 err("ov_create_sysfs failed");
5876 ov511_devused &= ~(1 << nr);
5877 mutex_unlock(&ov->lock);
5878 goto error;
5879 }
5880
5881 mutex_lock(&ov->lock);
5882
5883 return 0;
5884
5885error:
5886 if (ov->vdev) {
5887 if (-1 == ov->vdev->minor)
5888 video_device_release(ov->vdev);
5889 else
5890 video_unregister_device(ov->vdev);
5891 ov->vdev = NULL;
5892 }
5893
5894 if (ov->cbuf) {
5895 mutex_lock(&ov->cbuf_lock);
5896 kfree(ov->cbuf);
5897 ov->cbuf = NULL;
5898 mutex_unlock(&ov->cbuf_lock);
5899 }
5900
5901 kfree(ov);
5902 ov = NULL;
5903
5904error_out:
5905 err("Camera initialization failed");
5906 return -EIO;
5907}
5908
5909static void
5910ov51x_disconnect(struct usb_interface *intf)
5911{
5912 struct usb_ov511 *ov = usb_get_intfdata(intf);
5913 int n;
5914
5915 PDEBUG(3, "");
5916
5917 mutex_lock(&ov->lock);
5918 usb_set_intfdata (intf, NULL);
5919
5920 if (!ov) {
5921 mutex_unlock(&ov->lock);
5922 return;
5923 }
5924
5925
5926 ov511_devused &= ~(1 << ov->nr);
5927
5928 if (ov->vdev)
5929 video_unregister_device(ov->vdev);
5930
5931 for (n = 0; n < OV511_NUMFRAMES; n++)
5932 ov->frame[n].grabstate = FRAME_ERROR;
5933
5934 ov->curframe = -1;
5935
5936
5937 for (n = 0; n < OV511_NUMFRAMES; n++)
5938 wake_up_interruptible(&ov->frame[n].wq);
5939
5940 wake_up_interruptible(&ov->wq);
5941
5942 ov->streaming = 0;
5943 ov51x_unlink_isoc(ov);
5944 mutex_unlock(&ov->lock);
5945
5946 ov->dev = NULL;
5947
5948
5949 if (ov && !ov->user) {
5950 mutex_lock(&ov->cbuf_lock);
5951 kfree(ov->cbuf);
5952 ov->cbuf = NULL;
5953 mutex_unlock(&ov->cbuf_lock);
5954
5955 ov51x_dealloc(ov);
5956 kfree(ov);
5957 ov = NULL;
5958 }
5959
5960 PDEBUG(3, "Disconnect complete");
5961}
5962
5963static struct usb_driver ov511_driver = {
5964 .name = "ov511",
5965 .id_table = device_table,
5966 .probe = ov51x_probe,
5967 .disconnect = ov51x_disconnect
5968};
5969
5970
5971
5972
5973
5974
5975
5976static int __init
5977usb_ov511_init(void)
5978{
5979 int retval;
5980
5981 retval = usb_register(&ov511_driver);
5982 if (retval)
5983 goto out;
5984
5985 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
5986 DRIVER_DESC "\n");
5987
5988out:
5989 return retval;
5990}
5991
5992static void __exit
5993usb_ov511_exit(void)
5994{
5995 usb_deregister(&ov511_driver);
5996 printk(KERN_INFO KBUILD_MODNAME ": driver deregistered\n");
5997}
5998
5999module_init(usb_ov511_init);
6000module_exit(usb_ov511_exit);
6001
6002