spca561.c

来自「trident tm5600的linux驱动」· C语言 代码 · 共 1,264 行 · 第 1/3 页

C
1,264
字号
	{0x000a, 0x8801},	/* 0x01 //0x02 */	{0x0001, 0x8805},	{0x0000, 0x8800},	{0x0009, 0x8801},	/* 0x1061 - setexposure times && pixel clock				 * 0001 0 | 000 0110 0001 */	{0x0061, 0x8805},	/* 61 31 */	{0x0008, 0x8800},	/* 08 */	{0x0035, 0x8801},	/* 0x14 - set gain general */	{0x001f, 0x8805},	/* 0x14 */	{0x0000, 0x8800},	{0x000e, 0x8112},	/* white balance - was 30 */	{}};#if 0static void sensor_reset(struct gspca_dev *gspca_dev){	reg_w_val(gspca_dev->dev, 0x8631, 0xc8);	reg_w_val(gspca_dev->dev, 0x8634, 0xc8);	reg_w_val(gspca_dev->dev, 0x8112, 0x00);	reg_w_val(gspca_dev->dev, 0x8114, 0x00);	reg_w_val(gspca_dev->dev, 0x8118, 0x21);	reg_w_val(gspca_dev->dev, 0x8804, 0x92);	/* i2c init */	reg_w_val(gspca_dev->dev, 0x8802, 0x14);	i2c_write(gspca_dev, 0x0001, 0x0d);	i2c_write(gspca_dev, 0x0000, 0x0d);}#endif/******************** QC Express etch2 stuff ********************/static const __u16 Pb100_1map8300[][2] = {	/* reg, value */	{0x8320, 0x3304},	{0x8303, 0x0125},	/* image area */	{0x8304, 0x0169},	{0x8328, 0x000b},	{0x833c, 0x0001},		/*fixme: win:07*/	{0x832f, 0x1904},		/*fixme: was 0419*/	{0x8307, 0x00aa},	{0x8301, 0x0003},	{0x8302, 0x000e},	{}};static const __u16 Pb100_2map8300[][2] = {	/* reg, value */	{0x8339, 0x0000},	{0x8307, 0x00aa},	{}};static const __u16 spca561_161rev12A_data1[][2] = {	{0x29, 0x8118},		/* white balance - was 21 */	{0x08, 0x8114},		/* white balance - was 01 */	{0x0e, 0x8112},		/* white balance - was 00 */	{0x00, 0x8102},		/* white balance - new */	{0x92, 0x8804},	{0x04, 0x8802},		/* windows uses 08 */	{}};static const __u16 spca561_161rev12A_data2[][2] = {	{0x21, 0x8118},	{0x10, 0x8500},	{0x07, 0x8601},	{0x07, 0x8602},	{0x04, 0x8501},	{0x21, 0x8118},	{0x07, 0x8201},		/* windows uses 02 */	{0x08, 0x8200},	{0x01, 0x8200},	{0x00, 0x8114},	{0x01, 0x8114},		/* windows uses 00 */	{0x90, 0x8604},	{0x00, 0x8605},	{0xb0, 0x8603},	/* sensor gains */	{0x07, 0x8601},		/* white balance - new */	{0x07, 0x8602},		/* white balance - new */	{0x00, 0x8610},		/* *red */	{0x00, 0x8611},		/* 3f   *green */	{0x00, 0x8612},		/* green *blue */	{0x00, 0x8613},		/* blue *green */	{0x43, 0x8614},		/* green *red - white balance - was 0x35 */	{0x40, 0x8615},		/* 40   *green - white balance - was 0x35 */	{0x71, 0x8616},		/* 7a   *blue - white balance - was 0x35 */	{0x40, 0x8617},		/* 40   *green - white balance - was 0x35 */	{0x0c, 0x8620},		/* 0c */	{0xc8, 0x8631},		/* c8 */	{0xc8, 0x8634},		/* c8 */	{0x23, 0x8635},		/* 23 */	{0x1f, 0x8636},		/* 1f */	{0xdd, 0x8637},		/* dd */	{0xe1, 0x8638},		/* e1 */	{0x1d, 0x8639},		/* 1d */	{0x21, 0x863a},		/* 21 */	{0xe3, 0x863b},		/* e3 */	{0xdf, 0x863c},		/* df */	{0xf0, 0x8505},	{0x32, 0x850a},/*	{0x99, 0x8700},		 * - white balance - new (removed) */	{}};static void sensor_mapwrite(struct gspca_dev *gspca_dev,			    const __u16 sensormap[][2]){	int i = 0;	__u8 usbval[2];	while (sensormap[i][0]) {		usbval[0] = sensormap[i][1];		usbval[1] = sensormap[i][1] >> 8;		reg_w_buf(gspca_dev, sensormap[i][0], usbval, 2);		i++;	}}static void init_161rev12A(struct gspca_dev *gspca_dev){/*	sensor_reset(gspca_dev);	(not in win) */	write_vector(gspca_dev, spca561_161rev12A_data1);	sensor_mapwrite(gspca_dev, Pb100_1map8300);/*fixme: should be in sd_start*/	write_vector(gspca_dev, spca561_161rev12A_data2);	sensor_mapwrite(gspca_dev, Pb100_2map8300);}/* this function is called at probe time */static int sd_config(struct gspca_dev *gspca_dev,		     const struct usb_device_id *id){	struct sd *sd = (struct sd *) gspca_dev;	struct cam *cam;	__u16 vendor, product;	__u8 data1, data2;	/* Read frm global register the USB product and vendor IDs, just to	 * prove that we can communicate with the device.  This works, which	 * confirms at we are communicating properly and that the device	 * is a 561. */	reg_r(gspca_dev, 0x8104, 1);	data1 = gspca_dev->usb_buf[0];	reg_r(gspca_dev, 0x8105, 1);	data2 = gspca_dev->usb_buf[0];	vendor = (data2 << 8) | data1;	reg_r(gspca_dev, 0x8106, 1);	data1 = gspca_dev->usb_buf[0];	reg_r(gspca_dev, 0x8107, 1);	data2 = gspca_dev->usb_buf[0];	product = (data2 << 8) | data1;	if (vendor != id->idVendor || product != id->idProduct) {		PDEBUG(D_PROBE, "Bad vendor / product from device");		return -EINVAL;	}	cam = &gspca_dev->cam;	cam->epaddr = 0x01;	gspca_dev->nbalt = 7 + 1;	/* choose alternate 7 first */	sd->chip_revision = id->driver_info;	if (sd->chip_revision == Rev012A) {		cam->cam_mode = sif_012a_mode;		cam->nmodes = ARRAY_SIZE(sif_012a_mode);	} else {		cam->cam_mode = sif_072a_mode;		cam->nmodes = ARRAY_SIZE(sif_072a_mode);	}	sd->brightness = BRIGHTNESS_DEF;	sd->contrast = CONTRAST_DEF;	sd->white = WHITE_DEF;	sd->exposure = EXPOSURE_DEF;	sd->autogain = AUTOGAIN_DEF;	sd->gain = GAIN_DEF;	sd->expo12a = EXPO12A_DEF;	return 0;}/* this function is called at probe and resume time */static int sd_init_12a(struct gspca_dev *gspca_dev){	PDEBUG(D_STREAM, "Chip revision: 012a");	init_161rev12A(gspca_dev);	return 0;}static int sd_init_72a(struct gspca_dev *gspca_dev){	PDEBUG(D_STREAM, "Chip revision: 072a");	write_vector(gspca_dev, spca561_init_data);	return 0;}static void setcontrast(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	struct usb_device *dev = gspca_dev->dev;	__u8 lowb;	switch (sd->chip_revision) {	case Rev072A:		lowb = sd->contrast >> 8;		reg_w_val(dev, 0x8651, lowb);		reg_w_val(dev, 0x8652, lowb);		reg_w_val(dev, 0x8653, lowb);		reg_w_val(dev, 0x8654, lowb);		break;	default: {/*	case Rev012A: { */		static const __u8 Reg8391[] =			{ 0x92, 0x30, 0x20, 0x00, 0x0c, 0x00, 0x00, 0x00 };		reg_w_buf(gspca_dev, 0x8391, Reg8391, 8);		reg_w_buf(gspca_dev, 0x8390, Reg8391, 8);		break;	    }	}}/* rev12a only */static void setwhite(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	__u16 white;	__u8 reg8614, reg8616;	white = sd->white;	/* try to emulate MS-win as possible */	reg8616 = 0x90 - white * 5 / 8;	reg_w_val(gspca_dev->dev, 0x8616, reg8616);	reg8614 = 0x20 + white * 3 / 8;	reg_w_val(gspca_dev->dev, 0x8614, reg8614);}/* rev 12a only */static void setexposure(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	int expo;	int clock_divider;	__u8 data[2];	/* Register 0x8309 controls exposure for the spca561,	   the basic exposure setting goes from 1-2047, where 1 is completely	   dark and 2047 is very bright. It not only influences exposure but	   also the framerate (to allow for longer exposure) from 1 - 300 it	   only raises the exposure time then from 300 - 600 it halves the	   framerate to be able to further raise the exposure time and for every	   300 more it halves the framerate again. This allows for a maximum	   exposure time of circa 0.2 - 0.25 seconds (30 / (2000/3000) fps).	   Sometimes this is not enough, the 1-2047 uses bits 0-10, bits 11-12	   configure a divider for the base framerate which us used at the	   exposure setting of 1-300. These bits configure the base framerate	   according to the following formula: fps = 60 / (value + 2) */	if (sd->exposure < 2048) {		expo = sd->exposure;		clock_divider = 0;	} else {		/* Add 900 to make the 0 setting of the second part of the		   exposure equal to the 2047 setting of the first part. */		expo = (sd->exposure - 2048) + 900;		clock_divider = 3;	}	expo |= clock_divider << 11;	data[0] = expo;	data[1] = expo >> 8;	reg_w_buf(gspca_dev, 0x8309, data, 2);}/* rev 12a only */static void setgain(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	__u8 data[2];	data[0] = sd->gain;	data[1] = 0;	reg_w_buf(gspca_dev, 0x8335, data, 2);}static void setautogain(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	if (sd->autogain)		sd->ag_cnt = AG_CNT_START;	else		sd->ag_cnt = -1;}static int sd_start_12a(struct gspca_dev *gspca_dev){	struct usb_device *dev = gspca_dev->dev;	int Clck = 0x8a; /* lower 0x8X values lead to fps > 30 */	__u8 Reg8307[] = { 0xaa, 0x00 };	int mode;	mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;	if (mode <= 1) {		/* Use compression on 320x240 and above */		reg_w_val(dev, 0x8500, 0x10 | mode);	} else {		/* I couldn't get the compression to work below 320x240		 * Fortunately at these resolutions the bandwidth		 * is sufficient to push raw frames at ~20fps */		reg_w_val(dev, 0x8500, mode);	}		/* -- qq@kuku.eu.org */	reg_w_buf(gspca_dev, 0x8307, Reg8307, 2);	reg_w_val(gspca_dev->dev, 0x8700, Clck);					/* 0x8f 0x85 0x27 clock */	reg_w_val(gspca_dev->dev, 0x8112, 0x1e | 0x20);	reg_w_val(gspca_dev->dev, 0x850b, 0x03);	setcontrast(gspca_dev);	setwhite(gspca_dev);	setautogain(gspca_dev);	setexposure(gspca_dev);	return 0;}static int sd_start_72a(struct gspca_dev *gspca_dev){	struct usb_device *dev = gspca_dev->dev;	int Clck;	int mode;	mode = gspca_dev->cam.cam_mode[(int) gspca_dev->curr_mode].priv;	switch (mode) {	default:/*	case 0:	case 1: */		Clck = 0x25;		break;	case 2:		Clck = 0x22;		break;	case 3:		Clck = 0x21;		break;	}	reg_w_val(dev, 0x8500, mode);	/* mode */	reg_w_val(dev, 0x8700, Clck);	/* 0x27 clock */	reg_w_val(dev, 0x8112, 0x10 | 0x20);	setautogain(gspca_dev);	return 0;}static void sd_stopN(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	if (sd->chip_revision == Rev012A) {		reg_w_val(gspca_dev->dev, 0x8112, 0x0e);	} else {		reg_w_val(gspca_dev->dev, 0x8112, 0x20);/*		reg_w_val(gspca_dev->dev, 0x8102, 0x00); ?? */	}}static void sd_stop0(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	if (sd->chip_revision == Rev012A) {		reg_w_val(gspca_dev->dev, 0x8118, 0x29);		reg_w_val(gspca_dev->dev, 0x8114, 0x08);	}/*	reg_w_val(gspca_dev->dev, 0x8114, 0); */}static void do_autogain(struct gspca_dev *gspca_dev){	struct sd *sd = (struct sd *) gspca_dev;	int expotimes;	int pixelclk;	int gainG;	__u8 R, Gr, Gb, B;	int y;	__u8 luma_mean = 110;	__u8 luma_delta = 20;	__u8 spring = 4;	__u8 reg8339[2];	if (sd->ag_cnt < 0)		return;	if (--sd->ag_cnt >= 0)		return;	sd->ag_cnt = AG_CNT_START;	switch (sd->chip_revision) {	case Rev072A:		reg_r(gspca_dev, 0x8621, 1);		Gr = gspca_dev->usb_buf[0];		reg_r(gspca_dev, 0x8622, 1);		R = gspca_dev->usb_buf[0];		reg_r(gspca_dev, 0x8623, 1);		B = gspca_dev->usb_buf[0];		reg_r(gspca_dev, 0x8624, 1);		Gb = gspca_dev->usb_buf[0];		y = (77 * R + 75 * (Gr + Gb) + 29 * B) >> 8;		/* u= (128*B-(43*(Gr+Gb+R))) >> 8; */		/* v= (128*R-(53*(Gr+Gb))-21*B) >> 8; */		/* PDEBUG(D_CONF,"reading Y %d U %d V %d ",y,u,v); */		if (y < luma_mean - luma_delta ||		    y > luma_mean + luma_delta) {			expotimes = i2c_read(gspca_dev, 0x09, 0x10);			pixelclk = 0x0800;			expotimes = expotimes & 0x07ff;			/* PDEBUG(D_PACK,				"Exposition Times 0x%03X Clock 0x%04X ",				expotimes,pixelclk); */			gainG = i2c_read(gspca_dev, 0x35, 0x10);			/* PDEBUG(D_PACK,				"reading Gain register %d", gainG); */			expotimes += (luma_mean - y) >> spring;			gainG += (luma_mean - y) / 50;			/* PDEBUG(D_PACK,				"compute expotimes %d gain %d",				expotimes,gainG); */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?