Reg Use sn9c101 / sn9c102: 0x10 high nibble red gain low nibble blue gain 0x11 low nibble green gain sn9c103: 0x05 red gain 0-127 0x06 blue gain 0-127 0x07 green gain 0-127 all: 0x08-0x0f i2c / 3wire registers 0x12 hstart 0x13 vstart 0x15 hsize (hsize = register-value * 16) 0x16 vsize (vsize = register-value * 16) 0x17 bit 0 toggle compression quality (according to sn9c102 driver) 0x18 bit 7 enables compression, bit 4-5 set image down scaling: 00 scale 1, 01 scale 1/2, 10, scale 1/4 0x19 high-nibble is sensor clock divider, changes exposure on sensors which use a clock generated by the bridge. Some sensors have their own clock. 0x1c auto_exposure area (for avg_lum) startx (startx = register-value * 32) 0x1d auto_exposure area (for avg_lum) starty (starty = register-value * 32) 0x1e auto_exposure area (for avg_lum) stopx (hsize = (0x1e - 0x1c) * 32) 0x1f auto_exposure area (for avg_lum) stopy (vsize = (0x1f - 0x1d) * 32)
*/
#define MODULE_NAME "sonixb"
#include <linux/input.h> #include"gspca.h"
MODULE_AUTHOR("Jean-François Moine <http://moinejf.free.fr>");
MODULE_DESCRIPTION("GSPCA/SN9C102 USB Camera Driver");
MODULE_LICENSE("GPL");
/* specific webcam descriptor */ struct sd { struct gspca_dev gspca_dev; /* !! must be the first item */
/* priv field of struct v4l2_pix_format flags (do not use low nibble!) */ #define MODE_RAW 0x10 /* raw bayer mode */ #define MODE_REDUCED_SIF 0x20 /* vga mode (320x240 / 160x120) on sif cam */
/* We calculate the autogain at the end of the transfer of a frame, at this moment a frame with the old settings is being captured and transmitted. So if we adjust the gain or exposure we must ignore at least the next frame for
the new settings to come into effect before doing any other adjustments. */ #define AUTOGAIN_IGNORE_FRAMES 1
/* Reset sensor */
{0xa0, 0x60, 0x12, 0x80, 0x00, 0x00, 0x00, 0x10}, /* Set clock register 0x11 low nibble is clock divider */
{0xd0, 0x60, 0x11, 0xc0, 0x1b, 0x18, 0xc1, 0x10}, /* Next some unknown stuff */
{0xb0, 0x60, 0x15, 0x00, 0x02, 0x18, 0xc1, 0x10}, /* {0xa0, 0x60, 0x1b, 0x01, 0x02, 0x18, 0xc1, 0x10}, * THIS SET GREEN SCREEN * (pixels could be innverted in decode kind of "brg",
* but blue wont be there. Avoid this data ... */
{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10}, /* format out? */
{0xd0, 0x60, 0x26, 0x01, 0x14, 0xd8, 0xa4, 0x10},
{0xa0, 0x60, 0x30, 0x3d, 0x0a, 0xd8, 0xa4, 0x10}, /* Enable rgb brightness control */
{0xa0, 0x60, 0x61, 0x08, 0x00, 0x00, 0x00, 0x10}, /* HDG: Note windows uses the line below, which sets both register 0x60 and 0x61 I believe these registers of the ov6650 are identical as those of the ov7630, because if this is true the windows settings add a bit additional red gain and a lot additional blue gain, which matches my findings that the windows settings make blue much too blue and red a little too red.
{0xb0, 0x60, 0x60, 0x66, 0x68, 0xd8, 0xa4, 0x10}, */ /* Some more unknown stuff */
{0xa0, 0x60, 0x68, 0x04, 0x68, 0xd8, 0xa4, 0x10},
{0xd0, 0x60, 0x17, 0x24, 0xd6, 0x04, 0x94, 0x10}, /* Clipreg */
};
/* "Known" PAS106B registers: 0x02 clock divider 0x03 Variable framerate bits 4-11 0x04 Var framerate bits 0-3, one must leave the 4 msb's at 0 !! The variable framerate control must never be set lower then 300, which sets the framerate at 90 / reg02, otherwise vsync is lost. 0x05 Shutter Time Line Offset, this can be used as an exposure control: 0 = use full frame time, 255 = no exposure at all Note this may never be larger then "var-framerate control" / 2 - 2. When var-framerate control is < 514, no exposure is reached at the max allowed value for the framerate control value, rather then at 255. 0x06 Shutter Time Pixel Offset, like reg05 this influences exposure, but only a very little bit, leave at 0xcd 0x07 offset sign bit (bit0 1 > negative offset) 0x08 offset 0x09 Blue Gain 0x0a Green1 Gain 0x0b Green2 Gain 0x0c Red Gain 0x0e Global gain 0x13 Write 1 to commit settings to sensor
*/
/* "Known" PAS202BCB registers: 0x02 clock divider 0x04 Variable framerate bits 6-11 (*) 0x05 Var framerate bits 0-5, one must leave the 2 msb's at 0 !! 0x07 Blue Gain 0x08 Green Gain 0x09 Red Gain 0x0b offset sign bit (bit0 1 > negative offset) 0x0c offset 0x0e Unknown image is slightly brighter when bit 0 is 0, if reg0f is 0 too, leave at 1 otherwise we get a jump in our exposure control 0x0f Exposure 0-255, 0 = use full frame time, 255 = no exposure at all 0x10 Master gain 0 - 31 0x11 write 1 to apply changes (*) The variable framerate control must never be set lower then 500 which sets the framerate at 30 / reg02, otherwise vsync is lost.
*/ staticconst __u8 pas202_sensor_init[][8] = { /* Set the clock divider to 4 -> 30 / 4 = 7.5 fps, we would like to set it lower, but for some reason the bridge starts missing
vsync's then */
{0xa0, 0x40, 0x02, 0x04, 0x00, 0x00, 0x00, 0x10},
{0xd0, 0x40, 0x04, 0x07, 0x34, 0x00, 0x09, 0x10},
{0xd0, 0x40, 0x08, 0x01, 0x00, 0x00, 0x01, 0x10},
{0xd0, 0x40, 0x0c, 0x00, 0x0c, 0x01, 0x32, 0x10},
{0xd0, 0x40, 0x10, 0x00, 0x01, 0x00, 0x63, 0x10},
{0xa0, 0x40, 0x15, 0x70, 0x01, 0x00, 0x63, 0x10},
{0xa0, 0x40, 0x18, 0x00, 0x01, 0x00, 0x63, 0x10},
{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
{0xa0, 0x40, 0x03, 0x56, 0x01, 0x00, 0x63, 0x10},
{0xa0, 0x40, 0x11, 0x01, 0x01, 0x00, 0x63, 0x10},
};
staticconst __u8 initTas5110c[] = { 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x45, 0x09, 0x0a, 0x16, 0x12, 0x60, 0x86, 0x2b,
}; /* Same as above, except a different hstart */ staticconst __u8 initTas5110d[] = { 0x44, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x09, 0x0a, 0x16, 0x12, 0x60, 0x86, 0x2b,
}; /* tas5110c is 3 wire, tas5110d is 2 wire (regular i2c) */ staticconst __u8 tas5110c_sensor_init[][8] = {
{0x30, 0x11, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x10},
{0x30, 0x11, 0x02, 0x20, 0xa9, 0x00, 0x00, 0x10},
}; /* Known TAS5110D registers * reg02: gain, bit order reversed!! 0 == max gain, 255 == min gain * reg03: bit3: vflip, bit4: ~hflip, bit7: ~gainboost (~ == inverted) * Note: writing reg03 seems to only work when written together with 02
*/ staticconst __u8 tas5110d_sensor_init[][8] = {
{0xa0, 0x61, 0x9a, 0xca, 0x00, 0x00, 0x00, 0x17}, /* reset */
};
/* * The ov7630's gain is weird, at 32 the gain drops to the * same level as at 16, so skip 32-47 (of the 0-63 scale).
*/ if (sd->sensor == SENSOR_OV7630 && gain >= 32)
gain += 16;
switch (sd->sensor) { case SENSOR_HV7131D: { /* Note the datasheet wrongly says line mode exposure uses reg
0x26 and 0x27, testing has shown 0x25 + 0x26 */
__u8 i2c[] = {0xc0, 0x11, 0x25, 0x00, 0x00, 0x00, 0x00, 0x17};
u16 reg = gspca_dev->exposure->val;
i2c[3] = reg >> 8;
i2c[4] = reg & 0xff;
i2c_w(gspca_dev, i2c); break;
} case SENSOR_TAS5110C: case SENSOR_TAS5110D: { /* register 19's high nibble contains the sn9c10x clock divider The high nibble configures the no fps according to the
formula: 60 / high_nibble. With a maximum of 30 fps */
u8 reg = gspca_dev->exposure->val;
reg = (reg << 4) | 0x0b;
reg_w(gspca_dev, 0x19, ®, 1); break;
} case SENSOR_OV6650: case SENSOR_OV7630: { /* The ov6650 / ov7630 have 2 registers which both influence exposure, register 11, whose low nibble sets the nr off fps according to: fps = 30 / (low_nibble + 1)
The fps configures the maximum exposure setting, but it is possible to use less exposure then what the fps maximum allows by setting register 10. register 10 configures the actual exposure as quotient of the full exposure, with 0 being no exposure at all (not very useful) and reg10_max being max exposure possible at that framerate.
The code maps our 0 - 510 ms exposure ctrl to these 2 registers, trying to keep fps as high as possible.
*/
__u8 i2c[] = {0xb0, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x10}; int reg10, reg11, reg10_max;
/* ov6645 datasheet says reg10_max is 9a, but that uses tline * 2 * reg10 as formula for calculating texpo, the ov6650 probably uses the same formula as the 7730 which uses tline * 4 * reg10, which explains why the reg10max we've found experimentally for the ov6650 is exactly half that of
the ov6645. The ov7630 datasheet says the max is 0x41. */ if (sd->sensor == SENSOR_OV6650) {
reg10_max = 0x4d;
i2c[4] = 0xc0; /* OV6650 needs non default vsync pol */
} else
reg10_max = 0x41;
/* In 640x480, if the reg11 has less than 4, the image is unstable (the bridge goes into a higher compression mode
which we have not reverse engineered yet). */ if (gspca_dev->pixfmt.width == 640 && reg11 < 4)
reg11 = 4;
/* Don't allow this to get below 10 when using autogain, the steps become very large (relatively) when below 10 causing the image to oscillate from much too dark, to much too bright
and back again. */ if (gspca_dev->autogain->val && reg10 < 10)
reg10 = 10; elseif (reg10 > reg10_max)
reg10 = reg10_max;
/* The exposure knee for the autogain algorithm is 200 (100 ms / 10 fps on other sensors), for values below this use the control for setting the partial frame expose time, above that use variable framerate. This way we run at max framerate (640x480@7.5 fps, 320x240@10fps) until the knee is reached. Using the variable framerate control above 200 is better then playing around with both clockdiv + partial frame exposure times (like we are doing with the ov chips), as that sometimes leads to jumps in the exposure control,
which are bad for auto exposure. */ if (gspca_dev->exposure->val < 200) {
i2cpexpo[3] = 255 - (gspca_dev->exposure->val * 255)
/ 200;
framerate_ctrl = 500;
} else { /* The PAS202's exposure control goes from 0 - 4095, but anything below 500 causes vsync issues, so scale
our 200-1023 to 500-4095 */
framerate_ctrl = (gspca_dev->exposure->val - 200)
* 1000 / 229 + 500;
}
avg_lum = atomic_read(&sd->avg_lum); if (avg_lum == -1) return;
if (sd->autogain_ignore_frames > 0) {
sd->autogain_ignore_frames--; return;
}
/* SIF / VGA sensors have a different autoexposure area and thus
different avg_lum values for the same picture brightness */ if (sensor_data[sd->sensor].flags & F_SIF) {
deadzone = 500; /* SIF sensors tend to overexpose, so keep this small */
desired_avg_lum = 5000;
} else {
deadzone = 1500;
desired_avg_lum = 13000;
}
if (sd->brightness)
desired_avg_lum = sd->brightness->val * desired_avg_lum / 127;
if (gspca_dev->exposure->maximum < 500) { if (gspca_coarse_grained_expo_autogain(gspca_dev, avg_lum,
desired_avg_lum, deadzone))
sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
} else { int gain_knee = (s32)gspca_dev->gain->maximum * 9 / 10; if (gspca_expo_autogain(gspca_dev, avg_lum, desired_avg_lum,
deadzone, gain_knee, sd->exposure_knee))
sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
}
}
/* this function is called at probe time */ staticint sd_config(struct gspca_dev *gspca_dev, conststruct usb_device_id *id)
{ struct sd *sd = (struct sd *) gspca_dev; struct cam *cam;
reg_r(gspca_dev, 0x00); if (gspca_dev->usb_buf[0] != 0x10) return -ENODEV;
/* copy the webcam info from the device id */
sd->sensor = id->driver_info >> 8;
sd->bridge = id->driver_info & 0xff;
/* this function is called at probe and resume time */ staticint sd_init(struct gspca_dev *gspca_dev)
{ const __u8 stop = 0x09; /* Disable stream turn of LED */
if (ctrl->id == V4L2_CID_AUTOGAIN && ctrl->is_new && ctrl->val) { /* when switching to autogain set defaults to make sure we are on a valid point of the autogain gain / exposure knee graph, and give this change time to
take effect before doing autogain. */
gspca_dev->gain->val = gspca_dev->gain->default_value;
gspca_dev->exposure->val = gspca_dev->exposure->default_value;
sd->autogain_ignore_frames = AUTOGAIN_IGNORE_FRAMES;
}
if (!gspca_dev->streaming) return0;
switch (ctrl->id) { case V4L2_CID_BRIGHTNESS:
setbrightness(gspca_dev); break; case V4L2_CID_AUTOGAIN: if (gspca_dev->exposure->is_new || (ctrl->is_new && ctrl->val))
setexposure(gspca_dev); if (gspca_dev->gain->is_new || (ctrl->is_new && ctrl->val))
setgain(gspca_dev); break; case V4L2_CID_POWER_LINE_FREQUENCY:
setfreq(gspca_dev); break; default: return -EINVAL;
} return gspca_dev->usb_err;
}
/* this function is called at probe time */ staticint sd_init_controls(struct gspca_dev *gspca_dev)
{ struct sd *sd = (struct sd *) gspca_dev; struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
if (hdl->error) {
pr_err("Could not initialize controls\n"); return hdl->error;
}
if (gspca_dev->autogain)
v4l2_ctrl_auto_cluster(3, &gspca_dev->autogain, 0, false);
return0;
}
/* -- start the camera -- */ staticint sd_start(struct gspca_dev *gspca_dev)
{ struct sd *sd = (struct sd *) gspca_dev; struct cam *cam = &gspca_dev->cam; int i, mode;
__u8 regs[0x31];
mode = cam->cam_mode[gspca_dev->curr_mode].priv & 0x07; /* Copy registers 0x01 - 0x19 from the template */
memcpy(®s[0x01], sensor_data[sd->sensor].bridge_init, 0x19); /* Set the mode */
regs[0x18] |= mode << 4;
/* Set bridge gain to 1.0 */ if (sd->bridge == BRIDGE_103) {
regs[0x05] = 0x20; /* Red */
regs[0x06] = 0x20; /* Green */
regs[0x07] = 0x20; /* Blue */
} else {
regs[0x10] = 0x00; /* Red and blue */
regs[0x11] = 0x00; /* Green */
}
/* Setup pixel numbers and auto exposure window */ if (sensor_data[sd->sensor].flags & F_SIF) {
regs[0x1a] = 0x14; /* HO_SIZE 640, makes no sense */
regs[0x1b] = 0x0a; /* VO_SIZE 320, makes no sense */
regs[0x1c] = 0x02; /* AE H-start 64 */
regs[0x1d] = 0x02; /* AE V-start 64 */
regs[0x1e] = 0x09; /* AE H-end 288 */
regs[0x1f] = 0x07; /* AE V-end 224 */
} else {
regs[0x1a] = 0x1d; /* HO_SIZE 960, makes no sense */
regs[0x1b] = 0x10; /* VO_SIZE 512, makes no sense */
regs[0x1c] = 0x05; /* AE H-start 160 */
regs[0x1d] = 0x03; /* AE V-start 96 */
regs[0x1e] = 0x0f; /* AE H-end 480 */
regs[0x1f] = 0x0c; /* AE V-end 384 */
}
/* Setup the gamma table (only used with the sn9c103 bridge) */ for (i = 0; i < 16; i++)
regs[0x20 + i] = i * 16;
regs[0x20 + i] = 255;
/* Special cases where some regs depend on mode or bridge */ switch (sd->sensor) { case SENSOR_TAS5130CXX: /* FIXME / TESTME probably not mode specific at all most likely the upper nibble of 0x19 is exposure (clock divider) just as with
the tas5110, we need someone to test this. */
regs[0x19] = mode ? 0x23 : 0x43; break; case SENSOR_OV7630: /* FIXME / TESTME for some reason with the 101/102 bridge the clock is set to 12 Mhz (reg1 == 0x04), rather then 24. Also the hstart needs to go from 1 to 2 when using a 103,
which is likely related. This does not seem right. */ if (sd->bridge == BRIDGE_103) {
regs[0x01] = 0x44; /* Select 24 Mhz clock */
regs[0x12] = 0x02; /* Set hstart to 2 */
} break; case SENSOR_PAS202: /* For some unknown reason we need to increase hstart by 1 on
the sn9c103, otherwise we get wrong colors (bayer shift). */ if (sd->bridge == BRIDGE_103)
regs[0x12] += 1; break;
} /* Disable compression when the raw bayer format has been selected */ if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW)
regs[0x18] &= ~0x80;
sof = find_sof(gspca_dev, data, len); if (sof) { if (sd->bridge == BRIDGE_103) {
fr_h_sz = 18;
lum_offset = 3;
} else {
fr_h_sz = 12;
lum_offset = 2;
}
len_after_sof = len - (sof - data);
len = (sof - data) - fr_h_sz; if (len < 0)
len = 0;
}
if (cam->cam_mode[gspca_dev->curr_mode].priv & MODE_RAW) { /* In raw mode we sometimes get some garbage after the frame
ignore this */ int used; int size = cam->cam_mode[gspca_dev->curr_mode].sizeimage;
used = gspca_dev->image_len; if (used + len > size)
len = size - used;
}
if (sof) { int lum = sd->header[lum_offset] +
(sd->header[lum_offset + 1] << 8);
/* When exposure changes midway a frame we get a lum of 0 in this case drop 2 frames as the frames directly after an exposure change have an unstable image. Sometimes lum *really* is 0 (cam used in low light with low exposure setting), so do not drop frames
if the previous lum was 0 too. */ if (lum == 0 && sd->prev_avg_lum != 0) {
lum = -1;
sd->frames_to_drop = 2;
sd->prev_avg_lum = 0;
} else
sd->prev_avg_lum = lum;
atomic_set(&sd->avg_lum, lum);
if (sd->frames_to_drop)
sd->frames_to_drop--; else
gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.10Bemerkung:
(vorverarbeitet am 2026-06-07)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.