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/processor.h>
46 #include <linux/device.h>
48 #if defined (__i386__)
49 #include <asm/cpufeature.h>
57 #define DRIVER_VERSION "v1.64 for Linux 2.5"
58 #define EMAIL "mark@alpha.dyndns.org"
59 #define DRIVER_AUTHOR "Mark McClelland <mark@alpha.dyndns.org> & Bret Wallach \
60 & Orion Sky Lawlor <olawlor@acm.org> & Kevin Moore & Charl P. Botha \
61 <cpbotha@ieee.org> & Claudio Matsuoka <claudio@conectiva.com>"
62 #define DRIVER_DESC "ov511 USB Camera Driver"
64 #define OV511_I2C_RETRIES 3
65 #define ENABLE_Y_QUANTABLE 1
66 #define ENABLE_UV_QUANTABLE 1
68 #define OV511_MAX_UNIT_VIDEO 16
70 /* Pixel count * bytes per YUV420 pixel (1.5) */
71 #define MAX_FRAME_SIZE(w, h) ((w) * (h) * 3 / 2)
73 #define MAX_DATA_SIZE(w, h) (MAX_FRAME_SIZE(w, h) + sizeof(struct timeval))
75 /* Max size * bytes per YUV420 pixel (1.5) + one extra isoc frame for safety */
76 #define MAX_RAW_DATA_SIZE(w, h) ((w) * (h) * 3 / 2 + 1024)
78 #define FATAL_ERROR(rc) ((rc) < 0 && (rc) != -EPERM)
80 /**********************************************************************
82 * (See ov511.txt for detailed descriptions of these)
83 **********************************************************************/
85 /* These variables (and all static globals) default to zero */
86 static int autobright = 1;
87 static int autogain = 1;
88 static int autoexp = 1;
96 static int dump_bridge;
97 static int dump_sensor;
99 static int phy = 0x1f;
100 static int phuv = 0x05;
101 static int pvy = 0x06;
102 static int pvuv = 0x06;
103 static int qhy = 0x14;
104 static int qhuv = 0x03;
105 static int qvy = 0x04;
106 static int qvuv = 0x04;
107 static int lightfreq;
108 static int bandingfilter;
109 static int clockdiv = -1;
110 static int packetsize = -1;
111 static int framedrop = -1;
113 static int force_palette;
114 static int backlight;
115 static int unit_video[OV511_MAX_UNIT_VIDEO];
116 static int remove_zeros;
118 static int ov518_color;
120 module_param(autobright, int, 0);
121 MODULE_PARM_DESC(autobright, "Sensor automatically changes brightness");
122 module_param(autogain, int, 0);
123 MODULE_PARM_DESC(autogain, "Sensor automatically changes gain");
124 module_param(autoexp, int, 0);
125 MODULE_PARM_DESC(autoexp, "Sensor automatically changes exposure");
126 module_param(debug, int, 0);
127 MODULE_PARM_DESC(debug,
128 "Debug level: 0=none, 1=inits, 2=warning, 3=config, 4=functions, 5=max");
129 module_param(snapshot, int, 0);
130 MODULE_PARM_DESC(snapshot, "Enable snapshot mode");
131 module_param(cams, int, 0);
132 MODULE_PARM_DESC(cams, "Number of simultaneous cameras");
133 module_param(compress, int, 0);
134 MODULE_PARM_DESC(compress, "Turn on compression");
135 module_param(testpat, int, 0);
136 MODULE_PARM_DESC(testpat,
137 "Replace image with vertical bar testpattern (only partially working)");
138 module_param(dumppix, int, 0);
139 MODULE_PARM_DESC(dumppix, "Dump raw pixel data");
140 module_param(led, int, 0);
141 MODULE_PARM_DESC(led,
142 "LED policy (OV511+ or later). 0=off, 1=on (default), 2=auto (on when open)");
143 module_param(dump_bridge, int, 0);
144 MODULE_PARM_DESC(dump_bridge, "Dump the bridge registers");
145 module_param(dump_sensor, int, 0);
146 MODULE_PARM_DESC(dump_sensor, "Dump the sensor registers");
147 module_param(printph, int, 0);
148 MODULE_PARM_DESC(printph, "Print frame start/end headers");
149 module_param(phy, int, 0);
150 MODULE_PARM_DESC(phy, "Prediction range (horiz. Y)");
151 module_param(phuv, int, 0);
152 MODULE_PARM_DESC(phuv, "Prediction range (horiz. UV)");
153 module_param(pvy, int, 0);
154 MODULE_PARM_DESC(pvy, "Prediction range (vert. Y)");
155 module_param(pvuv, int, 0);
156 MODULE_PARM_DESC(pvuv, "Prediction range (vert. UV)");
157 module_param(qhy, int, 0);
158 MODULE_PARM_DESC(qhy, "Quantization threshold (horiz. Y)");
159 module_param(qhuv, int, 0);
160 MODULE_PARM_DESC(qhuv, "Quantization threshold (horiz. UV)");
161 module_param(qvy, int, 0);
162 MODULE_PARM_DESC(qvy, "Quantization threshold (vert. Y)");
163 module_param(qvuv, int, 0);
164 MODULE_PARM_DESC(qvuv, "Quantization threshold (vert. UV)");
165 module_param(lightfreq, int, 0);
166 MODULE_PARM_DESC(lightfreq,
167 "Light frequency. Set to 50 or 60 Hz, or zero for default settings");
168 module_param(bandingfilter, int, 0);
169 MODULE_PARM_DESC(bandingfilter,
170 "Enable banding filter (to reduce effects of fluorescent lighting)");
171 module_param(clockdiv, int, 0);
172 MODULE_PARM_DESC(clockdiv, "Force pixel clock divisor to a specific value");
173 module_param(packetsize, int, 0);
174 MODULE_PARM_DESC(packetsize, "Force a specific isoc packet size");
175 module_param(framedrop, int, 0);
176 MODULE_PARM_DESC(framedrop, "Force a specific frame drop register setting");
177 module_param(fastset, int, 0);
178 MODULE_PARM_DESC(fastset, "Allows picture settings to take effect immediately");
179 module_param(force_palette, int, 0);
180 MODULE_PARM_DESC(force_palette, "Force the palette to a specific value");
181 module_param(backlight, int, 0);
182 MODULE_PARM_DESC(backlight, "For objects that are lit from behind");
183 static unsigned int num_uv;
184 module_param_array(unit_video, int, &num_uv, 0);
185 MODULE_PARM_DESC(unit_video,
186 "Force use of specific minor number(s). 0 is not allowed.");
187 module_param(remove_zeros, int, 0);
188 MODULE_PARM_DESC(remove_zeros,
189 "Remove zero-padding from uncompressed incoming data");
190 module_param(mirror, int, 0);
191 MODULE_PARM_DESC(mirror, "Reverse image horizontally");
192 module_param(ov518_color, int, 0);
193 MODULE_PARM_DESC(ov518_color, "Enable OV518 color (experimental)");
195 MODULE_AUTHOR(DRIVER_AUTHOR);
196 MODULE_DESCRIPTION(DRIVER_DESC);
197 MODULE_LICENSE("GPL");
199 /**********************************************************************
200 * Miscellaneous Globals
201 **********************************************************************/
203 static struct usb_driver ov511_driver;
205 /* Number of times to retry a failed I2C transaction. Increase this if you
206 * are getting "Failed to read sensor ID..." */
207 static const int i2c_detect_tries = 5;
209 static struct usb_device_id device_table [] = {
210 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511) },
211 { USB_DEVICE(VEND_OMNIVISION, PROD_OV511PLUS) },
212 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518) },
213 { USB_DEVICE(VEND_OMNIVISION, PROD_OV518PLUS) },
214 { USB_DEVICE(VEND_MATTEL, PROD_ME2CAM) },
215 { } /* Terminating entry */
218 MODULE_DEVICE_TABLE (usb, device_table);
220 static unsigned char yQuanTable511[] = OV511_YQUANTABLE;
221 static unsigned char uvQuanTable511[] = OV511_UVQUANTABLE;
222 static unsigned char yQuanTable518[] = OV518_YQUANTABLE;
223 static unsigned char uvQuanTable518[] = OV518_UVQUANTABLE;
225 /**********************************************************************
227 **********************************************************************/
229 /* Known OV511-based cameras */
230 static struct symbolic_list camlist[] = {
231 { 0, "Generic Camera (no ID)" },
232 { 1, "Mustek WCam 3X" },
233 { 3, "D-Link DSB-C300" },
234 { 4, "Generic OV511/OV7610" },
235 { 5, "Puretek PT-6007" },
236 { 6, "Lifeview USB Life TV (NTSC)" },
237 { 21, "Creative Labs WebCam 3" },
238 { 22, "Lifeview USB Life TV (PAL D/K+B/G)" },
240 { 38, "Lifeview USB Life TV (PAL)" },
241 { 41, "Samsung Anycam MPC-M10" },
242 { 43, "Mtekvision Zeca MV402" },
244 { 70, "Lifeview USB Life TV (PAL/SECAM)" },
245 { 100, "Lifeview RoboCam" },
246 { 102, "AverMedia InterCam Elite" },
247 { 112, "MediaForte MV300" }, /* or OV7110 evaluation kit */
248 { 134, "Ezonics EZCam II" },
249 { 192, "Webeye 2000B" },
250 { 253, "Alpha Vision Tech. AlphaCam SE" },
254 /* Video4Linux1 Palettes */
255 static struct symbolic_list v4l1_plist[] = {
256 { VIDEO_PALETTE_GREY, "GREY" },
257 { VIDEO_PALETTE_HI240, "HI240" },
258 { VIDEO_PALETTE_RGB565, "RGB565" },
259 { VIDEO_PALETTE_RGB24, "RGB24" },
260 { VIDEO_PALETTE_RGB32, "RGB32" },
261 { VIDEO_PALETTE_RGB555, "RGB555" },
262 { VIDEO_PALETTE_YUV422, "YUV422" },
263 { VIDEO_PALETTE_YUYV, "YUYV" },
264 { VIDEO_PALETTE_UYVY, "UYVY" },
265 { VIDEO_PALETTE_YUV420, "YUV420" },
266 { VIDEO_PALETTE_YUV411, "YUV411" },
267 { VIDEO_PALETTE_RAW, "RAW" },
268 { VIDEO_PALETTE_YUV422P,"YUV422P" },
269 { VIDEO_PALETTE_YUV411P,"YUV411P" },
270 { VIDEO_PALETTE_YUV420P,"YUV420P" },
271 { VIDEO_PALETTE_YUV410P,"YUV410P" },
275 static struct symbolic_list brglist[] = {
276 { BRG_OV511, "OV511" },
277 { BRG_OV511PLUS, "OV511+" },
278 { BRG_OV518, "OV518" },
279 { BRG_OV518PLUS, "OV518+" },
283 static struct symbolic_list senlist[] = {
284 { SEN_OV76BE, "OV76BE" },
285 { SEN_OV7610, "OV7610" },
286 { SEN_OV7620, "OV7620" },
287 { SEN_OV7620AE, "OV7620AE" },
288 { SEN_OV6620, "OV6620" },
289 { SEN_OV6630, "OV6630" },
290 { SEN_OV6630AE, "OV6630AE" },
291 { SEN_OV6630AF, "OV6630AF" },
292 { SEN_OV8600, "OV8600" },
293 { SEN_KS0127, "KS0127" },
294 { SEN_KS0127B, "KS0127B" },
295 { SEN_SAA7111A, "SAA7111A" },
299 /* URB error codes: */
300 static struct symbolic_list urb_errlist[] = {
301 { -ENOSR, "Buffer error (overrun)" },
302 { -EPIPE, "Stalled (device not responding)" },
303 { -EOVERFLOW, "Babble (device sends too much data)" },
304 { -EPROTO, "Bit-stuff error (bad cable?)" },
305 { -EILSEQ, "CRC/Timeout (bad cable?)" },
306 { -ETIME, "Device does not respond to token" },
307 { -ETIMEDOUT, "Device does not respond to command" },
311 /**********************************************************************
313 **********************************************************************/
315 rvmalloc(unsigned long size)
320 size = PAGE_ALIGN(size);
321 mem = vmalloc_32(size);
325 memset(mem, 0, size); /* Clear the ram out, no junk to the user */
326 adr = (unsigned long) mem;
328 SetPageReserved(vmalloc_to_page((void *)adr));
337 rvfree(void *mem, unsigned long size)
344 adr = (unsigned long) mem;
345 while ((long) size > 0) {
346 ClearPageReserved(vmalloc_to_page((void *)adr));
353 /**********************************************************************
357 **********************************************************************/
359 /* Write an OV51x register */
361 reg_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
365 PDEBUG(5, "0x%02X:0x%02X", reg, value);
367 mutex_lock(&ov->cbuf_lock);
369 rc = usb_control_msg(ov->dev,
370 usb_sndctrlpipe(ov->dev, 0),
371 (ov->bclass == BCL_OV518)?1:2 /* REG_IO */,
372 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
373 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
374 mutex_unlock(&ov->cbuf_lock);
377 err("reg write: error %d: %s", rc, symbolic(urb_errlist, rc));
382 /* Read from an OV51x register */
383 /* returns: negative is error, pos or zero is data */
385 reg_r(struct usb_ov511 *ov, unsigned char reg)
389 mutex_lock(&ov->cbuf_lock);
390 rc = usb_control_msg(ov->dev,
391 usb_rcvctrlpipe(ov->dev, 0),
392 (ov->bclass == BCL_OV518)?1:3 /* REG_IO */,
393 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
394 0, (__u16)reg, &ov->cbuf[0], 1, 1000);
397 err("reg read: error %d: %s", rc, symbolic(urb_errlist, rc));
400 PDEBUG(5, "0x%02X:0x%02X", reg, ov->cbuf[0]);
403 mutex_unlock(&ov->cbuf_lock);
409 * Writes bits at positions specified by mask to an OV51x reg. Bits that are in
410 * the same position as 1's in "mask" are cleared and set to "value". Bits
411 * that are in the same position as 0's in "mask" are preserved, regardless
412 * of their respective state in "value".
415 reg_w_mask(struct usb_ov511 *ov,
421 unsigned char oldval, newval;
423 ret = reg_r(ov, reg);
427 oldval = (unsigned char) ret;
428 oldval &= (~mask); /* Clear the masked bits */
429 value &= mask; /* Enforce mask on value */
430 newval = oldval | value; /* Set the desired bits */
432 return (reg_w(ov, reg, newval));
436 * Writes multiple (n) byte value to a single register. Only valid with certain
437 * registers (0x30 and 0xc4 - 0xce).
440 ov518_reg_w32(struct usb_ov511 *ov, unsigned char reg, u32 val, int n)
444 PDEBUG(5, "0x%02X:%7d, n=%d", reg, val, n);
446 mutex_lock(&ov->cbuf_lock);
448 *((__le32 *)ov->cbuf) = __cpu_to_le32(val);
450 rc = usb_control_msg(ov->dev,
451 usb_sndctrlpipe(ov->dev, 0),
453 USB_TYPE_VENDOR | USB_RECIP_DEVICE,
454 0, (__u16)reg, ov->cbuf, n, 1000);
455 mutex_unlock(&ov->cbuf_lock);
458 err("reg write multiple: error %d: %s", rc,
459 symbolic(urb_errlist, rc));
465 ov511_upload_quan_tables(struct usb_ov511 *ov)
467 unsigned char *pYTable = yQuanTable511;
468 unsigned char *pUVTable = uvQuanTable511;
469 unsigned char val0, val1;
470 int i, rc, reg = R511_COMP_LUT_BEGIN;
472 PDEBUG(4, "Uploading quantization tables");
474 for (i = 0; i < OV511_QUANTABLESIZE / 2; i++) {
475 if (ENABLE_Y_QUANTABLE) {
481 rc = reg_w(ov, reg, val0);
486 if (ENABLE_UV_QUANTABLE) {
492 rc = reg_w(ov, reg + OV511_QUANTABLESIZE/2, val0);
503 /* OV518 quantization tables are 8x4 (instead of 8x8) */
505 ov518_upload_quan_tables(struct usb_ov511 *ov)
507 unsigned char *pYTable = yQuanTable518;
508 unsigned char *pUVTable = uvQuanTable518;
509 unsigned char val0, val1;
510 int i, rc, reg = R511_COMP_LUT_BEGIN;
512 PDEBUG(4, "Uploading quantization tables");
514 for (i = 0; i < OV518_QUANTABLESIZE / 2; i++) {
515 if (ENABLE_Y_QUANTABLE) {
521 rc = reg_w(ov, reg, val0);
526 if (ENABLE_UV_QUANTABLE) {
532 rc = reg_w(ov, reg + OV518_QUANTABLESIZE/2, val0);
544 ov51x_reset(struct usb_ov511 *ov, unsigned char reset_type)
548 /* Setting bit 0 not allowed on 518/518Plus */
549 if (ov->bclass == BCL_OV518)
552 PDEBUG(4, "Reset: type=0x%02X", reset_type);
554 rc = reg_w(ov, R51x_SYS_RESET, reset_type);
555 rc = reg_w(ov, R51x_SYS_RESET, 0);
558 err("reset: command failed");
563 /**********************************************************************
565 * Low-level I2C I/O functions
567 **********************************************************************/
569 /* NOTE: Do not call this function directly!
570 * The OV518 I2C I/O procedure is different, hence, this function.
571 * This is normally only called from i2c_w(). Note that this function
572 * always succeeds regardless of whether the sensor is present and working.
575 ov518_i2c_write_internal(struct usb_ov511 *ov,
581 PDEBUG(5, "0x%02X:0x%02X", reg, value);
583 /* Select camera register */
584 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
588 /* Write "value" to I2C data port of OV511 */
589 rc = reg_w(ov, R51x_I2C_DATA, value);
593 /* Initiate 3-byte write cycle */
594 rc = reg_w(ov, R518_I2C_CTL, 0x01);
601 /* NOTE: Do not call this function directly! */
603 ov511_i2c_write_internal(struct usb_ov511 *ov,
609 PDEBUG(5, "0x%02X:0x%02X", reg, value);
611 /* Three byte write cycle */
612 for (retries = OV511_I2C_RETRIES; ; ) {
613 /* Select camera register */
614 rc = reg_w(ov, R51x_I2C_SADDR_3, reg);
618 /* Write "value" to I2C data port of OV511 */
619 rc = reg_w(ov, R51x_I2C_DATA, value);
623 /* Initiate 3-byte write cycle */
624 rc = reg_w(ov, R511_I2C_CTL, 0x01);
628 /* Retry until idle */
630 rc = reg_r(ov, R511_I2C_CTL);
631 while (rc > 0 && ((rc&1) == 0));
642 reg_w(ov, R511_I2C_CTL, 0x10);
645 err("i2c write retries exhausted");
654 /* NOTE: Do not call this function directly!
655 * The OV518 I2C I/O procedure is different, hence, this function.
656 * This is normally only called from i2c_r(). Note that this function
657 * always succeeds regardless of whether the sensor is present and working.
660 ov518_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
664 /* Select camera register */
665 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
669 /* Initiate 2-byte write cycle */
670 rc = reg_w(ov, R518_I2C_CTL, 0x03);
674 /* Initiate 2-byte read cycle */
675 rc = reg_w(ov, R518_I2C_CTL, 0x05);
679 value = reg_r(ov, R51x_I2C_DATA);
681 PDEBUG(5, "0x%02X:0x%02X", reg, value);
686 /* NOTE: Do not call this function directly!
687 * returns: negative is error, pos or zero is data */
689 ov511_i2c_read_internal(struct usb_ov511 *ov, unsigned char reg)
691 int rc, value, retries;
693 /* Two byte write cycle */
694 for (retries = OV511_I2C_RETRIES; ; ) {
695 /* Select camera register */
696 rc = reg_w(ov, R51x_I2C_SADDR_2, reg);
700 /* Initiate 2-byte write cycle */
701 rc = reg_w(ov, R511_I2C_CTL, 0x03);
705 /* Retry until idle */
707 rc = reg_r(ov, R511_I2C_CTL);
708 while (rc > 0 && ((rc&1) == 0));
712 if ((rc&2) == 0) /* Ack? */
716 reg_w(ov, R511_I2C_CTL, 0x10);
719 err("i2c write retries exhausted");
724 /* Two byte read cycle */
725 for (retries = OV511_I2C_RETRIES; ; ) {
726 /* Initiate 2-byte read cycle */
727 rc = reg_w(ov, R511_I2C_CTL, 0x05);
731 /* Retry until idle */
733 rc = reg_r(ov, R511_I2C_CTL);
734 while (rc > 0 && ((rc&1) == 0));
738 if ((rc&2) == 0) /* Ack? */
742 rc = reg_w(ov, R511_I2C_CTL, 0x10);
747 err("i2c read retries exhausted");
752 value = reg_r(ov, R51x_I2C_DATA);
754 PDEBUG(5, "0x%02X:0x%02X", reg, value);
756 /* This is needed to make i2c_w() work */
757 rc = reg_w(ov, R511_I2C_CTL, 0x05);
764 /* returns: negative is error, pos or zero is data */
766 i2c_r(struct usb_ov511 *ov, unsigned char reg)
770 mutex_lock(&ov->i2c_lock);
772 if (ov->bclass == BCL_OV518)
773 rc = ov518_i2c_read_internal(ov, reg);
775 rc = ov511_i2c_read_internal(ov, reg);
777 mutex_unlock(&ov->i2c_lock);
783 i2c_w(struct usb_ov511 *ov, unsigned char reg, unsigned char value)
787 mutex_lock(&ov->i2c_lock);
789 if (ov->bclass == BCL_OV518)
790 rc = ov518_i2c_write_internal(ov, reg, value);
792 rc = ov511_i2c_write_internal(ov, reg, value);
794 mutex_unlock(&ov->i2c_lock);
799 /* Do not call this function directly! */
801 ov51x_i2c_write_mask_internal(struct usb_ov511 *ov,
807 unsigned char oldval, newval;
812 if (ov->bclass == BCL_OV518)
813 rc = ov518_i2c_read_internal(ov, reg);
815 rc = ov511_i2c_read_internal(ov, reg);
819 oldval = (unsigned char) rc;
820 oldval &= (~mask); /* Clear the masked bits */
821 value &= mask; /* Enforce mask on value */
822 newval = oldval | value; /* Set the desired bits */
825 if (ov->bclass == BCL_OV518)
826 return (ov518_i2c_write_internal(ov, reg, newval));
828 return (ov511_i2c_write_internal(ov, reg, newval));
831 /* Writes bits at positions specified by mask to an I2C reg. Bits that are in
832 * the same position as 1's in "mask" are cleared and set to "value". Bits
833 * that are in the same position as 0's in "mask" are preserved, regardless
834 * of their respective state in "value".
837 i2c_w_mask(struct usb_ov511 *ov,
844 mutex_lock(&ov->i2c_lock);
845 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
846 mutex_unlock(&ov->i2c_lock);
851 /* Set the read and write slave IDs. The "slave" argument is the write slave,
852 * and the read slave will be set to (slave + 1). ov->i2c_lock should be held
853 * when calling this. This should not be called from outside the i2c I/O
857 i2c_set_slave_internal(struct usb_ov511 *ov, unsigned char slave)
861 rc = reg_w(ov, R51x_I2C_W_SID, slave);
865 rc = reg_w(ov, R51x_I2C_R_SID, slave + 1);
872 /* Write to a specific I2C slave ID and register, using the specified mask */
874 i2c_w_slave(struct usb_ov511 *ov,
882 mutex_lock(&ov->i2c_lock);
884 /* Set new slave IDs */
885 rc = i2c_set_slave_internal(ov, slave);
889 rc = ov51x_i2c_write_mask_internal(ov, reg, value, mask);
892 /* Restore primary IDs */
893 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
894 err("Couldn't restore primary I2C slave");
896 mutex_unlock(&ov->i2c_lock);
900 /* Read from a specific I2C slave ID and register */
902 i2c_r_slave(struct usb_ov511 *ov,
908 mutex_lock(&ov->i2c_lock);
910 /* Set new slave IDs */
911 rc = i2c_set_slave_internal(ov, slave);
915 if (ov->bclass == BCL_OV518)
916 rc = ov518_i2c_read_internal(ov, reg);
918 rc = ov511_i2c_read_internal(ov, reg);
921 /* Restore primary IDs */
922 if (i2c_set_slave_internal(ov, ov->primary_i2c_slave) < 0)
923 err("Couldn't restore primary I2C slave");
925 mutex_unlock(&ov->i2c_lock);
929 /* Sets I2C read and write slave IDs. Returns <0 for error */
931 ov51x_set_slave_ids(struct usb_ov511 *ov, unsigned char sid)
935 mutex_lock(&ov->i2c_lock);
937 rc = i2c_set_slave_internal(ov, sid);
941 // FIXME: Is this actually necessary?
942 rc = ov51x_reset(ov, OV511_RESET_NOREGS);
944 mutex_unlock(&ov->i2c_lock);
949 write_regvals(struct usb_ov511 *ov, struct ov511_regvals * pRegvals)
953 while (pRegvals->bus != OV511_DONE_BUS) {
954 if (pRegvals->bus == OV511_REG_BUS) {
955 if ((rc = reg_w(ov, pRegvals->reg, pRegvals->val)) < 0)
957 } else if (pRegvals->bus == OV511_I2C_BUS) {
958 if ((rc = i2c_w(ov, pRegvals->reg, pRegvals->val)) < 0)
961 err("Bad regval array");
971 dump_i2c_range(struct usb_ov511 *ov, int reg1, int regn)
975 for (i = reg1; i <= regn; i++) {
977 info("Sensor[0x%02X] = 0x%02X", i, rc);
982 dump_i2c_regs(struct usb_ov511 *ov)
985 dump_i2c_range(ov, 0x00, 0x7C);
989 dump_reg_range(struct usb_ov511 *ov, int reg1, int regn)
993 for (i = reg1; i <= regn; i++) {
995 info("OV511[0x%02X] = 0x%02X", i, rc);
1000 ov511_dump_regs(struct usb_ov511 *ov)
1002 info("CAMERA INTERFACE REGS");
1003 dump_reg_range(ov, 0x10, 0x1f);
1004 info("DRAM INTERFACE REGS");
1005 dump_reg_range(ov, 0x20, 0x23);
1006 info("ISO FIFO REGS");
1007 dump_reg_range(ov, 0x30, 0x31);
1009 dump_reg_range(ov, 0x38, 0x39);
1010 dump_reg_range(ov, 0x3e, 0x3e);
1012 dump_reg_range(ov, 0x40, 0x49);
1013 info("SYSTEM CONTROL REGS");
1014 dump_reg_range(ov, 0x50, 0x55);
1015 dump_reg_range(ov, 0x5e, 0x5f);
1016 info("OmniCE REGS");
1017 dump_reg_range(ov, 0x70, 0x79);
1018 /* NOTE: Quantization tables are not readable. You will get the value
1019 * in reg. 0x79 for every table register */
1020 dump_reg_range(ov, 0x80, 0x9f);
1021 dump_reg_range(ov, 0xa0, 0xbf);
1026 ov518_dump_regs(struct usb_ov511 *ov)
1028 info("VIDEO MODE REGS");
1029 dump_reg_range(ov, 0x20, 0x2f);
1030 info("DATA PUMP AND SNAPSHOT REGS");
1031 dump_reg_range(ov, 0x30, 0x3f);
1033 dump_reg_range(ov, 0x40, 0x4f);
1034 info("SYSTEM CONTROL AND VENDOR REGS");
1035 dump_reg_range(ov, 0x50, 0x5f);
1037 dump_reg_range(ov, 0x60, 0x6f);
1039 dump_reg_range(ov, 0x70, 0x7f);
1040 info("Y QUANTIZATION TABLE");
1041 dump_reg_range(ov, 0x80, 0x8f);
1042 info("UV QUANTIZATION TABLE");
1043 dump_reg_range(ov, 0x90, 0x9f);
1045 dump_reg_range(ov, 0xa0, 0xbf);
1047 dump_reg_range(ov, 0xc0, 0xcf);
1051 /*****************************************************************************/
1053 /* Temporarily stops OV511 from functioning. Must do this before changing
1054 * registers while the camera is streaming */
1056 ov51x_stop(struct usb_ov511 *ov)
1058 PDEBUG(4, "stopping");
1060 if (ov->bclass == BCL_OV518)
1061 return (reg_w_mask(ov, R51x_SYS_RESET, 0x3a, 0x3a));
1063 return (reg_w(ov, R51x_SYS_RESET, 0x3d));
1066 /* Restarts OV511 after ov511_stop() is called. Has no effect if it is not
1067 * actually stopped (for performance). */
1069 ov51x_restart(struct usb_ov511 *ov)
1072 PDEBUG(4, "restarting");
1075 /* Reinitialize the stream */
1076 if (ov->bclass == BCL_OV518)
1077 reg_w(ov, 0x2f, 0x80);
1079 return (reg_w(ov, R51x_SYS_RESET, 0x00));
1085 /* Sleeps until no frames are active. Returns !0 if got signal */
1087 ov51x_wait_frames_inactive(struct usb_ov511 *ov)
1089 return wait_event_interruptible(ov->wq, ov->curframe < 0);
1092 /* Resets the hardware snapshot button */
1094 ov51x_clear_snapshot(struct usb_ov511 *ov)
1096 if (ov->bclass == BCL_OV511) {
1097 reg_w(ov, R51x_SYS_SNAP, 0x00);
1098 reg_w(ov, R51x_SYS_SNAP, 0x02);
1099 reg_w(ov, R51x_SYS_SNAP, 0x00);
1100 } else if (ov->bclass == BCL_OV518) {
1101 warn("snapshot reset not supported yet on OV518(+)");
1103 err("clear snap: invalid bridge type");
1108 /* Checks the status of the snapshot button. Returns 1 if it was pressed since
1109 * it was last cleared, and zero in all other cases (including errors) */
1111 ov51x_check_snapshot(struct usb_ov511 *ov)
1113 int ret, status = 0;
1115 if (ov->bclass == BCL_OV511) {
1116 ret = reg_r(ov, R51x_SYS_SNAP);
1118 err("Error checking snspshot status (%d)", ret);
1119 } else if (ret & 0x08) {
1122 } else if (ov->bclass == BCL_OV518) {
1123 warn("snapshot check not supported yet on OV518(+)");
1125 err("check snap: invalid bridge type");
1132 /* This does an initial reset of an OmniVision sensor and ensures that I2C
1133 * is synchronized. Returns <0 for failure.
1136 init_ov_sensor(struct usb_ov511 *ov)
1140 /* Reset the sensor */
1141 if (i2c_w(ov, 0x12, 0x80) < 0)
1144 /* Wait for it to initialize */
1147 for (i = 0, success = 0; i < i2c_detect_tries && !success; i++) {
1148 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
1149 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
1154 /* Reset the sensor */
1155 if (i2c_w(ov, 0x12, 0x80) < 0)
1157 /* Wait for it to initialize */
1159 /* Dummy read to sync I2C */
1160 if (i2c_r(ov, 0x00) < 0)
1167 PDEBUG(1, "I2C synced in %d attempt(s)", i);
1173 ov511_set_packet_size(struct usb_ov511 *ov, int size)
1177 if (ov51x_stop(ov) < 0)
1182 if (ov->bridge == BRG_OV511) {
1184 alt = OV511_ALT_SIZE_0;
1185 else if (size == 257)
1186 alt = OV511_ALT_SIZE_257;
1187 else if (size == 513)
1188 alt = OV511_ALT_SIZE_513;
1189 else if (size == 769)
1190 alt = OV511_ALT_SIZE_769;
1191 else if (size == 993)
1192 alt = OV511_ALT_SIZE_993;
1194 err("Set packet size: invalid size (%d)", size);
1197 } else if (ov->bridge == BRG_OV511PLUS) {
1199 alt = OV511PLUS_ALT_SIZE_0;
1200 else if (size == 33)
1201 alt = OV511PLUS_ALT_SIZE_33;
1202 else if (size == 129)
1203 alt = OV511PLUS_ALT_SIZE_129;
1204 else if (size == 257)
1205 alt = OV511PLUS_ALT_SIZE_257;
1206 else if (size == 385)
1207 alt = OV511PLUS_ALT_SIZE_385;
1208 else if (size == 513)
1209 alt = OV511PLUS_ALT_SIZE_513;
1210 else if (size == 769)
1211 alt = OV511PLUS_ALT_SIZE_769;
1212 else if (size == 961)
1213 alt = OV511PLUS_ALT_SIZE_961;
1215 err("Set packet size: invalid size (%d)", size);
1219 err("Set packet size: Invalid bridge type");
1223 PDEBUG(3, "%d, mult=%d, alt=%d", size, mult, alt);
1225 if (reg_w(ov, R51x_FIFO_PSIZE, mult) < 0)
1228 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1229 err("Set packet size: set interface error");
1233 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1236 ov->packet_size = size;
1238 if (ov51x_restart(ov) < 0)
1244 /* Note: Unlike the OV511/OV511+, the size argument does NOT include the
1245 * optional packet number byte. The actual size *is* stored in ov->packet_size,
1248 ov518_set_packet_size(struct usb_ov511 *ov, int size)
1252 if (ov51x_stop(ov) < 0)
1255 if (ov->bclass == BCL_OV518) {
1257 alt = OV518_ALT_SIZE_0;
1258 else if (size == 128)
1259 alt = OV518_ALT_SIZE_128;
1260 else if (size == 256)
1261 alt = OV518_ALT_SIZE_256;
1262 else if (size == 384)
1263 alt = OV518_ALT_SIZE_384;
1264 else if (size == 512)
1265 alt = OV518_ALT_SIZE_512;
1266 else if (size == 640)
1267 alt = OV518_ALT_SIZE_640;
1268 else if (size == 768)
1269 alt = OV518_ALT_SIZE_768;
1270 else if (size == 896)
1271 alt = OV518_ALT_SIZE_896;
1273 err("Set packet size: invalid size (%d)", size);
1277 err("Set packet size: Invalid bridge type");
1281 PDEBUG(3, "%d, alt=%d", size, alt);
1283 ov->packet_size = size;
1285 /* Program ISO FIFO size reg (packet number isn't included) */
1286 ov518_reg_w32(ov, 0x30, size, 2);
1288 if (ov->packet_numbering)
1292 if (usb_set_interface(ov->dev, ov->iface, alt) < 0) {
1293 err("Set packet size: set interface error");
1297 /* Initialize the stream */
1298 if (reg_w(ov, 0x2f, 0x80) < 0)
1301 if (ov51x_restart(ov) < 0)
1304 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
1310 /* Upload compression params and quantization tables. Returns 0 for success. */
1312 ov511_init_compression(struct usb_ov511 *ov)
1316 if (!ov->compress_inited) {
1317 reg_w(ov, 0x70, phy);
1318 reg_w(ov, 0x71, phuv);
1319 reg_w(ov, 0x72, pvy);
1320 reg_w(ov, 0x73, pvuv);
1321 reg_w(ov, 0x74, qhy);
1322 reg_w(ov, 0x75, qhuv);
1323 reg_w(ov, 0x76, qvy);
1324 reg_w(ov, 0x77, qvuv);
1326 if (ov511_upload_quan_tables(ov) < 0) {
1327 err("Error uploading quantization tables");
1333 ov->compress_inited = 1;
1338 /* Upload compression params and quantization tables. Returns 0 for success. */
1340 ov518_init_compression(struct usb_ov511 *ov)
1344 if (!ov->compress_inited) {
1345 if (ov518_upload_quan_tables(ov) < 0) {
1346 err("Error uploading quantization tables");
1352 ov->compress_inited = 1;
1357 /* -------------------------------------------------------------------------- */
1359 /* Sets sensor's contrast setting to "val" */
1361 sensor_set_contrast(struct usb_ov511 *ov, unsigned short val)
1365 PDEBUG(3, "%d", val);
1367 if (ov->stop_during_set)
1368 if (ov51x_stop(ov) < 0)
1371 switch (ov->sensor) {
1375 rc = i2c_w(ov, OV7610_REG_CNT, val >> 8);
1382 rc = i2c_w_mask(ov, OV7610_REG_CNT, val >> 12, 0x0f);
1389 unsigned char ctab[] = {
1390 0x01, 0x05, 0x09, 0x11, 0x15, 0x35, 0x37, 0x57,
1391 0x5b, 0xa5, 0xa7, 0xc7, 0xc9, 0xcf, 0xef, 0xff
1394 /* Use Y gamma control instead. Bit 0 enables it. */
1395 rc = i2c_w(ov, 0x64, ctab[val>>12]);
1402 rc = i2c_w(ov, 0x0b, val >> 9);
1409 PDEBUG(3, "Unsupported with this sensor");
1415 rc = 0; /* Success */
1418 if (ov51x_restart(ov) < 0)
1424 /* Gets sensor's contrast setting */
1426 sensor_get_contrast(struct usb_ov511 *ov, unsigned short *val)
1430 switch (ov->sensor) {
1433 rc = i2c_r(ov, OV7610_REG_CNT);
1440 rc = i2c_r(ov, OV7610_REG_CNT);
1447 /* Use Y gamma reg instead. Bit 0 is the enable bit. */
1448 rc = i2c_r(ov, 0x64);
1452 *val = (rc & 0xfe) << 8;
1455 *val = ov->contrast;
1458 PDEBUG(3, "Unsupported with this sensor");
1462 PDEBUG(3, "%d", *val);
1463 ov->contrast = *val;
1468 /* -------------------------------------------------------------------------- */
1470 /* Sets sensor's brightness setting to "val" */
1472 sensor_set_brightness(struct usb_ov511 *ov, unsigned short val)
1476 PDEBUG(4, "%d", val);
1478 if (ov->stop_during_set)
1479 if (ov51x_stop(ov) < 0)
1482 switch (ov->sensor) {
1487 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1492 /* 7620 doesn't like manual changes when in auto mode */
1493 if (!ov->auto_brt) {
1494 rc = i2c_w(ov, OV7610_REG_BRT, val >> 8);
1500 rc = i2c_w(ov, 0x0a, val >> 8);
1505 PDEBUG(3, "Unsupported with this sensor");
1510 rc = 0; /* Success */
1511 ov->brightness = val;
1513 if (ov51x_restart(ov) < 0)
1519 /* Gets sensor's brightness setting */
1521 sensor_get_brightness(struct usb_ov511 *ov, unsigned short *val)
1525 switch (ov->sensor) {
1531 rc = i2c_r(ov, OV7610_REG_BRT);
1538 *val = ov->brightness;
1541 PDEBUG(3, "Unsupported with this sensor");
1545 PDEBUG(3, "%d", *val);
1546 ov->brightness = *val;
1551 /* -------------------------------------------------------------------------- */
1553 /* Sets sensor's saturation (color intensity) setting to "val" */
1555 sensor_set_saturation(struct usb_ov511 *ov, unsigned short val)
1559 PDEBUG(3, "%d", val);
1561 if (ov->stop_during_set)
1562 if (ov51x_stop(ov) < 0)
1565 switch (ov->sensor) {
1570 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1575 // /* Use UV gamma control instead. Bits 0 & 7 are reserved. */
1576 // rc = ov_i2c_write(ov->dev, 0x62, (val >> 9) & 0x7e);
1579 rc = i2c_w(ov, OV7610_REG_SAT, val >> 8);
1584 rc = i2c_w(ov, 0x0c, val >> 9);
1589 PDEBUG(3, "Unsupported with this sensor");
1594 rc = 0; /* Success */
1597 if (ov51x_restart(ov) < 0)
1603 /* Gets sensor's saturation (color intensity) setting */
1605 sensor_get_saturation(struct usb_ov511 *ov, unsigned short *val)
1609 switch (ov->sensor) {
1614 rc = i2c_r(ov, OV7610_REG_SAT);
1621 // /* Use UV gamma reg instead. Bits 0 & 7 are reserved. */
1622 // rc = i2c_r(ov, 0x62);
1626 // *val = (rc & 0x7e) << 9;
1627 rc = i2c_r(ov, OV7610_REG_SAT);
1637 PDEBUG(3, "Unsupported with this sensor");
1641 PDEBUG(3, "%d", *val);
1647 /* -------------------------------------------------------------------------- */
1649 /* Sets sensor's hue (red/blue balance) setting to "val" */
1651 sensor_set_hue(struct usb_ov511 *ov, unsigned short val)
1655 PDEBUG(3, "%d", val);
1657 if (ov->stop_during_set)
1658 if (ov51x_stop(ov) < 0)
1661 switch (ov->sensor) {
1665 rc = i2c_w(ov, OV7610_REG_RED, 0xFF - (val >> 8));
1669 rc = i2c_w(ov, OV7610_REG_BLUE, val >> 8);
1674 // Hue control is causing problems. I will enable it once it's fixed.
1676 rc = i2c_w(ov, 0x7a, (unsigned char)(val >> 8) + 0xb);
1680 rc = i2c_w(ov, 0x79, (unsigned char)(val >> 8) + 0xb);
1686 rc = i2c_w(ov, 0x0d, (val + 32768) >> 8);
1691 PDEBUG(3, "Unsupported with this sensor");
1696 rc = 0; /* Success */
1699 if (ov51x_restart(ov) < 0)
1705 /* Gets sensor's hue (red/blue balance) setting */
1707 sensor_get_hue(struct usb_ov511 *ov, unsigned short *val)
1711 switch (ov->sensor) {
1715 rc = i2c_r(ov, OV7610_REG_BLUE);
1722 rc = i2c_r(ov, 0x7a);
1732 PDEBUG(3, "Unsupported with this sensor");
1736 PDEBUG(3, "%d", *val);
1742 /* -------------------------------------------------------------------------- */
1745 sensor_set_picture(struct usb_ov511 *ov, struct video_picture *p)
1749 PDEBUG(4, "sensor_set_picture");
1751 ov->whiteness = p->whiteness;
1753 /* Don't return error if a setting is unsupported, or rest of settings
1754 * will not be performed */
1756 rc = sensor_set_contrast(ov, p->contrast);
1757 if (FATAL_ERROR(rc))
1760 rc = sensor_set_brightness(ov, p->brightness);
1761 if (FATAL_ERROR(rc))
1764 rc = sensor_set_saturation(ov, p->colour);
1765 if (FATAL_ERROR(rc))
1768 rc = sensor_set_hue(ov, p->hue);
1769 if (FATAL_ERROR(rc))
1776 sensor_get_picture(struct usb_ov511 *ov, struct video_picture *p)
1780 PDEBUG(4, "sensor_get_picture");
1782 /* Don't return error if a setting is unsupported, or rest of settings
1783 * will not be performed */
1785 rc = sensor_get_contrast(ov, &(p->contrast));
1786 if (FATAL_ERROR(rc))
1789 rc = sensor_get_brightness(ov, &(p->brightness));
1790 if (FATAL_ERROR(rc))
1793 rc = sensor_get_saturation(ov, &(p->colour));
1794 if (FATAL_ERROR(rc))
1797 rc = sensor_get_hue(ov, &(p->hue));
1798 if (FATAL_ERROR(rc))
1801 p->whiteness = 105 << 8;
1807 // FIXME: Exposure range is only 0x00-0x7f in interlace mode
1808 /* Sets current exposure for sensor. This only has an effect if auto-exposure
1811 sensor_set_exposure(struct usb_ov511 *ov, unsigned char val)
1815 PDEBUG(3, "%d", val);
1817 if (ov->stop_during_set)
1818 if (ov51x_stop(ov) < 0)
1821 switch (ov->sensor) {
1828 rc = i2c_w(ov, 0x10, val);
1836 PDEBUG(3, "Unsupported with this sensor");
1839 err("Sensor not supported for set_exposure");
1843 rc = 0; /* Success */
1846 if (ov51x_restart(ov) < 0)
1853 /* Gets current exposure level from sensor, regardless of whether it is under
1854 * manual control. */
1856 sensor_get_exposure(struct usb_ov511 *ov, unsigned char *val)
1860 switch (ov->sensor) {
1867 rc = i2c_r(ov, 0x10);
1877 PDEBUG(3, "Unsupported with this sensor");
1880 err("Sensor not supported for get_exposure");
1884 PDEBUG(3, "%d", *val);
1885 ov->exposure = *val;
1890 /* Turns on or off the LED. Only has an effect with OV511+/OV518(+) */
1892 ov51x_led_control(struct usb_ov511 *ov, int enable)
1894 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1896 if (ov->bridge == BRG_OV511PLUS)
1897 reg_w(ov, R511_SYS_LED_CTL, enable ? 1 : 0);
1898 else if (ov->bclass == BCL_OV518)
1899 reg_w_mask(ov, R518_GPIO_OUT, enable ? 0x02 : 0x00, 0x02);
1904 /* Matches the sensor's internal frame rate to the lighting frequency.
1905 * Valid frequencies are:
1906 * 50 - 50Hz, for European and Asian lighting
1907 * 60 - 60Hz, for American lighting
1909 * Tested with: OV7610, OV7620, OV76BE, OV6620
1910 * Unsupported: KS0127, KS0127B, SAA7111A
1911 * Returns: 0 for success
1914 sensor_set_light_freq(struct usb_ov511 *ov, int freq)
1918 PDEBUG(4, "%d Hz", freq);
1922 else if (freq == 50)
1925 err("Invalid light freq (%d Hz)", freq);
1929 switch (ov->sensor) {
1931 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1932 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1933 i2c_w_mask(ov, 0x13, 0x10, 0x10);
1934 i2c_w_mask(ov, 0x13, 0x00, 0x10);
1939 i2c_w_mask(ov, 0x2a, sixty?0x00:0x80, 0x80);
1940 i2c_w(ov, 0x2b, sixty?0x00:0xac);
1941 i2c_w_mask(ov, 0x76, 0x01, 0x01);
1945 i2c_w(ov, 0x2b, sixty?0xa8:0x28);
1946 i2c_w(ov, 0x2a, sixty?0x84:0xa4);
1951 PDEBUG(5, "Unsupported with this sensor");
1954 err("Sensor not supported for set_light_freq");
1958 ov->lightfreq = freq;
1963 /* If enable is true, turn on the sensor's banding filter, otherwise turn it
1964 * off. This filter tries to reduce the pattern of horizontal light/dark bands
1965 * caused by some (usually fluorescent) lighting. The light frequency must be
1966 * set either before or after enabling it with ov51x_set_light_freq().
1968 * Tested with: OV7610, OV7620, OV76BE, OV6620.
1969 * Unsupported: KS0127, KS0127B, SAA7111A
1970 * Returns: 0 for success
1973 sensor_set_banding_filter(struct usb_ov511 *ov, int enable)
1977 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
1979 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
1980 || ov->sensor == SEN_SAA7111A) {
1981 PDEBUG(5, "Unsupported with this sensor");
1985 rc = i2c_w_mask(ov, 0x2d, enable?0x04:0x00, 0x04);
1989 ov->bandfilt = enable;
1994 /* If enable is true, turn on the sensor's auto brightness control, otherwise
1997 * Unsupported: KS0127, KS0127B, SAA7111A
1998 * Returns: 0 for success
2001 sensor_set_auto_brightness(struct usb_ov511 *ov, int enable)
2005 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2007 if (ov->sensor == SEN_KS0127 || ov->sensor == SEN_KS0127B
2008 || ov->sensor == SEN_SAA7111A) {
2009 PDEBUG(5, "Unsupported with this sensor");
2013 rc = i2c_w_mask(ov, 0x2d, enable?0x10:0x00, 0x10);
2017 ov->auto_brt = enable;
2022 /* If enable is true, turn on the sensor's auto exposure control, otherwise
2025 * Unsupported: KS0127, KS0127B, SAA7111A
2026 * Returns: 0 for success
2029 sensor_set_auto_exposure(struct usb_ov511 *ov, int enable)
2031 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2033 switch (ov->sensor) {
2035 i2c_w_mask(ov, 0x29, enable?0x00:0x80, 0x80);
2041 i2c_w_mask(ov, 0x13, enable?0x01:0x00, 0x01);
2044 i2c_w_mask(ov, 0x28, enable?0x00:0x10, 0x10);
2049 PDEBUG(5, "Unsupported with this sensor");
2052 err("Sensor not supported for set_auto_exposure");
2056 ov->auto_exp = enable;
2061 /* Modifies the sensor's exposure algorithm to allow proper exposure of objects
2062 * that are illuminated from behind.
2064 * Tested with: OV6620, OV7620
2065 * Unsupported: OV7610, OV76BE, KS0127, KS0127B, SAA7111A
2066 * Returns: 0 for success
2069 sensor_set_backlight(struct usb_ov511 *ov, int enable)
2071 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2073 switch (ov->sensor) {
2076 i2c_w_mask(ov, 0x68, enable?0xe0:0xc0, 0xe0);
2077 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2078 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2081 i2c_w_mask(ov, 0x4e, enable?0xe0:0xc0, 0xe0);
2082 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2083 i2c_w_mask(ov, 0x0e, enable?0x80:0x00, 0x80);
2086 i2c_w_mask(ov, 0x4e, enable?0x80:0x60, 0xe0);
2087 i2c_w_mask(ov, 0x29, enable?0x08:0x00, 0x08);
2088 i2c_w_mask(ov, 0x28, enable?0x02:0x00, 0x02);
2095 PDEBUG(5, "Unsupported with this sensor");
2098 err("Sensor not supported for set_backlight");
2102 ov->backlight = enable;
2108 sensor_set_mirror(struct usb_ov511 *ov, int enable)
2110 PDEBUG(4, " (%s)", enable ? "turn on" : "turn off");
2112 switch (ov->sensor) {
2119 i2c_w_mask(ov, 0x12, enable?0x40:0x00, 0x40);
2124 PDEBUG(5, "Unsupported with this sensor");
2127 err("Sensor not supported for set_mirror");
2131 ov->mirror = enable;
2136 /* Returns number of bits per pixel (regardless of where they are located;
2137 * planar or not), or zero for unsupported format.
2140 get_depth(int palette)
2143 case VIDEO_PALETTE_GREY: return 8;
2144 case VIDEO_PALETTE_YUV420: return 12;
2145 case VIDEO_PALETTE_YUV420P: return 12; /* Planar */
2146 default: return 0; /* Invalid format */
2150 /* Bytes per frame. Used by read(). Return of 0 indicates error */
2151 static inline long int
2152 get_frame_length(struct ov511_frame *frame)
2157 return ((frame->width * frame->height
2158 * get_depth(frame->format)) >> 3);
2162 mode_init_ov_sensor_regs(struct usb_ov511 *ov, int width, int height,
2163 int mode, int sub_flag, int qvga)
2167 /******** Mode (VGA/QVGA) and sensor specific regs ********/
2169 switch (ov->sensor) {
2171 i2c_w(ov, 0x14, qvga?0x24:0x04);
2172 // FIXME: Does this improve the image quality or frame rate?
2174 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2175 i2c_w(ov, 0x24, 0x10);
2176 i2c_w(ov, 0x25, qvga?0x40:0x8a);
2177 i2c_w(ov, 0x2f, qvga?0x30:0xb0);
2178 i2c_w(ov, 0x35, qvga?0x1c:0x9c);
2182 // i2c_w(ov, 0x2b, 0x00);
2183 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2184 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2185 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2186 i2c_w(ov, 0x25, qvga?0x30:0x60);
2187 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2188 i2c_w_mask(ov, 0x67, qvga?0xf0:0x90, 0xf0);
2189 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2192 // i2c_w(ov, 0x2b, 0x00);
2193 i2c_w(ov, 0x14, qvga?0xa4:0x84);
2194 // FIXME: Enable this once 7620AE uses 7620 initial settings
2196 i2c_w_mask(ov, 0x28, qvga?0x00:0x20, 0x20);
2197 i2c_w(ov, 0x24, qvga?0x20:0x3a);
2198 i2c_w(ov, 0x25, qvga?0x30:0x60);
2199 i2c_w_mask(ov, 0x2d, qvga?0x40:0x00, 0x40);
2200 i2c_w_mask(ov, 0x67, qvga?0xb0:0x90, 0xf0);
2201 i2c_w_mask(ov, 0x74, qvga?0x20:0x00, 0x20);
2205 i2c_w(ov, 0x14, qvga?0x24:0x04);
2208 i2c_w(ov, 0x14, qvga?0xa0:0x80);
2211 err("Invalid sensor");
2215 /******** Palette-specific regs ********/
2217 if (mode == VIDEO_PALETTE_GREY) {
2218 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2219 /* these aren't valid on the OV6620/OV7620/6630? */
2220 i2c_w_mask(ov, 0x0e, 0x40, 0x40);
2223 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2225 i2c_w_mask(ov, 0x12, 0x00, 0x10);
2226 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2228 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2231 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2232 /* not valid on the OV6620/OV7620/6630? */
2233 i2c_w_mask(ov, 0x0e, 0x00, 0x40);
2236 /* The OV518 needs special treatment. Although both the OV518
2237 * and the OV6630 support a 16-bit video bus, only the 8 bit Y
2238 * bus is actually used. The UV bus is tied to ground.
2239 * Therefore, the OV6630 needs to be in 8-bit multiplexed
2242 if (ov->sensor == SEN_OV6630 && ov->bridge == BRG_OV518
2244 i2c_w_mask(ov, 0x12, 0x10, 0x10);
2245 i2c_w_mask(ov, 0x13, 0x20, 0x20);
2247 i2c_w_mask(ov, 0x13, 0x00, 0x20);
2251 /******** Clock programming ********/
2253 /* The OV6620 needs special handling. This prevents the
2254 * severe banding that normally occurs */
2255 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630)
2259 i2c_w(ov, 0x2a, 0x04);
2262 // clock = 0; /* This ensures the highest frame rate */
2264 } else if (clockdiv == -1) { /* If user didn't override it */
2265 clock = 3; /* Gives better exposure time */
2270 PDEBUG(4, "Setting clock divisor to %d", clock);
2272 i2c_w(ov, 0x11, clock);
2274 i2c_w(ov, 0x2a, 0x84);
2275 /* This next setting is critical. It seems to improve
2276 * the gain or the contrast. The "reserved" bits seem
2277 * to have some effect in this case. */
2278 i2c_w(ov, 0x2d, 0x85);
2283 clock = 1; /* This ensures the highest frame rate */
2284 } else if (clockdiv == -1) { /* If user didn't override it */
2285 /* Calculate and set the clock divisor */
2286 clock = ((sub_flag ? ov->subw * ov->subh
2288 * (mode == VIDEO_PALETTE_GREY ? 2 : 3) / 2)
2294 PDEBUG(4, "Setting clock divisor to %d", clock);
2296 i2c_w(ov, 0x11, clock);
2299 /******** Special Features ********/
2302 i2c_w(ov, 0x16, framedrop);
2305 i2c_w_mask(ov, 0x12, (testpat?0x02:0x00), 0x02);
2307 /* Enable auto white balance */
2308 i2c_w_mask(ov, 0x12, 0x04, 0x04);
2310 // This will go away as soon as ov51x_mode_init_sensor_regs()
2312 /* 7620/6620/6630? don't have register 0x35, so play it safe */
2313 if (ov->sensor == SEN_OV7610 || ov->sensor == SEN_OV76BE) {
2314 if (width == 640 && height == 480)
2315 i2c_w(ov, 0x35, 0x9e);
2317 i2c_w(ov, 0x35, 0x1e);
2324 set_ov_sensor_window(struct usb_ov511 *ov, int width, int height, int mode,
2328 int hwsbase, hwebase, vwsbase, vwebase, hwsize, vwsize;
2329 int hoffset, voffset, hwscale = 0, vwscale = 0;
2331 /* The different sensor ICs handle setting up of window differently.
2332 * IF YOU SET IT WRONG, YOU WILL GET ALL ZERO ISOC DATA FROM OV51x!!! */
2333 switch (ov->sensor) {
2338 vwsbase = vwebase = 0x05;
2348 hwsbase = 0x2f; /* From 7620.SET (spec is wrong) */
2350 vwsbase = vwebase = 0x05;
2353 err("Invalid sensor");
2357 if (ov->sensor == SEN_OV6620 || ov->sensor == SEN_OV6630) {
2358 /* Note: OV518(+) does downsample on its own) */
2359 if ((width > 176 && height > 144)
2360 || ov->bclass == BCL_OV518) { /* CIF */
2361 ret = mode_init_ov_sensor_regs(ov, width, height,
2366 vwscale = 1; /* The datasheet says 0; it's wrong */
2369 } else if (width > 176 || height > 144) {
2370 err("Illegal dimensions");
2373 ret = mode_init_ov_sensor_regs(ov, width, height,
2381 if (width > 320 && height > 240) { /* VGA */
2382 ret = mode_init_ov_sensor_regs(ov, width, height,
2390 } else if (width > 320 || height > 240) {
2391 err("Illegal dimensions");
2394 ret = mode_init_ov_sensor_regs(ov, width, height,
2404 /* Center the window */
2405 hoffset = ((hwsize - width) / 2) >> hwscale;
2406 voffset = ((vwsize - height) / 2) >> vwscale;
2408 /* FIXME! - This needs to be changed to support 160x120 and 6620!!! */
2410 i2c_w(ov, 0x17, hwsbase+(ov->subx>>hwscale));
2411 i2c_w(ov, 0x18, hwebase+((ov->subx+ov->subw)>>hwscale));
2412 i2c_w(ov, 0x19, vwsbase+(ov->suby>>vwscale));
2413 i2c_w(ov, 0x1a, vwebase+((ov->suby+ov->subh)>>vwscale));
2415 i2c_w(ov, 0x17, hwsbase + hoffset);
2416 i2c_w(ov, 0x18, hwebase + hoffset + (hwsize>>hwscale));
2417 i2c_w(ov, 0x19, vwsbase + voffset);
2418 i2c_w(ov, 0x1a, vwebase + voffset + (vwsize>>vwscale));
2429 /* Set up the OV511/OV511+ with the given image parameters.
2431 * Do not put any sensor-specific code in here (including I2C I/O functions)
2434 ov511_mode_init_regs(struct usb_ov511 *ov,
2435 int width, int height, int mode, int sub_flag)
2444 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2445 width, height, mode, sub_flag);
2447 // FIXME: This should be moved to a 7111a-specific function once
2448 // subcapture is dealt with properly
2449 if (ov->sensor == SEN_SAA7111A) {
2450 if (width == 320 && height == 240) {
2451 /* No need to do anything special */
2452 } else if (width == 640 && height == 480) {
2453 /* Set the OV511 up as 320x480, but keep the
2454 * V4L resolution as 640x480 */
2457 err("SAA7111A only allows 320x240 or 640x480");
2462 /* Make sure width and height are a multiple of 8 */
2463 if (width % 8 || height % 8) {
2464 err("Invalid size (%d, %d) (mode = %d)", width, height, mode);
2468 if (width < ov->minwidth || height < ov->minheight) {
2469 err("Requested dimensions are too small");
2473 if (ov51x_stop(ov) < 0)
2476 if (mode == VIDEO_PALETTE_GREY) {
2477 reg_w(ov, R511_CAM_UV_EN, 0x00);
2478 reg_w(ov, R511_SNAP_UV_EN, 0x00);
2479 reg_w(ov, R511_SNAP_OPTS, 0x01);
2481 reg_w(ov, R511_CAM_UV_EN, 0x01);
2482 reg_w(ov, R511_SNAP_UV_EN, 0x01);
2483 reg_w(ov, R511_SNAP_OPTS, 0x03);
2486 /* Here I'm assuming that snapshot size == image size.
2487 * I hope that's always true. --claudio
2489 hsegs = (width >> 3) - 1;
2490 vsegs = (height >> 3) - 1;
2492 reg_w(ov, R511_CAM_PXCNT, hsegs);
2493 reg_w(ov, R511_CAM_LNCNT, vsegs);
2494 reg_w(ov, R511_CAM_PXDIV, 0x00);
2495 reg_w(ov, R511_CAM_LNDIV, 0x00);
2497 /* YUV420, low pass filter on */
2498 reg_w(ov, R511_CAM_OPTS, 0x03);
2500 /* Snapshot additions */
2501 reg_w(ov, R511_SNAP_PXCNT, hsegs);
2502 reg_w(ov, R511_SNAP_LNCNT, vsegs);
2503 reg_w(ov, R511_SNAP_PXDIV, 0x00);
2504 reg_w(ov, R511_SNAP_LNDIV, 0x00);
2507 /* Enable Y and UV quantization and compression */
2508 reg_w(ov, R511_COMP_EN, 0x07);
2509 reg_w(ov, R511_COMP_LUT_EN, 0x03);
2510 ov51x_reset(ov, OV511_RESET_OMNICE);
2513 if (ov51x_restart(ov) < 0)
2519 /* Sets up the OV518/OV518+ with the given image parameters
2521 * OV518 needs a completely different approach, until we can figure out what
2522 * the individual registers do. Also, only 15 FPS is supported now.
2524 * Do not put any sensor-specific code in here (including I2C I/O functions)
2527 ov518_mode_init_regs(struct usb_ov511 *ov,
2528 int width, int height, int mode, int sub_flag)
2530 int hsegs, vsegs, hi_res;
2537 PDEBUG(3, "width:%d, height:%d, mode:%d, sub:%d",
2538 width, height, mode, sub_flag);
2540 if (width % 16 || height % 8) {
2541 err("Invalid size (%d, %d)", width, height);
2545 if (width < ov->minwidth || height < ov->minheight) {
2546 err("Requested dimensions are too small");
2550 if (width >= 320 && height >= 240) {
2552 } else if (width >= 320 || height >= 240) {
2553 err("Invalid width/height combination (%d, %d)", width, height);
2559 if (ov51x_stop(ov) < 0)
2562 /******** Set the mode ********/
2573 if (ov->bridge == BRG_OV518 && ov518_color) {
2574 /* OV518 needs U and V swapped */
2575 i2c_w_mask(ov, 0x15, 0x00, 0x01);
2577 if (mode == VIDEO_PALETTE_GREY) {
2578 /* Set 16-bit input format (UV data are ignored) */
2579 reg_w_mask(ov, 0x20, 0x00, 0x08);
2581 /* Set 8-bit (4:0:0) output format */
2582 reg_w_mask(ov, 0x28, 0x00, 0xf0);
2583 reg_w_mask(ov, 0x38, 0x00, 0xf0);
2585 /* Set 8-bit (YVYU) input format */
2586 reg_w_mask(ov, 0x20, 0x08, 0x08);
2588 /* Set 12-bit (4:2:0) output format */
2589 reg_w_mask(ov, 0x28, 0x80, 0xf0);
2590 reg_w_mask(ov, 0x38, 0x80, 0xf0);
2593 reg_w(ov, 0x28, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2594 reg_w(ov, 0x38, (mode == VIDEO_PALETTE_GREY) ? 0x00:0x80);
2600 reg_w(ov, 0x29, hsegs);
2601 reg_w(ov, 0x2a, vsegs);
2603 reg_w(ov, 0x39, hsegs);
2604 reg_w(ov, 0x3a, vsegs);
2606 /* Windows driver does this here; who knows why */
2607 reg_w(ov, 0x2f, 0x80);
2609 /******** Set the framerate (to 15 FPS) ********/
2611 /* Mode independent, but framerate dependent, regs */
2612 reg_w(ov, 0x51, 0x02); /* Clock divider; lower==faster */
2613 reg_w(ov, 0x22, 0x18);
2614 reg_w(ov, 0x23, 0xff);
2616 if (ov->bridge == BRG_OV518PLUS)
2617 reg_w(ov, 0x21, 0x19);
2619 reg_w(ov, 0x71, 0x19); /* Compression-related? */
2621 // FIXME: Sensor-specific
2622 /* Bit 5 is what matters here. Of course, it is "reserved" */
2623 i2c_w(ov, 0x54, 0x23);
2625 reg_w(ov, 0x2f, 0x80);
2627 if (ov->bridge == BRG_OV518PLUS) {
2628 reg_w(ov, 0x24, 0x94);
2629 reg_w(ov, 0x25, 0x90);
2630 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2631 ov518_reg_w32(ov, 0xc6, 540, 2); /* 21ch */
2632 ov518_reg_w32(ov, 0xc7, 540, 2); /* 21ch */
2633 ov518_reg_w32(ov, 0xc8, 108, 2); /* 6ch */
2634 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2635 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2636 ov518_reg_w32(ov, 0xcc, 2400, 2); /* 960h */
2637 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2638 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2640 reg_w(ov, 0x24, 0x9f);
2641 reg_w(ov, 0x25, 0x90);
2642 ov518_reg_w32(ov, 0xc4, 400, 2); /* 190h */
2643 ov518_reg_w32(ov, 0xc6, 500, 2); /* 1f4h */
2644 ov518_reg_w32(ov, 0xc7, 500, 2); /* 1f4h */
2645 ov518_reg_w32(ov, 0xc8, 142, 2); /* 8eh */
2646 ov518_reg_w32(ov, 0xca, 131098, 3); /* 2001ah */
2647 ov518_reg_w32(ov, 0xcb, 532, 2); /* 214h */
2648 ov518_reg_w32(ov, 0xcc, 2000, 2); /* 7d0h */
2649 ov518_reg_w32(ov, 0xcd, 32, 2); /* 20h */
2650 ov518_reg_w32(ov, 0xce, 608, 2); /* 260h */
2653 reg_w(ov, 0x2f, 0x80);
2655 if (ov51x_restart(ov) < 0)
2658 /* Reset it just for good measure */
2659 if (ov51x_reset(ov, OV511_RESET_NOREGS) < 0)
2665 /* This is a wrapper around the OV511, OV518, and sensor specific functions */
2667 mode_init_regs(struct usb_ov511 *ov,
2668 int width, int height, int mode, int sub_flag)
2672 if (!ov || !ov->dev)
2675 if (ov->bclass == BCL_OV518) {
2676 rc = ov518_mode_init_regs(ov, width, height, mode, sub_flag);
2678 rc = ov511_mode_init_regs(ov, width, height, mode, sub_flag);
2681 if (FATAL_ERROR(rc))
2684 switch (ov->sensor) {
2691 rc = set_ov_sensor_window(ov, width, height, mode, sub_flag);
2695 err("KS0127-series decoders not supported yet");
2699 // rc = mode_init_saa_sensor_regs(ov, width, height, mode,
2702 PDEBUG(1, "SAA status = 0x%02X", i2c_r(ov, 0x1f));
2705 err("Unknown sensor");
2709 if (FATAL_ERROR(rc))
2712 /* Sensor-independent settings */
2713 rc = sensor_set_auto_brightness(ov, ov->auto_brt);
2714 if (FATAL_ERROR(rc))
2717 rc = sensor_set_auto_exposure(ov, ov->auto_exp);
2718 if (FATAL_ERROR(rc))
2721 rc = sensor_set_banding_filter(ov, bandingfilter);
2722 if (FATAL_ERROR(rc))
2725 if (ov->lightfreq) {
2726 rc = sensor_set_light_freq(ov, lightfreq);
2727 if (FATAL_ERROR(rc))
2731 rc = sensor_set_backlight(ov, ov->backlight);
2732 if (FATAL_ERROR(rc))
2735 rc = sensor_set_mirror(ov, ov->mirror);
2736 if (FATAL_ERROR(rc))
2742 /* This sets the default image parameters. This is useful for apps that use
2743 * read() and do not set these.
2746 ov51x_set_default_params(struct usb_ov511 *ov)
2750 /* Set default sizes in case IOCTL (VIDIOCMCAPTURE) is not used
2751 * (using read() instead). */
2752 for (i = 0; i < OV511_NUMFRAMES; i++) {
2753 ov->frame[i].width = ov->maxwidth;
2754 ov->frame[i].height = ov->maxheight;
2755 ov->frame[i].bytes_read = 0;
2757 ov->frame[i].format = force_palette;
2759 ov->frame[i].format = VIDEO_PALETTE_YUV420;
2761 ov->frame[i].depth = get_depth(ov->frame[i].format);
2764 PDEBUG(3, "%dx%d, %s", ov->maxwidth, ov->maxheight,
2765 symbolic(v4l1_plist, ov->frame[0].format));
2767 /* Initialize to max width/height, YUV420 or RGB24 (if supported) */
2768 if (mode_init_regs(ov, ov->maxwidth, ov->maxheight,
2769 ov->frame[0].format, 0) < 0)
2775 /**********************************************************************
2777 * Video decoder stuff
2779 **********************************************************************/
2781 /* Set analog input port of decoder */
2783 decoder_set_input(struct usb_ov511 *ov, int input)
2785 PDEBUG(4, "port %d", input);
2787 switch (ov->sensor) {
2791 i2c_w_mask(ov, 0x02, input, 0x07);
2792 /* Bypass chrominance trap for modes 4..7 */
2793 i2c_w_mask(ov, 0x09, (input > 3) ? 0x80:0x00, 0x80);
2803 /* Get ASCII name of video input */
2805 decoder_get_input_name(struct usb_ov511 *ov, int input, char *name)
2807 switch (ov->sensor) {
2810 if (input < 0 || input > 7)
2813 sprintf(name, "CVBS-%d", input);
2814 else // if (input < 8)
2815 sprintf(name, "S-Video-%d", input - 4);
2819 sprintf(name, "%s", "Camera");
2825 /* Set norm (NTSC, PAL, SECAM, AUTO) */
2827 decoder_set_norm(struct usb_ov511 *ov, int norm)
2829 PDEBUG(4, "%d", norm);
2831 switch (ov->sensor) {
2836 if (norm == VIDEO_MODE_NTSC) {
2837 reg_8 = 0x40; /* 60 Hz */
2838 reg_e = 0x00; /* NTSC M / PAL BGHI */
2839 } else if (norm == VIDEO_MODE_PAL) {
2840 reg_8 = 0x00; /* 50 Hz */
2841 reg_e = 0x00; /* NTSC M / PAL BGHI */
2842 } else if (norm == VIDEO_MODE_AUTO) {
2843 reg_8 = 0x80; /* Auto field detect */
2844 reg_e = 0x00; /* NTSC M / PAL BGHI */
2845 } else if (norm == VIDEO_MODE_SECAM) {
2846 reg_8 = 0x00; /* 50 Hz */
2847 reg_e = 0x50; /* SECAM / PAL 4.43 */
2852 i2c_w_mask(ov, 0x08, reg_8, 0xc0);
2853 i2c_w_mask(ov, 0x0e, reg_e, 0x70);
2863 /**********************************************************************
2867 **********************************************************************/
2869 /* Copies a 64-byte segment at pIn to an 8x8 block at pOut. The width of the
2870 * image at pOut is specified by w.
2873 make_8x8(unsigned char *pIn, unsigned char *pOut, int w)
2875 unsigned char *pOut1 = pOut;
2878 for (y = 0; y < 8; y++) {
2880 for (x = 0; x < 8; x++) {
2888 * For RAW BW (YUV 4:0:0) images, data show up in 256 byte segments.
2889 * The segments represent 4 squares of 8x8 pixels as follows:
2891 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2892 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2894 * 56 57 ... 63 120 121 ... 127 248 249 ... 255
2898 yuv400raw_to_yuv400p(struct ov511_frame *frame,
2899 unsigned char *pIn0, unsigned char *pOut0)
2902 unsigned char *pIn, *pOut, *pOutLine;
2907 for (y = 0; y < frame->rawheight - 1; y += 8) {
2909 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2910 make_8x8(pIn, pOut, frame->rawwidth);
2914 pOutLine += 8 * frame->rawwidth;
2919 * For YUV 4:2:0 images, the data show up in 384 byte segments.
2920 * The first 64 bytes of each segment are U, the next 64 are V. The U and
2921 * V are arranged as follows:
2928 * U and V are shipped at half resolution (1 U,V sample -> one 2x2 block).
2930 * The next 256 bytes are full resolution Y data and represent 4 squares
2931 * of 8x8 pixels as follows:
2933 * 0 1 ... 7 64 65 ... 71 ... 192 193 ... 199
2934 * 8 9 ... 15 72 73 ... 79 200 201 ... 207
2936 * 56 57 ... 63 120 121 ... 127 ... 248 249 ... 255
2938 * Note that the U and V data in one segment represent a 16 x 16 pixel
2939 * area, but the Y data represent a 32 x 8 pixel area. If the width is not an
2940 * even multiple of 32, the extra 8x8 blocks within a 32x8 block belong to the
2941 * next horizontal stripe.
2943 * If dumppix module param is set, _parse_data just dumps the incoming segments,
2944 * verbatim, in order, into the frame. When used with vidcat -f ppm -s 640x480
2945 * this puts the data on the standard output and can be analyzed with the
2946 * parseppm.c utility I wrote. That's a much faster way for figuring out how
2947 * these data are scrambled.
2950 /* Converts from raw, uncompressed segments at pIn0 to a YUV420P frame at pOut0.
2952 * FIXME: Currently only handles width and height that are multiples of 16
2955 yuv420raw_to_yuv420p(struct ov511_frame *frame,
2956 unsigned char *pIn0, unsigned char *pOut0)
2959 unsigned char *pIn, *pOut, *pOutLine;
2960 const unsigned int a = frame->rawwidth * frame->rawheight;
2961 const unsigned int w = frame->rawwidth / 2;
2965 pOutLine = pOut0 + a;
2966 for (y = 0; y < frame->rawheight - 1; y += 16) {
2968 for (x = 0; x < frame->rawwidth - 1; x += 16) {
2969 make_8x8(pIn, pOut, w);
2970 make_8x8(pIn + 64, pOut + a/4, w);
2981 for (y = 0; y < frame->rawheight - 1; y += 8) {
2983 for (x = 0; x < frame->rawwidth - 1; x += 8) {
2984 make_8x8(pIn, pOut, frame->rawwidth);
2992 pOutLine += 8 * frame->rawwidth;
2996 /**********************************************************************
3000 **********************************************************************/
3003 request_decompressor(struct usb_ov511 *ov)
3005 if (ov->bclass == BCL_OV511 || ov->bclass == BCL_OV518) {
3006 err("No decompressor available");
3008 err("Unknown bridge");
3015 decompress(struct usb_ov511 *ov, struct ov511_frame *frame,
3016 unsigned char *pIn0, unsigned char *pOut0)
3018 if (!ov->decomp_ops)
3019 if (request_decompressor(ov))
3024 /**********************************************************************
3028 **********************************************************************/
3030 /* Fuses even and odd fields together, and doubles width.
3031 * INPUT: an odd field followed by an even field at pIn0, in YUV planar format
3032 * OUTPUT: a normal YUV planar image, with correct aspect ratio
3035 deinterlace(struct ov511_frame *frame, int rawformat,
3036 unsigned char *pIn0, unsigned char *pOut0)
3038 const int fieldheight = frame->rawheight / 2;
3039 const int fieldpix = fieldheight * frame->rawwidth;
3040 const int w = frame->width;
3042 unsigned char *pInEven, *pInOdd, *pOut;
3044 PDEBUG(5, "fieldheight=%d", fieldheight);
3046 if (frame->rawheight != frame->height) {
3047 err("invalid height");
3051 if ((frame->rawwidth * 2) != frame->width) {
3052 err("invalid width");
3058 pInEven = pInOdd + fieldpix;
3060 for (y = 0; y < fieldheight; y++) {
3061 for (x = 0; x < frame->rawwidth; x++) {
3063 *(pOut+1) = *pInEven++;
3064 *(pOut+w) = *pInOdd;
3065 *(pOut+w+1) = *pInOdd++;
3071 if (rawformat == RAWFMT_YUV420) {
3073 pInOdd = pIn0 + fieldpix * 2;
3074 pInEven = pInOdd + fieldpix / 4;
3075 for (y = 0; y < fieldheight / 2; y++) {
3076 for (x = 0; x < frame->rawwidth / 2; x++) {
3078 *(pOut+1) = *pInEven++;
3079 *(pOut+w/2) = *pInOdd;
3080 *(pOut+w/2+1) = *pInOdd++;
3086 pInOdd = pIn0 + fieldpix * 2 + fieldpix / 2;
3087 pInEven = pInOdd + fieldpix / 4;
3088 for (y = 0; y < fieldheight / 2; y++) {
3089 for (x = 0; x < frame->rawwidth / 2; x++) {
3091 *(pOut+1) = *pInEven++;
3092 *(pOut+w/2) = *pInOdd;
3093 *(pOut+w/2+1) = *pInOdd++;
3102 ov51x_postprocess_grey(struct usb_ov511 *ov, struct ov511_frame *frame)
3104 /* Deinterlace frame, if necessary */
3105 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3106 if (frame->compressed)
3107 decompress(ov, frame, frame->rawdata,
3110 yuv400raw_to_yuv400p(frame, frame->rawdata,
3113 deinterlace(frame, RAWFMT_YUV400, frame->tempdata,
3116 if (frame->compressed)
3117 decompress(ov, frame, frame->rawdata,
3120 yuv400raw_to_yuv400p(frame, frame->rawdata,
3125 /* Process raw YUV420 data into standard YUV420P */
3127 ov51x_postprocess_yuv420(struct usb_ov511 *ov, struct ov511_frame *frame)
3129 /* Deinterlace frame, if necessary */
3130 if (ov->sensor == SEN_SAA7111A && frame->rawheight >= 480) {
3131 if (frame->compressed)
3132 decompress(ov, frame, frame->rawdata, frame->tempdata);
3134 yuv420raw_to_yuv420p(frame, frame->rawdata,
3137 deinterlace(frame, RAWFMT_YUV420, frame->tempdata,
3140 if (frame->compressed)
3141 decompress(ov, frame, frame->rawdata, frame->data);
3143 yuv420raw_to_yuv420p(frame, frame->rawdata,
3148 /* Post-processes the specified frame. This consists of:
3149 * 1. Decompress frame, if necessary
3150 * 2. Deinterlace frame and scale to proper size, if necessary
3151 * 3. Convert from YUV planar to destination format, if necessary
3152 * 4. Fix the RGB offset, if necessary
3155 ov51x_postprocess(struct usb_ov511 *ov, struct ov511_frame *frame)
3158 memset(frame->data, 0,
3159 MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3160 PDEBUG(4, "Dumping %d bytes", frame->bytes_recvd);
3161 memcpy(frame->data, frame->rawdata, frame->bytes_recvd);
3163 switch (frame->format) {
3164 case VIDEO_PALETTE_GREY:
3165 ov51x_postprocess_grey(ov, frame);
3167 case VIDEO_PALETTE_YUV420:
3168 case VIDEO_PALETTE_YUV420P:
3169 ov51x_postprocess_yuv420(ov, frame);
3172 err("Cannot convert data to %s",
3173 symbolic(v4l1_plist, frame->format));
3178 /**********************************************************************
3180 * OV51x data transfer, IRQ handler
3182 **********************************************************************/
3185 ov511_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3188 int pnum = in[ov->packet_size - 1]; /* Get packet number */
3189 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3190 struct ov511_frame *frame = &ov->frame[ov->curframe];
3193 /* SOF/EOF packets have 1st to 8th bytes zeroed and the 9th
3194 * byte non-zero. The EOF packet has image width/height in the
3195 * 10th and 11th bytes. The 9th byte is given as follows:
3198 * 6: compression enabled
3199 * 5: 422/420/400 modes
3200 * 4: 422/420/400 modes
3202 * 2: snapshot button on
3208 info("ph(%3d): %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x %2x",
3209 pnum, in[0], in[1], in[2], in[3], in[4], in[5], in[6],
3210 in[7], in[8], in[9], in[10], in[11]);
3213 /* Check for SOF/EOF packet */
3214 if ((in[0] | in[1] | in[2] | in[3] | in[4] | in[5] | in[6] | in[7]) ||
3220 ts = (struct timeval *)(frame->data
3221 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3222 do_gettimeofday(ts);
3224 /* Get the actual frame size from the EOF header */
3225 frame->rawwidth = ((int)(in[9]) + 1) * 8;
3226 frame->rawheight = ((int)(in[10]) + 1) * 8;
3228 PDEBUG(4, "Frame end, frame=%d, pnum=%d, w=%d, h=%d, recvd=%d",
3229 ov->curframe, pnum, frame->rawwidth, frame->rawheight,
3230 frame->bytes_recvd);
3232 /* Validate the header data */
3233 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3234 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight,
3237 /* Don't allow byte count to exceed buffer size */
3238 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3240 if (frame->scanstate == STATE_LINES) {
3243 frame->grabstate = FRAME_DONE;
3244 wake_up_interruptible(&frame->wq);
3246 /* If next frame is ready or grabbing,
3248 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3249 if (ov->frame[nextf].grabstate == FRAME_READY
3250 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3251 ov->curframe = nextf;
3252 ov->frame[nextf].scanstate = STATE_SCANNING;
3254 if (frame->grabstate == FRAME_DONE) {
3255 PDEBUG(4, "** Frame done **");
3257 PDEBUG(4, "Frame not ready? state = %d",
3258 ov->frame[nextf].grabstate);
3264 PDEBUG(5, "Frame done, but not scanning");
3266 /* Image corruption caused by misplaced frame->segment = 0
3267 * fixed by carlosf@conectiva.com.br
3271 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3273 /* Check to see if it's a snapshot frame */
3274 /* FIXME?? Should the snapshot reset go here? Performance? */
3276 frame->snapshot = 1;
3277 PDEBUG(3, "snapshot detected");
3280 frame->scanstate = STATE_LINES;
3281 frame->bytes_recvd = 0;
3282 frame->compressed = in[8] & 0x40;
3286 /* Are we in a frame? */
3287 if (frame->scanstate != STATE_LINES) {
3288 PDEBUG(5, "Not in a frame; packet skipped");
3292 /* If frame start, skip header */
3293 if (frame->bytes_recvd == 0)
3298 num = n - offset - 1;
3300 /* Dump all data exactly as received */
3302 frame->bytes_recvd += n - 1;
3303 if (frame->bytes_recvd <= max_raw)
3304 memcpy(frame->rawdata + frame->bytes_recvd - (n - 1),
3307 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3308 frame->bytes_recvd - max_raw);
3309 } else if (!frame->compressed && !remove_zeros) {
3310 frame->bytes_recvd += num;
3311 if (frame->bytes_recvd <= max_raw)
3312 memcpy(frame->rawdata + frame->bytes_recvd - num,
3315 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3316 frame->bytes_recvd - max_raw);
3317 } else { /* Remove all-zero FIFO lines (aligned 32-byte blocks) */
3318 int b, read = 0, allzero, copied = 0;
3320 frame->bytes_recvd += 32 - offset; // Bytes out
3321 memcpy(frame->rawdata, in + offset, 32 - offset);
3325 while (read < n - 1) {
3327 for (b = 0; b < 32; b++) {
3337 if (frame->bytes_recvd + copied + 32 <= max_raw)
3339 memcpy(frame->rawdata
3340 + frame->bytes_recvd + copied,
3344 PDEBUG(3, "Raw data buffer overrun!!");
3350 frame->bytes_recvd += copied;
3355 ov518_move_data(struct usb_ov511 *ov, unsigned char *in, int n)
3357 int max_raw = MAX_RAW_DATA_SIZE(ov->maxwidth, ov->maxheight);
3358 struct ov511_frame *frame = &ov->frame[ov->curframe];
3361 /* Don't copy the packet number byte */
3362 if (ov->packet_numbering)
3365 /* A false positive here is likely, until OVT gives me
3366 * the definitive SOF/EOF format */
3367 if ((!(in[0] | in[1] | in[2] | in[3] | in[5])) && in[6]) {
3369 info("ph: %2x %2x %2x %2x %2x %2x %2x %2x", in[0],
3370 in[1], in[2], in[3], in[4], in[5], in[6], in[7]);
3373 if (frame->scanstate == STATE_LINES) {
3374 PDEBUG(4, "Detected frame end/start");
3376 } else { //scanstate == STATE_SCANNING
3378 PDEBUG(4, "Frame start, framenum = %d", ov->curframe);
3386 ts = (struct timeval *)(frame->data
3387 + MAX_FRAME_SIZE(ov->maxwidth, ov->maxheight));
3388 do_gettimeofday(ts);
3390 PDEBUG(4, "Frame end, curframe = %d, hw=%d, vw=%d, recvd=%d",
3392 (int)(in[9]), (int)(in[10]), frame->bytes_recvd);
3394 // FIXME: Since we don't know the header formats yet,
3395 // there is no way to know what the actual image size is
3396 frame->rawwidth = frame->width;
3397 frame->rawheight = frame->height;
3399 /* Validate the header data */
3400 RESTRICT_TO_RANGE(frame->rawwidth, ov->minwidth, ov->maxwidth);
3401 RESTRICT_TO_RANGE(frame->rawheight, ov->minheight, ov->maxheight);
3403 /* Don't allow byte count to exceed buffer size */
3404 RESTRICT_TO_RANGE(frame->bytes_recvd, 8, max_raw);
3406 if (frame->scanstate == STATE_LINES) {
3409 frame->grabstate = FRAME_DONE;
3410 wake_up_interruptible(&frame->wq);
3412 /* If next frame is ready or grabbing,
3414 nextf = (ov->curframe + 1) % OV511_NUMFRAMES;
3415 if (ov->frame[nextf].grabstate == FRAME_READY
3416 || ov->frame[nextf].grabstate == FRAME_GRABBING) {
3417 ov->curframe = nextf;
3418 ov->frame[nextf].scanstate = STATE_SCANNING;
3419 frame = &ov->frame[nextf];
3421 if (frame->grabstate == FRAME_DONE) {
3422 PDEBUG(4, "** Frame done **");
3424 PDEBUG(4, "Frame not ready? state = %d",
3425 ov->frame[nextf].grabstate);
3429 PDEBUG(4, "SOF dropped (no active frame)");
3430 return; /* Nowhere to store this frame */
3434 PDEBUG(4, "Starting capture on frame %d", frame->framenum);
3436 // Snapshot not reverse-engineered yet.
3438 /* Check to see if it's a snapshot frame */
3439 /* FIXME?? Should the snapshot reset go here? Performance? */
3441 frame->snapshot = 1;
3442 PDEBUG(3, "snapshot detected");
3445 frame->scanstate = STATE_LINES;
3446 frame->bytes_recvd = 0;
3447 frame->compressed = 1;
3450 /* Are we in a frame? */
3451 if (frame->scanstate != STATE_LINES) {
3452 PDEBUG(4, "scanstate: no SOF yet");
3456 /* Dump all data exactly as received */
3458 frame->bytes_recvd += n;
3459 if (frame->bytes_recvd <= max_raw)
3460 memcpy(frame->rawdata + frame->bytes_recvd - n, in, n);
3462 PDEBUG(3, "Raw data buffer overrun!! (%d)",
3463 frame->bytes_recvd - max_raw);
3465 /* All incoming data are divided into 8-byte segments. If the
3466 * segment contains all zero bytes, it must be skipped. These
3467 * zero-segments allow the OV518 to mainain a constant data rate
3468 * regardless of the effectiveness of the compression. Segments
3469 * are aligned relative to the beginning of each isochronous
3470 * packet. The first segment in each image is a header (the
3471 * decompressor skips it later).
3474 int b, read = 0, allzero, copied = 0;
3478 for (b = 0; b < 8; b++) {
3488 if (frame->bytes_recvd + copied + 8 <= max_raw)
3490 memcpy(frame->rawdata
3491 + frame->bytes_recvd + copied,
3495 PDEBUG(3, "Raw data buffer overrun!!");
3500 frame->bytes_recvd += copied;
3505 ov51x_isoc_irq(struct urb *urb)
3508 struct usb_ov511 *ov;
3509 struct ov511_sbuf *sbuf;
3511 if (!urb->context) {
3512 PDEBUG(4, "no context");
3516 sbuf = urb->context;
3519 if (!ov || !ov->dev || !ov->user) {
3520 PDEBUG(4, "no device, or not open");
3524 if (!ov->streaming) {
3525 PDEBUG(4, "hmmm... not streaming, but got interrupt");
3529 if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
3530 PDEBUG(4, "URB unlinked");
3534 if (urb->status != -EINPROGRESS && urb->status != 0) {
3535 err("ERROR: urb->status=%d: %s", urb->status,
3536 symbolic(urb_errlist, urb->status));
3539 /* Copy the data received into our frame buffer */
3540 PDEBUG(5, "sbuf[%d]: Moving %d packets", sbuf->n,
3541 urb->number_of_packets);
3542 for (i = 0; i < urb->number_of_packets; i++) {
3543 /* Warning: Don't call *_move_data() if no frame active! */
3544 if (ov->curframe >= 0) {
3545 int n = urb->iso_frame_desc[i].actual_length;
3546 int st = urb->iso_frame_desc[i].status;
3547 unsigned char *cdata;
3549 urb->iso_frame_desc[i].actual_length = 0;
3550 urb->iso_frame_desc[i].status = 0;
3552 cdata = urb->transfer_buffer
3553 + urb->iso_frame_desc[i].offset;
3556 PDEBUG(4, "Zero-length packet");
3561 PDEBUG(2, "data error: [%d] len=%d, status=%d",
3564 if (ov->bclass == BCL_OV511)
3565 ov511_move_data(ov, cdata, n);
3566 else if (ov->bclass == BCL_OV518)
3567 ov518_move_data(ov, cdata, n);
3569 err("Unknown bridge device (%d)", ov->bridge);
3571 } else if (waitqueue_active(&ov->wq)) {
3572 wake_up_interruptible(&ov->wq);
3576 /* Resubmit this URB */
3578 if ((i = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
3579 err("usb_submit_urb() ret %d", i);
3584 /****************************************************************************
3586 * Stream initialization and termination
3588 ***************************************************************************/
3591 ov51x_init_isoc(struct usb_ov511 *ov)
3594 int fx, err, n, size;
3596 PDEBUG(3, "*** Initializing capture ***");
3600 if (ov->bridge == BRG_OV511) {
3605 else if (cams == 3 || cams == 4)
3608 err("\"cams\" parameter too high!");
3611 } else if (ov->bridge == BRG_OV511PLUS) {
3616 else if (cams == 3 || cams == 4)
3618 else if (cams >= 5 && cams <= 8)
3620 else if (cams >= 9 && cams <= 31)
3623 err("\"cams\" parameter too high!");
3626 } else if (ov->bclass == BCL_OV518) {
3631 else if (cams == 3 || cams == 4)
3633 else if (cams >= 5 && cams <= 8)
3636 err("\"cams\" parameter too high!");
3640 err("invalid bridge type");
3644 // FIXME: OV518 is hardcoded to 15 FPS (alternate 5) for now
3645 if (ov->bclass == BCL_OV518) {
3646 if (packetsize == -1) {
3647 ov518_set_packet_size(ov, 640);
3649 info("Forcing packet size to %d", packetsize);
3650 ov518_set_packet_size(ov, packetsize);
3653 if (packetsize == -1) {
3654 ov511_set_packet_size(ov, size);
3656 info("Forcing packet size to %d", packetsize);
3657 ov511_set_packet_size(ov, packetsize);
3661 for (n = 0; n < OV511_NUMSBUF; n++) {
3662 urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
3664 err("init isoc: usb_alloc_urb ret. NULL");
3667 ov->sbuf[n].urb = urb;
3669 urb->context = &ov->sbuf[n];
3670 urb->pipe = usb_rcvisocpipe(ov->dev, OV511_ENDPOINT_ADDRESS);
3671 urb->transfer_flags = URB_ISO_ASAP;
3672 urb->transfer_buffer = ov->sbuf[n].data;
3673 urb->complete = ov51x_isoc_irq;
3674 urb->number_of_packets = FRAMES_PER_DESC;
3675 urb->transfer_buffer_length = ov->packet_size * FRAMES_PER_DESC;
3677 for (fx = 0; fx < FRAMES_PER_DESC; fx++) {
3678 urb->iso_frame_desc[fx].offset = ov->packet_size * fx;
3679 urb->iso_frame_desc[fx].length = ov->packet_size;
3685 for (n = 0; n < OV511_NUMSBUF; n++) {
3686 ov->sbuf[n].urb->dev = ov->dev;
3687 err = usb_submit_urb(ov->sbuf[n].urb, GFP_KERNEL);
3689 err("init isoc: usb_submit_urb(%d) ret %d", n, err);
3698 ov51x_unlink_isoc(struct usb_ov511 *ov)
3702 /* Unschedule all of the iso td's */
3703 for (n = OV511_NUMSBUF - 1; n >= 0; n--) {
3704 if (ov->sbuf[n].urb) {
3705 usb_kill_urb(ov->sbuf[n].urb);
3706 usb_free_urb(ov->sbuf[n].urb);
3707 ov->sbuf[n].urb = NULL;
3713 ov51x_stop_isoc(struct usb_ov511 *ov)
3715 if (!ov->streaming || !ov->dev)
3718 PDEBUG(3, "*** Stopping capture ***");
3720 if (ov->bclass == BCL_OV518)
3721 ov518_set_packet_size(ov, 0);
3723 ov511_set_packet_size(ov, 0);
3727 ov51x_unlink_isoc(ov);
3731 ov51x_new_frame(struct usb_ov511 *ov, int framenum)
3733 struct ov511_frame *frame;
3736 PDEBUG(4, "ov->curframe = %d, framenum = %d", ov->curframe, framenum);
3741 /* If we're not grabbing a frame right now and the other frame is */
3742 /* ready to be grabbed into, then use it instead */
3743 if (ov->curframe == -1) {
3744 newnum = (framenum - 1 + OV511_NUMFRAMES) % OV511_NUMFRAMES;
3745 if (ov->frame[newnum].grabstate == FRAME_READY)
3750 frame = &ov->frame[framenum];
3752 PDEBUG(4, "framenum = %d, width = %d, height = %d", framenum,
3753 frame->width, frame->height);
3755 frame->grabstate = FRAME_GRABBING;
3756 frame->scanstate = STATE_SCANNING;
3757 frame->snapshot = 0;
3759 ov->curframe = framenum;
3761 /* Make sure it's not too big */
3762 if (frame->width > ov->maxwidth)
3763 frame->width = ov->maxwidth;
3765 frame->width &= ~7L; /* Multiple of 8 */
3767 if (frame->height > ov->maxheight)
3768 frame->height = ov->maxheight;
3770 frame->height &= ~3L; /* Multiple of 4 */
3775 /****************************************************************************
3779 ***************************************************************************/
3782 * - You must acquire buf_lock before entering this function.
3783 * - Because this code will free any non-null pointer, you must be sure to null
3784 * them if you explicitly free them somewhere else!
3787 ov51x_do_dealloc(struct usb_ov511 *ov)
3790 PDEBUG(4, "entered");
3793 rvfree(ov->fbuf, OV511_NUMFRAMES
3794 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight));
3801 vfree(ov->tempfbuf);
3802 ov->tempfbuf = NULL;
3804 for (i = 0; i < OV511_NUMSBUF; i++) {
3805 kfree(ov->sbuf[i].data);
3806 ov->sbuf[i].data = NULL;
3809 for (i = 0; i < OV511_NUMFRAMES; i++) {
3810 ov->frame[i].data = NULL;
3811 ov->frame[i].rawdata = NULL;
3812 ov->frame[i].tempdata = NULL;
3813 if (ov->frame[i].compbuf) {
3814 free_page((unsigned long) ov->frame[i].compbuf);
3815 ov->frame[i].compbuf = NULL;
3819 PDEBUG(4, "buffer memory deallocated");
3820 ov->buf_state = BUF_NOT_ALLOCATED;
3821 PDEBUG(4, "leaving");
3825 ov51x_alloc(struct usb_ov511 *ov)
3828 const int w = ov->maxwidth;
3829 const int h = ov->maxheight;
3830 const int data_bufsize = OV511_NUMFRAMES * MAX_DATA_SIZE(w, h);
3831 const int raw_bufsize = OV511_NUMFRAMES * MAX_RAW_DATA_SIZE(w, h);
3833 PDEBUG(4, "entered");
3834 mutex_lock(&ov->buf_lock);
3836 if (ov->buf_state == BUF_ALLOCATED)
3839 ov->fbuf = rvmalloc(data_bufsize);
3843 ov->rawfbuf = vmalloc(raw_bufsize);
3847 memset(ov->rawfbuf, 0, raw_bufsize);
3849 ov->tempfbuf = vmalloc(raw_bufsize);
3853 memset(ov->tempfbuf, 0, raw_bufsize);
3855 for (i = 0; i < OV511_NUMSBUF; i++) {
3856 ov->sbuf[i].data = kmalloc(FRAMES_PER_DESC *
3857 MAX_FRAME_SIZE_PER_DESC, GFP_KERNEL);
3858 if (!ov->sbuf[i].data)
3861 PDEBUG(4, "sbuf[%d] @ %p", i, ov->sbuf[i].data);
3864 for (i = 0; i < OV511_NUMFRAMES; i++) {
3865 ov->frame[i].data = ov->fbuf + i * MAX_DATA_SIZE(w, h);
3866 ov->frame[i].rawdata = ov->rawfbuf
3867 + i * MAX_RAW_DATA_SIZE(w, h);
3868 ov->frame[i].tempdata = ov->tempfbuf
3869 + i * MAX_RAW_DATA_SIZE(w, h);
3871 ov->frame[i].compbuf =
3872 (unsigned char *) __get_free_page(GFP_KERNEL);
3873 if (!ov->frame[i].compbuf)
3876 PDEBUG(4, "frame[%d] @ %p", i, ov->frame[i].data);
3879 ov->buf_state = BUF_ALLOCATED;
3881 mutex_unlock(&ov->buf_lock);
3882 PDEBUG(4, "leaving");
3885 ov51x_do_dealloc(ov);
3886 mutex_unlock(&ov->buf_lock);
3887 PDEBUG(4, "errored");
3892 ov51x_dealloc(struct usb_ov511 *ov)
3894 PDEBUG(4, "entered");
3895 mutex_lock(&ov->buf_lock);
3896 ov51x_do_dealloc(ov);
3897 mutex_unlock(&ov->buf_lock);
3898 PDEBUG(4, "leaving");
3901 /****************************************************************************
3905 ***************************************************************************/
3908 ov51x_v4l1_open(struct inode *inode, struct file *file)
3910 struct video_device *vdev = video_devdata(file);
3911 struct usb_ov511 *ov = video_get_drvdata(vdev);
3914 PDEBUG(4, "opening");
3916 mutex_lock(&ov->lock);
3924 /* In case app doesn't set them... */
3925 err = ov51x_set_default_params(ov);
3929 /* Make sure frames are reset */
3930 for (i = 0; i < OV511_NUMFRAMES; i++) {
3931 ov->frame[i].grabstate = FRAME_UNUSED;
3932 ov->frame[i].bytes_read = 0;
3935 /* If compression is on, make sure now that a
3936 * decompressor can be loaded */
3937 if (ov->compress && !ov->decomp_ops) {
3938 err = request_decompressor(ov);
3939 if (err && !dumppix)
3943 err = ov51x_alloc(ov);
3947 err = ov51x_init_isoc(ov);
3954 file->private_data = vdev;
3956 if (ov->led_policy == LED_AUTO)
3957 ov51x_led_control(ov, 1);
3960 mutex_unlock(&ov->lock);
3965 ov51x_v4l1_close(struct inode *inode, struct file *file)
3967 struct video_device *vdev = file->private_data;
3968 struct usb_ov511 *ov = video_get_drvdata(vdev);
3970 PDEBUG(4, "ov511_close");
3972 mutex_lock(&ov->lock);
3975 ov51x_stop_isoc(ov);
3977 if (ov->led_policy == LED_AUTO)
3978 ov51x_led_control(ov, 0);
3983 mutex_unlock(&ov->lock);
3985 /* Device unplugged while open. Only a minimum of unregistration is done
3986 * here; the disconnect callback already did the rest. */
3988 mutex_lock(&ov->cbuf_lock);
3991 mutex_unlock(&ov->cbuf_lock);
3998 file->private_data = NULL;
4002 /* Do not call this function directly! */
4004 ov51x_v4l1_ioctl_internal(struct inode *inode, struct file *file,
4005 unsigned int cmd, void *arg)
4007 struct video_device *vdev = file->private_data;
4008 struct usb_ov511 *ov = video_get_drvdata(vdev);
4009 PDEBUG(5, "IOCtl: 0x%X", cmd);
4017 struct video_capability *b = arg;
4019 PDEBUG(4, "VIDIOCGCAP");
4021 memset(b, 0, sizeof(struct video_capability));
4022 sprintf(b->name, "%s USB Camera",
4023 symbolic(brglist, ov->bridge));
4024 b->type = VID_TYPE_CAPTURE | VID_TYPE_SUBCAPTURE;
4025 b->channels = ov->num_inputs;
4027 b->maxwidth = ov->maxwidth;
4028 b->maxheight = ov->maxheight;
4029 b->minwidth = ov->minwidth;
4030 b->minheight = ov->minheight;
4036 struct video_channel *v = arg;
4038 PDEBUG(4, "VIDIOCGCHAN");
4040 if ((unsigned)(v->channel) >= ov->num_inputs) {
4041 err("Invalid channel (%d)", v->channel);
4046 v->type = VIDEO_TYPE_CAMERA;
4048 // v->flags |= (ov->has_decoder) ? VIDEO_VC_NORM : 0;
4050 decoder_get_input_name(ov, v->channel, v->name);
4056 struct video_channel *v = arg;
4059 PDEBUG(4, "VIDIOCSCHAN");
4061 /* Make sure it's not a camera */
4062 if (!ov->has_decoder) {
4063 if (v->channel == 0)
4069 if (v->norm != VIDEO_MODE_PAL &&
4070 v->norm != VIDEO_MODE_NTSC &&
4071 v->norm != VIDEO_MODE_SECAM &&
4072 v->norm != VIDEO_MODE_AUTO) {
4073 err("Invalid norm (%d)", v->norm);
4077 if ((unsigned)(v->channel) >= ov->num_inputs) {
4078 err("Invalid channel (%d)", v->channel);
4082 err = decoder_set_input(ov, v->channel);
4086 err = decoder_set_norm(ov, v->norm);
4094 struct video_picture *p = arg;
4096 PDEBUG(4, "VIDIOCGPICT");
4098 memset(p, 0, sizeof(struct video_picture));
4099 if (sensor_get_picture(ov, p))
4102 /* Can we get these from frame[0]? -claudio? */
4103 p->depth = ov->frame[0].depth;
4104 p->palette = ov->frame[0].format;
4110 struct video_picture *p = arg;
4113 PDEBUG(4, "VIDIOCSPICT");
4115 if (!get_depth(p->palette))
4118 if (sensor_set_picture(ov, p))
4121 if (force_palette && p->palette != force_palette) {
4122 info("Palette rejected (%s)",
4123 symbolic(v4l1_plist, p->palette));
4127 // FIXME: Format should be independent of frames
4128 if (p->palette != ov->frame[0].format) {
4129 PDEBUG(4, "Detected format change");
4131 rc = ov51x_wait_frames_inactive(ov);
4135 mode_init_regs(ov, ov->frame[0].width,
4136 ov->frame[0].height, p->palette, ov->sub_flag);
4139 PDEBUG(4, "Setting depth=%d, palette=%s",
4140 p->depth, symbolic(v4l1_plist, p->palette));
4142 for (i = 0; i < OV511_NUMFRAMES; i++) {
4143 ov->frame[i].depth = p->depth;
4144 ov->frame[i].format = p->palette;
4149 case VIDIOCGCAPTURE:
4153 PDEBUG(4, "VIDIOCGCAPTURE");
4158 case VIDIOCSCAPTURE:
4160 struct video_capture *vc = arg;
4162 PDEBUG(4, "VIDIOCSCAPTURE");
4178 if (vc->height == 0)
4183 ov->subw = vc->width;
4184 ov->subh = vc->height;
4190 struct video_window *vw = arg;
4193 PDEBUG(4, "VIDIOCSWIN: %dx%d", vw->width, vw->height);
4200 if (vw->height != ov->maxheight)
4202 if (vw->width != ov->maxwidth)
4206 rc = ov51x_wait_frames_inactive(ov);
4210 rc = mode_init_regs(ov, vw->width, vw->height,
4211 ov->frame[0].format, ov->sub_flag);
4215 for (i = 0; i < OV511_NUMFRAMES; i++) {
4216 ov->frame[i].width = vw->width;
4217 ov->frame[i].height = vw->height;
4224 struct video_window *vw = arg;
4226 memset(vw, 0, sizeof(struct video_window));
4227 vw->x = 0; /* FIXME */
4229 vw->width = ov->frame[0].width;
4230 vw->height = ov->frame[0].height;
4233 PDEBUG(4, "VIDIOCGWIN: %dx%d", vw->width, vw->height);
4239 struct video_mbuf *vm = arg;
4242 PDEBUG(4, "VIDIOCGMBUF");
4244 memset(vm, 0, sizeof(struct video_mbuf));
4245 vm->size = OV511_NUMFRAMES
4246 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4247 vm->frames = OV511_NUMFRAMES;
4250 for (i = 1; i < OV511_NUMFRAMES; i++) {
4251 vm->offsets[i] = vm->offsets[i-1]
4252 + MAX_DATA_SIZE(ov->maxwidth, ov->maxheight);
4257 case VIDIOCMCAPTURE:
4259 struct video_mmap *vm = arg;
4261 unsigned int f = vm->frame;
4263 PDEBUG(4, "VIDIOCMCAPTURE: frame: %d, %dx%d, %s", f, vm->width,
4264 vm->height, symbolic(v4l1_plist, vm->format));
4266 depth = get_depth(vm->format);
4268 PDEBUG(2, "VIDIOCMCAPTURE: invalid format (%s)",
4269 symbolic(v4l1_plist, vm->format));
4273 if (f >= OV511_NUMFRAMES) {
4274 err("VIDIOCMCAPTURE: invalid frame (%d)", f);
4278 if (vm->width > ov->maxwidth
4279 || vm->height > ov->maxheight) {
4280 err("VIDIOCMCAPTURE: requested dimensions too big");
4284 if (ov->frame[f].grabstate == FRAME_GRABBING) {
4285 PDEBUG(4, "VIDIOCMCAPTURE: already grabbing");
4289 if (force_palette && (vm->format != force_palette)) {
4290 PDEBUG(2, "palette rejected (%s)",
4291 symbolic(v4l1_plist, vm->format));
4295 if ((ov->frame[f].width != vm->width) ||
4296 (ov->frame[f].height != vm->height) ||
4297 (ov->frame[f].format != vm->format) ||
4298 (ov->frame[f].sub_flag != ov->sub_flag) ||
4299 (ov->frame[f].depth != depth)) {
4300 PDEBUG(4, "VIDIOCMCAPTURE: change in image parameters");
4302 rc = ov51x_wait_frames_inactive(ov);
4306 rc = mode_init_regs(ov, vm->width, vm->height,
4307 vm->format, ov->sub_flag);
4310 PDEBUG(1, "Got error while initializing regs ");
4314 ov->frame[f].width = vm->width;
4315 ov->frame[f].height = vm->height;
4316 ov->frame[f].format = vm->format;
4317 ov->frame[f].sub_flag = ov->sub_flag;
4318 ov->frame[f].depth = depth;
4321 /* Mark it as ready */
4322 ov->frame[f].grabstate = FRAME_READY;
4324 PDEBUG(4, "VIDIOCMCAPTURE: renewing frame %d", f);
4326 return ov51x_new_frame(ov, f);
4330 unsigned int fnum = *((unsigned int *) arg);
4331 struct ov511_frame *frame;
4334 if (fnum >= OV511_NUMFRAMES) {
4335 err("VIDIOCSYNC: invalid frame (%d)", fnum);
4339 frame = &ov->frame[fnum];
4341 PDEBUG(4, "syncing to frame %d, grabstate = %d", fnum,
4344 switch (frame->grabstate) {
4348 case FRAME_GRABBING:
4354 rc = wait_event_interruptible(frame->wq,
4355 (frame->grabstate == FRAME_DONE)
4356 || (frame->grabstate == FRAME_ERROR));
4361 if (frame->grabstate == FRAME_ERROR) {
4362 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4368 if (ov->snap_enabled && !frame->snapshot) {
4369 if ((rc = ov51x_new_frame(ov, fnum)) < 0)
4374 frame->grabstate = FRAME_UNUSED;
4376 /* Reset the hardware snapshot button */
4377 /* FIXME - Is this the best place for this? */
4378 if ((ov->snap_enabled) && (frame->snapshot)) {
4379 frame->snapshot = 0;
4380 ov51x_clear_snapshot(ov);
4383 /* Decompression, format conversion, etc... */
4384 ov51x_postprocess(ov, frame);
4393 struct video_buffer *vb = arg;
4395 PDEBUG(4, "VIDIOCGFBUF");
4397 memset(vb, 0, sizeof(struct video_buffer));
4403 struct video_unit *vu = arg;
4405 PDEBUG(4, "VIDIOCGUNIT");
4407 memset(vu, 0, sizeof(struct video_unit));
4409 vu->video = ov->vdev->minor;
4410 vu->vbi = VIDEO_NO_UNIT;
4411 vu->radio = VIDEO_NO_UNIT;
4412 vu->audio = VIDEO_NO_UNIT;
4413 vu->teletext = VIDEO_NO_UNIT;
4419 struct ov511_i2c_struct *w = arg;
4421 return i2c_w_slave(ov, w->slave, w->reg, w->value, w->mask);
4425 struct ov511_i2c_struct *r = arg;
4428 rc = i2c_r_slave(ov, r->slave, r->reg);
4436 PDEBUG(3, "Unsupported IOCtl: 0x%X", cmd);
4437 return -ENOIOCTLCMD;
4444 ov51x_v4l1_ioctl(struct inode *inode, struct file *file,
4445 unsigned int cmd, unsigned long arg)
4447 struct video_device *vdev = file->private_data;
4448 struct usb_ov511 *ov = video_get_drvdata(vdev);
4451 if (mutex_lock_interruptible(&ov->lock))
4454 rc = video_usercopy(inode, file, cmd, arg, ov51x_v4l1_ioctl_internal);
4456 mutex_unlock(&ov->lock);
4461 ov51x_v4l1_read(struct file *file, char __user *buf, size_t cnt, loff_t *ppos)
4463 struct video_device *vdev = file->private_data;
4464 int noblock = file->f_flags&O_NONBLOCK;
4465 unsigned long count = cnt;
4466 struct usb_ov511 *ov = video_get_drvdata(vdev);
4467 int i, rc = 0, frmx = -1;
4468 struct ov511_frame *frame;
4470 if (mutex_lock_interruptible(&ov->lock))
4473 PDEBUG(4, "%ld bytes, noblock=%d", count, noblock);
4475 if (!vdev || !buf) {
4485 // FIXME: Only supports two frames
4486 /* See if a frame is completed, then use it. */
4487 if (ov->frame[0].grabstate >= FRAME_DONE) /* _DONE or _ERROR */
4489 else if (ov->frame[1].grabstate >= FRAME_DONE)/* _DONE or _ERROR */
4492 /* If nonblocking we return immediately */
4493 if (noblock && (frmx == -1)) {
4498 /* If no FRAME_DONE, look for a FRAME_GRABBING state. */
4499 /* See if a frame is in process (grabbing), then use it. */
4501 if (ov->frame[0].grabstate == FRAME_GRABBING)
4503 else if (ov->frame[1].grabstate == FRAME_GRABBING)
4507 /* If no frame is active, start one. */
4509 if ((rc = ov51x_new_frame(ov, frmx = 0))) {
4510 err("read: ov51x_new_frame error");
4515 frame = &ov->frame[frmx];
4523 /* Wait while we're grabbing the image */
4524 PDEBUG(4, "Waiting image grabbing");
4525 rc = wait_event_interruptible(frame->wq,
4526 (frame->grabstate == FRAME_DONE)
4527 || (frame->grabstate == FRAME_ERROR));
4532 PDEBUG(4, "Got image, frame->grabstate = %d", frame->grabstate);
4533 PDEBUG(4, "bytes_recvd = %d", frame->bytes_recvd);
4535 if (frame->grabstate == FRAME_ERROR) {
4536 frame->bytes_read = 0;
4537 err("** ick! ** Errored frame %d", ov->curframe);
4538 if (ov51x_new_frame(ov, frmx)) {
4539 err("read: ov51x_new_frame error");
4546 /* Repeat until we get a snapshot frame */
4547 if (ov->snap_enabled)
4548 PDEBUG(4, "Waiting snapshot frame");
4549 if (ov->snap_enabled && !frame->snapshot) {
4550 frame->bytes_read = 0;
4551 if ((rc = ov51x_new_frame(ov, frmx))) {
4552 err("read: ov51x_new_frame error");
4558 /* Clear the snapshot */
4559 if (ov->snap_enabled && frame->snapshot) {
4560 frame->snapshot = 0;
4561 ov51x_clear_snapshot(ov);
4564 /* Decompression, format conversion, etc... */
4565 ov51x_postprocess(ov, frame);
4567 PDEBUG(4, "frmx=%d, bytes_read=%ld, length=%ld", frmx,
4569 get_frame_length(frame));
4571 /* copy bytes to user space; we allow for partials reads */
4572 // if ((count + frame->bytes_read)
4573 // > get_frame_length((struct ov511_frame *)frame))
4574 // count = frame->scanlength - frame->bytes_read;
4576 /* FIXME - count hardwired to be one frame... */
4577 count = get_frame_length(frame);
4579 PDEBUG(4, "Copy to user space: %ld bytes", count);
4580 if ((i = copy_to_user(buf, frame->data + frame->bytes_read, count))) {
4581 PDEBUG(4, "Copy failed! %d bytes not copied", i);
4586 frame->bytes_read += count;
4587 PDEBUG(4, "{copy} count used=%ld, new bytes_read=%ld",
4588 count, frame->bytes_read);
4590 /* If all data have been read... */
4591 if (frame->bytes_read
4592 >= get_frame_length(frame)) {
4593 frame->bytes_read = 0;
4595 // FIXME: Only supports two frames
4596 /* Mark it as available to be used again. */
4597 ov->frame[frmx].grabstate = FRAME_UNUSED;
4598 if ((rc = ov51x_new_frame(ov, !frmx))) {
4599 err("ov51x_new_frame returned error");
4604 PDEBUG(4, "read finished, returning %ld (sweet)", count);
4606 mutex_unlock(&ov->lock);
4610 mutex_unlock(&ov->lock);
4615 ov51x_v4l1_mmap(struct file *file, struct vm_area_struct *vma)
4617 struct video_device *vdev = file->private_data;
4618 unsigned long start = vma->vm_start;
4619 unsigned long size = vma->vm_end - vma->vm_start;
4620 struct usb_ov511 *ov = video_get_drvdata(vdev);
4621 unsigned long page, pos;
4623 if (ov->dev == NULL)
4626 PDEBUG(4, "mmap: %ld (%lX) bytes", size, size);
4628 if (size > (((OV511_NUMFRAMES
4629 * MAX_DATA_SIZE(ov->maxwidth, ov->maxheight)
4630 + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1))))
4633 if (mutex_lock_interruptible(&ov->lock))
4636 pos = (unsigned long)ov->fbuf;
4638 page = vmalloc_to_pfn((void *)pos);
4639 if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
4640 mutex_unlock(&ov->lock);
4645 if (size > PAGE_SIZE)
4651 mutex_unlock(&ov->lock);
4655 static const struct file_operations ov511_fops = {
4656 .owner = THIS_MODULE,
4657 .open = ov51x_v4l1_open,
4658 .release = ov51x_v4l1_close,
4659 .read = ov51x_v4l1_read,
4660 .mmap = ov51x_v4l1_mmap,
4661 .ioctl = ov51x_v4l1_ioctl,
4662 #ifdef CONFIG_COMPAT
4663 .compat_ioctl = v4l_compat_ioctl32,
4665 .llseek = no_llseek,
4668 static struct video_device vdev_template = {
4669 .owner = THIS_MODULE,
4670 .name = "OV511 USB Camera",
4671 .type = VID_TYPE_CAPTURE,
4672 .fops = &ov511_fops,
4673 .release = video_device_release,
4677 /****************************************************************************
4679 * OV511 and sensor configuration
4681 ***************************************************************************/
4683 /* This initializes the OV7610, OV7620, or OV76BE sensor. The OV76BE uses
4684 * the same register settings as the OV7610, since they are very similar.
4687 ov7xx0_configure(struct usb_ov511 *ov)
4692 /* Lawrence Glaister <lg@jfm.bc.ca> reports:
4694 * Register 0x0f in the 7610 has the following effects:
4696 * 0x85 (AEC method 1): Best overall, good contrast range
4697 * 0x45 (AEC method 2): Very overexposed
4698 * 0xa5 (spec sheet default): Ok, but the black level is
4699 * shifted resulting in loss of contrast
4700 * 0x05 (old driver setting): very overexposed, too much
4703 static struct ov511_regvals aRegvalsNorm7610[] = {
4704 { OV511_I2C_BUS, 0x10, 0xff },
4705 { OV511_I2C_BUS, 0x16, 0x06 },
4706 { OV511_I2C_BUS, 0x28, 0x24 },
4707 { OV511_I2C_BUS, 0x2b, 0xac },
4708 { OV511_I2C_BUS, 0x12, 0x00 },
4709 { OV511_I2C_BUS, 0x38, 0x81 },
4710 { OV511_I2C_BUS, 0x28, 0x24 }, /* 0c */
4711 { OV511_I2C_BUS, 0x0f, 0x85 }, /* lg's setting */
4712 { OV511_I2C_BUS, 0x15, 0x01 },
4713 { OV511_I2C_BUS, 0x20, 0x1c },
4714 { OV511_I2C_BUS, 0x23, 0x2a },
4715 { OV511_I2C_BUS, 0x24, 0x10 },
4716 { OV511_I2C_BUS, 0x25, 0x8a },
4717 { OV511_I2C_BUS, 0x26, 0xa2 },
4718 { OV511_I2C_BUS, 0x27, 0xc2 },
4719 { OV511_I2C_BUS, 0x2a, 0x04 },
4720 { OV511_I2C_BUS, 0x2c, 0xfe },
4721 { OV511_I2C_BUS, 0x2d, 0x93 },
4722 { OV511_I2C_BUS, 0x30, 0x71 },
4723 { OV511_I2C_BUS, 0x31, 0x60 },
4724 { OV511_I2C_BUS, 0x32, 0x26 },
4725 { OV511_I2C_BUS, 0x33, 0x20 },
4726 { OV511_I2C_BUS, 0x34, 0x48 },
4727 { OV511_I2C_BUS, 0x12, 0x24 },
4728 { OV511_I2C_BUS, 0x11, 0x01 },
4729 { OV511_I2C_BUS, 0x0c, 0x24 },
4730 { OV511_I2C_BUS, 0x0d, 0x24 },
4731 { OV511_DONE_BUS, 0x0, 0x00 },
4734 static struct ov511_regvals aRegvalsNorm7620[] = {
4735 { OV511_I2C_BUS, 0x00, 0x00 },
4736 { OV511_I2C_BUS, 0x01, 0x80 },
4737 { OV511_I2C_BUS, 0x02, 0x80 },
4738 { OV511_I2C_BUS, 0x03, 0xc0 },
4739 { OV511_I2C_BUS, 0x06, 0x60 },
4740 { OV511_I2C_BUS, 0x07, 0x00 },
4741 { OV511_I2C_BUS, 0x0c, 0x24 },
4742 { OV511_I2C_BUS, 0x0c, 0x24 },
4743 { OV511_I2C_BUS, 0x0d, 0x24 },
4744 { OV511_I2C_BUS, 0x11, 0x01 },
4745 { OV511_I2C_BUS, 0x12, 0x24 },
4746 { OV511_I2C_BUS, 0x13, 0x01 },
4747 { OV511_I2C_BUS, 0x14, 0x84 },
4748 { OV511_I2C_BUS, 0x15, 0x01 },
4749 { OV511_I2C_BUS, 0x16, 0x03 },
4750 { OV511_I2C_BUS, 0x17, 0x2f },
4751 { OV511_I2C_BUS, 0x18, 0xcf },
4752 { OV511_I2C_BUS, 0x19, 0x06 },
4753 { OV511_I2C_BUS, 0x1a, 0xf5 },
4754 { OV511_I2C_BUS, 0x1b, 0x00 },
4755 { OV511_I2C_BUS, 0x20, 0x18 },
4756 { OV511_I2C_BUS, 0x21, 0x80 },
4757 { OV511_I2C_BUS, 0x22, 0x80 },
4758 { OV511_I2C_BUS, 0x23, 0x00 },
4759 { OV511_I2C_BUS, 0x26, 0xa2 },
4760 { OV511_I2C_BUS, 0x27, 0xea },
4761 { OV511_I2C_BUS, 0x28, 0x20 },
4762 { OV511_I2C_BUS, 0x29, 0x00 },
4763 { OV511_I2C_BUS, 0x2a, 0x10 },
4764 { OV511_I2C_BUS, 0x2b, 0x00 },
4765 { OV511_I2C_BUS, 0x2c, 0x88 },
4766 { OV511_I2C_BUS, 0x2d, 0x91 },
4767 { OV511_I2C_BUS, 0x2e, 0x80 },
4768 { OV511_I2C_BUS, 0x2f, 0x44 },
4769 { OV511_I2C_BUS, 0x60, 0x27 },
4770 { OV511_I2C_BUS, 0x61, 0x02 },
4771 { OV511_I2C_BUS, 0x62, 0x5f },
4772 { OV511_I2C_BUS, 0x63, 0xd5 },
4773 { OV511_I2C_BUS, 0x64, 0x57 },
4774 { OV511_I2C_BUS, 0x65, 0x83 },
4775 { OV511_I2C_BUS, 0x66, 0x55 },
4776 { OV511_I2C_BUS, 0x67, 0x92 },
4777 { OV511_I2C_BUS, 0x68, 0xcf },
4778 { OV511_I2C_BUS, 0x69, 0x76 },
4779 { OV511_I2C_BUS, 0x6a, 0x22 },
4780 { OV511_I2C_BUS, 0x6b, 0x00 },
4781 { OV511_I2C_BUS, 0x6c, 0x02 },
4782 { OV511_I2C_BUS, 0x6d, 0x44 },
4783 { OV511_I2C_BUS, 0x6e, 0x80 },
4784 { OV511_I2C_BUS, 0x6f, 0x1d },
4785 { OV511_I2C_BUS, 0x70, 0x8b },
4786 { OV511_I2C_BUS, 0x71, 0x00 },
4787 { OV511_I2C_BUS, 0x72, 0x14 },
4788 { OV511_I2C_BUS, 0x73, 0x54 },
4789 { OV511_I2C_BUS, 0x74, 0x00 },
4790 { OV511_I2C_BUS, 0x75, 0x8e },
4791 { OV511_I2C_BUS, 0x76, 0x00 },
4792 { OV511_I2C_BUS, 0x77, 0xff },
4793 { OV511_I2C_BUS, 0x78, 0x80 },
4794 { OV511_I2C_BUS, 0x79, 0x80 },
4795 { OV511_I2C_BUS, 0x7a, 0x80 },
4796 { OV511_I2C_BUS, 0x7b, 0xe2 },
4797 { OV511_I2C_BUS, 0x7c, 0x00 },
4798 { OV511_DONE_BUS, 0x0, 0x00 },
4801 PDEBUG(4, "starting configuration");
4803 /* This looks redundant, but is necessary for WebCam 3 */
4804 ov->primary_i2c_slave = OV7xx0_SID;
4805 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
4808 if (init_ov_sensor(ov) >= 0) {
4809 PDEBUG(1, "OV7xx0 sensor initalized (method 1)");
4811 /* Reset the 76xx */
4812 if (i2c_w(ov, 0x12, 0x80) < 0)
4815 /* Wait for it to initialize */
4820 while (i <= i2c_detect_tries) {
4821 if ((i2c_r(ov, OV7610_REG_ID_HIGH) == 0x7F) &&
4822 (i2c_r(ov, OV7610_REG_ID_LOW) == 0xA2)) {
4830 // Was (i == i2c_detect_tries) previously. This obviously used to always report
4831 // success. Whether anyone actually depended on that bug is unknown
4832 if ((i >= i2c_detect_tries) && (success == 0)) {
4833 err("Failed to read sensor ID. You might not have an");
4834 err("OV7610/20, or it may be not responding. Report");
4835 err("this to " EMAIL);
4836 err("This is only a warning. You can attempt to use");
4837 err("your camera anyway");
4838 // Only issue a warning for now
4841 PDEBUG(1, "OV7xx0 initialized (method 2, %dx)", i+1);
4845 /* Detect sensor (sub)type */
4846 rc = i2c_r(ov, OV7610_REG_COM_I);
4849 err("Error detecting sensor type");
4851 } else if ((rc & 3) == 3) {
4852 info("Sensor is an OV7610");
4853 ov->sensor = SEN_OV7610;
4854 } else if ((rc & 3) == 1) {
4855 /* I don't know what's different about the 76BE yet. */
4856 if (i2c_r(ov, 0x15) & 1)
4857 info("Sensor is an OV7620AE");
4859 info("Sensor is an OV76BE");
4861 /* OV511+ will return all zero isoc data unless we
4862 * configure the sensor as a 7620. Someone needs to
4863 * find the exact reg. setting that causes this. */
4864 if (ov->bridge == BRG_OV511PLUS) {
4865 info("Enabling 511+/7620AE workaround");
4866 ov->sensor = SEN_OV7620;
4868 ov->sensor = SEN_OV76BE;
4870 } else if ((rc & 3) == 0) {
4871 info("Sensor is an OV7620");
4872 ov->sensor = SEN_OV7620;
4874 err("Unknown image sensor version: %d", rc & 3);
4878 if (ov->sensor == SEN_OV7620) {
4879 PDEBUG(4, "Writing 7620 registers");
4880 if (write_regvals(ov, aRegvalsNorm7620))
4883 PDEBUG(4, "Writing 7610 registers");
4884 if (write_regvals(ov, aRegvalsNorm7610))
4888 /* Set sensor-specific vars */
4890 ov->maxheight = 480;
4894 // FIXME: These do not match the actual settings yet
4895 ov->brightness = 0x80 << 8;
4896 ov->contrast = 0x80 << 8;
4897 ov->colour = 0x80 << 8;
4898 ov->hue = 0x80 << 8;
4903 /* This initializes the OV6620, OV6630, OV6630AE, or OV6630AF sensor. */
4905 ov6xx0_configure(struct usb_ov511 *ov)
4909 static struct ov511_regvals aRegvalsNorm6x20[] = {
4910 { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4911 { OV511_I2C_BUS, 0x11, 0x01 },
4912 { OV511_I2C_BUS, 0x03, 0x60 },
4913 { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4914 { OV511_I2C_BUS, 0x07, 0xa8 },
4915 /* The ratio of 0x0c and 0x0d controls the white point */
4916 { OV511_I2C_BUS, 0x0c, 0x24 },
4917 { OV511_I2C_BUS, 0x0d, 0x24 },
4918 { OV511_I2C_BUS, 0x0f, 0x15 }, /* COMS */
4919 { OV511_I2C_BUS, 0x10, 0x75 }, /* AEC Exposure time */
4920 { OV511_I2C_BUS, 0x12, 0x24 }, /* Enable AGC */
4921 { OV511_I2C_BUS, 0x14, 0x04 },
4922 /* 0x16: 0x06 helps frame stability with moving objects */
4923 { OV511_I2C_BUS, 0x16, 0x06 },
4924 // { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4925 { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4926 /* 0x28: 0x05 Selects RGB format if RGB on */
4927 { OV511_I2C_BUS, 0x28, 0x05 },
4928 { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4929 // { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4930 { OV511_I2C_BUS, 0x2d, 0x99 },
4931 { OV511_I2C_BUS, 0x33, 0xa0 }, /* Color Processing Parameter */
4932 { OV511_I2C_BUS, 0x34, 0xd2 }, /* Max A/D range */
4933 { OV511_I2C_BUS, 0x38, 0x8b },
4934 { OV511_I2C_BUS, 0x39, 0x40 },
4936 { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4937 { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4938 { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4940 { OV511_I2C_BUS, 0x3d, 0x80 },
4941 /* These next two registers (0x4a, 0x4b) are undocumented. They
4942 * control the color balance */
4943 { OV511_I2C_BUS, 0x4a, 0x80 },
4944 { OV511_I2C_BUS, 0x4b, 0x80 },
4945 { OV511_I2C_BUS, 0x4d, 0xd2 }, /* This reduces noise a bit */
4946 { OV511_I2C_BUS, 0x4e, 0xc1 },
4947 { OV511_I2C_BUS, 0x4f, 0x04 },
4948 // Do 50-53 have any effect?
4949 // Toggle 0x12[2] off and on here?
4950 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
4953 static struct ov511_regvals aRegvalsNorm6x30[] = {
4954 /*OK*/ { OV511_I2C_BUS, 0x12, 0x80 }, /* reset */
4955 { OV511_I2C_BUS, 0x11, 0x00 },
4956 /*OK*/ { OV511_I2C_BUS, 0x03, 0x60 },
4957 /*0A?*/ { OV511_I2C_BUS, 0x05, 0x7f }, /* For when autoadjust is off */
4958 { OV511_I2C_BUS, 0x07, 0xa8 },
4959 /* The ratio of 0x0c and 0x0d controls the white point */
4960 /*OK*/ { OV511_I2C_BUS, 0x0c, 0x24 },
4961 /*OK*/ { OV511_I2C_BUS, 0x0d, 0x24 },
4962 /*A*/ { OV511_I2C_BUS, 0x0e, 0x20 },
4963 // /*04?*/ { OV511_I2C_BUS, 0x14, 0x80 },
4964 { OV511_I2C_BUS, 0x16, 0x03 },
4965 // /*OK*/ { OV511_I2C_BUS, 0x20, 0x30 }, /* Aperture correction enable */
4966 // 21 & 22? The suggested values look wrong. Go with default
4967 /*A*/ { OV511_I2C_BUS, 0x23, 0xc0 },
4968 /*A*/ { OV511_I2C_BUS, 0x25, 0x9a }, // Check this against default
4969 // /*OK*/ { OV511_I2C_BUS, 0x26, 0xb2 }, /* BLC enable */
4971 /* 0x28: 0x05 Selects RGB format if RGB on */
4972 // /*04?*/ { OV511_I2C_BUS, 0x28, 0x05 },
4973 // /*04?*/ { OV511_I2C_BUS, 0x28, 0x45 }, // DEBUG: Tristate UV bus
4975 /*OK*/ { OV511_I2C_BUS, 0x2a, 0x04 }, /* Disable framerate adjust */
4976 // /*OK*/ { OV511_I2C_BUS, 0x2b, 0xac }, /* Framerate; Set 2a[7] first */
4977 { OV511_I2C_BUS, 0x2d, 0x99 },
4978 // /*A*/ { OV511_I2C_BUS, 0x33, 0x26 }, // Reserved bits on 6620
4979 // /*d2?*/ { OV511_I2C_BUS, 0x34, 0x03 }, /* Max A/D range */
4980 // /*8b?*/ { OV511_I2C_BUS, 0x38, 0x83 },
4981 // /*40?*/ { OV511_I2C_BUS, 0x39, 0xc0 }, // 6630 adds bit 7
4982 // { OV511_I2C_BUS, 0x3c, 0x39 }, /* Enable AEC mode changing */
4983 // { OV511_I2C_BUS, 0x3c, 0x3c }, /* Change AEC mode */
4984 // { OV511_I2C_BUS, 0x3c, 0x24 }, /* Disable AEC mode changing */
4985 { OV511_I2C_BUS, 0x3d, 0x80 },
4986 // /*A*/ { OV511_I2C_BUS, 0x3f, 0x0e },
4988 /* These next two registers (0x4a, 0x4b) are undocumented. They
4989 * control the color balance */
4990 // /*OK?*/ { OV511_I2C_BUS, 0x4a, 0x80 }, // Check these
4991 // /*OK?*/ { OV511_I2C_BUS, 0x4b, 0x80 },
4992 { OV511_I2C_BUS, 0x4d, 0x10 }, /* U = 0.563u, V = 0.714v */
4993 /*c1?*/ { OV511_I2C_BUS, 0x4e, 0x40 },
4995 /* UV average mode, color killer: strongest */
4996 { OV511_I2C_BUS, 0x4f, 0x07 },
4998 { OV511_I2C_BUS, 0x54, 0x23 }, /* Max AGC gain: 18dB */
4999 { OV511_I2C_BUS, 0x57, 0x81 }, /* (default) */
5000 { OV511_I2C_BUS, 0x59, 0x01 }, /* AGC dark current comp: +1 */
5001 { OV511_I2C_BUS, 0x5a, 0x2c }, /* (undocumented) */
5002 { OV511_I2C_BUS, 0x5b, 0x0f }, /* AWB chrominance levels */
5003 // { OV511_I2C_BUS, 0x5c, 0x10 },
5004 { OV511_DONE_BUS, 0x0, 0x00 }, /* END MARKER */
5007 PDEBUG(4, "starting sensor configuration");
5009 if (init_ov_sensor(ov) < 0) {
5010 err("Failed to read sensor ID. You might not have an OV6xx0,");
5011 err("or it may be not responding. Report this to " EMAIL);
5014 PDEBUG(1, "OV6xx0 sensor detected");
5017 /* Detect sensor (sub)type */
5018 rc = i2c_r(ov, OV7610_REG_COM_I);
5021 err("Error detecting sensor type");
5025 if ((rc & 3) == 0) {
5026 ov->sensor = SEN_OV6630;
5027 info("Sensor is an OV6630");
5028 } else if ((rc & 3) == 1) {
5029 ov->sensor = SEN_OV6620;
5030 info("Sensor is an OV6620");
5031 } else if ((rc & 3) == 2) {
5032 ov->sensor = SEN_OV6630;
5033 info("Sensor is an OV6630AE");
5034 } else if ((rc & 3) == 3) {
5035 ov->sensor = SEN_OV6630;
5036 info("Sensor is an OV6630AF");
5039 /* Set sensor-specific vars */
5041 ov->maxheight = 288;
5045 // FIXME: These do not match the actual settings yet
5046 ov->brightness = 0x80 << 8;
5047 ov->contrast = 0x80 << 8;
5048 ov->colour = 0x80 << 8;
5049 ov->hue = 0x80 << 8;
5051 if (ov->sensor == SEN_OV6620) {
5052 PDEBUG(4, "Writing 6x20 registers");
5053 if (write_regvals(ov, aRegvalsNorm6x20))
5056 PDEBUG(4, "Writing 6x30 registers");
5057 if (write_regvals(ov, aRegvalsNorm6x30))
5064 /* This initializes the KS0127 and KS0127B video decoders. */
5066 ks0127_configure(struct usb_ov511 *ov)
5070 // FIXME: I don't know how to sync or reset it yet
5072 if (ov51x_init_ks_sensor(ov) < 0) {
5073 err("Failed to initialize the KS0127");
5076 PDEBUG(1, "KS012x(B) sensor detected");
5080 /* Detect decoder subtype */
5081 rc = i2c_r(ov, 0x00);
5083 err("Error detecting sensor type");
5085 } else if (rc & 0x08) {
5086 rc = i2c_r(ov, 0x3d);
5088 err("Error detecting sensor type");
5090 } else if ((rc & 0x0f) == 0) {
5091 info("Sensor is a KS0127");
5092 ov->sensor = SEN_KS0127;
5093 } else if ((rc & 0x0f) == 9) {
5094 info("Sensor is a KS0127B Rev. A");
5095 ov->sensor = SEN_KS0127B;
5098 err("Error: Sensor is an unsupported KS0122");
5102 /* Set sensor-specific vars */
5104 ov->maxheight = 480;
5108 // FIXME: These do not match the actual settings yet
5109 ov->brightness = 0x80 << 8;
5110 ov->contrast = 0x80 << 8;
5111 ov->colour = 0x80 << 8;
5112 ov->hue = 0x80 << 8;
5114 /* This device is not supported yet. Bail out now... */
5115 err("This sensor is not supported yet.");
5121 /* This initializes the SAA7111A video decoder. */
5123 saa7111a_configure(struct usb_ov511 *ov)
5127 /* Since there is no register reset command, all registers must be
5128 * written, otherwise gives erratic results */
5129 static struct ov511_regvals aRegvalsNormSAA7111A[] = {
5130 { OV511_I2C_BUS, 0x06, 0xce },
5131 { OV511_I2C_BUS, 0x07, 0x00 },
5132 { OV511_I2C_BUS, 0x10, 0x44 }, /* YUV422, 240/286 lines */
5133 { OV511_I2C_BUS, 0x0e, 0x01 }, /* NTSC M or PAL BGHI */
5134 { OV511_I2C_BUS, 0x00, 0x00 },
5135 { OV511_I2C_BUS, 0x01, 0x00 },
5136 { OV511_I2C_BUS, 0x03, 0x23 },
5137 { OV511_I2C_BUS, 0x04, 0x00 },
5138 { OV511_I2C_BUS, 0x05, 0x00 },
5139 { OV511_I2C_BUS, 0x08, 0xc8 }, /* Auto field freq */
5140 { OV511_I2C_BUS, 0x09, 0x01 }, /* Chrom. trap off, APER=0.25 */
5141 { OV511_I2C_BUS, 0x0a, 0x80 }, /* BRIG=128 */
5142 { OV511_I2C_BUS, 0x0b, 0x40 }, /* CONT=1.0 */
5143 { OV511_I2C_BUS, 0x0c, 0x40 }, /* SATN=1.0 */
5144 { OV511_I2C_BUS, 0x0d, 0x00 }, /* HUE=0 */
5145 { OV511_I2C_BUS, 0x0f, 0x00 },
5146 { OV511_I2C_BUS, 0x11, 0x0c },
5147 { OV511_I2C_BUS, 0x12, 0x00 },
5148 { OV511_I2C_BUS, 0x13, 0x00 },
5149 { OV511_I2C_BUS, 0x14, 0x00 },
5150 { OV511_I2C_BUS, 0x15, 0x00 },
5151 { OV511_I2C_BUS, 0x16, 0x00 },
5152 { OV511_I2C_BUS, 0x17, 0x00 },
5153 { OV511_I2C_BUS, 0x02, 0xc0 }, /* Composite input 0 */
5154 { OV511_DONE_BUS, 0x0, 0x00 },
5157 // FIXME: I don't know how to sync or reset it yet
5159 if (ov51x_init_saa_sensor(ov) < 0) {
5160 err("Failed to initialize the SAA7111A");
5163 PDEBUG(1, "SAA7111A sensor detected");
5167 /* 640x480 not supported with PAL */
5170 ov->maxheight = 240; /* Even field only */
5173 ov->maxheight = 480; /* Even/Odd fields */
5177 ov->minheight = 240; /* Even field only */
5179 ov->has_decoder = 1;
5181 ov->norm = VIDEO_MODE_AUTO;
5182 ov->stop_during_set = 0; /* Decoder guarantees stable image */
5184 /* Decoder doesn't change these values, so we use these instead of
5185 * acutally reading the registers (which doesn't work) */
5186 ov->brightness = 0x80 << 8;
5187 ov->contrast = 0x40 << 9;
5188 ov->colour = 0x40 << 9;
5191 PDEBUG(4, "Writing SAA7111A registers");
5192 if (write_regvals(ov, aRegvalsNormSAA7111A))
5195 /* Detect version of decoder. This must be done after writing the
5196 * initial regs or the decoder will lock up. */
5197 rc = i2c_r(ov, 0x00);
5200 err("Error detecting sensor version");
5203 info("Sensor is an SAA7111A (version 0x%x)", rc);
5204 ov->sensor = SEN_SAA7111A;
5207 // FIXME: Fix this for OV518(+)
5208 /* Latch to negative edge of clock. Otherwise, we get incorrect
5209 * colors and jitter in the digital signal. */
5210 if (ov->bclass == BCL_OV511)
5211 reg_w(ov, 0x11, 0x00);
5213 warn("SAA7111A not yet supported with OV518/OV518+");
5218 /* This initializes the OV511/OV511+ and the sensor */
5220 ov511_configure(struct usb_ov511 *ov)
5222 static struct ov511_regvals aRegvalsInit511[] = {
5223 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5224 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5225 { OV511_REG_BUS, R51x_SYS_RESET, 0x7f },
5226 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5227 { OV511_REG_BUS, R51x_SYS_RESET, 0x3f },
5228 { OV511_REG_BUS, R51x_SYS_INIT, 0x01 },
5229 { OV511_REG_BUS, R51x_SYS_RESET, 0x3d },
5230 { OV511_DONE_BUS, 0x0, 0x00},
5233 static struct ov511_regvals aRegvalsNorm511[] = {
5234 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0x01 },
5235 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5236 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5237 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5238 { OV511_REG_BUS, R511_FIFO_OPTS, 0x1f },
5239 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5240 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5241 { OV511_DONE_BUS, 0x0, 0x00 },
5244 static struct ov511_regvals aRegvalsNorm511Plus[] = {
5245 { OV511_REG_BUS, R511_DRAM_FLOW_CTL, 0xff },
5246 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5247 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 },
5248 { OV511_REG_BUS, R51x_SYS_SNAP, 0x00 },
5249 { OV511_REG_BUS, R511_FIFO_OPTS, 0xff },
5250 { OV511_REG_BUS, R511_COMP_EN, 0x00 },
5251 { OV511_REG_BUS, R511_COMP_LUT_EN, 0x03 },
5252 { OV511_DONE_BUS, 0x0, 0x00 },
5257 ov->customid = reg_r(ov, R511_SYS_CUST_ID);
5258 if (ov->customid < 0) {
5259 err("Unable to read camera bridge registers");
5263 PDEBUG (1, "CustomID = %d", ov->customid);
5264 ov->desc = symbolic(camlist, ov->customid);
5265 info("model: %s", ov->desc);
5267 if (0 == strcmp(ov->desc, NOT_DEFINED_STR)) {
5268 err("Camera type (%d) not recognized", ov->customid);
5269 err("Please notify " EMAIL " of the name,");
5270 err("manufacturer, model, and this number of your camera.");
5271 err("Also include the output of the detection process.");
5274 if (ov->customid == 70) /* USB Life TV (PAL/SECAM) */
5277 if (write_regvals(ov, aRegvalsInit511))
5280 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5281 ov51x_led_control(ov, 0);
5283 /* The OV511+ has undocumented bits in the flow control register.
5284 * Setting it to 0xff fixes the corruption with moving objects. */
5285 if (ov->bridge == BRG_OV511) {
5286 if (write_regvals(ov, aRegvalsNorm511))
5288 } else if (ov->bridge == BRG_OV511PLUS) {
5289 if (write_regvals(ov, aRegvalsNorm511Plus))
5292 err("Invalid bridge");
5295 if (ov511_init_compression(ov))
5298 ov->packet_numbering = 1;
5299 ov511_set_packet_size(ov, 0);
5301 ov->snap_enabled = snapshot;
5304 PDEBUG(3, "Testing for 0V7xx0");
5305 ov->primary_i2c_slave = OV7xx0_SID;
5306 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5309 if (i2c_w(ov, 0x12, 0x80) < 0) {
5311 PDEBUG(3, "Testing for 0V6xx0");
5312 ov->primary_i2c_slave = OV6xx0_SID;
5313 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5316 if (i2c_w(ov, 0x12, 0x80) < 0) {
5318 PDEBUG(3, "Testing for 0V8xx0");
5319 ov->primary_i2c_slave = OV8xx0_SID;
5320 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5323 if (i2c_w(ov, 0x12, 0x80) < 0) {
5324 /* Test for SAA7111A */
5325 PDEBUG(3, "Testing for SAA7111A");
5326 ov->primary_i2c_slave = SAA7111A_SID;
5327 if (ov51x_set_slave_ids(ov, SAA7111A_SID) < 0)
5330 if (i2c_w(ov, 0x0d, 0x00) < 0) {
5331 /* Test for KS0127 */
5332 PDEBUG(3, "Testing for KS0127");
5333 ov->primary_i2c_slave = KS0127_SID;
5334 if (ov51x_set_slave_ids(ov, KS0127_SID) < 0)
5337 if (i2c_w(ov, 0x10, 0x00) < 0) {
5338 err("Can't determine sensor slave IDs");
5341 if (ks0127_configure(ov) < 0) {
5342 err("Failed to configure KS0127");
5347 if (saa7111a_configure(ov) < 0) {
5348 err("Failed to configure SAA7111A");
5353 err("Detected unsupported OV8xx0 sensor");
5357 if (ov6xx0_configure(ov) < 0) {
5358 err("Failed to configure OV6xx0");
5363 if (ov7xx0_configure(ov) < 0) {
5364 err("Failed to configure OV7xx0");
5372 err("OV511 Config failed");
5377 /* This initializes the OV518/OV518+ and the sensor */
5379 ov518_configure(struct usb_ov511 *ov)
5381 /* For 518 and 518+ */
5382 static struct ov511_regvals aRegvalsInit518[] = {
5383 { OV511_REG_BUS, R51x_SYS_RESET, 0x40 },
5384 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5385 { OV511_REG_BUS, R51x_SYS_RESET, 0x3e },
5386 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5387 { OV511_REG_BUS, R51x_SYS_RESET, 0x00 },
5388 { OV511_REG_BUS, R51x_SYS_INIT, 0xe1 },
5389 { OV511_REG_BUS, 0x46, 0x00 },
5390 { OV511_REG_BUS, 0x5d, 0x03 },
5391 { OV511_DONE_BUS, 0x0, 0x00},
5394 static struct ov511_regvals aRegvalsNorm518[] = {
5395 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5396 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5397 { OV511_REG_BUS, 0x31, 0x0f },
5398 { OV511_REG_BUS, 0x5d, 0x03 },
5399 { OV511_REG_BUS, 0x24, 0x9f },
5400 { OV511_REG_BUS, 0x25, 0x90 },
5401 { OV511_REG_BUS, 0x20, 0x00 },
5402 { OV511_REG_BUS, 0x51, 0x04 },
5403 { OV511_REG_BUS, 0x71, 0x19 },
5404 { OV511_DONE_BUS, 0x0, 0x00 },
5407 static struct ov511_regvals aRegvalsNorm518Plus[] = {
5408 { OV511_REG_BUS, R51x_SYS_SNAP, 0x02 }, /* Reset */
5409 { OV511_REG_BUS, R51x_SYS_SNAP, 0x01 }, /* Enable */
5410 { OV511_REG_BUS, 0x31, 0x0f },
5411 { OV511_REG_BUS, 0x5d, 0x03 },
5412 { OV511_REG_BUS, 0x24, 0x9f },
5413 { OV511_REG_BUS, 0x25, 0x90 },
5414 { OV511_REG_BUS, 0x20, 0x60 },
5415 { OV511_REG_BUS, 0x51, 0x02 },
5416 { OV511_REG_BUS, 0x71, 0x19 },
5417 { OV511_REG_BUS, 0x40, 0xff },
5418 { OV511_REG_BUS, 0x41, 0x42 },
5419 { OV511_REG_BUS, 0x46, 0x00 },
5420 { OV511_REG_BUS, 0x33, 0x04 },
5421 { OV511_REG_BUS, 0x21, 0x19 },
5422 { OV511_REG_BUS, 0x3f, 0x10 },
5423 { OV511_DONE_BUS, 0x0, 0x00 },
5428 /* First 5 bits of custom ID reg are a revision ID on OV518 */
5429 info("Device revision %d", 0x1F & reg_r(ov, R511_SYS_CUST_ID));
5431 /* Give it the default description */
5432 ov->desc = symbolic(camlist, 0);
5434 if (write_regvals(ov, aRegvalsInit518))
5437 /* Set LED GPIO pin to output mode */
5438 if (reg_w_mask(ov, 0x57, 0x00, 0x02) < 0)
5441 /* LED is off by default with OV518; have to explicitly turn it on */
5442 if (ov->led_policy == LED_OFF || ov->led_policy == LED_AUTO)
5443 ov51x_led_control(ov, 0);
5445 ov51x_led_control(ov, 1);
5447 /* Don't require compression if dumppix is enabled; otherwise it's
5448 * required. OV518 has no uncompressed mode, to save RAM. */
5449 if (!dumppix && !ov->compress) {
5451 warn("Compression required with OV518...enabling");
5454 if (ov->bridge == BRG_OV518) {
5455 if (write_regvals(ov, aRegvalsNorm518))
5457 } else if (ov->bridge == BRG_OV518PLUS) {
5458 if (write_regvals(ov, aRegvalsNorm518Plus))
5461 err("Invalid bridge");
5464 if (reg_w(ov, 0x2f, 0x80) < 0)
5467 if (ov518_init_compression(ov))
5470 if (ov->bridge == BRG_OV518)
5472 struct usb_interface *ifp;
5473 struct usb_host_interface *alt;
5476 ifp = usb_ifnum_to_if(ov->dev, 0);
5478 alt = usb_altnum_to_altsetting(ifp, 7);
5480 mxps = le16_to_cpu(alt->endpoint[0].desc.wMaxPacketSize);
5483 /* Some OV518s have packet numbering by default, some don't */
5485 ov->packet_numbering = 1;
5487 ov->packet_numbering = 0;
5489 /* OV518+ has packet numbering turned on by default */
5490 ov->packet_numbering = 1;
5493 ov518_set_packet_size(ov, 0);
5495 ov->snap_enabled = snapshot;
5498 ov->primary_i2c_slave = OV7xx0_SID;
5499 if (ov51x_set_slave_ids(ov, OV7xx0_SID) < 0)
5502 /* The OV518 must be more aggressive about sensor detection since
5503 * I2C write will never fail if the sensor is not present. We have
5504 * to try to initialize the sensor to detect its presence */
5506 if (init_ov_sensor(ov) < 0) {
5508 ov->primary_i2c_slave = OV6xx0_SID;
5509 if (ov51x_set_slave_ids(ov, OV6xx0_SID) < 0)
5512 if (init_ov_sensor(ov) < 0) {
5514 ov->primary_i2c_slave = OV8xx0_SID;
5515 if (ov51x_set_slave_ids(ov, OV8xx0_SID) < 0)
5518 if (init_ov_sensor(ov) < 0) {
5519 err("Can't determine sensor slave IDs");
5522 err("Detected unsupported OV8xx0 sensor");
5526 if (ov6xx0_configure(ov) < 0) {
5527 err("Failed to configure OV6xx0");
5532 if (ov7xx0_configure(ov) < 0) {
5533 err("Failed to configure OV7xx0");
5539 ov->maxheight = 288;
5541 // The OV518 cannot go as low as the sensor can
5543 ov->minheight = 120;
5548 err("OV518 Config failed");
5553 /****************************************************************************
5555 ***************************************************************************/
5557 static inline struct usb_ov511 *cd_to_ov(struct device *cd)
5559 struct video_device *vdev = to_video_device(cd);
5560 return video_get_drvdata(vdev);
5563 static ssize_t show_custom_id(struct device *cd,
5564 struct device_attribute *attr, char *buf)
5566 struct usb_ov511 *ov = cd_to_ov(cd);
5567 return sprintf(buf, "%d\n", ov->customid);
5569 static DEVICE_ATTR(custom_id, S_IRUGO, show_custom_id, NULL);
5571 static ssize_t show_model(struct device *cd,
5572 struct device_attribute *attr, char *buf)
5574 struct usb_ov511 *ov = cd_to_ov(cd);
5575 return sprintf(buf, "%s\n", ov->desc);
5577 static DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
5579 static ssize_t show_bridge(struct device *cd,
5580 struct device_attribute *attr, char *buf)
5582 struct usb_ov511 *ov = cd_to_ov(cd);
5583 return sprintf(buf, "%s\n", symbolic(brglist, ov->bridge));
5585 static DEVICE_ATTR(bridge, S_IRUGO, show_bridge, NULL);
5587 static ssize_t show_sensor(struct device *cd,
5588 struct device_attribute *attr, char *buf)
5590 struct usb_ov511 *ov = cd_to_ov(cd);
5591 return sprintf(buf, "%s\n", symbolic(senlist, ov->sensor));
5593 static DEVICE_ATTR(sensor, S_IRUGO, show_sensor, NULL);
5595 static ssize_t show_brightness(struct device *cd,
5596 struct device_attribute *attr, char *buf)
5598 struct usb_ov511 *ov = cd_to_ov(cd);
5603 sensor_get_brightness(ov, &x);
5604 return sprintf(buf, "%d\n", x >> 8);
5606 static DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
5608 static ssize_t show_saturation(struct device *cd,
5609 struct device_attribute *attr, char *buf)
5611 struct usb_ov511 *ov = cd_to_ov(cd);
5616 sensor_get_saturation(ov, &x);
5617 return sprintf(buf, "%d\n", x >> 8);
5619 static DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
5621 static ssize_t show_contrast(struct device *cd,
5622 struct device_attribute *attr, char *buf)
5624 struct usb_ov511 *ov = cd_to_ov(cd);
5629 sensor_get_contrast(ov, &x);
5630 return sprintf(buf, "%d\n", x >> 8);
5632 static DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
5634 static ssize_t show_hue(struct device *cd,
5635 struct device_attribute *attr, char *buf)
5637 struct usb_ov511 *ov = cd_to_ov(cd);
5642 sensor_get_hue(ov, &x);
5643 return sprintf(buf, "%d\n", x >> 8);
5645 static DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
5647 static ssize_t show_exposure(struct device *cd,
5648 struct device_attribute *attr, char *buf)
5650 struct usb_ov511 *ov = cd_to_ov(cd);
5651 unsigned char exp = 0;
5655 sensor_get_exposure(ov, &exp);
5656 return sprintf(buf, "%d\n", exp >> 8);
5658 static DEVICE_ATTR(exposure, S_IRUGO, show_exposure, NULL);
5660 static int ov_create_sysfs(struct video_device *vdev)
5664 rc = video_device_create_file(vdev, &dev_attr_custom_id);
5666 rc = video_device_create_file(vdev, &dev_attr_model);
5667 if (rc) goto err_id;
5668 rc = video_device_create_file(vdev, &dev_attr_bridge);
5669 if (rc) goto err_model;
5670 rc = video_device_create_file(vdev, &dev_attr_sensor);
5671 if (rc) goto err_bridge;
5672 rc = video_device_create_file(vdev, &dev_attr_brightness);
5673 if (rc) goto err_sensor;
5674 rc = video_device_create_file(vdev, &dev_attr_saturation);
5675 if (rc) goto err_bright;
5676 rc = video_device_create_file(vdev, &dev_attr_contrast);
5677 if (rc) goto err_sat;
5678 rc = video_device_create_file(vdev, &dev_attr_hue);
5679 if (rc) goto err_contrast;
5680 rc = video_device_create_file(vdev, &dev_attr_exposure);
5681 if (rc) goto err_hue;
5686 video_device_remove_file(vdev, &dev_attr_hue);
5688 video_device_remove_file(vdev, &dev_attr_contrast);
5690 video_device_remove_file(vdev, &dev_attr_saturation);
5692 video_device_remove_file(vdev, &dev_attr_brightness);
5694 video_device_remove_file(vdev, &dev_attr_sensor);
5696 video_device_remove_file(vdev, &dev_attr_bridge);
5698 video_device_remove_file(vdev, &dev_attr_model);
5700 video_device_remove_file(vdev, &dev_attr_custom_id);
5705 /****************************************************************************
5707 ***************************************************************************/
5710 ov51x_probe(struct usb_interface *intf, const struct usb_device_id *id)
5712 struct usb_device *dev = interface_to_usbdev(intf);
5713 struct usb_interface_descriptor *idesc;
5714 struct usb_ov511 *ov;
5717 PDEBUG(1, "probing for device...");
5719 /* We don't handle multi-config cameras */
5720 if (dev->descriptor.bNumConfigurations != 1)
5723 idesc = &intf->cur_altsetting->desc;
5725 if (idesc->bInterfaceClass != 0xFF)
5727 if (idesc->bInterfaceSubClass != 0x00)
5730 if ((ov = kzalloc(sizeof(*ov), GFP_KERNEL)) == NULL) {
5731 err("couldn't kmalloc ov struct");
5736 ov->iface = idesc->bInterfaceNumber;
5737 ov->led_policy = led;
5738 ov->compress = compress;
5739 ov->lightfreq = lightfreq;
5740 ov->num_inputs = 1; /* Video decoder init functs. change this */
5741 ov->stop_during_set = !fastset;
5742 ov->backlight = backlight;
5743 ov->mirror = mirror;
5744 ov->auto_brt = autobright;
5745 ov->auto_gain = autogain;
5746 ov->auto_exp = autoexp;
5748 switch (le16_to_cpu(dev->descriptor.idProduct)) {
5750 ov->bridge = BRG_OV511;
5751 ov->bclass = BCL_OV511;
5753 case PROD_OV511PLUS:
5754 ov->bridge = BRG_OV511PLUS;
5755 ov->bclass = BCL_OV511;
5758 ov->bridge = BRG_OV518;
5759 ov->bclass = BCL_OV518;
5761 case PROD_OV518PLUS:
5762 ov->bridge = BRG_OV518PLUS;
5763 ov->bclass = BCL_OV518;
5766 if (le16_to_cpu(dev->descriptor.idVendor) != VEND_MATTEL)
5768 ov->bridge = BRG_OV511PLUS;
5769 ov->bclass = BCL_OV511;
5772 err("Unknown product ID 0x%04x", le16_to_cpu(dev->descriptor.idProduct));
5776 info("USB %s video device found", symbolic(brglist, ov->bridge));
5778 init_waitqueue_head(&ov->wq);
5780 mutex_init(&ov->lock); /* to 1 == available */
5781 mutex_init(&ov->buf_lock);
5782 mutex_init(&ov->i2c_lock);
5783 mutex_init(&ov->cbuf_lock);
5785 ov->buf_state = BUF_NOT_ALLOCATED;
5787 if (usb_make_path(dev, ov->usb_path, OV511_USB_PATH_LEN) < 0) {
5788 err("usb_make_path error");
5792 /* Allocate control transfer buffer. */
5793 /* Must be kmalloc()'ed, for DMA compatibility */
5794 ov->cbuf = kmalloc(OV511_CBUF_SIZE, GFP_KERNEL);
5798 if (ov->bclass == BCL_OV518) {
5799 if (ov518_configure(ov) < 0)
5802 if (ov511_configure(ov) < 0)
5806 for (i = 0; i < OV511_NUMFRAMES; i++) {
5807 ov->frame[i].framenum = i;
5808 init_waitqueue_head(&ov->frame[i].wq);
5811 for (i = 0; i < OV511_NUMSBUF; i++) {
5812 ov->sbuf[i].ov = ov;
5813 spin_lock_init(&ov->sbuf[i].lock);
5817 /* Unnecessary? (This is done on open(). Need to make sure variables
5818 * are properly initialized without this before removing it, though). */
5819 if (ov51x_set_default_params(ov) < 0)
5824 if (ov->bclass == BCL_OV511)
5825 ov511_dump_regs(ov);
5827 ov518_dump_regs(ov);
5831 ov->vdev = video_device_alloc();
5835 memcpy(ov->vdev, &vdev_template, sizeof(*ov->vdev));
5836 ov->vdev->dev = &intf->dev;
5837 video_set_drvdata(ov->vdev, ov);
5839 for (i = 0; i < OV511_MAX_UNIT_VIDEO; i++) {
5840 /* Minor 0 cannot be specified; assume user wants autodetect */
5841 if (unit_video[i] == 0)
5844 if (video_register_device(ov->vdev, VFL_TYPE_GRABBER,
5845 unit_video[i]) >= 0) {
5850 /* Use the next available one */
5851 if ((ov->vdev->minor == -1) &&
5852 video_register_device(ov->vdev, VFL_TYPE_GRABBER, -1) < 0) {
5853 err("video_register_device failed");
5857 info("Device at %s registered to minor %d", ov->usb_path,
5860 usb_set_intfdata(intf, ov);
5861 if (ov_create_sysfs(ov->vdev)) {
5862 err("ov_create_sysfs failed");
5870 if (-1 == ov->vdev->minor)
5871 video_device_release(ov->vdev);
5873 video_unregister_device(ov->vdev);
5878 mutex_lock(&ov->cbuf_lock);
5881 mutex_unlock(&ov->cbuf_lock);
5888 err("Camera initialization failed");
5893 ov51x_disconnect(struct usb_interface *intf)
5895 struct usb_ov511 *ov = usb_get_intfdata(intf);
5900 usb_set_intfdata (intf, NULL);
5906 video_unregister_device(ov->vdev);
5908 for (n = 0; n < OV511_NUMFRAMES; n++)
5909 ov->frame[n].grabstate = FRAME_ERROR;
5913 /* This will cause the process to request another frame */
5914 for (n = 0; n < OV511_NUMFRAMES; n++)
5915 wake_up_interruptible(&ov->frame[n].wq);
5917 wake_up_interruptible(&ov->wq);
5920 ov51x_unlink_isoc(ov);
5924 /* Free the memory */
5925 if (ov && !ov->user) {
5926 mutex_lock(&ov->cbuf_lock);
5929 mutex_unlock(&ov->cbuf_lock);
5936 PDEBUG(3, "Disconnect complete");
5939 static struct usb_driver ov511_driver = {
5941 .id_table = device_table,
5942 .probe = ov51x_probe,
5943 .disconnect = ov51x_disconnect
5946 /****************************************************************************
5950 ***************************************************************************/
5953 usb_ov511_init(void)
5957 retval = usb_register(&ov511_driver);
5961 info(DRIVER_VERSION " : " DRIVER_DESC);
5968 usb_ov511_exit(void)
5970 usb_deregister(&ov511_driver);
5971 info("driver deregistered");
5975 module_init(usb_ov511_init);
5976 module_exit(usb_ov511_exit);