xref: /linux/drivers/media/usb/gspca/gl860/gl860.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1a112cc39SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
20c0d06caSMauro Carvalho Chehab /* GSPCA subdrivers for Genesys Logic webcams with the GL860 chip
30c0d06caSMauro Carvalho Chehab  * Subdriver core
40c0d06caSMauro Carvalho Chehab  *
50c0d06caSMauro Carvalho Chehab  * 2009/09/24 Olivier Lorin <o.lorin@laposte.net>
60c0d06caSMauro Carvalho Chehab  * GSPCA by Jean-Francois Moine <http://moinejf.free.fr>
70c0d06caSMauro Carvalho Chehab  * Thanks BUGabundo and Malmostoso for your amazing help!
80c0d06caSMauro Carvalho Chehab  */
90c0d06caSMauro Carvalho Chehab 
100c0d06caSMauro Carvalho Chehab #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
110c0d06caSMauro Carvalho Chehab 
120c0d06caSMauro Carvalho Chehab #include "gspca.h"
130c0d06caSMauro Carvalho Chehab #include "gl860.h"
140c0d06caSMauro Carvalho Chehab 
150c0d06caSMauro Carvalho Chehab MODULE_AUTHOR("Olivier Lorin <o.lorin@laposte.net>");
160c0d06caSMauro Carvalho Chehab MODULE_DESCRIPTION("Genesys Logic USB PC Camera Driver");
170c0d06caSMauro Carvalho Chehab MODULE_LICENSE("GPL");
180c0d06caSMauro Carvalho Chehab 
190c0d06caSMauro Carvalho Chehab /*======================== static function declarations ====================*/
200c0d06caSMauro Carvalho Chehab 
210c0d06caSMauro Carvalho Chehab static void (*dev_init_settings)(struct gspca_dev *gspca_dev);
220c0d06caSMauro Carvalho Chehab 
230c0d06caSMauro Carvalho Chehab static int  sd_config(struct gspca_dev *gspca_dev,
240c0d06caSMauro Carvalho Chehab 			const struct usb_device_id *id);
250c0d06caSMauro Carvalho Chehab static int  sd_init(struct gspca_dev *gspca_dev);
260c0d06caSMauro Carvalho Chehab static int  sd_isoc_init(struct gspca_dev *gspca_dev);
270c0d06caSMauro Carvalho Chehab static int  sd_start(struct gspca_dev *gspca_dev);
280c0d06caSMauro Carvalho Chehab static void sd_stop0(struct gspca_dev *gspca_dev);
290c0d06caSMauro Carvalho Chehab static void sd_pkt_scan(struct gspca_dev *gspca_dev,
300c0d06caSMauro Carvalho Chehab 			u8 *data, int len);
310c0d06caSMauro Carvalho Chehab static void sd_callback(struct gspca_dev *gspca_dev);
320c0d06caSMauro Carvalho Chehab 
330c0d06caSMauro Carvalho Chehab static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
340c0d06caSMauro Carvalho Chehab 				u16 vendor_id, u16 product_id);
350c0d06caSMauro Carvalho Chehab 
360c0d06caSMauro Carvalho Chehab /*============================ driver options ==============================*/
370c0d06caSMauro Carvalho Chehab 
380c0d06caSMauro Carvalho Chehab static s32 AC50Hz = 0xff;
390c0d06caSMauro Carvalho Chehab module_param(AC50Hz, int, 0644);
400c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(AC50Hz, " Does AC power frequency is 50Hz? (0/1)");
410c0d06caSMauro Carvalho Chehab 
420c0d06caSMauro Carvalho Chehab static char sensor[7];
430c0d06caSMauro Carvalho Chehab module_param_string(sensor, sensor, sizeof(sensor), 0644);
440c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(sensor,
450c0d06caSMauro Carvalho Chehab 		" Driver sensor ('MI1320'/'MI2020'/'OV9655'/'OV2640')");
460c0d06caSMauro Carvalho Chehab 
470c0d06caSMauro Carvalho Chehab /*============================ webcam controls =============================*/
480c0d06caSMauro Carvalho Chehab 
sd_s_ctrl(struct v4l2_ctrl * ctrl)49676fdd85SHans de Goede static int sd_s_ctrl(struct v4l2_ctrl *ctrl)
50676fdd85SHans de Goede {
51676fdd85SHans de Goede 	struct gspca_dev *gspca_dev =
52676fdd85SHans de Goede 		container_of(ctrl->handler, struct gspca_dev, ctrl_handler);
53676fdd85SHans de Goede 	struct sd *sd = (struct sd *) gspca_dev;
54676fdd85SHans de Goede 
55676fdd85SHans de Goede 	switch (ctrl->id) {
56676fdd85SHans de Goede 	case V4L2_CID_BRIGHTNESS:
57676fdd85SHans de Goede 		sd->vcur.brightness = ctrl->val;
58676fdd85SHans de Goede 		break;
59676fdd85SHans de Goede 	case V4L2_CID_CONTRAST:
60676fdd85SHans de Goede 		sd->vcur.contrast = ctrl->val;
61676fdd85SHans de Goede 		break;
62676fdd85SHans de Goede 	case V4L2_CID_SATURATION:
63676fdd85SHans de Goede 		sd->vcur.saturation = ctrl->val;
64676fdd85SHans de Goede 		break;
65676fdd85SHans de Goede 	case V4L2_CID_HUE:
66676fdd85SHans de Goede 		sd->vcur.hue = ctrl->val;
67676fdd85SHans de Goede 		break;
68676fdd85SHans de Goede 	case V4L2_CID_GAMMA:
69676fdd85SHans de Goede 		sd->vcur.gamma = ctrl->val;
70676fdd85SHans de Goede 		break;
71676fdd85SHans de Goede 	case V4L2_CID_HFLIP:
72676fdd85SHans de Goede 		sd->vcur.mirror = ctrl->val;
73676fdd85SHans de Goede 		break;
74676fdd85SHans de Goede 	case V4L2_CID_VFLIP:
75676fdd85SHans de Goede 		sd->vcur.flip = ctrl->val;
76676fdd85SHans de Goede 		break;
77676fdd85SHans de Goede 	case V4L2_CID_POWER_LINE_FREQUENCY:
78676fdd85SHans de Goede 		sd->vcur.AC50Hz = ctrl->val;
79676fdd85SHans de Goede 		break;
80676fdd85SHans de Goede 	case V4L2_CID_WHITE_BALANCE_TEMPERATURE:
81676fdd85SHans de Goede 		sd->vcur.whitebal = ctrl->val;
82676fdd85SHans de Goede 		break;
83676fdd85SHans de Goede 	case V4L2_CID_SHARPNESS:
84676fdd85SHans de Goede 		sd->vcur.sharpness = ctrl->val;
85676fdd85SHans de Goede 		break;
86676fdd85SHans de Goede 	case V4L2_CID_BACKLIGHT_COMPENSATION:
87676fdd85SHans de Goede 		sd->vcur.backlight = ctrl->val;
88676fdd85SHans de Goede 		break;
89676fdd85SHans de Goede 	default:
90676fdd85SHans de Goede 		return -EINVAL;
910c0d06caSMauro Carvalho Chehab 	}
920c0d06caSMauro Carvalho Chehab 
93676fdd85SHans de Goede 	if (gspca_dev->streaming)
94676fdd85SHans de Goede 		sd->waitSet = 1;
950c0d06caSMauro Carvalho Chehab 
96676fdd85SHans de Goede 	return 0;
970c0d06caSMauro Carvalho Chehab }
980c0d06caSMauro Carvalho Chehab 
99676fdd85SHans de Goede static const struct v4l2_ctrl_ops sd_ctrl_ops = {
100676fdd85SHans de Goede 	.s_ctrl = sd_s_ctrl,
101676fdd85SHans de Goede };
102676fdd85SHans de Goede 
sd_init_controls(struct gspca_dev * gspca_dev)103676fdd85SHans de Goede static int sd_init_controls(struct gspca_dev *gspca_dev)
1040c0d06caSMauro Carvalho Chehab {
1050c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
106676fdd85SHans de Goede 	struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
1070c0d06caSMauro Carvalho Chehab 
108676fdd85SHans de Goede 	gspca_dev->vdev.ctrl_handler = hdl;
109676fdd85SHans de Goede 	v4l2_ctrl_handler_init(hdl, 11);
110676fdd85SHans de Goede 
111676fdd85SHans de Goede 	if (sd->vmax.brightness)
112676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_BRIGHTNESS,
113676fdd85SHans de Goede 				  0, sd->vmax.brightness, 1,
114676fdd85SHans de Goede 				  sd->vcur.brightness);
115676fdd85SHans de Goede 
116676fdd85SHans de Goede 	if (sd->vmax.contrast)
117676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_CONTRAST,
118676fdd85SHans de Goede 				  0, sd->vmax.contrast, 1,
119676fdd85SHans de Goede 				  sd->vcur.contrast);
120676fdd85SHans de Goede 
121676fdd85SHans de Goede 	if (sd->vmax.saturation)
122676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_SATURATION,
123676fdd85SHans de Goede 				  0, sd->vmax.saturation, 1,
124676fdd85SHans de Goede 				  sd->vcur.saturation);
125676fdd85SHans de Goede 
126676fdd85SHans de Goede 	if (sd->vmax.hue)
127676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_HUE,
128676fdd85SHans de Goede 				  0, sd->vmax.hue, 1, sd->vcur.hue);
129676fdd85SHans de Goede 
130676fdd85SHans de Goede 	if (sd->vmax.gamma)
131676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_GAMMA,
132676fdd85SHans de Goede 				  0, sd->vmax.gamma, 1, sd->vcur.gamma);
133676fdd85SHans de Goede 
134676fdd85SHans de Goede 	if (sd->vmax.mirror)
135676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_HFLIP,
136676fdd85SHans de Goede 				  0, sd->vmax.mirror, 1, sd->vcur.mirror);
137676fdd85SHans de Goede 
138676fdd85SHans de Goede 	if (sd->vmax.flip)
139676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_VFLIP,
140676fdd85SHans de Goede 				  0, sd->vmax.flip, 1, sd->vcur.flip);
141676fdd85SHans de Goede 
142676fdd85SHans de Goede 	if (sd->vmax.AC50Hz)
143676fdd85SHans de Goede 		v4l2_ctrl_new_std_menu(hdl, &sd_ctrl_ops,
144676fdd85SHans de Goede 				  V4L2_CID_POWER_LINE_FREQUENCY,
145676fdd85SHans de Goede 				  sd->vmax.AC50Hz, 0, sd->vcur.AC50Hz);
146676fdd85SHans de Goede 
147676fdd85SHans de Goede 	if (sd->vmax.whitebal)
148676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
149676fdd85SHans de Goede 				  V4L2_CID_WHITE_BALANCE_TEMPERATURE,
150676fdd85SHans de Goede 				  0, sd->vmax.whitebal, 1, sd->vcur.whitebal);
151676fdd85SHans de Goede 
152676fdd85SHans de Goede 	if (sd->vmax.sharpness)
153676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops, V4L2_CID_SHARPNESS,
154676fdd85SHans de Goede 				  0, sd->vmax.sharpness, 1,
155676fdd85SHans de Goede 				  sd->vcur.sharpness);
156676fdd85SHans de Goede 
157676fdd85SHans de Goede 	if (sd->vmax.backlight)
158676fdd85SHans de Goede 		v4l2_ctrl_new_std(hdl, &sd_ctrl_ops,
159676fdd85SHans de Goede 				  V4L2_CID_BACKLIGHT_COMPENSATION,
160676fdd85SHans de Goede 				  0, sd->vmax.backlight, 1,
161676fdd85SHans de Goede 				  sd->vcur.backlight);
162676fdd85SHans de Goede 
163676fdd85SHans de Goede 	if (hdl->error) {
164676fdd85SHans de Goede 		pr_err("Could not initialize controls\n");
165676fdd85SHans de Goede 		return hdl->error;
166676fdd85SHans de Goede 	}
167676fdd85SHans de Goede 
1680c0d06caSMauro Carvalho Chehab 	return 0;
1690c0d06caSMauro Carvalho Chehab }
1700c0d06caSMauro Carvalho Chehab 
1710c0d06caSMauro Carvalho Chehab /*==================== sud-driver structure initialisation =================*/
1720c0d06caSMauro Carvalho Chehab 
1730c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_mi1320 = {
1740c0d06caSMauro Carvalho Chehab 	.name        = MODULE_NAME,
1750c0d06caSMauro Carvalho Chehab 	.config      = sd_config,
1760c0d06caSMauro Carvalho Chehab 	.init        = sd_init,
177676fdd85SHans de Goede 	.init_controls = sd_init_controls,
1780c0d06caSMauro Carvalho Chehab 	.isoc_init   = sd_isoc_init,
1790c0d06caSMauro Carvalho Chehab 	.start       = sd_start,
1800c0d06caSMauro Carvalho Chehab 	.stop0       = sd_stop0,
1810c0d06caSMauro Carvalho Chehab 	.pkt_scan    = sd_pkt_scan,
1820c0d06caSMauro Carvalho Chehab 	.dq_callback = sd_callback,
1830c0d06caSMauro Carvalho Chehab };
1840c0d06caSMauro Carvalho Chehab 
1850c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_mi2020 = {
1860c0d06caSMauro Carvalho Chehab 	.name        = MODULE_NAME,
1870c0d06caSMauro Carvalho Chehab 	.config      = sd_config,
1880c0d06caSMauro Carvalho Chehab 	.init        = sd_init,
189676fdd85SHans de Goede 	.init_controls = sd_init_controls,
1900c0d06caSMauro Carvalho Chehab 	.isoc_init   = sd_isoc_init,
1910c0d06caSMauro Carvalho Chehab 	.start       = sd_start,
1920c0d06caSMauro Carvalho Chehab 	.stop0       = sd_stop0,
1930c0d06caSMauro Carvalho Chehab 	.pkt_scan    = sd_pkt_scan,
1940c0d06caSMauro Carvalho Chehab 	.dq_callback = sd_callback,
1950c0d06caSMauro Carvalho Chehab };
1960c0d06caSMauro Carvalho Chehab 
1970c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_ov2640 = {
1980c0d06caSMauro Carvalho Chehab 	.name        = MODULE_NAME,
1990c0d06caSMauro Carvalho Chehab 	.config      = sd_config,
2000c0d06caSMauro Carvalho Chehab 	.init        = sd_init,
201676fdd85SHans de Goede 	.init_controls = sd_init_controls,
2020c0d06caSMauro Carvalho Chehab 	.isoc_init   = sd_isoc_init,
2030c0d06caSMauro Carvalho Chehab 	.start       = sd_start,
2040c0d06caSMauro Carvalho Chehab 	.stop0       = sd_stop0,
2050c0d06caSMauro Carvalho Chehab 	.pkt_scan    = sd_pkt_scan,
2060c0d06caSMauro Carvalho Chehab 	.dq_callback = sd_callback,
2070c0d06caSMauro Carvalho Chehab };
2080c0d06caSMauro Carvalho Chehab 
2090c0d06caSMauro Carvalho Chehab static const struct sd_desc sd_desc_ov9655 = {
2100c0d06caSMauro Carvalho Chehab 	.name        = MODULE_NAME,
2110c0d06caSMauro Carvalho Chehab 	.config      = sd_config,
2120c0d06caSMauro Carvalho Chehab 	.init        = sd_init,
213676fdd85SHans de Goede 	.init_controls = sd_init_controls,
2140c0d06caSMauro Carvalho Chehab 	.isoc_init   = sd_isoc_init,
2150c0d06caSMauro Carvalho Chehab 	.start       = sd_start,
2160c0d06caSMauro Carvalho Chehab 	.stop0       = sd_stop0,
2170c0d06caSMauro Carvalho Chehab 	.pkt_scan    = sd_pkt_scan,
2180c0d06caSMauro Carvalho Chehab 	.dq_callback = sd_callback,
2190c0d06caSMauro Carvalho Chehab };
2200c0d06caSMauro Carvalho Chehab 
2210c0d06caSMauro Carvalho Chehab /*=========================== sub-driver image sizes =======================*/
2220c0d06caSMauro Carvalho Chehab 
2230c0d06caSMauro Carvalho Chehab static struct v4l2_pix_format mi2020_mode[] = {
2240c0d06caSMauro Carvalho Chehab 	{ 640,  480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2250c0d06caSMauro Carvalho Chehab 		.bytesperline = 640,
2260c0d06caSMauro Carvalho Chehab 		.sizeimage = 640 * 480,
2270c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2280c0d06caSMauro Carvalho Chehab 		.priv = 0
2290c0d06caSMauro Carvalho Chehab 	},
2300c0d06caSMauro Carvalho Chehab 	{ 800,  598, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2310c0d06caSMauro Carvalho Chehab 		.bytesperline = 800,
2320c0d06caSMauro Carvalho Chehab 		.sizeimage = 800 * 598,
2330c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2340c0d06caSMauro Carvalho Chehab 		.priv = 1
2350c0d06caSMauro Carvalho Chehab 	},
2360c0d06caSMauro Carvalho Chehab 	{1280, 1024, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2370c0d06caSMauro Carvalho Chehab 		.bytesperline = 1280,
2380c0d06caSMauro Carvalho Chehab 		.sizeimage = 1280 * 1024,
2390c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2400c0d06caSMauro Carvalho Chehab 		.priv = 2
2410c0d06caSMauro Carvalho Chehab 	},
2420c0d06caSMauro Carvalho Chehab 	{1600, 1198, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2430c0d06caSMauro Carvalho Chehab 		.bytesperline = 1600,
2440c0d06caSMauro Carvalho Chehab 		.sizeimage = 1600 * 1198,
2450c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2460c0d06caSMauro Carvalho Chehab 		.priv = 3
2470c0d06caSMauro Carvalho Chehab 	},
2480c0d06caSMauro Carvalho Chehab };
2490c0d06caSMauro Carvalho Chehab 
2500c0d06caSMauro Carvalho Chehab static struct v4l2_pix_format ov2640_mode[] = {
2510c0d06caSMauro Carvalho Chehab 	{ 640,  480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2520c0d06caSMauro Carvalho Chehab 		.bytesperline = 640,
2530c0d06caSMauro Carvalho Chehab 		.sizeimage = 640 * 480,
2540c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2550c0d06caSMauro Carvalho Chehab 		.priv = 0
2560c0d06caSMauro Carvalho Chehab 	},
2570c0d06caSMauro Carvalho Chehab 	{ 800,  600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2580c0d06caSMauro Carvalho Chehab 		.bytesperline = 800,
2590c0d06caSMauro Carvalho Chehab 		.sizeimage = 800 * 600,
2600c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2610c0d06caSMauro Carvalho Chehab 		.priv = 1
2620c0d06caSMauro Carvalho Chehab 	},
2630c0d06caSMauro Carvalho Chehab 	{1280,  960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2640c0d06caSMauro Carvalho Chehab 		.bytesperline = 1280,
2650c0d06caSMauro Carvalho Chehab 		.sizeimage = 1280 * 960,
2660c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2670c0d06caSMauro Carvalho Chehab 		.priv = 2
2680c0d06caSMauro Carvalho Chehab 	},
2690c0d06caSMauro Carvalho Chehab 	{1600, 1200, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2700c0d06caSMauro Carvalho Chehab 		.bytesperline = 1600,
2710c0d06caSMauro Carvalho Chehab 		.sizeimage = 1600 * 1200,
2720c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2730c0d06caSMauro Carvalho Chehab 		.priv = 3
2740c0d06caSMauro Carvalho Chehab 	},
2750c0d06caSMauro Carvalho Chehab };
2760c0d06caSMauro Carvalho Chehab 
2770c0d06caSMauro Carvalho Chehab static struct v4l2_pix_format mi1320_mode[] = {
2780c0d06caSMauro Carvalho Chehab 	{ 640,  480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2790c0d06caSMauro Carvalho Chehab 		.bytesperline = 640,
2800c0d06caSMauro Carvalho Chehab 		.sizeimage = 640 * 480,
2810c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2820c0d06caSMauro Carvalho Chehab 		.priv = 0
2830c0d06caSMauro Carvalho Chehab 	},
2840c0d06caSMauro Carvalho Chehab 	{ 800,  600, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2850c0d06caSMauro Carvalho Chehab 		.bytesperline = 800,
2860c0d06caSMauro Carvalho Chehab 		.sizeimage = 800 * 600,
2870c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2880c0d06caSMauro Carvalho Chehab 		.priv = 1
2890c0d06caSMauro Carvalho Chehab 	},
2900c0d06caSMauro Carvalho Chehab 	{1280,  960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
2910c0d06caSMauro Carvalho Chehab 		.bytesperline = 1280,
2920c0d06caSMauro Carvalho Chehab 		.sizeimage = 1280 * 960,
2930c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
2940c0d06caSMauro Carvalho Chehab 		.priv = 2
2950c0d06caSMauro Carvalho Chehab 	},
2960c0d06caSMauro Carvalho Chehab };
2970c0d06caSMauro Carvalho Chehab 
2980c0d06caSMauro Carvalho Chehab static struct v4l2_pix_format ov9655_mode[] = {
2990c0d06caSMauro Carvalho Chehab 	{ 640,  480, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
3000c0d06caSMauro Carvalho Chehab 		.bytesperline = 640,
3010c0d06caSMauro Carvalho Chehab 		.sizeimage = 640 * 480,
3020c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
3030c0d06caSMauro Carvalho Chehab 		.priv = 0
3040c0d06caSMauro Carvalho Chehab 	},
3050c0d06caSMauro Carvalho Chehab 	{1280,  960, V4L2_PIX_FMT_SGBRG8, V4L2_FIELD_NONE,
3060c0d06caSMauro Carvalho Chehab 		.bytesperline = 1280,
3070c0d06caSMauro Carvalho Chehab 		.sizeimage = 1280 * 960,
3080c0d06caSMauro Carvalho Chehab 		.colorspace = V4L2_COLORSPACE_SRGB,
3090c0d06caSMauro Carvalho Chehab 		.priv = 1
3100c0d06caSMauro Carvalho Chehab 	},
3110c0d06caSMauro Carvalho Chehab };
3120c0d06caSMauro Carvalho Chehab 
3130c0d06caSMauro Carvalho Chehab /*========================= sud-driver functions ===========================*/
3140c0d06caSMauro Carvalho Chehab 
3150c0d06caSMauro Carvalho Chehab /* This function is called at probe time */
sd_config(struct gspca_dev * gspca_dev,const struct usb_device_id * id)3160c0d06caSMauro Carvalho Chehab static int sd_config(struct gspca_dev *gspca_dev,
3170c0d06caSMauro Carvalho Chehab 			const struct usb_device_id *id)
3180c0d06caSMauro Carvalho Chehab {
3190c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
3200c0d06caSMauro Carvalho Chehab 	struct cam *cam;
3210c0d06caSMauro Carvalho Chehab 	u16 vendor_id, product_id;
3220c0d06caSMauro Carvalho Chehab 
3230c0d06caSMauro Carvalho Chehab 	/* Get USB VendorID and ProductID */
3240c0d06caSMauro Carvalho Chehab 	vendor_id  = id->idVendor;
3250c0d06caSMauro Carvalho Chehab 	product_id = id->idProduct;
3260c0d06caSMauro Carvalho Chehab 
3270c0d06caSMauro Carvalho Chehab 	sd->nbRightUp = 1;
3280c0d06caSMauro Carvalho Chehab 	sd->nbIm = -1;
3290c0d06caSMauro Carvalho Chehab 
3300c0d06caSMauro Carvalho Chehab 	sd->sensor = 0xff;
3310c0d06caSMauro Carvalho Chehab 	if (strcmp(sensor, "MI1320") == 0)
3320c0d06caSMauro Carvalho Chehab 		sd->sensor = ID_MI1320;
3330c0d06caSMauro Carvalho Chehab 	else if (strcmp(sensor, "OV2640") == 0)
3340c0d06caSMauro Carvalho Chehab 		sd->sensor = ID_OV2640;
3350c0d06caSMauro Carvalho Chehab 	else if (strcmp(sensor, "OV9655") == 0)
3360c0d06caSMauro Carvalho Chehab 		sd->sensor = ID_OV9655;
3370c0d06caSMauro Carvalho Chehab 	else if (strcmp(sensor, "MI2020") == 0)
3380c0d06caSMauro Carvalho Chehab 		sd->sensor = ID_MI2020;
3390c0d06caSMauro Carvalho Chehab 
3400c0d06caSMauro Carvalho Chehab 	/* Get sensor and set the suitable init/start/../stop functions */
3410c0d06caSMauro Carvalho Chehab 	if (gl860_guess_sensor(gspca_dev, vendor_id, product_id) == -1)
3420c0d06caSMauro Carvalho Chehab 		return -1;
3430c0d06caSMauro Carvalho Chehab 
3440c0d06caSMauro Carvalho Chehab 	cam = &gspca_dev->cam;
3450c0d06caSMauro Carvalho Chehab 
3460c0d06caSMauro Carvalho Chehab 	switch (sd->sensor) {
3470c0d06caSMauro Carvalho Chehab 	case ID_MI1320:
3480c0d06caSMauro Carvalho Chehab 		gspca_dev->sd_desc = &sd_desc_mi1320;
3490c0d06caSMauro Carvalho Chehab 		cam->cam_mode = mi1320_mode;
3500c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(mi1320_mode);
3510c0d06caSMauro Carvalho Chehab 		dev_init_settings   = mi1320_init_settings;
3520c0d06caSMauro Carvalho Chehab 		break;
3530c0d06caSMauro Carvalho Chehab 
3540c0d06caSMauro Carvalho Chehab 	case ID_MI2020:
3550c0d06caSMauro Carvalho Chehab 		gspca_dev->sd_desc = &sd_desc_mi2020;
3560c0d06caSMauro Carvalho Chehab 		cam->cam_mode = mi2020_mode;
3570c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(mi2020_mode);
3580c0d06caSMauro Carvalho Chehab 		dev_init_settings   = mi2020_init_settings;
3590c0d06caSMauro Carvalho Chehab 		break;
3600c0d06caSMauro Carvalho Chehab 
3610c0d06caSMauro Carvalho Chehab 	case ID_OV2640:
3620c0d06caSMauro Carvalho Chehab 		gspca_dev->sd_desc = &sd_desc_ov2640;
3630c0d06caSMauro Carvalho Chehab 		cam->cam_mode = ov2640_mode;
3640c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(ov2640_mode);
3650c0d06caSMauro Carvalho Chehab 		dev_init_settings   = ov2640_init_settings;
3660c0d06caSMauro Carvalho Chehab 		break;
3670c0d06caSMauro Carvalho Chehab 
3680c0d06caSMauro Carvalho Chehab 	case ID_OV9655:
3690c0d06caSMauro Carvalho Chehab 		gspca_dev->sd_desc = &sd_desc_ov9655;
3700c0d06caSMauro Carvalho Chehab 		cam->cam_mode = ov9655_mode;
3710c0d06caSMauro Carvalho Chehab 		cam->nmodes = ARRAY_SIZE(ov9655_mode);
3720c0d06caSMauro Carvalho Chehab 		dev_init_settings   = ov9655_init_settings;
3730c0d06caSMauro Carvalho Chehab 		break;
3740c0d06caSMauro Carvalho Chehab 	}
3750c0d06caSMauro Carvalho Chehab 
3760c0d06caSMauro Carvalho Chehab 	dev_init_settings(gspca_dev);
3770c0d06caSMauro Carvalho Chehab 	if (AC50Hz != 0xff)
3780c0d06caSMauro Carvalho Chehab 		((struct sd *) gspca_dev)->vcur.AC50Hz = AC50Hz;
3790c0d06caSMauro Carvalho Chehab 
3800c0d06caSMauro Carvalho Chehab 	return 0;
3810c0d06caSMauro Carvalho Chehab }
3820c0d06caSMauro Carvalho Chehab 
3830c0d06caSMauro Carvalho Chehab /* This function is called at probe time after sd_config */
sd_init(struct gspca_dev * gspca_dev)3840c0d06caSMauro Carvalho Chehab static int sd_init(struct gspca_dev *gspca_dev)
3850c0d06caSMauro Carvalho Chehab {
3860c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
3870c0d06caSMauro Carvalho Chehab 
3880c0d06caSMauro Carvalho Chehab 	return sd->dev_init_at_startup(gspca_dev);
3890c0d06caSMauro Carvalho Chehab }
3900c0d06caSMauro Carvalho Chehab 
3910c0d06caSMauro Carvalho Chehab /* This function is called before to choose the alt setting */
sd_isoc_init(struct gspca_dev * gspca_dev)3920c0d06caSMauro Carvalho Chehab static int sd_isoc_init(struct gspca_dev *gspca_dev)
3930c0d06caSMauro Carvalho Chehab {
3940c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
3950c0d06caSMauro Carvalho Chehab 
3960c0d06caSMauro Carvalho Chehab 	return sd->dev_configure_alt(gspca_dev);
3970c0d06caSMauro Carvalho Chehab }
3980c0d06caSMauro Carvalho Chehab 
3990c0d06caSMauro Carvalho Chehab /* This function is called to start the webcam */
sd_start(struct gspca_dev * gspca_dev)4000c0d06caSMauro Carvalho Chehab static int sd_start(struct gspca_dev *gspca_dev)
4010c0d06caSMauro Carvalho Chehab {
4020c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4030c0d06caSMauro Carvalho Chehab 
4040c0d06caSMauro Carvalho Chehab 	return sd->dev_init_pre_alt(gspca_dev);
4050c0d06caSMauro Carvalho Chehab }
4060c0d06caSMauro Carvalho Chehab 
4070c0d06caSMauro Carvalho Chehab /* This function is called to stop the webcam */
sd_stop0(struct gspca_dev * gspca_dev)4080c0d06caSMauro Carvalho Chehab static void sd_stop0(struct gspca_dev *gspca_dev)
4090c0d06caSMauro Carvalho Chehab {
4100c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4110c0d06caSMauro Carvalho Chehab 
4120c0d06caSMauro Carvalho Chehab 	if (!sd->gspca_dev.present)
4130c0d06caSMauro Carvalho Chehab 		return;
4140c0d06caSMauro Carvalho Chehab 
4150c0d06caSMauro Carvalho Chehab 	return sd->dev_post_unset_alt(gspca_dev);
4160c0d06caSMauro Carvalho Chehab }
4170c0d06caSMauro Carvalho Chehab 
4180c0d06caSMauro Carvalho Chehab /* This function is called when an image is being received */
sd_pkt_scan(struct gspca_dev * gspca_dev,u8 * data,int len)4190c0d06caSMauro Carvalho Chehab static void sd_pkt_scan(struct gspca_dev *gspca_dev,
4200c0d06caSMauro Carvalho Chehab 			u8 *data, int len)
4210c0d06caSMauro Carvalho Chehab {
4220c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4230c0d06caSMauro Carvalho Chehab 	static s32 nSkipped;
4240c0d06caSMauro Carvalho Chehab 
4250c0d06caSMauro Carvalho Chehab 	s32 mode = (s32) gspca_dev->curr_mode;
4260c0d06caSMauro Carvalho Chehab 	s32 nToSkip =
4270c0d06caSMauro Carvalho Chehab 		sd->swapRB * (gspca_dev->cam.cam_mode[mode].bytesperline + 1);
4280c0d06caSMauro Carvalho Chehab 
429f58c91ceSJonathan McCrohan 	/* Test only against 0202h, so endianness does not matter */
4300c0d06caSMauro Carvalho Chehab 	switch (*(s16 *) data) {
4310c0d06caSMauro Carvalho Chehab 	case 0x0202:		/* End of frame, start a new one */
4320c0d06caSMauro Carvalho Chehab 		gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
4330c0d06caSMauro Carvalho Chehab 		nSkipped = 0;
4340c0d06caSMauro Carvalho Chehab 		if (sd->nbIm >= 0 && sd->nbIm < 10)
4350c0d06caSMauro Carvalho Chehab 			sd->nbIm++;
4360c0d06caSMauro Carvalho Chehab 		gspca_frame_add(gspca_dev, FIRST_PACKET, NULL, 0);
4370c0d06caSMauro Carvalho Chehab 		break;
4380c0d06caSMauro Carvalho Chehab 
4390c0d06caSMauro Carvalho Chehab 	default:
4400c0d06caSMauro Carvalho Chehab 		data += 2;
4410c0d06caSMauro Carvalho Chehab 		len  -= 2;
4420c0d06caSMauro Carvalho Chehab 		if (nSkipped + len <= nToSkip)
4430c0d06caSMauro Carvalho Chehab 			nSkipped += len;
4440c0d06caSMauro Carvalho Chehab 		else {
4450c0d06caSMauro Carvalho Chehab 			if (nSkipped < nToSkip && nSkipped + len > nToSkip) {
4460c0d06caSMauro Carvalho Chehab 				data += nToSkip - nSkipped;
4470c0d06caSMauro Carvalho Chehab 				len  -= nToSkip - nSkipped;
4480c0d06caSMauro Carvalho Chehab 				nSkipped = nToSkip + 1;
4490c0d06caSMauro Carvalho Chehab 			}
4500c0d06caSMauro Carvalho Chehab 			gspca_frame_add(gspca_dev,
4510c0d06caSMauro Carvalho Chehab 				INTER_PACKET, data, len);
4520c0d06caSMauro Carvalho Chehab 		}
4530c0d06caSMauro Carvalho Chehab 		break;
4540c0d06caSMauro Carvalho Chehab 	}
4550c0d06caSMauro Carvalho Chehab }
4560c0d06caSMauro Carvalho Chehab 
4570c0d06caSMauro Carvalho Chehab /* This function is called when an image has been read */
4580c0d06caSMauro Carvalho Chehab /* This function is used to monitor webcam orientation */
sd_callback(struct gspca_dev * gspca_dev)4590c0d06caSMauro Carvalho Chehab static void sd_callback(struct gspca_dev *gspca_dev)
4600c0d06caSMauro Carvalho Chehab {
4610c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
4620c0d06caSMauro Carvalho Chehab 
4630c0d06caSMauro Carvalho Chehab 	if (!_OV9655_) {
4640c0d06caSMauro Carvalho Chehab 		u8 state;
4650c0d06caSMauro Carvalho Chehab 		u8 upsideDown;
4660c0d06caSMauro Carvalho Chehab 
4670c0d06caSMauro Carvalho Chehab 		/* Probe sensor orientation */
4680c0d06caSMauro Carvalho Chehab 		ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0000, 1, (void *)&state);
4690c0d06caSMauro Carvalho Chehab 
4700c0d06caSMauro Carvalho Chehab 		/* C8/40 means upside-down (looking backwards) */
4710c0d06caSMauro Carvalho Chehab 		/* D8/50 means right-up (looking onwards) */
4720c0d06caSMauro Carvalho Chehab 		upsideDown = (state == 0xc8 || state == 0x40);
4730c0d06caSMauro Carvalho Chehab 
4740c0d06caSMauro Carvalho Chehab 		if (upsideDown && sd->nbRightUp > -4) {
4750c0d06caSMauro Carvalho Chehab 			if (sd->nbRightUp > 0)
4760c0d06caSMauro Carvalho Chehab 				sd->nbRightUp = 0;
4770c0d06caSMauro Carvalho Chehab 			if (sd->nbRightUp == -3) {
4780c0d06caSMauro Carvalho Chehab 				sd->mirrorMask = 1;
4790c0d06caSMauro Carvalho Chehab 				sd->waitSet = 1;
4800c0d06caSMauro Carvalho Chehab 			}
4810c0d06caSMauro Carvalho Chehab 			sd->nbRightUp--;
4820c0d06caSMauro Carvalho Chehab 		}
4830c0d06caSMauro Carvalho Chehab 		if (!upsideDown && sd->nbRightUp < 4) {
4840c0d06caSMauro Carvalho Chehab 			if (sd->nbRightUp  < 0)
4850c0d06caSMauro Carvalho Chehab 				sd->nbRightUp = 0;
4860c0d06caSMauro Carvalho Chehab 			if (sd->nbRightUp == 3) {
4870c0d06caSMauro Carvalho Chehab 				sd->mirrorMask = 0;
4880c0d06caSMauro Carvalho Chehab 				sd->waitSet = 1;
4890c0d06caSMauro Carvalho Chehab 			}
4900c0d06caSMauro Carvalho Chehab 			sd->nbRightUp++;
4910c0d06caSMauro Carvalho Chehab 		}
4920c0d06caSMauro Carvalho Chehab 	}
4930c0d06caSMauro Carvalho Chehab 
4940c0d06caSMauro Carvalho Chehab 	if (sd->waitSet)
4950c0d06caSMauro Carvalho Chehab 		sd->dev_camera_settings(gspca_dev);
4960c0d06caSMauro Carvalho Chehab }
4970c0d06caSMauro Carvalho Chehab 
4980c0d06caSMauro Carvalho Chehab /*=================== USB driver structure initialisation ==================*/
4990c0d06caSMauro Carvalho Chehab 
5000c0d06caSMauro Carvalho Chehab static const struct usb_device_id device_table[] = {
5010c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x05e3, 0x0503)},
5020c0d06caSMauro Carvalho Chehab 	{USB_DEVICE(0x05e3, 0xf191)},
5030c0d06caSMauro Carvalho Chehab 	{}
5040c0d06caSMauro Carvalho Chehab };
5050c0d06caSMauro Carvalho Chehab 
5060c0d06caSMauro Carvalho Chehab MODULE_DEVICE_TABLE(usb, device_table);
5070c0d06caSMauro Carvalho Chehab 
sd_probe(struct usb_interface * intf,const struct usb_device_id * id)5080c0d06caSMauro Carvalho Chehab static int sd_probe(struct usb_interface *intf,
5090c0d06caSMauro Carvalho Chehab 				const struct usb_device_id *id)
5100c0d06caSMauro Carvalho Chehab {
5110c0d06caSMauro Carvalho Chehab 	return gspca_dev_probe(intf, id,
5120c0d06caSMauro Carvalho Chehab 			&sd_desc_mi1320, sizeof(struct sd), THIS_MODULE);
5130c0d06caSMauro Carvalho Chehab }
5140c0d06caSMauro Carvalho Chehab 
sd_disconnect(struct usb_interface * intf)5150c0d06caSMauro Carvalho Chehab static void sd_disconnect(struct usb_interface *intf)
5160c0d06caSMauro Carvalho Chehab {
5170c0d06caSMauro Carvalho Chehab 	gspca_disconnect(intf);
5180c0d06caSMauro Carvalho Chehab }
5190c0d06caSMauro Carvalho Chehab 
5200c0d06caSMauro Carvalho Chehab static struct usb_driver sd_driver = {
5210c0d06caSMauro Carvalho Chehab 	.name       = MODULE_NAME,
5220c0d06caSMauro Carvalho Chehab 	.id_table   = device_table,
5230c0d06caSMauro Carvalho Chehab 	.probe      = sd_probe,
5240c0d06caSMauro Carvalho Chehab 	.disconnect = sd_disconnect,
5250c0d06caSMauro Carvalho Chehab #ifdef CONFIG_PM
5260c0d06caSMauro Carvalho Chehab 	.suspend    = gspca_suspend,
5270c0d06caSMauro Carvalho Chehab 	.resume     = gspca_resume,
5280c0d06caSMauro Carvalho Chehab 	.reset_resume = gspca_resume,
5290c0d06caSMauro Carvalho Chehab #endif
5300c0d06caSMauro Carvalho Chehab };
5310c0d06caSMauro Carvalho Chehab 
5320c0d06caSMauro Carvalho Chehab /*====================== Init and Exit module functions ====================*/
5330c0d06caSMauro Carvalho Chehab 
5340c0d06caSMauro Carvalho Chehab module_usb_driver(sd_driver);
5350c0d06caSMauro Carvalho Chehab 
5360c0d06caSMauro Carvalho Chehab /*==========================================================================*/
5370c0d06caSMauro Carvalho Chehab 
gl860_RTx(struct gspca_dev * gspca_dev,unsigned char pref,u32 req,u16 val,u16 index,s32 len,void * pdata)5380c0d06caSMauro Carvalho Chehab int gl860_RTx(struct gspca_dev *gspca_dev,
5390c0d06caSMauro Carvalho Chehab 		unsigned char pref, u32 req, u16 val, u16 index,
5400c0d06caSMauro Carvalho Chehab 		s32 len, void *pdata)
5410c0d06caSMauro Carvalho Chehab {
5420c0d06caSMauro Carvalho Chehab 	struct usb_device *udev = gspca_dev->dev;
5430c0d06caSMauro Carvalho Chehab 	s32 r = 0;
5440c0d06caSMauro Carvalho Chehab 
5450c0d06caSMauro Carvalho Chehab 	if (pref == 0x40) { /* Send */
5460c0d06caSMauro Carvalho Chehab 		if (len > 0) {
5470c0d06caSMauro Carvalho Chehab 			memcpy(gspca_dev->usb_buf, pdata, len);
5480c0d06caSMauro Carvalho Chehab 			r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5490c0d06caSMauro Carvalho Chehab 					req, pref, val, index,
5500c0d06caSMauro Carvalho Chehab 					gspca_dev->usb_buf,
5510c0d06caSMauro Carvalho Chehab 					len, 400 + 200 * (len > 1));
5520c0d06caSMauro Carvalho Chehab 		} else {
5530c0d06caSMauro Carvalho Chehab 			r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
5540c0d06caSMauro Carvalho Chehab 					req, pref, val, index, NULL, len, 400);
5550c0d06caSMauro Carvalho Chehab 		}
5560c0d06caSMauro Carvalho Chehab 	} else { /* Receive */
5570c0d06caSMauro Carvalho Chehab 		if (len > 0) {
5580c0d06caSMauro Carvalho Chehab 			r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
5590c0d06caSMauro Carvalho Chehab 					req, pref, val, index,
5600c0d06caSMauro Carvalho Chehab 					gspca_dev->usb_buf,
5610c0d06caSMauro Carvalho Chehab 					len, 400 + 200 * (len > 1));
5620c0d06caSMauro Carvalho Chehab 			memcpy(pdata, gspca_dev->usb_buf, len);
5630c0d06caSMauro Carvalho Chehab 		} else {
564*8ed339f2SJohan Hovold 			gspca_err(gspca_dev, "zero-length read request\n");
565*8ed339f2SJohan Hovold 			r = -EINVAL;
5660c0d06caSMauro Carvalho Chehab 		}
5670c0d06caSMauro Carvalho Chehab 	}
5680c0d06caSMauro Carvalho Chehab 
5690c0d06caSMauro Carvalho Chehab 	if (r < 0)
5700c0d06caSMauro Carvalho Chehab 		pr_err("ctrl transfer failed %4d [p%02x r%d v%04x i%04x len%d]\n",
5710c0d06caSMauro Carvalho Chehab 		       r, pref, req, val, index, len);
5720c0d06caSMauro Carvalho Chehab 	else if (len > 1 && r < len)
57352173c5fSJoe Perches 		gspca_err(gspca_dev, "short ctrl transfer %d/%d\n", r, len);
5740c0d06caSMauro Carvalho Chehab 
5750c0d06caSMauro Carvalho Chehab 	msleep(1);
5760c0d06caSMauro Carvalho Chehab 
5770c0d06caSMauro Carvalho Chehab 	return r;
5780c0d06caSMauro Carvalho Chehab }
5790c0d06caSMauro Carvalho Chehab 
fetch_validx(struct gspca_dev * gspca_dev,struct validx * tbl,int len)5800c0d06caSMauro Carvalho Chehab int fetch_validx(struct gspca_dev *gspca_dev, struct validx *tbl, int len)
5810c0d06caSMauro Carvalho Chehab {
5820c0d06caSMauro Carvalho Chehab 	int n;
5830c0d06caSMauro Carvalho Chehab 
5840c0d06caSMauro Carvalho Chehab 	for (n = 0; n < len; n++) {
5850c0d06caSMauro Carvalho Chehab 		if (tbl[n].idx != 0xffff)
5860c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 1, tbl[n].val,
5870c0d06caSMauro Carvalho Chehab 					tbl[n].idx, 0, NULL);
5880c0d06caSMauro Carvalho Chehab 		else if (tbl[n].val == 0xffff)
5890c0d06caSMauro Carvalho Chehab 			break;
5900c0d06caSMauro Carvalho Chehab 		else
5910c0d06caSMauro Carvalho Chehab 			msleep(tbl[n].val);
5920c0d06caSMauro Carvalho Chehab 	}
5930c0d06caSMauro Carvalho Chehab 	return n;
5940c0d06caSMauro Carvalho Chehab }
5950c0d06caSMauro Carvalho Chehab 
keep_on_fetching_validx(struct gspca_dev * gspca_dev,struct validx * tbl,int len,int n)5960c0d06caSMauro Carvalho Chehab int keep_on_fetching_validx(struct gspca_dev *gspca_dev, struct validx *tbl,
5970c0d06caSMauro Carvalho Chehab 				int len, int n)
5980c0d06caSMauro Carvalho Chehab {
5990c0d06caSMauro Carvalho Chehab 	while (++n < len) {
6000c0d06caSMauro Carvalho Chehab 		if (tbl[n].idx != 0xffff)
6010c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 1, tbl[n].val, tbl[n].idx,
6020c0d06caSMauro Carvalho Chehab 					0, NULL);
6030c0d06caSMauro Carvalho Chehab 		else if (tbl[n].val == 0xffff)
6040c0d06caSMauro Carvalho Chehab 			break;
6050c0d06caSMauro Carvalho Chehab 		else
6060c0d06caSMauro Carvalho Chehab 			msleep(tbl[n].val);
6070c0d06caSMauro Carvalho Chehab 	}
6080c0d06caSMauro Carvalho Chehab 	return n;
6090c0d06caSMauro Carvalho Chehab }
6100c0d06caSMauro Carvalho Chehab 
fetch_idxdata(struct gspca_dev * gspca_dev,struct idxdata * tbl,int len)6110c0d06caSMauro Carvalho Chehab void fetch_idxdata(struct gspca_dev *gspca_dev, struct idxdata *tbl, int len)
6120c0d06caSMauro Carvalho Chehab {
6130c0d06caSMauro Carvalho Chehab 	int n;
6140c0d06caSMauro Carvalho Chehab 
6150c0d06caSMauro Carvalho Chehab 	for (n = 0; n < len; n++) {
6160c0d06caSMauro Carvalho Chehab 		if (memcmp(tbl[n].data, "\xff\xff\xff", 3) != 0)
6170c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 3, 0x7a00, tbl[n].idx,
6180c0d06caSMauro Carvalho Chehab 					3, tbl[n].data);
6190c0d06caSMauro Carvalho Chehab 		else
6200c0d06caSMauro Carvalho Chehab 			msleep(tbl[n].idx);
6210c0d06caSMauro Carvalho Chehab 	}
6220c0d06caSMauro Carvalho Chehab }
6230c0d06caSMauro Carvalho Chehab 
gl860_guess_sensor(struct gspca_dev * gspca_dev,u16 vendor_id,u16 product_id)6240c0d06caSMauro Carvalho Chehab static int gl860_guess_sensor(struct gspca_dev *gspca_dev,
6250c0d06caSMauro Carvalho Chehab 				u16 vendor_id, u16 product_id)
6260c0d06caSMauro Carvalho Chehab {
6270c0d06caSMauro Carvalho Chehab 	struct sd *sd = (struct sd *) gspca_dev;
6280c0d06caSMauro Carvalho Chehab 	u8 probe, nb26, nb96, nOV, ntry;
6290c0d06caSMauro Carvalho Chehab 
6300c0d06caSMauro Carvalho Chehab 	if (product_id == 0xf191)
6310c0d06caSMauro Carvalho Chehab 		sd->sensor = ID_MI1320;
6320c0d06caSMauro Carvalho Chehab 
6330c0d06caSMauro Carvalho Chehab 	if (sd->sensor == 0xff) {
6340c0d06caSMauro Carvalho Chehab 		ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
6350c0d06caSMauro Carvalho Chehab 		ctrl_in(gspca_dev, 0xc0, 2, 0x0000, 0x0004, 1, &probe);
6360c0d06caSMauro Carvalho Chehab 
6370c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0000, 0x0000, 0, NULL);
6380c0d06caSMauro Carvalho Chehab 		msleep(3);
6390c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0010, 0x0010, 0, NULL);
6400c0d06caSMauro Carvalho Chehab 		msleep(3);
6410c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0008, 0x00c0, 0, NULL);
6420c0d06caSMauro Carvalho Chehab 		msleep(3);
6430c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c1, 0, NULL);
6440c0d06caSMauro Carvalho Chehab 		msleep(3);
6450c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0001, 0x00c2, 0, NULL);
6460c0d06caSMauro Carvalho Chehab 		msleep(3);
6470c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x0020, 0x0006, 0, NULL);
6480c0d06caSMauro Carvalho Chehab 		msleep(3);
6490c0d06caSMauro Carvalho Chehab 		ctrl_out(gspca_dev, 0x40, 1, 0x006a, 0x000d, 0, NULL);
6500c0d06caSMauro Carvalho Chehab 		msleep(56);
6510c0d06caSMauro Carvalho Chehab 
65237d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "probing for sensor MI2020 or OVXXXX\n");
6530c0d06caSMauro Carvalho Chehab 		nOV = 0;
6540c0d06caSMauro Carvalho Chehab 		for (ntry = 0; ntry < 4; ntry++) {
6550c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000, 0, NULL);
6560c0d06caSMauro Carvalho Chehab 			msleep(3);
6570c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 1, 0x0063, 0x0006, 0, NULL);
6580c0d06caSMauro Carvalho Chehab 			msleep(3);
6590c0d06caSMauro Carvalho Chehab 			ctrl_out(gspca_dev, 0x40, 1, 0x7a00, 0x8030, 0, NULL);
6600c0d06caSMauro Carvalho Chehab 			msleep(10);
6610c0d06caSMauro Carvalho Chehab 			ctrl_in(gspca_dev, 0xc0, 2, 0x7a00, 0x8030, 1, &probe);
66237d5efb0SJoe Perches 			gspca_dbg(gspca_dev, D_PROBE, "probe=0x%02x\n", probe);
6630c0d06caSMauro Carvalho Chehab 			if (probe == 0xff)
6640c0d06caSMauro Carvalho Chehab 				nOV++;
6650c0d06caSMauro Carvalho Chehab 		}
6660c0d06caSMauro Carvalho Chehab 
6670c0d06caSMauro Carvalho Chehab 		if (nOV) {
66837d5efb0SJoe Perches 			gspca_dbg(gspca_dev, D_PROBE, "0xff -> OVXXXX\n");
66937d5efb0SJoe Perches 			gspca_dbg(gspca_dev, D_PROBE, "probing for sensor OV2640 or OV9655");
6700c0d06caSMauro Carvalho Chehab 
6710c0d06caSMauro Carvalho Chehab 			nb26 = nb96 = 0;
6720c0d06caSMauro Carvalho Chehab 			for (ntry = 0; ntry < 4; ntry++) {
6730c0d06caSMauro Carvalho Chehab 				ctrl_out(gspca_dev, 0x40, 1, 0x0040, 0x0000,
6740c0d06caSMauro Carvalho Chehab 						0, NULL);
6750c0d06caSMauro Carvalho Chehab 				msleep(3);
6760c0d06caSMauro Carvalho Chehab 				ctrl_out(gspca_dev, 0x40, 1, 0x6000, 0x800a,
6770c0d06caSMauro Carvalho Chehab 						0, NULL);
6780c0d06caSMauro Carvalho Chehab 				msleep(10);
6790c0d06caSMauro Carvalho Chehab 
6800c0d06caSMauro Carvalho Chehab 				/* Wait for 26(OV2640) or 96(OV9655) */
6810c0d06caSMauro Carvalho Chehab 				ctrl_in(gspca_dev, 0xc0, 2, 0x6000, 0x800a,
6820c0d06caSMauro Carvalho Chehab 						1, &probe);
6830c0d06caSMauro Carvalho Chehab 
6840c0d06caSMauro Carvalho Chehab 				if (probe == 0x26 || probe == 0x40) {
68537d5efb0SJoe Perches 					gspca_dbg(gspca_dev, D_PROBE,
68637d5efb0SJoe Perches 						  "probe=0x%02x -> OV2640\n",
6870c0d06caSMauro Carvalho Chehab 						  probe);
6880c0d06caSMauro Carvalho Chehab 					sd->sensor = ID_OV2640;
6890c0d06caSMauro Carvalho Chehab 					nb26 += 4;
6900c0d06caSMauro Carvalho Chehab 					break;
6910c0d06caSMauro Carvalho Chehab 				}
6920c0d06caSMauro Carvalho Chehab 				if (probe == 0x96 || probe == 0x55) {
69337d5efb0SJoe Perches 					gspca_dbg(gspca_dev, D_PROBE,
69437d5efb0SJoe Perches 						  "probe=0x%02x -> OV9655\n",
6950c0d06caSMauro Carvalho Chehab 						  probe);
6960c0d06caSMauro Carvalho Chehab 					sd->sensor = ID_OV9655;
6970c0d06caSMauro Carvalho Chehab 					nb96 += 4;
6980c0d06caSMauro Carvalho Chehab 					break;
6990c0d06caSMauro Carvalho Chehab 				}
70037d5efb0SJoe Perches 				gspca_dbg(gspca_dev, D_PROBE, "probe=0x%02x\n",
70137d5efb0SJoe Perches 					  probe);
7020c0d06caSMauro Carvalho Chehab 				if (probe == 0x00)
7030c0d06caSMauro Carvalho Chehab 					nb26++;
7040c0d06caSMauro Carvalho Chehab 				if (probe == 0xff)
7050c0d06caSMauro Carvalho Chehab 					nb96++;
7060c0d06caSMauro Carvalho Chehab 				msleep(3);
7070c0d06caSMauro Carvalho Chehab 			}
7080c0d06caSMauro Carvalho Chehab 			if (nb26 < 4 && nb96 < 4)
7090c0d06caSMauro Carvalho Chehab 				return -1;
7100c0d06caSMauro Carvalho Chehab 		} else {
71137d5efb0SJoe Perches 			gspca_dbg(gspca_dev, D_PROBE, "Not any 0xff -> MI2020\n");
7120c0d06caSMauro Carvalho Chehab 			sd->sensor = ID_MI2020;
7130c0d06caSMauro Carvalho Chehab 		}
7140c0d06caSMauro Carvalho Chehab 	}
7150c0d06caSMauro Carvalho Chehab 
7160c0d06caSMauro Carvalho Chehab 	if (_MI1320_) {
71737d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "05e3:f191 sensor MI1320 (1.3M)\n");
7180c0d06caSMauro Carvalho Chehab 	} else if (_MI2020_) {
71937d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "05e3:0503 sensor MI2020 (2.0M)\n");
7200c0d06caSMauro Carvalho Chehab 	} else if (_OV9655_) {
72137d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "05e3:0503 sensor OV9655 (1.3M)\n");
7220c0d06caSMauro Carvalho Chehab 	} else if (_OV2640_) {
72337d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "05e3:0503 sensor OV2640 (2.0M)\n");
7240c0d06caSMauro Carvalho Chehab 	} else {
72537d5efb0SJoe Perches 		gspca_dbg(gspca_dev, D_PROBE, "***** Unknown sensor *****\n");
7260c0d06caSMauro Carvalho Chehab 		return -1;
7270c0d06caSMauro Carvalho Chehab 	}
7280c0d06caSMauro Carvalho Chehab 
7290c0d06caSMauro Carvalho Chehab 	return 0;
7300c0d06caSMauro Carvalho Chehab }
731