2 * OmniVision OV511 Camera-to-USB Bridge Driver
4 * Copyright (c) 1999-2003 Mark W. McClelland
5 * Original decompression code Copyright 1998-2000 OmniVision Technologies
6 * Many improvements by Bret Wallach <bwallac1@san.rr.com>
7 * Color fixes by by Orion Sky Lawlor <olawlor@acm.org> (2/26/2000)
8 * Snapshot code by Kevin Moore
9 * OV7620 fixes by Charl P. Botha <cpbotha@ieee.org>
10 * Changes by Claudio Matsuoka <claudio@conectiva.com>
11 * Original SAA7111A code by Dave Perks <dperks@ibm.net>
12 * URB error messages from pwc driver by Nemosoft
13 * generic_ioctl() code from videodev.c by Gerd Knorr and Alan Cox
14 * Memory management (rvmalloc) code from bttv driver, by Gerd Knorr and others
16 * Based on the Linux CPiA driver written by Peter Pregler,
17 * Scott J. Bertin and Johannes Erdfelt.
19 * Please see the file: Documentation/usb/ov511.txt
20 * and the website at: http://alpha.dyndns.org/ov511
23 * This program is free software; you can redistribute it and/or modify it
24 * under the terms of the GNU General Public License as published by the
25 * Free Software Foundation; either version 2 of the License, or (at your
26 * option) any later version.
28 * This program is distributed in the hope that it will be useful, but
29 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
30 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software Foundation,
35 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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/semaphore.h>
45 #include <asm/processor.h>
47 #include <linux/device.h>
49 #if defined (__i386__)
50 #include <asm/cpufeature.h>
58 #define DRIVER_VERSION "v1.64 for Linux 2.5"
59 #define EMAIL "mark@alpha.dyndns.org"
60 #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
61 & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
62 <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
63 #define DRIVER_DESC "ov511 USB Camera Driver"
65 #define OV511_I2C_RETRIES 3
66 #define ENABLE_Y_QUANTABLE 1
67 #define ENABLE_UV_QUANTABLE 1
69 #define OV511_MAX_UNIT_VIDEO 16
71 /* Pixel count * bytes per YUV420 pixel (1.5) */
72 #define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
74 #define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
76 /* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */
77 #define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
79 #define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
81 /**********************************************************************
83 * (See ov511.txt for detailed descriptions of these)
84 **********************************************************************/
86 /* These variables (and all static globals) default to zero */
87 static int autobright = 1;
88 static int autogain = 1;
89 static int autoexp = 1;
97 static int dump_bridge;
98 static int dump_sensor;
100 static int phy = 0x1f;
101 static int phuv = 0x05;
102 static int pvy = 0x06;
103 static int pvuv = 0x06;
104 static int qhy = 0x14;
105 static int qhuv = 0x03;
106 static int qvy = 0x04;
107 static int qvuv = 0x04;
108 static int lightfreq;
109 static int bandingfilter;
110 static int clockdiv = -1;
111 static int packetsize = -1;
112 static int framedrop = -1;
114 static int force_palette;
115 static int backlight;
116 static int unit_video[OV511_MAX_UNIT_VIDEO];
117 static int remove_zeros;
119 static int ov518_color;
121 module_param(autobright, int, 0);
122 MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
123 module_param(autogain, int, 0);
124 MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
125 module_param(autoexp, int, 0);
126 MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
127 module_param(debug, int, 0);
128 MODULE_PARM_DESC(debug,
129 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
130 module_param(snapshot, int, 0);
131 MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
132 module_param(cams, int, 0);
133 MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
134 module_param(compress, int, 0);
135 MODULE_PARM_DESC(compress, "Turn on compression");
136 module_param(testpat, int, 0);
137 MODULE_PARM_DESC(testpat,
138 "Replace image with vertical bar testpattern (only partially working)");
139 module_param(dumppix, int, 0);
140 MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
141 module_param(led, int, 0);
142 MODULE_PARM_DESC(led,
143 "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
144 module_param(dump_bridge, int, 0);
145 MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
146 module_param(dump_sensor, int, 0);
147 MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
148 module_param(printph, int, 0);
149 MODULE_PARM_DESC(printph, "Print frame start/end headers");
150 module_param(phy, int, 0);
151 MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
152 module_param(phuv, int, 0);
153 MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
154 module_param(pvy, int, 0);
155 MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
156 module_param(pvuv, int, 0);
157 MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
158 module_param(qhy, int, 0);
159 MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
160 module_param(qhuv, int, 0);
161 MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
162 module_param(qvy, int, 0);
163 MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
164 module_param(qvuv, int, 0);
165 MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
166 module_param(lightfreq, int, 0);
167 MODULE_PARM_DESC(lightfreq,
168 "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
169 module_param(bandingfilter, int, 0);
170 MODULE_PARM_DESC(bandingfilter,
171 "Enable banding filter (to reduce effects of fluorescent lighting)");
172 module_param(clockdiv, int, 0);
173 MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
174 module_param(packetsize, int, 0);
175 MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
176 module_param(framedrop, int, 0);
177 MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
178 module_param(fastset, int, 0);
179 MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
180 module_param(force_palette, int, 0);
181 MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
182 module_param(backlight, int, 0);
183 MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
184 static unsigned int num_uv;
185 module_param_array(unit_video, int, &num_uv, 0);
186 MODULE_PARM_DESC(unit_video,
187 "Force use of specific minor number(s). 0 is not allowed.");
188 module_param(remove_zeros, int, 0);
189 MODULE_PARM_DESC(remove_zeros,
190 "Remove zero-padding from uncompressed incoming data");
191 module_param(mirror, int, 0);
192 MODULE_PARM_DESC(mirror, "Reverse image horizontally");
193 module_param(ov518_color, int, 0);
194 MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
196 MODULE_AUTHOR(DRIVER_AUTHOR);
197 MODULE_DESCRIPTION(DRIVER_DESC);
198 MODULE_LICENSE("GPL");
200 /**********************************************************************
201 * Miscellaneous Globals
202 **********************************************************************/
204 static struct usb_driver ov511_driver;
206 /* Number of times to retry a failed I2C transaction. Increase this if you
207 * are getting "Failed to read sensor ID..." */
208 static const int i2c_detect_tries = 5;
210 static struct usb_device_id device_table [] = {
211 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
212 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
213 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518) },
214 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518PLUS) },
215 { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
216 { } /* Terminating entry */
219 MODULE_DEVICE_TABLE (usb, device_table);
221 static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
222 static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
223 static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
224 static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
226 /**********************************************************************
228 **********************************************************************/
230 /* Known OV511-based cameras */
231 static struct symbolic_list camlist[] = {
232 { 0, "Generic Camera (no ID)" },
233 { 1, "Mustek WCam 3X" },
234 { 3, "D-Link DSB-C300" },
235 { 4, "Generic OV511/OV7610" },
236 { 5, "Puretek PT-6007" },
237 { 6, "Lifeview USB Life TV (NTSC)" },
238 { 21, "Creative Labs WebCam 3" },
239 { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
241 { 38, "Lifeview USB Life TV (PAL)" },
242 { 41, "Samsung Anycam MPC-M10" },
243 { 43, "Mtekvision Zeca MV402" },
245 { 70, "Lifeview USB Life TV (PAL/SECAM)" },
246 { 100, "Lifeview RoboCam" },
247 { 102, "AverMedia InterCam Elite" },
248 { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
249 { 134, "Ezonics EZCam II" },
250 { 192, "Webeye 2000B" },
251 { 253, "Alpha Vision Tech. AlphaCam SE" },
255 /* Video4Linux1 Palettes */
256 static struct symbolic_list v4l1_plist[] = {
257 { VIDEO_PALETTE_GREY, "GREY" },
258 { VIDEO_PALETTE_HI240, "HI240" },
259 { VIDEO_PALETTE_RGB565, "RGB565" },
260 { VIDEO_PALETTE_RGB24, "RGB24" },
261 { VIDEO_PALETTE_RGB32, "RGB32" },
262 { VIDEO_PALETTE_RGB555, "RGB555" },
263 { VIDEO_PALETTE_YUV422, "YUV422" },
264 { VIDEO_PALETTE_YUYV, "YUYV" },
265 { VIDEO_PALETTE_UYVY, "UYVY" },
266 { VIDEO_PALETTE_YUV420, "YUV420" },
267 { VIDEO_PALETTE_YUV411, "YUV411" },
268 { VIDEO_PALETTE_RAW, "RAW" },
269 { VIDEO_PALETTE_YUV422P,"YUV422P" },
270 { VIDEO_PALETTE_YUV411P,"YUV411P" },
271 { VIDEO_PALETTE_YUV420P,"YUV420P" },
272 { VIDEO_PALETTE_YUV410P,"YUV410P" },
276 static struct symbolic_list brglist[] = {
277 { BRG_OV511, "OV511" },
278 { BRG_OV511PLUS, "OV511+" },
279 { BRG_OV518, "OV518" },
280 { BRG_OV518PLUS, "OV518+" },
284 static struct symbolic_list senlist[] = {
285 { SEN_OV76BE, "OV76BE" },
286 { SEN_OV7610, "OV7610" },
287 { SEN_OV7620, "OV7620" },
288 { SEN_OV7620AE, "OV7620AE" },
289 { SEN_OV6620, "OV6620" },
290 { SEN_OV6630, "OV6630" },
291 { SEN_OV6630AE, "OV6630AE" },
292 { SEN_OV6630AF, "OV6630AF" },
293 { SEN_OV8600, "OV8600" },
294 { SEN_KS0127, "KS0127" },
295 { SEN_KS0127B, "KS0127B" },
296 { SEN_SAA7111A, "SAA7111A" },
300 /* URB error codes: */
301 static struct symbolic_list urb_errlist[] = {
302 { -ENOSR, "Buffer error (overrun)" },
303 { -EPIPE, "Stalled (device not responding)" },
304 { -EOVERFLOW, "Babble (device sends too much data)" },
305 { -EPROTO, "Bit-stuff error (bad cable?)" },
306 { -EILSEQ, "CRC/Timeout (bad cable?)" },
307 { -ETIME, "Device does not respond to token" },
308 { -ETIMEDOUT, "Device does not respond to command" },
312 /**********************************************************************
314 **********************************************************************/
316 rvmalloc(unsigned long size)
321 size = PAGE_ALIGN(size);
322 mem = vmalloc_32(size);
326 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
327 adr = (unsigned long) mem;
329 SetPageReserved(vmalloc_to_page((void *)adr));
338 rvfree(void *mem, unsigned long size)
345 adr = (unsigned long) mem;
346 while ((long) size > 0) {
347 ClearPageReserved(vmalloc_to_page((void *)adr));
354 /**********************************************************************
358 **********************************************************************/
360 /* Write an OV51x register */
362 reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
366 PDEBUG(5, "0x%02X:0x%02X", reg, value);
368 mutex_lock(&ov->cbuf_lock);
370 rc = usb_control_msg(ov->dev,
371 usb_sndctrlpipe(ov->dev, 0),
372 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
373 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
374 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
375 mutex_unlock(&ov->cbuf_lock);
378 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
383 /* Read from an OV51x register */
384 /* returns: negative is error, pos or zero is data */
386 reg_r(struct usb_ov511 *ov, unsigned char reg)
390 mutex_lock(&ov->cbuf_lock);
391 rc = usb_control_msg(ov->dev,
392 usb_rcvctrlpipe(ov->dev, 0),
393 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
394 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
395 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
398 err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
401 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
404 mutex_unlock(&ov->cbuf_lock);
410 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
411 * the same position as 1's in "mask" are cleared and set to "value". Bits
412 * that are in the same position as 0's in "mask" are preserved, regardless
413 * of their respective state in "value".
416 reg_w_mask(struct usb_ov511 *ov,
422 unsigned char oldval, newval;
424 ret = reg_r(ov, reg);
428 oldval = (unsigned char) ret;
429 oldval &= (~mask); /* Clear the masked bits */
430 value &= mask; /* Enforce mask on value */
431 newval = oldval | value; /* Set the desired bits */
433 return (reg_w(ov, reg, newval));
437 * Writes multiple (n) byte value to a single register. Only valid with certain
438 * registers (0x30 and 0xc4 - 0xce).
441 ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
445 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
447 mutex_lock(&ov->cbuf_lock);
449 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
451 rc = usb_control_msg(ov->dev,
452 usb_sndctrlpipe(ov->dev, 0),
454 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
455 0, (__u16)reg, ov->cbuf, n, 1000);
456 mutex_unlock(&ov->cbuf_lock);
459 err("reg write multiple: error %d: %s", rc,
460 symbolic(urb_errlist, rc));
466 ov511_upload_quan_tables(struct usb_ov511 *ov)
468 unsigned char *pYTable = yQuanTable511;
469 unsigned char *pUVTable = uvQuanTable511;
470 unsigned char val0, val1;
471 int i, rc, reg = R511_COMP_LUT_BEGIN;
473 PDEBUG(4, "Uploading quantization tables");
475 for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
476 if (ENABLE_Y_QUANTABLE) {
482 rc = reg_w(ov, reg, val0);
487 if (ENABLE_UV_QUANTABLE) {
493 rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
504 /* OV518 quantization tables are 8x4 (instead of 8x8) */
506 ov518_upload_quan_tables(struct usb_ov511 *ov)
508 unsigned char *pYTable = yQuanTable518;
509 unsigned char *pUVTable = uvQuanTable518;
510 unsigned char val0, val1;
511 int i, rc, reg = R511_COMP_LUT_BEGIN;
513 PDEBUG(4, "Uploading quantization tables");
515 for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
516 if (ENABLE_Y_QUANTABLE) {
522 rc = reg_w(ov, reg, val0);
527 if (ENABLE_UV_QUANTABLE) {
533 rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
545 ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
549 /* Setting bit 0 not allowed on 518/518Plus */
550 if (ov->bclass == BCL_OV518)
553 PDEBUG(4, "Reset: type=0x%02X", reset_type);
555 rc = reg_w(ov, R51x_SYS_RESET, reset_type);
556 rc = reg_w(ov, R51x_SYS_RESET, 0);
559 err("reset: command failed");
564 /**********************************************************************
566 * Low-level I2C I/O functions
568 **********************************************************************/
570 /* NOTE: Do not call this function directly!
571 * The OV518 I2C I/O procedure is different, hence, this function.
572 * This is normally only called from i2c_w(). Note that this function
573 * always succeeds regardless of whether the sensor is present and working.
576 ov518_i2c_write_internal(struct usb_ov511 *ov,
582 PDEBUG(5, "0x%02X:0x%02X", reg, value);
584 /* Select camera register */
585 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
589 /* Write "value" to I2C data port of OV511 */
590 rc = reg_w(ov, R51x_I2C_DATA, value);
594 /* Initiate 3-byte write cycle */
595 rc = reg_w(ov, R518_I2C_CTL, 0x01);
602 /* NOTE: Do not call this function directly! */
604 ov511_i2c_write_internal(struct usb_ov511 *ov,
610 PDEBUG(5, "0x%02X:0x%02X", reg, value);
612 /* Three byte write cycle */
613 for (retries = OV511_I2C_RETRIES; ; ) {
614 /* Select camera register */
615 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
619 /* Write "value" to I2C data port of OV511 */
620 rc = reg_w(ov, R51x_I2C_DATA, value);
624 /* Initiate 3-byte write cycle */
625 rc = reg_w(ov, R511_I2C_CTL, 0x01);
629 /* Retry until idle */
631 rc = reg_r(ov, R511_I2C_CTL);
632 while (rc > 0 && ((rc&1) == 0));
643 reg_w(ov, R511_I2C_CTL, 0x10);
646 err("i2c write retries exhausted");
655 /* NOTE: Do not call this function directly!
656 * The OV518 I2C I/O procedure is different, hence, this function.
657 * This is normally only called from i2c_r(). Note that this function
658 * always succeeds regardless of whether the sensor is present and working.
661 ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
665 /* Select camera register */
666 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
670 /* Initiate 2-byte write cycle */
671 rc = reg_w(ov, R518_I2C_CTL, 0x03);
675 /* Initiate 2-byte read cycle */
676 rc = reg_w(ov, R518_I2C_CTL, 0x05);
680 value = reg_r(ov, R51x_I2C_DATA);
682 PDEBUG(5, "0x%02X:0x%02X", reg, value);
687 /* NOTE: Do not call this function directly!
688 * returns: negative is error, pos or zero is data */
690 ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
692 int rc, value, retries;
694 /* Two byte write cycle */
695 for (retries = OV511_I2C_RETRIES; ; ) {
696 /* Select camera register */
697 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
701 /* Initiate 2-byte write cycle */
702 rc = reg_w(ov, R511_I2C_CTL, 0x03);
706 /* Retry until idle */
708 rc = reg_r(ov, R511_I2C_CTL);
709 while (rc > 0 && ((rc&1) == 0));
713 if ((rc&2) == 0) /* Ack? */
717 reg_w(ov, R511_I2C_CTL, 0x10);
720 err("i2c write retries exhausted");
725 /* Two byte read cycle */
726 for (retries = OV511_I2C_RETRIES; ; ) {
727 /* Initiate 2-byte read cycle */
728 rc = reg_w(ov, R511_I2C_CTL, 0x05);
732 /* Retry until idle */
734 rc = reg_r(ov, R511_I2C_CTL);
735 while (rc > 0 && ((rc&1) == 0));
739 if ((rc&2) == 0) /* Ack? */
743 rc = reg_w(ov, R511_I2C_CTL, 0x10);
748 err("i2c read retries exhausted");
753 value = reg_r(ov, R51x_I2C_DATA);
755 PDEBUG(5, "0x%02X:0x%02X", reg, value);
757 /* This is needed to make i2c_w() work */
758 rc = reg_w(ov, R511_I2C_CTL, 0x05);
765 /* returns: negative is error, pos or zero is data */
767 i2c_r(struct usb_ov511 *ov, unsigned char reg)
771 mutex_lock(&ov->i2c_lock);
773 if (ov->bclass == BCL_OV518)
774 rc = ov518_i2c_read_internal(ov, reg);
776 rc = ov511_i2c_read_internal(ov, reg);
778 mutex_unlock(&ov->i2c_lock);
784 i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
788 mutex_lock(&ov->i2c_lock);
790 if (ov->bclass == BCL_OV518)
791 rc = ov518_i2c_write_internal(ov, reg, value);
793 rc = ov511_i2c_write_internal(ov, reg, value);
795 mutex_unlock(&ov->i2c_lock);
800 /* Do not call this function directly! */
802 ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
808 unsigned char oldval, newval;
813 if (ov->bclass == BCL_OV518)
814 rc = ov518_i2c_read_internal(ov, reg);
816 rc = ov511_i2c_read_internal(ov, reg);
820 oldval = (unsigned char) rc;
821 oldval &= (~mask); /* Clear the masked bits */
822 value &= mask; /* Enforce mask on value */
823 newval = oldval | value; /* Set the desired bits */
826 if (ov->bclass == BCL_OV518)
827 return (ov518_i2c_write_internal(ov, reg, newval));
829 return (ov511_i2c_write_internal(ov, reg, newval));
832 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
833 * the same position as 1's in "mask" are cleared and set to "value". Bits
834 * that are in the same position as 0's in "mask" are preserved, regardless
835 * of their respective state in "value".
838 i2c_w_mask(struct usb_ov511 *ov,
845 mutex_lock(&ov->i2c_lock);
846 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
847 mutex_unlock(&ov->i2c_lock);
852 /* Set the read and write slave IDs. The "slave" argument is the write slave,
853 * and the read slave will be set to (slave + 1). ov->i2c_lock should be held
854 * when calling this. This should not be called from outside the i2c I/O
858 i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
862 rc = reg_w(ov, R51x_I2C_W_SID, slave);
866 rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
873 /* Write to a specific I2C slave ID and register, using the specified mask */
875 i2c_w_slave(struct usb_ov511 *ov,
883 mutex_lock(&ov->i2c_lock);
885 /* Set new slave IDs */
886 rc = i2c_set_slave_internal(ov, slave);
890 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
893 /* Restore primary IDs */
894 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
895 err("Couldn't restore primary I2C slave");
897 mutex_unlock(&ov->i2c_lock);
901 /* Read from a specific I2C slave ID and register */
903 i2c_r_slave(struct usb_ov511 *ov,
909 mutex_lock(&ov->i2c_lock);
911 /* Set new slave IDs */
912 rc = i2c_set_slave_internal(ov, slave);
916 if (ov->bclass == BCL_OV518)
917 rc = ov518_i2c_read_internal(ov, reg);
919 rc = ov511_i2c_read_internal(ov, reg);
922 /* Restore primary IDs */
923 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
924 err("Couldn't restore primary I2C slave");
926 mutex_unlock(&ov->i2c_lock);
930 /* Sets I2C read and write slave IDs. Returns <0 for error */
932 ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
936 mutex_lock(&ov->i2c_lock);
938 rc = i2c_set_slave_internal(ov, sid);
942 // FIXME: Is this actually necessary?
943 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
945 mutex_unlock(&ov->i2c_lock);
950 write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
954 while (pRegvals->bus != OV511_DONE_BUS) {
955 if (pRegvals->bus == OV511_REG_BUS) {
956 if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
958 } else if (pRegvals->bus == OV511_I2C_BUS) {
959 if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
962 err("Bad regval array");
972 dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
976 for (i = reg1; i <= regn; i++) {
978 info("Sensor[0x%02X] = 0x%02X", i, rc);
983 dump_i2c_regs(struct usb_ov511 *ov)
986 dump_i2c_range(ov, 0x00, 0x7C);
990 dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
994 for (i = reg1; i <= regn; i++) {
996 info("OV511[0x%02X] = 0x%02X", i, rc);
1001 ov511_dump_regs(struct usb_ov511 *ov)
1003 info("CAMERA INTERFACE REGS");
1004 dump_reg_range(ov, 0x10, 0x1f);
1005 info("DRAM INTERFACE REGS");
1006 dump_reg_range(ov, 0x20, 0x23);
1007 info("ISO FIFO REGS");
1008 dump_reg_range(ov, 0x30, 0x31);
1010 dump_reg_range(ov, 0x38, 0x39);
1011 dump_reg_range(ov, 0x3e, 0x3e);
1013 dump_reg_range(ov, 0x40, 0x49);
1014 info("SYSTEM CONTROL REGS");
1015 dump_reg_range(ov, 0x50, 0x55);
1016 dump_reg_range(ov, 0x5e, 0x5f);
1017 info("OmniCE REGS");
1018 dump_reg_range(ov, 0x70, 0x79);
1019 /* NOTE: Quantization tables are not readable. You will get the value
1020 * in reg. 0x79 for every table register */
1021 dump_reg_range(ov, 0x80, 0x9f);
1022 dump_reg_range(ov, 0xa0, 0xbf);
1027 ov518_dump_regs(struct usb_ov511 *ov)
1029 info("VIDEO MODE REGS");
1030 dump_reg_range(ov, 0x20, 0x2f);
1031 info("DATA PUMP AND SNAPSHOT REGS");
1032 dump_reg_range(ov, 0x30, 0x3f);
1034 dump_reg_range(ov, 0x40, 0x4f);
1035 info("SYSTEM CONTROL AND VENDOR REGS");
1036 dump_reg_range(ov, 0x50, 0x5f);
1038 dump_reg_range(ov, 0x60, 0x6f);
1040 dump_reg_range(ov, 0x70, 0x7f);
1041 info("Y QUANTIZATION TABLE");
1042 dump_reg_range(ov, 0x80, 0x8f);
1043 info("UV QUANTIZATION TABLE");
1044 dump_reg_range(ov, 0x90, 0x9f);
1046 dump_reg_range(ov, 0xa0, 0xbf);
1048 dump_reg_range(ov, 0xc0, 0xcf);
1052 /*****************************************************************************/
1054 /* Temporarily stops OV511 from functioning. Must do this before changing
1055 * registers while the camera is streaming */
1057 ov51x_stop(struct usb_ov511 *ov)
1059 PDEBUG(4, "stopping");
1061 if (ov->bclass == BCL_OV518)
1062 return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
1064 return (reg_w(ov, R51x_SYS_RESET, 0x3d));
1067 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1068 * actually stopped (for performance). */
1070 ov51x_restart(struct usb_ov511 *ov)
1073 PDEBUG(4, "restarting");
1076 /* Reinitialize the stream */
1077 if (ov->bclass == BCL_OV518)
1078 reg_w(ov, 0x2f, 0x80);
1080 return (reg_w(ov, R51x_SYS_RESET, 0x00));
1086 /* Sleeps until no frames are active. Returns !0 if got signal */
1088 ov51x_wait_frames_inactive(struct usb_ov511 *ov)
1090 return wait_event_interruptible(ov->wq, ov->curframe < 0);
1093 /* Resets the hardware snapshot button */
1095 ov51x_clear_snapshot(struct usb_ov511 *ov)
1097 if (ov->bclass == BCL_OV511) {
1098 reg_w(ov, R51x_SYS_SNAP, 0x00);
1099 reg_w(ov, R51x_SYS_SNAP, 0x02);
1100 reg_w(ov, R51x_SYS_SNAP, 0x00);
1101 } else if (ov->bclass == BCL_OV518) {
1102 warn("snapshot reset not supported yet on OV518(+)");
1104 err("clear snap: invalid bridge type");
1109 /* Checks the status of the snapshot button. Returns 1 if it was pressed since
1110 * it was last cleared, and zero in all other cases (including errors) */
1112 ov51x_check_snapshot(struct usb_ov511 *ov)
1114 int ret, status = 0;
1116 if (ov->bclass == BCL_OV511) {
1117 ret = reg_r(ov, R51x_SYS_SNAP);
1119 err("Error checking snspshot status (%d)", ret);
1120 } else if (ret & 0x08) {
1123 } else if (ov->bclass == BCL_OV518) {
1124 warn("snapshot check not supported yet on OV518(+)");
1126 err("check snap: invalid bridge type");
1133 /* This does an initial reset of an OmniVision sensor and ensures that I2C
1134 * is synchronized. Returns <0 for failure.
1137 init_ov_sensor(struct usb_ov511 *ov)
1141 /* Reset the sensor */
1142 if (i2c_w(ov, 0x12, 0x80) < 0)
1145 /* Wait for it to initialize */
1148 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
1149 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
1150 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
1155 /* Reset the sensor */
1156 if (i2c_w(ov, 0x12, 0x80) < 0)
1158 /* Wait for it to initialize */
1160 /* Dummy read to sync I2C */
1161 if (i2c_r(ov, 0x00) < 0)
1168 PDEBUG(1, "I2C synced in %d attempt(s)", i);
1174 ov511_set_packet_size(struct usb_ov511 *ov, int size)
1178 if (ov51x_stop(ov) < 0)
1183 if (ov->bridge == BRG_OV511) {
1185 alt = OV511_ALT_SIZE_0;
1186 else if (size == 257)
1187 alt = OV511_ALT_SIZE_257;
1188 else if (size == 513)
1189 alt = OV511_ALT_SIZE_513;
1190 else if (size == 769)
1191 alt = OV511_ALT_SIZE_769;
1192 else if (size == 993)
1193 alt = OV511_ALT_SIZE_993;
1195 err("Set packet size: invalid size (%d)", size);
1198 } else if (ov->bridge == BRG_OV511PLUS) {
1200 alt = OV511PLUS_ALT_SIZE_0;
1201 else if (size == 33)
1202 alt = OV511PLUS_ALT_SIZE_33;
1203 else if (size == 129)
1204 alt = OV511PLUS_ALT_SIZE_129;
1205 else if (size == 257)
1206 alt = OV511PLUS_ALT_SIZE_257;
1207 else if (size == 385)
1208 alt = OV511PLUS_ALT_SIZE_385;
1209 else if (size == 513)
1210 alt = OV511PLUS_ALT_SIZE_513;
1211 else if (size == 769)
1212 alt = OV511PLUS_ALT_SIZE_769;
1213 else if (size == 961)
1214 alt = OV511PLUS_ALT_SIZE_961;
1216 err("Set packet size: invalid size (%d)", size);
1220 err("Set packet size: Invalid bridge type");
1224 PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
1226 if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
1229 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1230 err("Set packet size: set interface error");
1234 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1237 ov->packet_size = size;
1239 if (ov51x_restart(ov) < 0)
1245 /* Note: Unlike the OV511/OV511+, the size argument does NOT include the
1246 * optional packet number byte. The actual size *is* stored in ov->packet_size,
1249 ov518_set_packet_size(struct usb_ov511 *ov, int size)
1253 if (ov51x_stop(ov) < 0)
1256 if (ov->bclass == BCL_OV518) {
1258 alt = OV518_ALT_SIZE_0;
1259 else if (size == 128)
1260 alt = OV518_ALT_SIZE_128;
1261 else if (size == 256)
1262 alt = OV518_ALT_SIZE_256;
1263 else if (size == 384)
1264 alt = OV518_ALT_SIZE_384;
1265 else if (size == 512)
1266 alt = OV518_ALT_SIZE_512;
1267 else if (size == 640)
1268 alt = OV518_ALT_SIZE_640;
1269 else if (size == 768)
1270 alt = OV518_ALT_SIZE_768;
1271 else if (size == 896)
1272 alt = OV518_ALT_SIZE_896;
1274 err("Set packet size: invalid size (%d)", size);
1278 err("Set packet size: Invalid bridge type");
1282 PDEBUG(3, "%d, alt=%d", size, alt);
1284 ov->packet_size = size;
1286 /* Program ISO FIFO size reg (packet number isn't included) */
1287 ov518_reg_w32(ov, 0x30, size, 2);
1289 if (ov->packet_numbering)
1293 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1294 err("Set packet size: set interface error");
1298 /* Initialize the stream */
1299 if (reg_w(ov, 0x2f, 0x80) < 0)
1302 if (ov51x_restart(ov) < 0)
1305 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1311 /* Upload compression params and quantization tables. Returns 0 for success. */
1313 ov511_init_compression(struct usb_ov511 *ov)
1317 if (!ov->compress_inited) {
1318 reg_w(ov, 0x70, phy);
1319 reg_w(ov, 0x71, phuv);
1320 reg_w(ov, 0x72, pvy);
1321 reg_w(ov, 0x73, pvuv);
1322 reg_w(ov, 0x74, qhy);
1323 reg_w(ov, 0x75, qhuv);
1324 reg_w(ov, 0x76, qvy);
1325 reg_w(ov, 0x77, qvuv);
1327 if (ov511_upload_quan_tables(ov) < 0) {
1328 err("Error uploading quantization tables");
1334 ov->compress_inited = 1;
1339 /* Upload compression params and quantization tables. Returns 0 for success. */
1341 ov518_init_compression(struct usb_ov511 *ov)
1345 if (!ov->compress_inited) {
1346 if (ov518_upload_quan_tables(ov) < 0) {
1347 err("Error uploading quantization tables");
1353 ov->compress_inited = 1;
1358 /* -------------------------------------------------------------------------- */
1360 /* Sets sensor's contrast setting to "val" */
1362 sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
1366 PDEBUG(3, "%d", val);
1368 if (ov->stop_during_set)
1369 if (ov51x_stop(ov) < 0)
1372 switch (ov->sensor) {
1376 rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
1383 rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
1390 unsigned char ctab[] = {
1391 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1392 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1395 /* Use Y gamma control instead. Bit 0 enables it. */
1396 rc = i2c_w(ov, 0x64, ctab[val>>12]);
1403 rc = i2c_w(ov, 0x0b, val >> 9);
1410 PDEBUG(3, "Unsupported with this sensor");
1416 rc = 0; /* Success */
1419 if (ov51x_restart(ov) < 0)
1425 /* Gets sensor's contrast setting */
1427 sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
1431 switch (ov->sensor) {
1434 rc = i2c_r(ov, OV7610_REG_CNT);
1441 rc = i2c_r(ov, OV7610_REG_CNT);
1448 /* Use Y gamma reg instead. Bit 0 is the enable bit. */
1449 rc = i2c_r(ov, 0x64);
1453 *val = (rc & 0xfe) << 8;
1456 *val = ov->contrast;
1459 PDEBUG(3, "Unsupported with this sensor");
1463 PDEBUG(3, "%d", *val);
1464 ov->contrast = *val;
1469 /* -------------------------------------------------------------------------- */
1471 /* Sets sensor's brightness setting to "val" */
1473 sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
1477 PDEBUG(4, "%d", val);
1479 if (ov->stop_during_set)
1480 if (ov51x_stop(ov) < 0)
1483 switch (ov->sensor) {
1488 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1493 /* 7620 doesn't like manual changes when in auto mode */
1494 if (!ov->auto_brt) {
1495 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1501 rc = i2c_w(ov, 0x0a, val >> 8);
1506 PDEBUG(3, "Unsupported with this sensor");
1511 rc = 0; /* Success */
1512 ov->brightness = val;
1514 if (ov51x_restart(ov) < 0)
1520 /* Gets sensor's brightness setting */
1522 sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
1526 switch (ov->sensor) {
1532 rc = i2c_r(ov, OV7610_REG_BRT);
1539 *val = ov->brightness;
1542 PDEBUG(3, "Unsupported with this sensor");
1546 PDEBUG(3, "%d", *val);
1547 ov->brightness = *val;
1552 /* -------------------------------------------------------------------------- */
1554 /* Sets sensor's saturation (color intensity) setting to "val" */
1556 sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
1560 PDEBUG(3, "%d", val);
1562 if (ov->stop_during_set)
1563 if (ov51x_stop(ov) < 0)
1566 switch (ov->sensor) {
1571 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1576 // /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
1577 // rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
1580 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1585 rc = i2c_w(ov, 0x0c, val >> 9);
1590 PDEBUG(3, "Unsupported with this sensor");
1595 rc = 0; /* Success */
1598 if (ov51x_restart(ov) < 0)
1604 /* Gets sensor's saturation (color intensity) setting */
1606 sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
1610 switch (ov->sensor) {
1615 rc = i2c_r(ov, OV7610_REG_SAT);
1622 // /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
1623 // rc = i2c_r(ov, 0x62);
1627 // *val = (rc & 0x7e) << 9;
1628 rc = i2c_r(ov, OV7610_REG_SAT);
1638 PDEBUG(3, "Unsupported with this sensor");
1642 PDEBUG(3, "%d", *val);
1648 /* -------------------------------------------------------------------------- */
1650 /* Sets sensor's hue (red/blue balance) setting to "val" */
1652 sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
1656 PDEBUG(3, "%d", val);
1658 if (ov->stop_during_set)
1659 if (ov51x_stop(ov) < 0)
1662 switch (ov->sensor) {
1666 rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
1670 rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
1675 // Hue control is causing problems. I will enable it once it's fixed.
1677 rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
1681 rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
1687 rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
1692 PDEBUG(3, "Unsupported with this sensor");
1697 rc = 0; /* Success */
1700 if (ov51x_restart(ov) < 0)
1706 /* Gets sensor's hue (red/blue balance) setting */
1708 sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
1712 switch (ov->sensor) {
1716 rc = i2c_r(ov, OV7610_REG_BLUE);
1723 rc = i2c_r(ov, 0x7a);
1733 PDEBUG(3, "Unsupported with this sensor");
1737 PDEBUG(3, "%d", *val);
1743 /* -------------------------------------------------------------------------- */
1746 sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
1750 PDEBUG(4, "sensor_set_picture");
1752 ov->whiteness = p->whiteness;
1754 /* Don't return error if a setting is unsupported, or rest of settings
1755 * will not be performed */
1757 rc = sensor_set_contrast(ov, p->contrast);
1758 if (FATAL_ERROR(rc))
1761 rc = sensor_set_brightness(ov, p->brightness);
1762 if (FATAL_ERROR(rc))
1765 rc = sensor_set_saturation(ov, p->colour);
1766 if (FATAL_ERROR(rc))
1769 rc = sensor_set_hue(ov, p->hue);
1770 if (FATAL_ERROR(rc))
1777 sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
1781 PDEBUG(4, "sensor_get_picture");
1783 /* Don't return error if a setting is unsupported, or rest of settings
1784 * will not be performed */
1786 rc = sensor_get_contrast(ov, &(p->contrast));
1787 if (FATAL_ERROR(rc))
1790 rc = sensor_get_brightness(ov, &(p->brightness));
1791 if (FATAL_ERROR(rc))
1794 rc = sensor_get_saturation(ov, &(p->colour));
1795 if (FATAL_ERROR(rc))
1798 rc = sensor_get_hue(ov, &(p->hue));
1799 if (FATAL_ERROR(rc))
1802 p->whiteness = 105 << 8;
1808 // FIXME: Exposure range is only 0x00-0x7f in interlace mode
1809 /* Sets current exposure for sensor. This only has an effect if auto-exposure
1812 sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
1816 PDEBUG(3, "%d", val);
1818 if (ov->stop_during_set)
1819 if (ov51x_stop(ov) < 0)
1822 switch (ov->sensor) {
1829 rc = i2c_w(ov, 0x10, val);
1837 PDEBUG(3, "Unsupported with this sensor");
1840 err("Sensor not supported for set_exposure");
1844 rc = 0; /* Success */
1847 if (ov51x_restart(ov) < 0)
1854 /* Gets current exposure level from sensor, regardless of whether it is under
1855 * manual control. */
1857 sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
1861 switch (ov->sensor) {
1868 rc = i2c_r(ov, 0x10);
1878 PDEBUG(3, "Unsupported with this sensor");
1881 err("Sensor not supported for get_exposure");
1885 PDEBUG(3, "%d", *val);
1886 ov->exposure = *val;
1891 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
1893 ov51x_led_control(struct usb_ov511 *ov, int enable)
1895 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1897 if (ov->bridge == BRG_OV511PLUS)
1898 reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
1899 else if (ov->bclass == BCL_OV518)
1900 reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
1905 /* Matches the sensor's internal frame rate to the lighting frequency.
1906 * Valid frequencies are:
1907 * 50 - 50Hz, for European and Asian lighting
1908 * 60 - 60Hz, for American lighting
1910 * Tested with: OV7610, OV7620, OV76BE, OV6620
1911 * Unsupported: KS0127, KS0127B, SAA7111A
1912 * Returns: 0 for success
1915 sensor_set_light_freq(struct usb_ov511 *ov, int freq)
1919 PDEBUG(4, "%d Hz", freq);
1923 else if (freq == 50)
1926 err("Invalid light freq (%d Hz)", freq);
1930 switch (ov->sensor) {
1932 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1933 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1934 i2c_w_mask(ov, 0x13, 0x10, 0x10);
1935 i2c_w_mask(ov, 0x13, 0x00, 0x10);
1940 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1941 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1942 i2c_w_mask(ov, 0x76, 0x01, 0x01);
1946 i2c_w(ov, 0x2b, sixty?0xa8:0x28);
1947 i2c_w(ov, 0x2a, sixty?0x84:0xa4);
1952 PDEBUG(5, "Unsupported with this sensor");
1955 err("Sensor not supported for set_light_freq");
1959 ov->lightfreq = freq;
1964 /* If enable is true, turn on the sensor's banding filter, otherwise turn it
1965 * off. This filter tries to reduce the pattern of horizontal light/dark bands
1966 * caused by some (usually fluorescent) lighting. The light frequency must be
1967 * set either before or after enabling it with ov51x_set_light_freq().
1969 * Tested with: OV7610, OV7620, OV76BE, OV6620.
1970 * Unsupported: KS0127, KS0127B, SAA7111A
1971 * Returns: 0 for success
1974 sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
1978 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1980 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
1981 || ov->sensor == SEN_SAA7111A) {
1982 PDEBUG(5, "Unsupported with this sensor");
1986 rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
1990 ov->bandfilt = enable;
1995 /* If enable is true, turn on the sensor's auto brightness control, otherwise
1998 * Unsupported: KS0127, KS0127B, SAA7111A
1999 * Returns: 0 for success
2002 sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
2006 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2008 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
2009 || ov->sensor == SEN_SAA7111A) {
2010 PDEBUG(5, "Unsupported with this sensor");
2014 rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
2018 ov->auto_brt = enable;
2023 /* If enable is true, turn on the sensor's auto exposure control, otherwise
2026 * Unsupported: KS0127, KS0127B, SAA7111A
2027 * Returns: 0 for success
2030 sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
2032 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2034 switch (ov->sensor) {
2036 i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
2042 i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
2045 i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
2050 PDEBUG(5, "Unsupported with this sensor");
2053 err("Sensor not supported for set_auto_exposure");
2057 ov->auto_exp = enable;
2062 /* Modifies the sensor's exposure algorithm to allow proper exposure of objects
2063 * that are illuminated from behind.
2065 * Tested with: OV6620, OV7620
2066 * Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
2067 * Returns: 0 for success
2070 sensor_set_backlight(struct usb_ov511 *ov, int enable)
2072 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2074 switch (ov->sensor) {
2077 i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
2078 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2079 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2082 i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
2083 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2084 i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
2087 i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
2088 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2089 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2096 PDEBUG(5, "Unsupported with this sensor");
2099 err("Sensor not supported for set_backlight");
2103 ov->backlight = enable;
2109 sensor_set_mirror(struct usb_ov511 *ov, int enable)
2111 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2113 switch (ov->sensor) {
2120 i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
2125 PDEBUG(5, "Unsupported with this sensor");
2128 err("Sensor not supported for set_mirror");
2132 ov->mirror = enable;
2137 /* Returns number of bits per pixel (regardless of where they are located;
2138 * planar or not), or zero for unsupported format.
2141 get_depth(int palette)
2144 case VIDEO_PALETTE_GREY: return 8;
2145 case VIDEO_PALETTE_YUV420: return 12;
2146 case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
2147 default: return 0; /* Invalid format */
2151 /* Bytes per frame. Used by read(). Return of 0 indicates error */
2152 static inline long int
2153 get_frame_length(struct ov511_frame *frame)
2158 return ((frame->width * frame->height
2159 * get_depth(frame->format)) >> 3);
2163 mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
2164 int mode, int sub_flag, int qvga)
2168 /******** Mode (VGA/QVGA) and sensor specific regs ********/
2170 switch (ov->sensor) {
2172 i2c_w(ov, 0x14, qvga?0x24:0x04);
2173 // FIXME: Does this improve the image quality or frame rate?
2175 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2176 i2c_w(ov, 0x24, 0x10);
2177 i2c_w(ov, 0x25, qvga?0x40:0x8a);
2178 i2c_w(ov, 0x2f, qvga?0x30:0xb0);
2179 i2c_w(ov, 0x35, qvga?0x1c:0x9c);
2183 // i2c_w(ov, 0x2b, 0x00);
2184 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2185 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2186 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2187 i2c_w(ov, 0x25, qvga?0x30:0x60);
2188 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2189 i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
2190 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2193 // i2c_w(ov, 0x2b, 0x00);
2194 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2195 // FIXME: Enable this once 7620AE uses 7620 initial settings
2197 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2198 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2199 i2c_w(ov, 0x25, qvga?0x30:0x60);
2200 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2201 i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
2202 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2206 i2c_w(ov, 0x14, qvga?0x24:0x04);
2209 i2c_w(ov, 0x14, qvga?0xa0:0x80);
2212 err("Invalid sensor");
2216 /******** Palette-specific regs ********/
2218 if (mode == VIDEO_PALETTE_GREY) {
2219 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2220 /* these aren't valid on the OV6620/OV7620/6630? */
2221 i2c_w_mask(ov, 0x0e, 0x40, 0x40);
2224 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2226 i2c_w_mask(ov, 0x12, 0x00, 0x10);
2227 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2229 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2232 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2233 /* not valid on the OV6620/OV7620/6630? */
2234 i2c_w_mask(ov, 0x0e, 0x00, 0x40);
2237 /* The OV518 needs special treatment. Although both the OV518
2238 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2239 * bus is actually used. The UV bus is tied to ground.
2240 * Therefore, the OV6630 needs to be in 8-bit multiplexed
2243 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2245 i2c_w_mask(ov, 0x12, 0x10, 0x10);
2246 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2248 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2252 /******** Clock programming ********/
2254 /* The OV6620 needs special handling. This prevents the
2255 * severe banding that normally occurs */
2256 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
2260 i2c_w(ov, 0x2a, 0x04);
2263 // clock = 0; /* This ensures the highest frame rate */
2265 } else if (clockdiv == -1) { /* If user didn't override it */
2266 clock = 3; /* Gives better exposure time */
2271 PDEBUG(4, "Setting clock divisor to %d", clock);
2273 i2c_w(ov, 0x11, clock);
2275 i2c_w(ov, 0x2a, 0x84);
2276 /* This next setting is critical. It seems to improve
2277 * the gain or the contrast. The "reserved" bits seem
2278 * to have some effect in this case. */
2279 i2c_w(ov, 0x2d, 0x85);
2284 clock = 1; /* This ensures the highest frame rate */
2285 } else if (clockdiv == -1) { /* If user didn't override it */
2286 /* Calculate and set the clock divisor */
2287 clock = ((sub_flag ? ov->subw * ov->subh
2289 * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
2295 PDEBUG(4, "Setting clock divisor to %d", clock);
2297 i2c_w(ov, 0x11, clock);
2300 /******** Special Features ********/
2303 i2c_w(ov, 0x16, framedrop);
2306 i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
2308 /* Enable auto white balance */
2309 i2c_w_mask(ov, 0x12, 0x04, 0x04);
2311 // This will go away as soon as ov51x_mode_init_sensor_regs()
2313 /* 7620/6620/6630? don't have register 0x35, so play it safe */
2314 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2315 if (width == 640 && height == 480)
2316 i2c_w(ov, 0x35, 0x9e);
2318 i2c_w(ov, 0x35, 0x1e);
2325 set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
2329 int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
2330 int hoffset, voffset, hwscale = 0, vwscale = 0;
2332 /* The different sensor ICs handle setting up of window differently.
2333 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
2334 switch (ov->sensor) {
2339 vwsbase = vwebase = 0x05;
2349 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
2351 vwsbase = vwebase = 0x05;
2354 err("Invalid sensor");
2358 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
2359 /* Note: OV518(+) does downsample on its own) */
2360 if ((width > 176 && height > 144)
2361 || ov->bclass == BCL_OV518) { /* CIF */
2362 ret = mode_init_ov_sensor_regs(ov, width, height,
2367 vwscale = 1; /* The datasheet says 0; it's wrong */
2370 } else if (width > 176 || height > 144) {
2371 err("Illegal dimensions");
2374 ret = mode_init_ov_sensor_regs(ov, width, height,
2382 if (width > 320 && height > 240) { /* VGA */
2383 ret = mode_init_ov_sensor_regs(ov, width, height,
2391 } else if (width > 320 || height > 240) {
2392 err("Illegal dimensions");
2395 ret = mode_init_ov_sensor_regs(ov, width, height,
2405 /* Center the window */
2406 hoffset = ((hwsize - width) / 2) >> hwscale;
2407 voffset = ((vwsize - height) / 2) >> vwscale;
2409 /* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
2411 i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
2412 i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
2413 i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
2414 i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
2416 i2c_w(ov, 0x17, hwsbase + hoffset);
2417 i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
2418 i2c_w(ov, 0x19, vwsbase + voffset);
2419 i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
2430 /* Set up the OV511/OV511+ with the given image parameters.
2432 * Do not put any sensor-specific code in here (including I2C I/O functions)
2435 ov511_mode_init_regs(struct usb_ov511 *ov,
2436 int width, int height, int mode, int sub_flag)
2445 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2446 width, height, mode, sub_flag);
2448 // FIXME: This should be moved to a 7111a-specific function once
2449 // subcapture is dealt with properly
2450 if (ov->sensor == SEN_SAA7111A) {
2451 if (width == 320 && height == 240) {
2452 /* No need to do anything special */
2453 } else if (width == 640 && height == 480) {
2454 /* Set the OV511 up as 320x480, but keep the
2455 * V4L resolution as 640x480 */
2458 err("SAA7111A only allows 320x240 or 640x480");
2463 /* Make sure width and height are a multiple of 8 */
2464 if (width % 8 || height % 8) {
2465 err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
2469 if (width < ov->minwidth || height < ov->minheight) {
2470 err("Requested dimensions are too small");
2474 if (ov51x_stop(ov) < 0)
2477 if (mode == VIDEO_PALETTE_GREY) {
2478 reg_w(ov, R511_CAM_UV_EN, 0x00);
2479 reg_w(ov, R511_SNAP_UV_EN, 0x00);
2480 reg_w(ov, R511_SNAP_OPTS, 0x01);
2482 reg_w(ov, R511_CAM_UV_EN, 0x01);
2483 reg_w(ov, R511_SNAP_UV_EN, 0x01);
2484 reg_w(ov, R511_SNAP_OPTS, 0x03);
2487 /* Here I'm assuming that snapshot size == image size.
2488 * I hope that's always true. --claudio
2490 hsegs = (width >> 3) - 1;
2491 vsegs = (height >> 3) - 1;
2493 reg_w(ov, R511_CAM_PXCNT, hsegs);
2494 reg_w(ov, R511_CAM_LNCNT, vsegs);
2495 reg_w(ov, R511_CAM_PXDIV, 0x00);
2496 reg_w(ov, R511_CAM_LNDIV, 0x00);
2498 /* YUV420, low pass filter on */
2499 reg_w(ov, R511_CAM_OPTS, 0x03);
2501 /* Snapshot additions */
2502 reg_w(ov, R511_SNAP_PXCNT, hsegs);
2503 reg_w(ov, R511_SNAP_LNCNT, vsegs);
2504 reg_w(ov, R511_SNAP_PXDIV, 0x00);
2505 reg_w(ov, R511_SNAP_LNDIV, 0x00);
2508 /* Enable Y and UV quantization and compression */
2509 reg_w(ov, R511_COMP_EN, 0x07);
2510 reg_w(ov, R511_COMP_LUT_EN, 0x03);
2511 ov51x_reset(ov, OV511_RESET_OMNICE);
2514 if (ov51x_restart(ov) < 0)
2520 /* Sets up the OV518/OV518+ with the given image parameters
2522 * OV518 needs a completely different approach, until we can figure out what
2523 * the individual registers do. Also, only 15 FPS is supported now.
2525 * Do not put any sensor-specific code in here (including I2C I/O functions)
2528 ov518_mode_init_regs(struct usb_ov511 *ov,
2529 int width, int height, int mode, int sub_flag)
2531 int hsegs, vsegs, hi_res;
2538 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2539 width, height, mode, sub_flag);
2541 if (width % 16 || height % 8) {
2542 err("Invalid size (%d, %d)", width, height);
2546 if (width < ov->minwidth || height < ov->minheight) {
2547 err("Requested dimensions are too small");
2551 if (width >= 320 && height >= 240) {
2553 } else if (width >= 320 || height >= 240) {
2554 err("Invalid width/height combination (%d, %d)", width, height);
2560 if (ov51x_stop(ov) < 0)
2563 /******** Set the mode ********/
2574 if (ov->bridge == BRG_OV518 && ov518_color) {
2575 /* OV518 needs U and V swapped */
2576 i2c_w_mask(ov, 0x15, 0x00, 0x01);
2578 if (mode == VIDEO_PALETTE_GREY) {
2579 /* Set 16-bit input format (UV data are ignored) */
2580 reg_w_mask(ov, 0x20, 0x00, 0x08);
2582 /* Set 8-bit (4:0:0) output format */
2583 reg_w_mask(ov, 0x28, 0x00, 0xf0);
2584 reg_w_mask(ov, 0x38, 0x00, 0xf0);
2586 /* Set 8-bit (YVYU) input format */
2587 reg_w_mask(ov, 0x20, 0x08, 0x08);
2589 /* Set 12-bit (4:2:0) output format */
2590 reg_w_mask(ov, 0x28, 0x80, 0xf0);
2591 reg_w_mask(ov, 0x38, 0x80, 0xf0);
2594 reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2595 reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2601 reg_w(ov, 0x29, hsegs);
2602 reg_w(ov, 0x2a, vsegs);
2604 reg_w(ov, 0x39, hsegs);
2605 reg_w(ov, 0x3a, vsegs);
2607 /* Windows driver does this here; who knows why */
2608 reg_w(ov, 0x2f, 0x80);
2610 /******** Set the framerate (to 15 FPS) ********/
2612 /* Mode independent, but framerate dependent, regs */
2613 reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
2614 reg_w(ov, 0x22, 0x18);
2615 reg_w(ov, 0x23, 0xff);
2617 if (ov->bridge == BRG_OV518PLUS)
2618 reg_w(ov, 0x21, 0x19);
2620 reg_w(ov, 0x71, 0x19); /* Compression-related? */
2622 // FIXME: Sensor-specific
2623 /* Bit 5 is what matters here. Of course, it is "reserved" */
2624 i2c_w(ov, 0x54, 0x23);
2626 reg_w(ov, 0x2f, 0x80);
2628 if (ov->bridge == BRG_OV518PLUS) {
2629 reg_w(ov, 0x24, 0x94);
2630 reg_w(ov, 0x25, 0x90);
2631 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2632 ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
2633 ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
2634 ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
2635 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2636 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2637 ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
2638 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2639 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2641 reg_w(ov, 0x24, 0x9f);
2642 reg_w(ov, 0x25, 0x90);
2643 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2644 ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
2645 ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
2646 ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
2647 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2648 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2649 ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
2650 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2651 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2654 reg_w(ov, 0x2f, 0x80);
2656 if (ov51x_restart(ov) < 0)
2659 /* Reset it just for good measure */
2660 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
2666 /* This is a wrapper around the OV511, OV518, and sensor specific functions */
2668 mode_init_regs(struct usb_ov511 *ov,
2669 int width, int height, int mode, int sub_flag)
2673 if (!ov || !ov->dev)
2676 if (ov->bclass == BCL_OV518) {
2677 rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
2679 rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
2682 if (FATAL_ERROR(rc))
2685 switch (ov->sensor) {
2692 rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
2696 err("KS0127-series decoders not supported yet");
2700 // rc = mode_init_saa_sensor_regs(ov, width, height, mode,
2703 PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
2706 err("Unknown sensor");
2710 if (FATAL_ERROR(rc))
2713 /* Sensor-independent settings */
2714 rc = sensor_set_auto_brightness(ov, ov->auto_brt);
2715 if (FATAL_ERROR(rc))
2718 rc = sensor_set_auto_exposure(ov, ov->auto_exp);
2719 if (FATAL_ERROR(rc))
2722 rc = sensor_set_banding_filter(ov, bandingfilter);
2723 if (FATAL_ERROR(rc))
2726 if (ov->lightfreq) {
2727 rc = sensor_set_light_freq(ov, lightfreq);
2728 if (FATAL_ERROR(rc))
2732 rc = sensor_set_backlight(ov, ov->backlight);
2733 if (FATAL_ERROR(rc))
2736 rc = sensor_set_mirror(ov, ov->mirror);
2737 if (FATAL_ERROR(rc))
2743 /* This sets the default image parameters. This is useful for apps that use
2744 * read() and do not set these.
2747 ov51x_set_default_params(struct usb_ov511 *ov)
2751 /* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
2752 * (using read() instead). */
2753 for (i = 0; i < OV511_NUMFRAMES; i++) {
2754 ov->frame[i].width = ov->maxwidth;
2755 ov->frame[i].height = ov->maxheight;
2756 ov->frame[i].bytes_read = 0;
2758 ov->frame[i].format = force_palette;
2760 ov->frame[i].format = VIDEO_PALETTE_YUV420;
2762 ov->frame[i].depth = get_depth(ov->frame[i].format);
2765 PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
2766 symbolic(v4l1_plist, ov->frame[0].format));
2768 /* Initialize to max width/height, YUV420 or RGB24 (if supported) */
2769 if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
2770 ov->frame[0].format, 0) < 0)
2776 /**********************************************************************
2778 * Video decoder stuff
2780 **********************************************************************/
2782 /* Set analog input port of decoder */
2784 decoder_set_input(struct usb_ov511 *ov, int input)
2786 PDEBUG(4, "port %d", input);
2788 switch (ov->sensor) {
2792 i2c_w_mask(ov, 0x02, input, 0x07);
2793 /* Bypass chrominance trap for modes 4..7 */
2794 i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
2804 /* Get ASCII name of video input */
2806 decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
2808 switch (ov->sensor) {
2811 if (input < 0 || input > 7)
2814 sprintf(name, "CVBS-%d", input);
2815 else // if (input < 8)
2816 sprintf(name, "S-Video-%d", input - 4);
2820 sprintf(name, "%s", "Camera");
2826 /* Set norm (NTSC, PAL, SECAM, AUTO) */
2828 decoder_set_norm(struct usb_ov511 *ov, int norm)
2830 PDEBUG(4, "%d", norm);
2832 switch (ov->sensor) {
2837 if (norm == VIDEO_MODE_NTSC) {
2838 reg_8 = 0x40; /* 60 Hz */
2839 reg_e = 0x00; /* NTSC M / PAL BGHI */
2840 } else if (norm == VIDEO_MODE_PAL) {
2841 reg_8 = 0x00; /* 50 Hz */
2842 reg_e = 0x00; /* NTSC M / PAL BGHI */
2843 } else if (norm == VIDEO_MODE_AUTO) {
2844 reg_8 = 0x80; /* Auto field detect */
2845 reg_e = 0x00; /* NTSC M / PAL BGHI */
2846 } else if (norm == VIDEO_MODE_SECAM) {
2847 reg_8 = 0x00; /* 50 Hz */
2848 reg_e = 0x50; /* SECAM / PAL 4.43 */
2853 i2c_w_mask(ov, 0x08, reg_8, 0xc0);
2854 i2c_w_mask(ov, 0x0e, reg_e, 0x70);
2864 /**********************************************************************
2868 **********************************************************************/
2870 /* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
2871 * image at pOut is specified by w.
2874 make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
2876 unsigned char *pOut1 = pOut;
2879 for (y = 0; y < 8; y++) {
2881 for (x = 0; x < 8; x++) {
2889 * For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
2890 * The segments represent 4 squares of 8x8 pixels as follows:
2892 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2893 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2895 * 56 57 ... 63 120 121 ... 127 248 249 ... 255
2899 yuv400raw_to_yuv400p(struct ov511_frame *frame,
2900 unsigned char *pIn0, unsigned char *pOut0)
2903 unsigned char *pIn, *pOut, *pOutLine;
2908 for (y = 0; y < frame->rawheight - 1; y += 8) {
2910 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2911 make_8x8(pIn, pOut, frame->rawwidth);
2915 pOutLine += 8 * frame->rawwidth;
2920 * For YUV 4:2:0 images, the data show up in 384 byte segments.
2921 * The first 64 bytes of each segment are U, the next 64 are V. The U and
2922 * V are arranged as follows:
2929 * U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
2931 * The next 256 bytes are full resolution Y data and represent 4 squares
2932 * of 8x8 pixels as follows:
2934 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2935 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2937 * 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
2939 * Note that the U and V data in one segment represent a 16 x 16 pixel
2940 * area, but the Y data represent a 32 x 8 pixel area. If the width is not an
2941 * even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
2942 * next horizontal stripe.
2944 * If dumppix module param is set, _parse_data just dumps the incoming segments,
2945 * verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
2946 * this puts the data on the standard output and can be analyzed with the
2947 * parseppm.c utility I wrote. That's a much faster way for figuring out how
2948 * these data are scrambled.
2951 /* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
2953 * FIXME: Currently only handles width and height that are multiples of 16
2956 yuv420raw_to_yuv420p(struct ov511_frame *frame,
2957 unsigned char *pIn0, unsigned char *pOut0)
2960 unsigned char *pIn, *pOut, *pOutLine;
2961 const unsigned int a = frame->rawwidth * frame->rawheight;
2962 const unsigned int w = frame->rawwidth / 2;
2966 pOutLine = pOut0 + a;
2967 for (y = 0; y < frame->rawheight - 1; y += 16) {
2969 for (x = 0; x < frame->rawwidth - 1; x += 16) {
2970 make_8x8(pIn, pOut, w);
2971 make_8x8(pIn + 64, pOut + a/4, w);
2982 for (y = 0; y < frame->rawheight - 1; y += 8) {
2984 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2985 make_8x8(pIn, pOut, frame->rawwidth);
2993 pOutLine += 8 * frame->rawwidth;
2997 /**********************************************************************
3001 **********************************************************************/
3004 request_decompressor(struct usb_ov511 *ov)
3006 if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
3007 err("No decompressor available");
3009 err("Unknown bridge");
3016 decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3017 unsigned char *pIn0, unsigned char *pOut0)
3019 if (!ov->decomp_ops)
3020 if (request_decompressor(ov))
3025 /**********************************************************************
3029 **********************************************************************/
3031 /* Fuses even and odd fields together, and doubles width.
3032 * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
3033 * OUTPUT: a normal YUV planar image, with correct aspect ratio
3036 deinterlace(struct ov511_frame *frame, int rawformat,
3037 unsigned char *pIn0, unsigned char *pOut0)
3039 const int fieldheight = frame->rawheight / 2;
3040 const int fieldpix = fieldheight * frame->rawwidth;
3041 const int w = frame->width;
3043 unsigned char *pInEven, *pInOdd, *pOut;
3045 PDEBUG(5, "fieldheight=%d", fieldheight);
3047 if (frame->rawheight != frame->height) {
3048 err("invalid height");
3052 if ((frame->rawwidth * 2) != frame->width) {
3053 err("invalid width");
3059 pInEven = pInOdd + fieldpix;
3061 for (y = 0; y < fieldheight; y++) {
3062 for (x = 0; x < frame->rawwidth; x++) {
3064 *(pOut+1) = *pInEven++;
3065 *(pOut+w) = *pInOdd;
3066 *(pOut+w+1) = *pInOdd++;
3072 if (rawformat == RAWFMT_YUV420) {
3074 pInOdd = pIn0 + fieldpix * 2;
3075 pInEven = pInOdd + fieldpix / 4;
3076 for (y = 0; y < fieldheight / 2; y++) {
3077 for (x = 0; x < frame->rawwidth / 2; x++) {
3079 *(pOut+1) = *pInEven++;
3080 *(pOut+w/2) = *pInOdd;
3081 *(pOut+w/2+1) = *pInOdd++;
3087 pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
3088 pInEven = pInOdd + fieldpix / 4;
3089 for (y = 0; y < fieldheight / 2; y++) {
3090 for (x = 0; x < frame->rawwidth / 2; x++) {
3092 *(pOut+1) = *pInEven++;
3093 *(pOut+w/2) = *pInOdd;
3094 *(pOut+w/2+1) = *pInOdd++;
3103 ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
3105 /* Deinterlace frame, if necessary */
3106 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3107 if (frame->compressed)
3108 decompress(ov, frame, frame->rawdata,
3111 yuv400raw_to_yuv400p(frame, frame->rawdata,
3114 deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
3117 if (frame->compressed)
3118 decompress(ov, frame, frame->rawdata,
3121 yuv400raw_to_yuv400p(frame, frame->rawdata,
3126 /* Process raw YUV420 data into standard YUV420P */
3128 ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
3130 /* Deinterlace frame, if necessary */
3131 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3132 if (frame->compressed)
3133 decompress(ov, frame, frame->rawdata, frame->tempdata);
3135 yuv420raw_to_yuv420p(frame, frame->rawdata,
3138 deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
3141 if (frame->compressed)
3142 decompress(ov, frame, frame->rawdata, frame->data);
3144 yuv420raw_to_yuv420p(frame, frame->rawdata,
3149 /* Post-processes the specified frame. This consists of:
3150 * 1. Decompress frame, if necessary
3151 * 2. Deinterlace frame and scale to proper size, if necessary
3152 * 3. Convert from YUV planar to destination format, if necessary
3153 * 4. Fix the RGB offset, if necessary
3156 ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
3159 memset(frame->data, 0,
3160 MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3161 PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
3162 memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
3164 switch (frame->format) {
3165 case VIDEO_PALETTE_GREY:
3166 ov51x_postprocess_grey(ov, frame);
3168 case VIDEO_PALETTE_YUV420:
3169 case VIDEO_PALETTE_YUV420P:
3170 ov51x_postprocess_yuv420(ov, frame);
3173 err("Cannot convert data to %s",
3174 symbolic(v4l1_plist, frame->format));
3179 /**********************************************************************
3181 * OV51x data transfer, IRQ handler
3183 **********************************************************************/
3186 ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3189 int pnum = in[ov->packet_size - 1]; /* Get packet number */
3190 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3191 struct ov511_frame *frame = &ov->frame[ov->curframe];
3194 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3195 * byte non-zero. The EOF packet has image width/height in the
3196 * 10th and 11th bytes. The 9th byte is given as follows:
3199 * 6: compression enabled
3200 * 5: 422/420/400 modes
3201 * 4: 422/420/400 modes
3203 * 2: snapshot button on
3209 info("ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
3210 pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
3211 in[7], in[8], in[9], in[10], in[11]);
3214 /* Check for SOF/EOF packet */
3215 if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
3221 ts = (struct timeval *)(frame->data
3222 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3223 do_gettimeofday(ts);
3225 /* Get the actual frame size from the EOF header */
3226 frame->rawwidth = ((int)(in[9]) + 1) * 8;
3227 frame->rawheight = ((int)(in[10]) + 1) * 8;
3229 PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
3230 ov->curframe, pnum, frame->rawwidth, frame->rawheight,
3231 frame->bytes_recvd);
3233 /* Validate the header data */
3234 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3235 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
3238 /* Don't allow byte count to exceed buffer size */
3239 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3241 if (frame->scanstate == STATE_LINES) {
3244 frame->grabstate = FRAME_DONE;
3245 wake_up_interruptible(&frame->wq);
3247 /* If next frame is ready or grabbing,
3249 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3250 if (ov->frame[nextf].grabstate == FRAME_READY
3251 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3252 ov->curframe = nextf;
3253 ov->frame[nextf].scanstate = STATE_SCANNING;
3255 if (frame->grabstate == FRAME_DONE) {
3256 PDEBUG(4, "** Frame done **");
3258 PDEBUG(4, "Frame not ready? state = %d",
3259 ov->frame[nextf].grabstate);
3265 PDEBUG(5, "Frame done, but not scanning");
3267 /* Image corruption caused by misplaced frame->segment = 0
3268 * fixed by carlosf@conectiva.com.br
3272 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3274 /* Check to see if it's a snapshot frame */
3275 /* FIXME?? Should the snapshot reset go here? Performance? */
3277 frame->snapshot = 1;
3278 PDEBUG(3, "snapshot detected");
3281 frame->scanstate = STATE_LINES;
3282 frame->bytes_recvd = 0;
3283 frame->compressed = in[8] & 0x40;
3287 /* Are we in a frame? */
3288 if (frame->scanstate != STATE_LINES) {
3289 PDEBUG(5, "Not in a frame; packet skipped");
3293 /* If frame start, skip header */
3294 if (frame->bytes_recvd == 0)
3299 num = n - offset - 1;
3301 /* Dump all data exactly as received */
3303 frame->bytes_recvd += n - 1;
3304 if (frame->bytes_recvd <= max_raw)
3305 memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
3308 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3309 frame->bytes_recvd - max_raw);
3310 } else if (!frame->compressed && !remove_zeros) {
3311 frame->bytes_recvd += num;
3312 if (frame->bytes_recvd <= max_raw)
3313 memcpy(frame->rawdata + frame->bytes_recvd - num,
3316 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3317 frame->bytes_recvd - max_raw);
3318 } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
3319 int b, read = 0, allzero, copied = 0;
3321 frame->bytes_recvd += 32 - offset; // Bytes out
3322 memcpy(frame->rawdata, in + offset, 32 - offset);
3326 while (read < n - 1) {
3328 for (b = 0; b < 32; b++) {
3338 if (frame->bytes_recvd + copied + 32 <= max_raw)
3340 memcpy(frame->rawdata
3341 + frame->bytes_recvd + copied,
3345 PDEBUG(3, "Raw data buffer overrun!!");
3351 frame->bytes_recvd += copied;
3356 ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3358 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3359 struct ov511_frame *frame = &ov->frame[ov->curframe];
3362 /* Don't copy the packet number byte */
3363 if (ov->packet_numbering)
3366 /* A false positive here is likely, until OVT gives me
3367 * the definitive SOF/EOF format */
3368 if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
3370 info("ph: %2x %2x %2x %2x %2x %2x %2x %2x", in[0],
3371 in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
3374 if (frame->scanstate == STATE_LINES) {
3375 PDEBUG(4, "Detected frame end/start");
3377 } else { //scanstate == STATE_SCANNING
3379 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3387 ts = (struct timeval *)(frame->data
3388 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3389 do_gettimeofday(ts);
3391 PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
3393 (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
3395 // FIXME: Since we don't know the header formats yet,
3396 // there is no way to know what the actual image size is
3397 frame->rawwidth = frame->width;
3398 frame->rawheight = frame->height;
3400 /* Validate the header data */
3401 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3402 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
3404 /* Don't allow byte count to exceed buffer size */
3405 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3407 if (frame->scanstate == STATE_LINES) {
3410 frame->grabstate = FRAME_DONE;
3411 wake_up_interruptible(&frame->wq);
3413 /* If next frame is ready or grabbing,
3415 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3416 if (ov->frame[nextf].grabstate == FRAME_READY
3417 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3418 ov->curframe = nextf;
3419 ov->frame[nextf].scanstate = STATE_SCANNING;
3420 frame = &ov->frame[nextf];
3422 if (frame->grabstate == FRAME_DONE) {
3423 PDEBUG(4, "** Frame done **");
3425 PDEBUG(4, "Frame not ready? state = %d",
3426 ov->frame[nextf].grabstate);
3430 PDEBUG(4, "SOF dropped (no active frame)");
3431 return; /* Nowhere to store this frame */
3435 PDEBUG(4, "Starting capture on frame %d", frame->framenum);
3437 // Snapshot not reverse-engineered yet.
3439 /* Check to see if it's a snapshot frame */
3440 /* FIXME?? Should the snapshot reset go here? Performance? */
3442 frame->snapshot = 1;
3443 PDEBUG(3, "snapshot detected");
3446 frame->scanstate = STATE_LINES;
3447 frame->bytes_recvd = 0;
3448 frame->compressed = 1;
3451 /* Are we in a frame? */
3452 if (frame->scanstate != STATE_LINES) {
3453 PDEBUG(4, "scanstate: no SOF yet");
3457 /* Dump all data exactly as received */
3459 frame->bytes_recvd += n;
3460 if (frame->bytes_recvd <= max_raw)
3461 memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
3463 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3464 frame->bytes_recvd - max_raw);
3466 /* All incoming data are divided into 8-byte segments. If the
3467 * segment contains all zero bytes, it must be skipped. These
3468 * zero-segments allow the OV518 to mainain a constant data rate
3469 * regardless of the effectiveness of the compression. Segments
3470 * are aligned relative to the beginning of each isochronous
3471 * packet. The first segment in each image is a header (the
3472 * decompressor skips it later).
3475 int b, read = 0, allzero, copied = 0;
3479 for (b = 0; b < 8; b++) {
3489 if (frame->bytes_recvd + copied + 8 <= max_raw)
3491 memcpy(frame->rawdata
3492 + frame->bytes_recvd + copied,
3496 PDEBUG(3, "Raw data buffer overrun!!");
3501 frame->bytes_recvd += copied;
3506 ov51x_isoc_irq(struct urb *urb)
3509 struct usb_ov511 *ov;
3510 struct ov511_sbuf *sbuf;
3512 if (!urb->context) {
3513 PDEBUG(4, "no context");
3517 sbuf = urb->context;
3520 if (!ov || !ov->dev || !ov->user) {
3521 PDEBUG(4, "no device, or not open");
3525 if (!ov->streaming) {
3526 PDEBUG(4, "hmmm... not streaming, but got interrupt");
3530 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
3531 PDEBUG(4, "URB unlinked");
3535 if (urb->status != -EINPROGRESS && urb->status != 0) {
3536 err("ERROR: urb->status=%d: %s", urb->status,
3537 symbolic(urb_errlist, urb->status));
3540 /* Copy the data received into our frame buffer */
3541 PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
3542 urb->number_of_packets);
3543 for (i = 0; i < urb->number_of_packets; i++) {
3544 /* Warning: Don't call *_move_data() if no frame active! */
3545 if (ov->curframe >= 0) {
3546 int n = urb->iso_frame_desc[i].actual_length;
3547 int st = urb->iso_frame_desc[i].status;
3548 unsigned char *cdata;
3550 urb->iso_frame_desc[i].actual_length = 0;
3551 urb->iso_frame_desc[i].status = 0;
3553 cdata = urb->transfer_buffer
3554 + urb->iso_frame_desc[i].offset;
3557 PDEBUG(4, "Zero-length packet");
3562 PDEBUG(2, "data error: [%d] len=%d, status=%d",
3565 if (ov->bclass == BCL_OV511)
3566 ov511_move_data(ov, cdata, n);
3567 else if (ov->bclass == BCL_OV518)
3568 ov518_move_data(ov, cdata, n);
3570 err("Unknown bridge device (%d)", ov->bridge);
3572 } else if (waitqueue_active(&ov->wq)) {
3573 wake_up_interruptible(&ov->wq);
3577 /* Resubmit this URB */
3579 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
3580 err("usb_submit_urb() ret %d", i);
3585 /****************************************************************************
3587 * Stream initialization and termination
3589 ***************************************************************************/
3592 ov51x_init_isoc(struct usb_ov511 *ov)
3595 int fx, err, n, size;
3597 PDEBUG(3, "*** Initializing capture ***");
3601 if (ov->bridge == BRG_OV511) {
3606 else if (cams == 3 || cams == 4)
3609 err("\"cams\" parameter too high!");
3612 } else if (ov->bridge == BRG_OV511PLUS) {
3617 else if (cams == 3 || cams == 4)
3619 else if (cams >= 5 && cams <= 8)
3621 else if (cams >= 9 && cams <= 31)
3624 err("\"cams\" parameter too high!");
3627 } else if (ov->bclass == BCL_OV518) {
3632 else if (cams == 3 || cams == 4)
3634 else if (cams >= 5 && cams <= 8)
3637 err("\"cams\" parameter too high!");
3641 err("invalid bridge type");
3645 // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
3646 if (ov->bclass == BCL_OV518) {
3647 if (packetsize == -1) {
3648 ov518_set_packet_size(ov, 640);
3650 info("Forcing packet size to %d", packetsize);
3651 ov518_set_packet_size(ov, packetsize);
3654 if (packetsize == -1) {
3655 ov511_set_packet_size(ov, size);
3657 info("Forcing packet size to %d", packetsize);
3658 ov511_set_packet_size(ov, packetsize);
3662 for (n = 0; n < OV511_NUMSBUF; n++) {
3663 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
3665 err("init isoc: usb_alloc_urb ret. NULL");
3668 ov->sbuf[n].urb = urb;
3670 urb->context = &ov->sbuf[n];
3671 urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
3672 urb->transfer_flags = URB_ISO_ASAP;
3673 urb->transfer_buffer = ov->sbuf[n].data;
3674 urb->complete = ov51x_isoc_irq;
3675 urb->number_of_packets = FRAMES_PER_DESC;
3676 urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
3678 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
3679 urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
3680 urb->iso_frame_desc[fx].length = ov->packet_size;
3686 for (n = 0; n < OV511_NUMSBUF; n++) {
3687 ov->sbuf[n].urb->dev = ov->dev;
3688 err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
3690 err("init isoc: usb_submit_urb(%d) ret %d", n, err);
3699 ov51x_unlink_isoc(struct usb_ov511 *ov)
3703 /* Unschedule all of the iso td's */
3704 for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
3705 if (ov->sbuf[n].urb) {
3706 usb_kill_urb(ov->sbuf[n].urb);
3707 usb_free_urb(ov->sbuf[n].urb);
3708 ov->sbuf[n].urb = NULL;
3714 ov51x_stop_isoc(struct usb_ov511 *ov)
3716 if (!ov->streaming || !ov->dev)
3719 PDEBUG(3, "*** Stopping capture ***");
3721 if (ov->bclass == BCL_OV518)
3722 ov518_set_packet_size(ov, 0);
3724 ov511_set_packet_size(ov, 0);
3728 ov51x_unlink_isoc(ov);
3732 ov51x_new_frame(struct usb_ov511 *ov, int framenum)
3734 struct ov511_frame *frame;
3737 PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
3742 /* If we're not grabbing a frame right now and the other frame is */
3743 /* ready to be grabbed into, then use it instead */
3744 if (ov->curframe == -1) {
3745 newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
3746 if (ov->frame[newnum].grabstate == FRAME_READY)
3751 frame = &ov->frame[framenum];
3753 PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
3754 frame->width, frame->height);
3756 frame->grabstate = FRAME_GRABBING;
3757 frame->scanstate = STATE_SCANNING;
3758 frame->snapshot = 0;
3760 ov->curframe = framenum;
3762 /* Make sure it's not too big */
3763 if (frame->width > ov->maxwidth)
3764 frame->width = ov->maxwidth;
3766 frame->width &= ~7L; /* Multiple of 8 */
3768 if (frame->height > ov->maxheight)
3769 frame->height = ov->maxheight;
3771 frame->height &= ~3L; /* Multiple of 4 */
3776 /****************************************************************************
3780 ***************************************************************************/
3783 * - You must acquire buf_lock before entering this function.
3784 * - Because this code will free any non-null pointer, you must be sure to null
3785 * them if you explicitly free them somewhere else!
3788 ov51x_do_dealloc(struct usb_ov511 *ov)
3791 PDEBUG(4, "entered");
3794 rvfree(ov->fbuf, OV511_NUMFRAMES
3795 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3802 vfree(ov->tempfbuf);
3803 ov->tempfbuf = NULL;
3805 for (i = 0; i < OV511_NUMSBUF; i++) {
3806 kfree(ov->sbuf[i].data);
3807 ov->sbuf[i].data = NULL;
3810 for (i = 0; i < OV511_NUMFRAMES; i++) {
3811 ov->frame[i].data = NULL;
3812 ov->frame[i].rawdata = NULL;
3813 ov->frame[i].tempdata = NULL;
3814 if (ov->frame[i].compbuf) {
3815 free_page((unsigned long) ov->frame[i].compbuf);
3816 ov->frame[i].compbuf = NULL;
3820 PDEBUG(4, "buffer memory deallocated");
3821 ov->buf_state = BUF_NOT_ALLOCATED;
3822 PDEBUG(4, "leaving");
3826 ov51x_alloc(struct usb_ov511 *ov)
3829 const int w = ov->maxwidth;
3830 const int h = ov->maxheight;
3831 const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
3832 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3834 PDEBUG(4, "entered");
3835 mutex_lock(&ov->buf_lock);
3837 if (ov->buf_state == BUF_ALLOCATED)
3840 ov->fbuf = rvmalloc(data_bufsize);
3844 ov->rawfbuf = vmalloc(raw_bufsize);
3848 memset(ov->rawfbuf, 0, raw_bufsize);
3850 ov->tempfbuf = vmalloc(raw_bufsize);
3854 memset(ov->tempfbuf, 0, raw_bufsize);
3856 for (i = 0; i < OV511_NUMSBUF; i++) {
3857 ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
3858 MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
3859 if (!ov->sbuf[i].data)
3862 PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
3865 for (i = 0; i < OV511_NUMFRAMES; i++) {
3866 ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
3867 ov->frame[i].rawdata = ov->rawfbuf
3868 + i * MAX_RAW_DATA_SIZE(w, h);
3869 ov->frame[i].tempdata = ov->tempfbuf
3870 + i * MAX_RAW_DATA_SIZE(w, h);
3872 ov->frame[i].compbuf =
3873 (unsigned char *) __get_free_page(GFP_KERNEL);
3874 if (!ov->frame[i].compbuf)
3877 PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
3880 ov->buf_state = BUF_ALLOCATED;
3882 mutex_unlock(&ov->buf_lock);
3883 PDEBUG(4, "leaving");
3886 ov51x_do_dealloc(ov);
3887 mutex_unlock(&ov->buf_lock);
3888 PDEBUG(4, "errored");
3893 ov51x_dealloc(struct usb_ov511 *ov)
3895 PDEBUG(4, "entered");
3896 mutex_lock(&ov->buf_lock);
3897 ov51x_do_dealloc(ov);
3898 mutex_unlock(&ov->buf_lock);
3899 PDEBUG(4, "leaving");
3902 /****************************************************************************
3906 ***************************************************************************/
3909 ov51x_v4l1_open(struct inode *inode, struct file *file)
3911 struct video_device *vdev = video_devdata(file);
3912 struct usb_ov511 *ov = video_get_drvdata(vdev);
3915 PDEBUG(4, "opening");
3917 mutex_lock(&ov->lock);
3925 /* In case app doesn't set them... */
3926 err = ov51x_set_default_params(ov);
3930 /* Make sure frames are reset */
3931 for (i = 0; i < OV511_NUMFRAMES; i++) {
3932 ov->frame[i].grabstate = FRAME_UNUSED;
3933 ov->frame[i].bytes_read = 0;
3936 /* If compression is on, make sure now that a
3937 * decompressor can be loaded */
3938 if (ov->compress && !ov->decomp_ops) {
3939 err = request_decompressor(ov);
3940 if (err && !dumppix)
3944 err = ov51x_alloc(ov);
3948 err = ov51x_init_isoc(ov);
3955 file->private_data = vdev;
3957 if (ov->led_policy == LED_AUTO)
3958 ov51x_led_control(ov, 1);
3961 mutex_unlock(&ov->lock);
3966 ov51x_v4l1_close(struct inode *inode, struct file *file)
3968 struct video_device *vdev = file->private_data;
3969 struct usb_ov511 *ov = video_get_drvdata(vdev);
3971 PDEBUG(4, "ov511_close");
3973 mutex_lock(&ov->lock);
3976 ov51x_stop_isoc(ov);
3978 if (ov->led_policy == LED_AUTO)
3979 ov51x_led_control(ov, 0);
3984 mutex_unlock(&ov->lock);
3986 /* Device unplugged while open. Only a minimum of unregistration is done
3987 * here; the disconnect callback already did the rest. */
3989 mutex_lock(&ov->cbuf_lock);
3992 mutex_unlock(&ov->cbuf_lock);
3999 file->private_data = NULL;
4003 /* Do not call this function directly! */
4005 ov51x_v4l1_ioctl_internal(struct inode *inode, struct file *file,
4006 unsigned int cmd, void *arg)
4008 struct video_device *vdev = file->private_data;
4009 struct usb_ov511 *ov = video_get_drvdata(vdev);
4010 PDEBUG(5, "IOCtl: 0x%X", cmd);
4018 struct video_capability *b = arg;
4020 PDEBUG(4, "VIDIOCGCAP");
4022 memset(b, 0, sizeof(struct video_capability));
4023 sprintf(b->name, "%s USB Camera",
4024 symbolic(brglist, ov->bridge));
4025 b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
4026 b->channels = ov->num_inputs;
4028 b->maxwidth = ov->maxwidth;
4029 b->maxheight = ov->maxheight;
4030 b->minwidth = ov->minwidth;
4031 b->minheight = ov->minheight;
4037 struct video_channel *v = arg;
4039 PDEBUG(4, "VIDIOCGCHAN");
4041 if ((unsigned)(v->channel) >= ov->num_inputs) {
4042 err("Invalid channel (%d)", v->channel);
4047 v->type = VIDEO_TYPE_CAMERA;
4049 // v->flags |= (ov->has_decoder) ? VIDEO_VC_NORM : 0;
4051 decoder_get_input_name(ov, v->channel, v->name);
4057 struct video_channel *v = arg;
4060 PDEBUG(4, "VIDIOCSCHAN");
4062 /* Make sure it's not a camera */
4063 if (!ov->has_decoder) {
4064 if (v->channel == 0)
4070 if (v->norm != VIDEO_MODE_PAL &&
4071 v->norm != VIDEO_MODE_NTSC &&
4072 v->norm != VIDEO_MODE_SECAM &&
4073 v->norm != VIDEO_MODE_AUTO) {
4074 err("Invalid norm (%d)", v->norm);
4078 if ((unsigned)(v->channel) >= ov->num_inputs) {
4079 err("Invalid channel (%d)", v->channel);
4083 err = decoder_set_input(ov, v->channel);
4087 err = decoder_set_norm(ov, v->norm);
4095 struct video_picture *p = arg;
4097 PDEBUG(4, "VIDIOCGPICT");
4099 memset(p, 0, sizeof(struct video_picture));
4100 if (sensor_get_picture(ov, p))
4103 /* Can we get these from frame[0]? -claudio? */
4104 p->depth = ov->frame[0].depth;
4105 p->palette = ov->frame[0].format;
4111 struct video_picture *p = arg;
4114 PDEBUG(4, "VIDIOCSPICT");
4116 if (!get_depth(p->palette))
4119 if (sensor_set_picture(ov, p))
4122 if (force_palette && p->palette != force_palette) {
4123 info("Palette rejected (%s)",
4124 symbolic(v4l1_plist, p->palette));
4128 // FIXME: Format should be independent of frames
4129 if (p->palette != ov->frame[0].format) {
4130 PDEBUG(4, "Detected format change");
4132 rc = ov51x_wait_frames_inactive(ov);
4136 mode_init_regs(ov, ov->frame[0].width,
4137 ov->frame[0].height, p->palette, ov->sub_flag);
4140 PDEBUG(4, "Setting depth=%d, palette=%s",
4141 p->depth, symbolic(v4l1_plist, p->palette));
4143 for (i = 0; i < OV511_NUMFRAMES; i++) {
4144 ov->frame[i].depth = p->depth;
4145 ov->frame[i].format = p->palette;
4150 case VIDIOCGCAPTURE:
4154 PDEBUG(4, "VIDIOCGCAPTURE");
4159 case VIDIOCSCAPTURE:
4161 struct video_capture *vc = arg;
4163 PDEBUG(4, "VIDIOCSCAPTURE");
4179 if (vc->height == 0)
4184 ov->subw = vc->width;
4185 ov->subh = vc->height;
4191 struct video_window *vw = arg;
4194 PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
4201 if (vw->height != ov->maxheight)
4203 if (vw->width != ov->maxwidth)
4207 rc = ov51x_wait_frames_inactive(ov);
4211 rc = mode_init_regs(ov, vw->width, vw->height,
4212 ov->frame[0].format, ov->sub_flag);
4216 for (i = 0; i < OV511_NUMFRAMES; i++) {
4217 ov->frame[i].width = vw->width;
4218 ov->frame[i].height = vw->height;
4225 struct video_window *vw = arg;
4227 memset(vw, 0, sizeof(struct video_window));
4228 vw->x = 0; /* FIXME */
4230 vw->width = ov->frame[0].width;
4231 vw->height = ov->frame[0].height;
4234 PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
4240 struct video_mbuf *vm = arg;
4243 PDEBUG(4, "VIDIOCGMBUF");
4245 memset(vm, 0, sizeof(struct video_mbuf));
4246 vm->size = OV511_NUMFRAMES
4247 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4248 vm->frames = OV511_NUMFRAMES;
4251 for (i = 1; i < OV511_NUMFRAMES; i++) {
4252 vm->offsets[i] = vm->offsets[i-1]
4253 + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4258 case VIDIOCMCAPTURE:
4260 struct video_mmap *vm = arg;
4262 unsigned int f = vm->frame;
4264 PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
4265 vm->height, symbolic(v4l1_plist, vm->format));
4267 depth = get_depth(vm->format);
4269 PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
4270 symbolic(v4l1_plist, vm->format));
4274 if (f >= OV511_NUMFRAMES) {
4275 err("VIDIOCMCAPTURE: invalid frame (%d)", f);
4279 if (vm->width > ov->maxwidth
4280 || vm->height > ov->maxheight) {
4281 err("VIDIOCMCAPTURE: requested dimensions too big");
4285 if (ov->frame[f].grabstate == FRAME_GRABBING) {
4286 PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
4290 if (force_palette && (vm->format != force_palette)) {
4291 PDEBUG(2, "palette rejected (%s)",
4292 symbolic(v4l1_plist, vm->format));
4296 if ((ov->frame[f].width != vm->width) ||
4297 (ov->frame[f].height != vm->height) ||
4298 (ov->frame[f].format != vm->format) ||
4299 (ov->frame[f].sub_flag != ov->sub_flag) ||
4300 (ov->frame[f].depth != depth)) {
4301 PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
4303 rc = ov51x_wait_frames_inactive(ov);
4307 rc = mode_init_regs(ov, vm->width, vm->height,
4308 vm->format, ov->sub_flag);
4311 PDEBUG(1, "Got error while initializing regs ");
4315 ov->frame[f].width = vm->width;
4316 ov->frame[f].height = vm->height;
4317 ov->frame[f].format = vm->format;
4318 ov->frame[f].sub_flag = ov->sub_flag;
4319 ov->frame[f].depth = depth;
4322 /* Mark it as ready */
4323 ov->frame[f].grabstate = FRAME_READY;
4325 PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
4327 return ov51x_new_frame(ov, f);
4331 unsigned int fnum = *((unsigned int *) arg);
4332 struct ov511_frame *frame;
4335 if (fnum >= OV511_NUMFRAMES) {
4336 err("VIDIOCSYNC: invalid frame (%d)", fnum);
4340 frame = &ov->frame[fnum];
4342 PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
4345 switch (frame->grabstate) {
4349 case FRAME_GRABBING:
4355 rc = wait_event_interruptible(frame->wq,
4356 (frame->grabstate == FRAME_DONE)
4357 || (frame->grabstate == FRAME_ERROR));
4362 if (frame->grabstate == FRAME_ERROR) {
4363 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4369 if (ov->snap_enabled && !frame->snapshot) {
4370 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4375 frame->grabstate = FRAME_UNUSED;
4377 /* Reset the hardware snapshot button */
4378 /* FIXME - Is this the best place for this? */
4379 if ((ov->snap_enabled) && (frame->snapshot)) {
4380 frame->snapshot = 0;
4381 ov51x_clear_snapshot(ov);
4384 /* Decompression, format conversion, etc... */
4385 ov51x_postprocess(ov, frame);
4394 struct video_buffer *vb = arg;
4396 PDEBUG(4, "VIDIOCGFBUF");
4398 memset(vb, 0, sizeof(struct video_buffer));
4404 struct video_unit *vu = arg;
4406 PDEBUG(4, "VIDIOCGUNIT");
4408 memset(vu, 0, sizeof(struct video_unit));
4410 vu->video = ov->vdev->minor;
4411 vu->vbi = VIDEO_NO_UNIT;
4412 vu->radio = VIDEO_NO_UNIT;
4413 vu->audio = VIDEO_NO_UNIT;
4414 vu->teletext = VIDEO_NO_UNIT;
4420 struct ov511_i2c_struct *w = arg;
4422 return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
4426 struct ov511_i2c_struct *r = arg;
4429 rc = i2c_r_slave(ov, r->slave, r->reg);
4437 PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
4438 return -ENOIOCTLCMD;
4445 ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
4446 unsigned int cmd, unsigned long arg)
4448 struct video_device *vdev = file->private_data;
4449 struct usb_ov511 *ov = video_get_drvdata(vdev);
4452 if (mutex_lock_interruptible(&ov->lock))
4455 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
4457 mutex_unlock(&ov->lock);
4462 ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4464 struct video_device *vdev = file->private_data;
4465 int noblock = file->f_flags&O_NONBLOCK;
4466 unsigned long count = cnt;
4467 struct usb_ov511 *ov = video_get_drvdata(vdev);
4468 int i, rc = 0, frmx = -1;
4469 struct ov511_frame *frame;
4471 if (mutex_lock_interruptible(&ov->lock))
4474 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
4476 if (!vdev || !buf) {
4486 // FIXME: Only supports two frames
4487 /* See if a frame is completed, then use it. */
4488 if (ov->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
4490 else if (ov->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
4493 /* If nonblocking we return immediately */
4494 if (noblock && (frmx == -1)) {
4499 /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
4500 /* See if a frame is in process (grabbing), then use it. */
4502 if (ov->frame[0].grabstate == FRAME_GRABBING)
4504 else if (ov->frame[1].grabstate == FRAME_GRABBING)
4508 /* If no frame is active, start one. */
4510 if ((rc = ov51x_new_frame(ov, frmx = 0))) {
4511 err("read: ov51x_new_frame error");
4516 frame = &ov->frame[frmx];
4524 /* Wait while we're grabbing the image */
4525 PDEBUG(4, "Waiting image grabbing");
4526 rc = wait_event_interruptible(frame->wq,
4527 (frame->grabstate == FRAME_DONE)
4528 || (frame->grabstate == FRAME_ERROR));
4533 PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
4534 PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
4536 if (frame->grabstate == FRAME_ERROR) {
4537 frame->bytes_read = 0;
4538 err("** ick! ** Errored frame %d", ov->curframe);
4539 if (ov51x_new_frame(ov, frmx)) {
4540 err("read: ov51x_new_frame error");
4547 /* Repeat until we get a snapshot frame */
4548 if (ov->snap_enabled)
4549 PDEBUG(4, "Waiting snapshot frame");
4550 if (ov->snap_enabled && !frame->snapshot) {
4551 frame->bytes_read = 0;
4552 if ((rc = ov51x_new_frame(ov, frmx))) {
4553 err("read: ov51x_new_frame error");
4559 /* Clear the snapshot */
4560 if (ov->snap_enabled && frame->snapshot) {
4561 frame->snapshot = 0;
4562 ov51x_clear_snapshot(ov);
4565 /* Decompression, format conversion, etc... */
4566 ov51x_postprocess(ov, frame);
4568 PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
4570 get_frame_length(frame));
4572 /* copy bytes to user space; we allow for partials reads */
4573 // if ((count + frame->bytes_read)
4574 // > get_frame_length((struct ov511_frame *)frame))
4575 // count = frame->scanlength - frame->bytes_read;
4577 /* FIXME - count hardwired to be one frame... */
4578 count = get_frame_length(frame);
4580 PDEBUG(4, "Copy to user space: %ld bytes", count);
4581 if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
4582 PDEBUG(4, "Copy failed! %d bytes not copied", i);
4587 frame->bytes_read += count;
4588 PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
4589 count, frame->bytes_read);
4591 /* If all data have been read... */
4592 if (frame->bytes_read
4593 >= get_frame_length(frame)) {
4594 frame->bytes_read = 0;
4596 // FIXME: Only supports two frames
4597 /* Mark it as available to be used again. */
4598 ov->frame[frmx].grabstate = FRAME_UNUSED;
4599 if ((rc = ov51x_new_frame(ov, !frmx))) {
4600 err("ov51x_new_frame returned error");
4605 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4607 mutex_unlock(&ov->lock);
4611 mutex_unlock(&ov->lock);
4616 ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4618 struct video_device *vdev = file->private_data;
4619 unsigned long start = vma->vm_start;
4620 unsigned long size = vma->vm_end - vma->vm_start;
4621 struct usb_ov511 *ov = video_get_drvdata(vdev);
4622 unsigned long page, pos;
4624 if (ov->dev == NULL)
4627 PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
4629 if (size > (((OV511_NUMFRAMES
4630 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
4631 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4634 if (mutex_lock_interruptible(&ov->lock))
4637 pos = (unsigned long)ov->fbuf;
4639 page = vmalloc_to_pfn((void *)pos);
4640 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4641 mutex_unlock(&ov->lock);
4646 if (size > PAGE_SIZE)
4652 mutex_unlock(&ov->lock);
4656 static const struct file_operations ov511_fops = {
4657 .owner = THIS_MODULE,
4658 .open = ov51x_v4l1_open,
4659 .release = ov51x_v4l1_close,
4660 .read = ov51x_v4l1_read,
4661 .mmap = ov51x_v4l1_mmap,
4662 .ioctl = ov51x_v4l1_ioctl,
4663 .compat_ioctl = v4l_compat_ioctl32,
4664 .llseek = no_llseek,
4667 static struct video_device vdev_template = {
4668 .owner = THIS_MODULE,
4669 .name = "OV511 USB Camera",
4670 .type = VID_TYPE_CAPTURE,
4671 .fops = &ov511_fops,
4672 .release = video_device_release,
4676 /****************************************************************************
4678 * OV511 and sensor configuration
4680 ***************************************************************************/
4682 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
4683 * the same register settings as the OV7610, since they are very similar.
4686 ov7xx0_configure(struct usb_ov511 *ov)
4691 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
4693 * Register 0x0f in the 7610 has the following effects:
4695 * 0x85 (AEC method 1): Best overall, good contrast range
4696 * 0x45 (AEC method 2): Very overexposed
4697 * 0xa5 (spec sheet default): Ok, but the black level is
4698 * shifted resulting in loss of contrast
4699 * 0x05 (old driver setting): very overexposed, too much
4702 static struct ov511_regvals aRegvalsNorm7610[] = {
4703 { OV511_I2C_BUS, 0x10, 0xff },
4704 { OV511_I2C_BUS, 0x16, 0x06 },
4705 { OV511_I2C_BUS, 0x28, 0x24 },
4706 { OV511_I2C_BUS, 0x2b, 0xac },
4707 { OV511_I2C_BUS, 0x12, 0x00 },
4708 { OV511_I2C_BUS, 0x38, 0x81 },
4709 { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
4710 { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
4711 { OV511_I2C_BUS, 0x15, 0x01 },
4712 { OV511_I2C_BUS, 0x20, 0x1c },
4713 { OV511_I2C_BUS, 0x23, 0x2a },
4714 { OV511_I2C_BUS, 0x24, 0x10 },
4715 { OV511_I2C_BUS, 0x25, 0x8a },
4716 { OV511_I2C_BUS, 0x26, 0xa2 },
4717 { OV511_I2C_BUS, 0x27, 0xc2 },
4718 { OV511_I2C_BUS, 0x2a, 0x04 },
4719 { OV511_I2C_BUS, 0x2c, 0xfe },
4720 { OV511_I2C_BUS, 0x2d, 0x93 },
4721 { OV511_I2C_BUS, 0x30, 0x71 },
4722 { OV511_I2C_BUS, 0x31, 0x60 },
4723 { OV511_I2C_BUS, 0x32, 0x26 },
4724 { OV511_I2C_BUS, 0x33, 0x20 },
4725 { OV511_I2C_BUS, 0x34, 0x48 },
4726 { OV511_I2C_BUS, 0x12, 0x24 },
4727 { OV511_I2C_BUS, 0x11, 0x01 },
4728 { OV511_I2C_BUS, 0x0c, 0x24 },
4729 { OV511_I2C_BUS, 0x0d, 0x24 },
4730 { OV511_DONE_BUS, 0x0, 0x00 },
4733 static struct ov511_regvals aRegvalsNorm7620[] = {
4734 { OV511_I2C_BUS, 0x00, 0x00 },
4735 { OV511_I2C_BUS, 0x01, 0x80 },
4736 { OV511_I2C_BUS, 0x02, 0x80 },
4737 { OV511_I2C_BUS, 0x03, 0xc0 },
4738 { OV511_I2C_BUS, 0x06, 0x60 },
4739 { OV511_I2C_BUS, 0x07, 0x00 },
4740 { OV511_I2C_BUS, 0x0c, 0x24 },
4741 { OV511_I2C_BUS, 0x0c, 0x24 },
4742 { OV511_I2C_BUS, 0x0d, 0x24 },
4743 { OV511_I2C_BUS, 0x11, 0x01 },
4744 { OV511_I2C_BUS, 0x12, 0x24 },
4745 { OV511_I2C_BUS, 0x13, 0x01 },
4746 { OV511_I2C_BUS, 0x14, 0x84 },
4747 { OV511_I2C_BUS, 0x15, 0x01 },
4748 { OV511_I2C_BUS, 0x16, 0x03 },
4749 { OV511_I2C_BUS, 0x17, 0x2f },
4750 { OV511_I2C_BUS, 0x18, 0xcf },
4751 { OV511_I2C_BUS, 0x19, 0x06 },
4752 { OV511_I2C_BUS, 0x1a, 0xf5 },
4753 { OV511_I2C_BUS, 0x1b, 0x00 },
4754 { OV511_I2C_BUS, 0x20, 0x18 },
4755 { OV511_I2C_BUS, 0x21, 0x80 },
4756 { OV511_I2C_BUS, 0x22, 0x80 },
4757 { OV511_I2C_BUS, 0x23, 0x00 },
4758 { OV511_I2C_BUS, 0x26, 0xa2 },
4759 { OV511_I2C_BUS, 0x27, 0xea },
4760 { OV511_I2C_BUS, 0x28, 0x20 },
4761 { OV511_I2C_BUS, 0x29, 0x00 },
4762 { OV511_I2C_BUS, 0x2a, 0x10 },
4763 { OV511_I2C_BUS, 0x2b, 0x00 },
4764 { OV511_I2C_BUS, 0x2c, 0x88 },
4765 { OV511_I2C_BUS, 0x2d, 0x91 },
4766 { OV511_I2C_BUS, 0x2e, 0x80 },
4767 { OV511_I2C_BUS, 0x2f, 0x44 },
4768 { OV511_I2C_BUS, 0x60, 0x27 },
4769 { OV511_I2C_BUS, 0x61, 0x02 },
4770 { OV511_I2C_BUS, 0x62, 0x5f },
4771 { OV511_I2C_BUS, 0x63, 0xd5 },
4772 { OV511_I2C_BUS, 0x64, 0x57 },
4773 { OV511_I2C_BUS, 0x65, 0x83 },
4774 { OV511_I2C_BUS, 0x66, 0x55 },
4775 { OV511_I2C_BUS, 0x67, 0x92 },
4776 { OV511_I2C_BUS, 0x68, 0xcf },
4777 { OV511_I2C_BUS, 0x69, 0x76 },
4778 { OV511_I2C_BUS, 0x6a, 0x22 },
4779 { OV511_I2C_BUS, 0x6b, 0x00 },
4780 { OV511_I2C_BUS, 0x6c, 0x02 },
4781 { OV511_I2C_BUS, 0x6d, 0x44 },
4782 { OV511_I2C_BUS, 0x6e, 0x80 },
4783 { OV511_I2C_BUS, 0x6f, 0x1d },
4784 { OV511_I2C_BUS, 0x70, 0x8b },
4785 { OV511_I2C_BUS, 0x71, 0x00 },
4786 { OV511_I2C_BUS, 0x72, 0x14 },
4787 { OV511_I2C_BUS, 0x73, 0x54 },
4788 { OV511_I2C_BUS, 0x74, 0x00 },
4789 { OV511_I2C_BUS, 0x75, 0x8e },
4790 { OV511_I2C_BUS, 0x76, 0x00 },
4791 { OV511_I2C_BUS, 0x77, 0xff },
4792 { OV511_I2C_BUS, 0x78, 0x80 },
4793 { OV511_I2C_BUS, 0x79, 0x80 },
4794 { OV511_I2C_BUS, 0x7a, 0x80 },
4795 { OV511_I2C_BUS, 0x7b, 0xe2 },
4796 { OV511_I2C_BUS, 0x7c, 0x00 },
4797 { OV511_DONE_BUS, 0x0, 0x00 },
4800 PDEBUG(4, "starting configuration");
4802 /* This looks redundant, but is necessary for WebCam 3 */
4803 ov->primary_i2c_slave = OV7xx0_SID;
4804 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
4807 if (init_ov_sensor(ov) >= 0) {
4808 PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
4810 /* Reset the 76xx */
4811 if (i2c_w(ov, 0x12, 0x80) < 0)
4814 /* Wait for it to initialize */
4819 while (i <= i2c_detect_tries) {
4820 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
4821 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
4829 // Was (i == i2c_detect_tries) previously. This obviously used to always report
4830 // success. Whether anyone actually depended on that bug is unknown
4831 if ((i >= i2c_detect_tries) && (success == 0)) {
4832 err("Failed to read sensor ID. You might not have an");
4833 err("OV7610/20, or it may be not responding. Report");
4834 err("this to " EMAIL);
4835 err("This is only a warning. You can attempt to use");
4836 err("your camera anyway");
4837 // Only issue a warning for now
4840 PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
4844 /* Detect sensor (sub)type */
4845 rc = i2c_r(ov, OV7610_REG_COM_I);
4848 err("Error detecting sensor type");
4850 } else if ((rc & 3) == 3) {
4851 info("Sensor is an OV7610");
4852 ov->sensor = SEN_OV7610;
4853 } else if ((rc & 3) == 1) {
4854 /* I don't know what's different about the 76BE yet. */
4855 if (i2c_r(ov, 0x15) & 1)
4856 info("Sensor is an OV7620AE");
4858 info("Sensor is an OV76BE");
4860 /* OV511+ will return all zero isoc data unless we
4861 * configure the sensor as a 7620. Someone needs to
4862 * find the exact reg. setting that causes this. */
4863 if (ov->bridge == BRG_OV511PLUS) {
4864 info("Enabling 511+/7620AE workaround");
4865 ov->sensor = SEN_OV7620;
4867 ov->sensor = SEN_OV76BE;
4869 } else if ((rc & 3) == 0) {
4870 info("Sensor is an OV7620");
4871 ov->sensor = SEN_OV7620;
4873 err("Unknown image sensor version: %d", rc & 3);
4877 if (ov->sensor == SEN_OV7620) {
4878 PDEBUG(4, "Writing 7620 registers");
4879 if (write_regvals(ov, aRegvalsNorm7620))
4882 PDEBUG(4, "Writing 7610 registers");
4883 if (write_regvals(ov, aRegvalsNorm7610))
4887 /* Set sensor-specific vars */
4889 ov->maxheight = 480;
4893 // FIXME: These do not match the actual settings yet
4894 ov->brightness = 0x80 << 8;
4895 ov->contrast = 0x80 << 8;
4896 ov->colour = 0x80 << 8;
4897 ov->hue = 0x80 << 8;
4902 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
4904 ov6xx0_configure(struct usb_ov511 *ov)
4908 static struct ov511_regvals aRegvalsNorm6x20[] = {
4909 { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4910 { OV511_I2C_BUS, 0x11, 0x01 },
4911 { OV511_I2C_BUS, 0x03, 0x60 },
4912 { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4913 { OV511_I2C_BUS, 0x07, 0xa8 },
4914 /* The ratio of 0x0c and 0x0d controls the white point */
4915 { OV511_I2C_BUS, 0x0c, 0x24 },
4916 { OV511_I2C_BUS, 0x0d, 0x24 },
4917 { OV511_I2C_BUS, 0x0f, 0x15 }, /* COMS */
4918 { OV511_I2C_BUS, 0x10, 0x75 }, /* AEC Exposure time */
4919 { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
4920 { OV511_I2C_BUS, 0x14, 0x04 },
4921 /* 0x16: 0x06 helps frame stability with moving objects */
4922 { OV511_I2C_BUS, 0x16, 0x06 },
4923 // { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4924 { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4925 /* 0x28: 0x05 Selects RGB format if RGB on */
4926 { OV511_I2C_BUS, 0x28, 0x05 },
4927 { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4928 // { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4929 { OV511_I2C_BUS, 0x2d, 0x99 },
4930 { OV511_I2C_BUS, 0x33, 0xa0 }, /* Color Processing Parameter */
4931 { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
4932 { OV511_I2C_BUS, 0x38, 0x8b },
4933 { OV511_I2C_BUS, 0x39, 0x40 },
4935 { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4936 { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4937 { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4939 { OV511_I2C_BUS, 0x3d, 0x80 },
4940 /* These next two registers (0x4a, 0x4b) are undocumented. They
4941 * control the color balance */
4942 { OV511_I2C_BUS, 0x4a, 0x80 },
4943 { OV511_I2C_BUS, 0x4b, 0x80 },
4944 { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
4945 { OV511_I2C_BUS, 0x4e, 0xc1 },
4946 { OV511_I2C_BUS, 0x4f, 0x04 },
4947 // Do 50-53 have any effect?
4948 // Toggle 0x12[2] off and on here?
4949 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
4952 static struct ov511_regvals aRegvalsNorm6x30[] = {
4953 /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4954 { OV511_I2C_BUS, 0x11, 0x00 },
4955 /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
4956 /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4957 { OV511_I2C_BUS, 0x07, 0xa8 },
4958 /* The ratio of 0x0c and 0x0d controls the white point */
4959 /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
4960 /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
4961 /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
4962 // /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
4963 { OV511_I2C_BUS, 0x16, 0x03 },
4964 // /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4965 // 21 & 22? The suggested values look wrong. Go with default
4966 /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
4967 /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
4968 // /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4970 /* 0x28: 0x05 Selects RGB format if RGB on */
4971 // /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
4972 // /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
4974 /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4975 // /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4976 { OV511_I2C_BUS, 0x2d, 0x99 },
4977 // /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
4978 // /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
4979 // /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
4980 // /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
4981 // { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4982 // { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4983 // { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4984 { OV511_I2C_BUS, 0x3d, 0x80 },
4985 // /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
4987 /* These next two registers (0x4a, 0x4b) are undocumented. They
4988 * control the color balance */
4989 // /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
4990 // /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
4991 { OV511_I2C_BUS, 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
4992 /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
4994 /* UV average mode, color killer: strongest */
4995 { OV511_I2C_BUS, 0x4f, 0x07 },
4997 { OV511_I2C_BUS, 0x54, 0x23 }, /* Max AGC gain: 18dB */
4998 { OV511_I2C_BUS, 0x57, 0x81 }, /* (default) */
4999 { OV511_I2C_BUS, 0x59, 0x01 }, /* AGC dark current comp: +1 */
5000 { OV511_I2C_BUS, 0x5a, 0x2c }, /* (undocumented) */
5001 { OV511_I2C_BUS, 0x5b, 0x0f }, /* AWB chrominance levels */
5002 // { OV511_I2C_BUS, 0x5c, 0x10 },
5003 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
5006 PDEBUG(4, "starting sensor configuration");
5008 if (init_ov_sensor(ov) < 0) {
5009 err("Failed to read sensor ID. You might not have an OV6xx0,");
5010 err("or it may be not responding. Report this to " EMAIL);
5013 PDEBUG(1, "OV6xx0 sensor detected");
5016 /* Detect sensor (sub)type */
5017 rc = i2c_r(ov, OV7610_REG_COM_I);
5020 err("Error detecting sensor type");
5024 if ((rc & 3) == 0) {
5025 ov->sensor = SEN_OV6630;
5026 info("Sensor is an OV6630");
5027 } else if ((rc & 3) == 1) {
5028 ov->sensor = SEN_OV6620;
5029 info("Sensor is an OV6620");
5030 } else if ((rc & 3) == 2) {
5031 ov->sensor = SEN_OV6630;
5032 info("Sensor is an OV6630AE");
5033 } else if ((rc & 3) == 3) {
5034 ov->sensor = SEN_OV6630;
5035 info("Sensor is an OV6630AF");
5038 /* Set sensor-specific vars */
5040 ov->maxheight = 288;
5044 // FIXME: These do not match the actual settings yet
5045 ov->brightness = 0x80 << 8;
5046 ov->contrast = 0x80 << 8;
5047 ov->colour = 0x80 << 8;
5048 ov->hue = 0x80 << 8;
5050 if (ov->sensor == SEN_OV6620) {
5051 PDEBUG(4, "Writing 6x20 registers");
5052 if (write_regvals(ov, aRegvalsNorm6x20))
5055 PDEBUG(4, "Writing 6x30 registers");
5056 if (write_regvals(ov, aRegvalsNorm6x30))
5063 /* This initializes the KS0127 and KS0127B video decoders. */
5065 ks0127_configure(struct usb_ov511 *ov)
5069 // FIXME: I don't know how to sync or reset it yet
5071 if (ov51x_init_ks_sensor(ov) < 0) {
5072 err("Failed to initialize the KS0127");
5075 PDEBUG(1, "KS012x(B) sensor detected");
5079 /* Detect decoder subtype */
5080 rc = i2c_r(ov, 0x00);
5082 err("Error detecting sensor type");
5084 } else if (rc & 0x08) {
5085 rc = i2c_r(ov, 0x3d);
5087 err("Error detecting sensor type");
5089 } else if ((rc & 0x0f) == 0) {
5090 info("Sensor is a KS0127");
5091 ov->sensor = SEN_KS0127;
5092 } else if ((rc & 0x0f) == 9) {
5093 info("Sensor is a KS0127B Rev. A");
5094 ov->sensor = SEN_KS0127B;
5097 err("Error: Sensor is an unsupported KS0122");
5101 /* Set sensor-specific vars */
5103 ov->maxheight = 480;
5107 // FIXME: These do not match the actual settings yet
5108 ov->brightness = 0x80 << 8;
5109 ov->contrast = 0x80 << 8;
5110 ov->colour = 0x80 << 8;
5111 ov->hue = 0x80 << 8;
5113 /* This device is not supported yet. Bail out now... */
5114 err("This sensor is not supported yet.");
5120 /* This initializes the SAA7111A video decoder. */
5122 saa7111a_configure(struct usb_ov511 *ov)
5126 /* Since there is no register reset command, all registers must be
5127 * written, otherwise gives erratic results */
5128 static struct ov511_regvals aRegvalsNormSAA7111A[] = {
5129 { OV511_I2C_BUS, 0x06, 0xce },
5130 { OV511_I2C_BUS, 0x07, 0x00 },
5131 { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
5132 { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
5133 { OV511_I2C_BUS, 0x00, 0x00 },
5134 { OV511_I2C_BUS, 0x01, 0x00 },
5135 { OV511_I2C_BUS, 0x03, 0x23 },
5136 { OV511_I2C_BUS, 0x04, 0x00 },
5137 { OV511_I2C_BUS, 0x05, 0x00 },
5138 { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
5139 { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
5140 { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
5141 { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
5142 { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
5143 { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
5144 { OV511_I2C_BUS, 0x0f, 0x00 },
5145 { OV511_I2C_BUS, 0x11, 0x0c },
5146 { OV511_I2C_BUS, 0x12, 0x00 },
5147 { OV511_I2C_BUS, 0x13, 0x00 },
5148 { OV511_I2C_BUS, 0x14, 0x00 },
5149 { OV511_I2C_BUS, 0x15, 0x00 },
5150 { OV511_I2C_BUS, 0x16, 0x00 },
5151 { OV511_I2C_BUS, 0x17, 0x00 },
5152 { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
5153 { OV511_DONE_BUS, 0x0, 0x00 },
5156 // FIXME: I don't know how to sync or reset it yet
5158 if (ov51x_init_saa_sensor(ov) < 0) {
5159 err("Failed to initialize the SAA7111A");
5162 PDEBUG(1, "SAA7111A sensor detected");
5166 /* 640x480 not supported with PAL */
5169 ov->maxheight = 240; /* Even field only */
5172 ov->maxheight = 480; /* Even/Odd fields */
5176 ov->minheight = 240; /* Even field only */
5178 ov->has_decoder = 1;
5180 ov->norm = VIDEO_MODE_AUTO;
5181 ov->stop_during_set = 0; /* Decoder guarantees stable image */
5183 /* Decoder doesn't change these values, so we use these instead of
5184 * acutally reading the registers (which doesn't work) */
5185 ov->brightness = 0x80 << 8;
5186 ov->contrast = 0x40 << 9;
5187 ov->colour = 0x40 << 9;
5190 PDEBUG(4, "Writing SAA7111A registers");
5191 if (write_regvals(ov, aRegvalsNormSAA7111A))
5194 /* Detect version of decoder. This must be done after writing the
5195 * initial regs or the decoder will lock up. */
5196 rc = i2c_r(ov, 0x00);
5199 err("Error detecting sensor version");
5202 info("Sensor is an SAA7111A (version 0x%x)", rc);
5203 ov->sensor = SEN_SAA7111A;
5206 // FIXME: Fix this for OV518(+)
5207 /* Latch to negative edge of clock. Otherwise, we get incorrect
5208 * colors and jitter in the digital signal. */
5209 if (ov->bclass == BCL_OV511)
5210 reg_w(ov, 0x11, 0x00);
5212 warn("SAA7111A not yet supported with OV518/OV518+");
5217 /* This initializes the OV511/OV511+ and the sensor */
5219 ov511_configure(struct usb_ov511 *ov)
5221 static struct ov511_regvals aRegvalsInit511[] = {
5222 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5223 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5224 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5225 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5226 { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
5227 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5228 { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
5229 { OV511_DONE_BUS, 0x0, 0x00},
5232 static struct ov511_regvals aRegvalsNorm511[] = {
5233 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
5234 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5235 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5236 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5237 { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
5238 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5239 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5240 { OV511_DONE_BUS, 0x0, 0x00 },
5243 static struct ov511_regvals aRegvalsNorm511Plus[] = {
5244 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
5245 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5246 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5247 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5248 { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
5249 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5250 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5251 { OV511_DONE_BUS, 0x0, 0x00 },
5256 ov->customid = reg_r(ov, R511_SYS_CUST_ID);
5257 if (ov->customid < 0) {
5258 err("Unable to read camera bridge registers");
5262 PDEBUG (1, "CustomID = %d", ov->customid);
5263 ov->desc = symbolic(camlist, ov->customid);
5264 info("model: %s", ov->desc);
5266 if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
5267 err("Camera type (%d) not recognized", ov->customid);
5268 err("Please notify " EMAIL " of the name,");
5269 err("manufacturer, model, and this number of your camera.");
5270 err("Also include the output of the detection process.");
5273 if (ov->customid == 70) /* USB Life TV (PAL/SECAM) */
5276 if (write_regvals(ov, aRegvalsInit511))
5279 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5280 ov51x_led_control(ov, 0);
5282 /* The OV511+ has undocumented bits in the flow control register.
5283 * Setting it to 0xff fixes the corruption with moving objects. */
5284 if (ov->bridge == BRG_OV511) {
5285 if (write_regvals(ov, aRegvalsNorm511))
5287 } else if (ov->bridge == BRG_OV511PLUS) {
5288 if (write_regvals(ov, aRegvalsNorm511Plus))
5291 err("Invalid bridge");
5294 if (ov511_init_compression(ov))
5297 ov->packet_numbering = 1;
5298 ov511_set_packet_size(ov, 0);
5300 ov->snap_enabled = snapshot;
5303 PDEBUG(3, "Testing for 0V7xx0");
5304 ov->primary_i2c_slave = OV7xx0_SID;
5305 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5308 if (i2c_w(ov, 0x12, 0x80) < 0) {
5310 PDEBUG(3, "Testing for 0V6xx0");
5311 ov->primary_i2c_slave = OV6xx0_SID;
5312 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5315 if (i2c_w(ov, 0x12, 0x80) < 0) {
5317 PDEBUG(3, "Testing for 0V8xx0");
5318 ov->primary_i2c_slave = OV8xx0_SID;
5319 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5322 if (i2c_w(ov, 0x12, 0x80) < 0) {
5323 /* Test for SAA7111A */
5324 PDEBUG(3, "Testing for SAA7111A");
5325 ov->primary_i2c_slave = SAA7111A_SID;
5326 if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
5329 if (i2c_w(ov, 0x0d, 0x00) < 0) {
5330 /* Test for KS0127 */
5331 PDEBUG(3, "Testing for KS0127");
5332 ov->primary_i2c_slave = KS0127_SID;
5333 if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
5336 if (i2c_w(ov, 0x10, 0x00) < 0) {
5337 err("Can't determine sensor slave IDs");
5340 if (ks0127_configure(ov) < 0) {
5341 err("Failed to configure KS0127");
5346 if (saa7111a_configure(ov) < 0) {
5347 err("Failed to configure SAA7111A");
5352 err("Detected unsupported OV8xx0 sensor");
5356 if (ov6xx0_configure(ov) < 0) {
5357 err("Failed to configure OV6xx0");
5362 if (ov7xx0_configure(ov) < 0) {
5363 err("Failed to configure OV7xx0");
5371 err("OV511 Config failed");
5376 /* This initializes the OV518/OV518+ and the sensor */
5378 ov518_configure(struct usb_ov511 *ov)
5380 /* For 518 and 518+ */
5381 static struct ov511_regvals aRegvalsInit518[] = {
5382 { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
5383 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5384 { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
5385 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5386 { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
5387 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5388 { OV511_REG_BUS, 0x46, 0x00 },
5389 { OV511_REG_BUS, 0x5d, 0x03 },
5390 { OV511_DONE_BUS, 0x0, 0x00},
5393 static struct ov511_regvals aRegvalsNorm518[] = {
5394 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5395 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5396 { OV511_REG_BUS, 0x31, 0x0f },
5397 { OV511_REG_BUS, 0x5d, 0x03 },
5398 { OV511_REG_BUS, 0x24, 0x9f },
5399 { OV511_REG_BUS, 0x25, 0x90 },
5400 { OV511_REG_BUS, 0x20, 0x00 },
5401 { OV511_REG_BUS, 0x51, 0x04 },
5402 { OV511_REG_BUS, 0x71, 0x19 },
5403 { OV511_DONE_BUS, 0x0, 0x00 },
5406 static struct ov511_regvals aRegvalsNorm518Plus[] = {
5407 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5408 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5409 { OV511_REG_BUS, 0x31, 0x0f },
5410 { OV511_REG_BUS, 0x5d, 0x03 },
5411 { OV511_REG_BUS, 0x24, 0x9f },
5412 { OV511_REG_BUS, 0x25, 0x90 },
5413 { OV511_REG_BUS, 0x20, 0x60 },
5414 { OV511_REG_BUS, 0x51, 0x02 },
5415 { OV511_REG_BUS, 0x71, 0x19 },
5416 { OV511_REG_BUS, 0x40, 0xff },
5417 { OV511_REG_BUS, 0x41, 0x42 },
5418 { OV511_REG_BUS, 0x46, 0x00 },
5419 { OV511_REG_BUS, 0x33, 0x04 },
5420 { OV511_REG_BUS, 0x21, 0x19 },
5421 { OV511_REG_BUS, 0x3f, 0x10 },
5422 { OV511_DONE_BUS, 0x0, 0x00 },
5427 /* First 5 bits of custom ID reg are a revision ID on OV518 */
5428 info("Device revision %d", 0x1F & reg_r(ov, R511_SYS_CUST_ID));
5430 /* Give it the default description */
5431 ov->desc = symbolic(camlist, 0);
5433 if (write_regvals(ov, aRegvalsInit518))
5436 /* Set LED GPIO pin to output mode */
5437 if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
5440 /* LED is off by default with OV518; have to explicitly turn it on */
5441 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5442 ov51x_led_control(ov, 0);
5444 ov51x_led_control(ov, 1);
5446 /* Don't require compression if dumppix is enabled; otherwise it's
5447 * required. OV518 has no uncompressed mode, to save RAM. */
5448 if (!dumppix && !ov->compress) {
5450 warn("Compression required with OV518...enabling");
5453 if (ov->bridge == BRG_OV518) {
5454 if (write_regvals(ov, aRegvalsNorm518))
5456 } else if (ov->bridge == BRG_OV518PLUS) {
5457 if (write_regvals(ov, aRegvalsNorm518Plus))
5460 err("Invalid bridge");
5463 if (reg_w(ov, 0x2f, 0x80) < 0)
5466 if (ov518_init_compression(ov))
5469 if (ov->bridge == BRG_OV518)
5471 struct usb_interface *ifp;
5472 struct usb_host_interface *alt;
5475 ifp = usb_ifnum_to_if(ov->dev, 0);
5477 alt = usb_altnum_to_altsetting(ifp, 7);
5479 mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
5482 /* Some OV518s have packet numbering by default, some don't */
5484 ov->packet_numbering = 1;
5486 ov->packet_numbering = 0;
5488 /* OV518+ has packet numbering turned on by default */
5489 ov->packet_numbering = 1;
5492 ov518_set_packet_size(ov, 0);
5494 ov->snap_enabled = snapshot;
5497 ov->primary_i2c_slave = OV7xx0_SID;
5498 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5501 /* The OV518 must be more aggressive about sensor detection since
5502 * I2C write will never fail if the sensor is not present. We have
5503 * to try to initialize the sensor to detect its presence */
5505 if (init_ov_sensor(ov) < 0) {
5507 ov->primary_i2c_slave = OV6xx0_SID;
5508 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5511 if (init_ov_sensor(ov) < 0) {
5513 ov->primary_i2c_slave = OV8xx0_SID;
5514 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5517 if (init_ov_sensor(ov) < 0) {
5518 err("Can't determine sensor slave IDs");
5521 err("Detected unsupported OV8xx0 sensor");
5525 if (ov6xx0_configure(ov) < 0) {
5526 err("Failed to configure OV6xx0");
5531 if (ov7xx0_configure(ov) < 0) {
5532 err("Failed to configure OV7xx0");
5538 ov->maxheight = 288;
5540 // The OV518 cannot go as low as the sensor can
5542 ov->minheight = 120;
5547 err("OV518 Config failed");
5552 /****************************************************************************
5554 ***************************************************************************/
5556 static inline struct usb_ov511 *cd_to_ov(struct device *cd)
5558 struct video_device *vdev = to_video_device(cd);
5559 return video_get_drvdata(vdev);
5562 static ssize_t show_custom_id(struct device *cd,
5563 struct device_attribute *attr, char *buf)
5565 struct usb_ov511 *ov = cd_to_ov(cd);
5566 return sprintf(buf, "%d\n", ov->customid);
5568 static DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
5570 static ssize_t show_model(struct device *cd,
5571 struct device_attribute *attr, char *buf)
5573 struct usb_ov511 *ov = cd_to_ov(cd);
5574 return sprintf(buf, "%s\n", ov->desc);
5576 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
5578 static ssize_t show_bridge(struct device *cd,
5579 struct device_attribute *attr, char *buf)
5581 struct usb_ov511 *ov = cd_to_ov(cd);
5582 return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
5584 static DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
5586 static ssize_t show_sensor(struct device *cd,
5587 struct device_attribute *attr, char *buf)
5589 struct usb_ov511 *ov = cd_to_ov(cd);
5590 return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
5592 static DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
5594 static ssize_t show_brightness(struct device *cd,
5595 struct device_attribute *attr, char *buf)
5597 struct usb_ov511 *ov = cd_to_ov(cd);
5602 sensor_get_brightness(ov, &x);
5603 return sprintf(buf, "%d\n", x >> 8);
5605 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
5607 static ssize_t show_saturation(struct device *cd,
5608 struct device_attribute *attr, char *buf)
5610 struct usb_ov511 *ov = cd_to_ov(cd);
5615 sensor_get_saturation(ov, &x);
5616 return sprintf(buf, "%d\n", x >> 8);
5618 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
5620 static ssize_t show_contrast(struct device *cd,
5621 struct device_attribute *attr, char *buf)
5623 struct usb_ov511 *ov = cd_to_ov(cd);
5628 sensor_get_contrast(ov, &x);
5629 return sprintf(buf, "%d\n", x >> 8);
5631 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
5633 static ssize_t show_hue(struct device *cd,
5634 struct device_attribute *attr, char *buf)
5636 struct usb_ov511 *ov = cd_to_ov(cd);
5641 sensor_get_hue(ov, &x);
5642 return sprintf(buf, "%d\n", x >> 8);
5644 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5646 static ssize_t show_exposure(struct device *cd,
5647 struct device_attribute *attr, char *buf)
5649 struct usb_ov511 *ov = cd_to_ov(cd);
5650 unsigned char exp = 0;
5654 sensor_get_exposure(ov, &exp);
5655 return sprintf(buf, "%d\n", exp >> 8);
5657 static DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
5659 static int ov_create_sysfs(struct video_device *vdev)
5663 rc = video_device_create_file(vdev, &dev_attr_custom_id);
5665 rc = video_device_create_file(vdev, &dev_attr_model);
5666 if (rc) goto err_id;
5667 rc = video_device_create_file(vdev, &dev_attr_bridge);
5668 if (rc) goto err_model;
5669 rc = video_device_create_file(vdev, &dev_attr_sensor);
5670 if (rc) goto err_bridge;
5671 rc = video_device_create_file(vdev, &dev_attr_brightness);
5672 if (rc) goto err_sensor;
5673 rc = video_device_create_file(vdev, &dev_attr_saturation);
5674 if (rc) goto err_bright;
5675 rc = video_device_create_file(vdev, &dev_attr_contrast);
5676 if (rc) goto err_sat;
5677 rc = video_device_create_file(vdev, &dev_attr_hue);
5678 if (rc) goto err_contrast;
5679 rc = video_device_create_file(vdev, &dev_attr_exposure);
5680 if (rc) goto err_hue;
5685 video_device_remove_file(vdev, &dev_attr_hue);
5687 video_device_remove_file(vdev, &dev_attr_contrast);
5689 video_device_remove_file(vdev, &dev_attr_saturation);
5691 video_device_remove_file(vdev, &dev_attr_brightness);
5693 video_device_remove_file(vdev, &dev_attr_sensor);
5695 video_device_remove_file(vdev, &dev_attr_bridge);
5697 video_device_remove_file(vdev, &dev_attr_model);
5699 video_device_remove_file(vdev, &dev_attr_custom_id);
5704 /****************************************************************************
5706 ***************************************************************************/
5709 ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5711 struct usb_device *dev = interface_to_usbdev(intf);
5712 struct usb_interface_descriptor *idesc;
5713 struct usb_ov511 *ov;
5716 PDEBUG(1, "probing for device...");
5718 /* We don't handle multi-config cameras */
5719 if (dev->descriptor.bNumConfigurations != 1)
5722 idesc = &intf->cur_altsetting->desc;
5724 if (idesc->bInterfaceClass != 0xFF)
5726 if (idesc->bInterfaceSubClass != 0x00)
5729 if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5730 err("couldn't kmalloc ov struct");
5735 ov->iface = idesc->bInterfaceNumber;
5736 ov->led_policy = led;
5737 ov->compress = compress;
5738 ov->lightfreq = lightfreq;
5739 ov->num_inputs = 1; /* Video decoder init functs. change this */
5740 ov->stop_during_set = !fastset;
5741 ov->backlight = backlight;
5742 ov->mirror = mirror;
5743 ov->auto_brt = autobright;
5744 ov->auto_gain = autogain;
5745 ov->auto_exp = autoexp;
5747 switch (le16_to_cpu(dev->descriptor.idProduct)) {
5749 ov->bridge = BRG_OV511;
5750 ov->bclass = BCL_OV511;
5752 case PROD_OV511PLUS:
5753 ov->bridge = BRG_OV511PLUS;
5754 ov->bclass = BCL_OV511;
5757 ov->bridge = BRG_OV518;
5758 ov->bclass = BCL_OV518;
5760 case PROD_OV518PLUS:
5761 ov->bridge = BRG_OV518PLUS;
5762 ov->bclass = BCL_OV518;
5765 if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
5767 ov->bridge = BRG_OV511PLUS;
5768 ov->bclass = BCL_OV511;
5771 err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
5775 info("USB %s video device found", symbolic(brglist, ov->bridge));
5777 init_waitqueue_head(&ov->wq);
5779 mutex_init(&ov->lock); /* to 1 == available */
5780 mutex_init(&ov->buf_lock);
5781 mutex_init(&ov->i2c_lock);
5782 mutex_init(&ov->cbuf_lock);
5784 ov->buf_state = BUF_NOT_ALLOCATED;
5786 if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
5787 err("usb_make_path error");
5791 /* Allocate control transfer buffer. */
5792 /* Must be kmalloc()'ed, for DMA compatibility */
5793 ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
5797 if (ov->bclass == BCL_OV518) {
5798 if (ov518_configure(ov) < 0)
5801 if (ov511_configure(ov) < 0)
5805 for (i = 0; i < OV511_NUMFRAMES; i++) {
5806 ov->frame[i].framenum = i;
5807 init_waitqueue_head(&ov->frame[i].wq);
5810 for (i = 0; i < OV511_NUMSBUF; i++) {
5811 ov->sbuf[i].ov = ov;
5812 spin_lock_init(&ov->sbuf[i].lock);
5816 /* Unnecessary? (This is done on open(). Need to make sure variables
5817 * are properly initialized without this before removing it, though). */
5818 if (ov51x_set_default_params(ov) < 0)
5823 if (ov->bclass == BCL_OV511)
5824 ov511_dump_regs(ov);
5826 ov518_dump_regs(ov);
5830 ov->vdev = video_device_alloc();
5834 memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
5835 ov->vdev->dev = &dev->dev;
5836 video_set_drvdata(ov->vdev, ov);
5838 for (i = 0; i < OV511_MAX_UNIT_VIDEO; i++) {
5839 /* Minor 0 cannot be specified; assume user wants autodetect */
5840 if (unit_video[i] == 0)
5843 if (video_register_device(ov->vdev, VFL_TYPE_GRABBER,
5844 unit_video[i]) >= 0) {
5849 /* Use the next available one */
5850 if ((ov->vdev->minor == -1) &&
5851 video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1) < 0) {
5852 err("video_register_device failed");
5856 info("Device at %s registered to minor %d", ov->usb_path,
5859 usb_set_intfdata(intf, ov);
5860 if (ov_create_sysfs(ov->vdev)) {
5861 err("ov_create_sysfs failed");
5869 if (-1 == ov->vdev->minor)
5870 video_device_release(ov->vdev);
5872 video_unregister_device(ov->vdev);
5877 mutex_lock(&ov->cbuf_lock);
5880 mutex_unlock(&ov->cbuf_lock);
5887 err("Camera initialization failed");
5892 ov51x_disconnect(struct usb_interface *intf)
5894 struct usb_ov511 *ov = usb_get_intfdata(intf);
5899 usb_set_intfdata (intf, NULL);
5905 video_unregister_device(ov->vdev);
5907 for (n = 0; n < OV511_NUMFRAMES; n++)
5908 ov->frame[n].grabstate = FRAME_ERROR;
5912 /* This will cause the process to request another frame */
5913 for (n = 0; n < OV511_NUMFRAMES; n++)
5914 wake_up_interruptible(&ov->frame[n].wq);
5916 wake_up_interruptible(&ov->wq);
5919 ov51x_unlink_isoc(ov);
5923 /* Free the memory */
5924 if (ov && !ov->user) {
5925 mutex_lock(&ov->cbuf_lock);
5928 mutex_unlock(&ov->cbuf_lock);
5935 PDEBUG(3, "Disconnect complete");
5938 static struct usb_driver ov511_driver = {
5940 .id_table = device_table,
5941 .probe = ov51x_probe,
5942 .disconnect = ov51x_disconnect
5945 /****************************************************************************
5949 ***************************************************************************/
5952 usb_ov511_init(void)
5956 retval = usb_register(&ov511_driver);
5960 info(DRIVER_VERSION " : " DRIVER_DESC);
5967 usb_ov511_exit(void)
5969 usb_deregister(&ov511_driver);
5970 info("driver deregistered");
5974 module_init(usb_ov511_init);
5975 module_exit(usb_ov511_exit);