mars.c 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *		Mars-Semi MR97311A library
 *		Copyright (C) 2005 <bradlch@hotmail.com>
 *
 * V4L2 by Jean-Francois Moine <http://moinejf.free.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#define MODULE_NAME "mars"

#include "gspca.h"
#include "jpeg.h"

27 28
#define DRIVER_VERSION_NUMBER	KERNEL_VERSION(2, 1, 7)
static const char version[] = "2.1.7";
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

MODULE_AUTHOR("Michel Xhaard <mxhaard@users.sourceforge.net>");
MODULE_DESCRIPTION("GSPCA/Mars USB Camera Driver");
MODULE_LICENSE("GPL");

/* specific webcam descriptor */
struct sd {
	struct gspca_dev gspca_dev;	/* !! must be the first item */

	char qindex;
};

/* V4L2 controls supported by the driver */
static struct ctrl sd_ctrls[] = {
};

45 46 47 48 49 50 51 52 53 54 55
static struct v4l2_pix_format vga_mode[] = {
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 589,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 2},
	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 640,
		.sizeimage = 640 * 480 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 1},
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
};

/* MI Register table //elvis */
enum {
	REG_HW_MI_0,
	REG_HW_MI_1,
	REG_HW_MI_2,
	REG_HW_MI_3,
	REG_HW_MI_4,
	REG_HW_MI_5,
	REG_HW_MI_6,
	REG_HW_MI_7,
	REG_HW_MI_9 = 0x09,
	REG_HW_MI_B = 0x0B,
	REG_HW_MI_C,
	REG_HW_MI_D,
	REG_HW_MI_1E = 0x1E,
	REG_HW_MI_20 = 0x20,
	REG_HW_MI_2B = 0x2B,
	REG_HW_MI_2C,
	REG_HW_MI_2D,
	REG_HW_MI_2E,
	REG_HW_MI_35 = 0x35,
	REG_HW_MI_5F = 0x5f,
	REG_HW_MI_60,
	REG_HW_MI_61,
	REG_HW_MI_62,
	REG_HW_MI_63,
	REG_HW_MI_64,
	REG_HW_MI_F1 = 0xf1,
86
	ATTR_TOTAL_MI_REG = 0xf2
87 88
};

89 90 91
/* the bytes to write are in gspca_dev->usb_buf */
static int reg_w(struct gspca_dev *gspca_dev,
		 __u16 index, int len)
92 93 94
{
	int rc;

95 96
	rc = usb_control_msg(gspca_dev->dev,
			 usb_sndbulkpipe(gspca_dev->dev, 4),
97
			 0x12,
98
			 0xc8,		/* ?? */
99
			 0,		/* value */
100
			 index, gspca_dev->usb_buf, len, 500);
101
	if (rc < 0)
102
		PDEBUG(D_ERR, "reg write [%02x] error %d", index, rc);
103 104 105
	return rc;
}

106 107
static int reg_w_buf(struct gspca_dev *gspca_dev,
			__u16 index, __u8 *buf, int len)
108
{
109 110 111 112 113 114 115 116 117 118 119 120
	int rc;

	rc = usb_control_msg(gspca_dev->dev,
			 usb_sndbulkpipe(gspca_dev->dev, 4),
			 0x12,
			 0xc8,		/* ?? */
			 0,		/* value */
			 index, buf, len, 500);
	if (rc < 0)
		PDEBUG(D_ERR, "reg write [%02x] error %d", index, rc);
	return rc;
}
121

122 123 124 125 126 127 128 129 130
static void bulk_w(struct gspca_dev *gspca_dev,
		   __u16 *pch,
		   __u16 Address)
{
	gspca_dev->usb_buf[0] = 0x1f;
	gspca_dev->usb_buf[1] = 0;			/* control byte */
	gspca_dev->usb_buf[2] = Address;
	gspca_dev->usb_buf[3] = *pch >> 8;		/* high byte */
	gspca_dev->usb_buf[4] = *pch;			/* low byte */
131

132
	reg_w(gspca_dev, Address, 5);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}

/* 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;

	cam = &gspca_dev->cam;
	cam->dev_name = (char *) id->driver_info;
	cam->epaddr = 0x01;
	cam->cam_mode = vga_mode;
	cam->nmodes = sizeof vga_mode / sizeof vga_mode[0];
	sd->qindex = 1;			/* set the quantization table */
	return 0;
}

/* this function is called at open time */
static int sd_open(struct gspca_dev *gspca_dev)
{
	return 0;
}

static void sd_start(struct gspca_dev *gspca_dev)
{
	int err_code;
160 161
	__u8 *data;
	__u16 *MI_buf;
162 163 164 165
	int h_size, v_size;
	int intpipe;

	PDEBUG(D_STREAM, "camera start, iface %d, alt 8", gspca_dev->iface);
166
	if (usb_set_interface(gspca_dev->dev, gspca_dev->iface, 8) < 0) {
167 168 169 170
		PDEBUG(D_ERR|D_STREAM, "Set packet size: set interface error");
		return;
	}

171
	data = gspca_dev->usb_buf;
172 173 174
	data[0] = 0x01;		/* address */
	data[1] = 0x01;

175
	err_code = reg_w(gspca_dev, data[0], 2);
176 177 178 179 180 181
	if (err_code < 0)
		return;

	/*
	   Initialize the MR97113 chip register
	 */
182
	data = kmalloc(16, GFP_KERNEL);
183 184 185 186 187 188 189 190 191 192 193 194
	data[0] = 0x00;		/* address */
	data[1] = 0x0c | 0x01;	/* reg 0 */
	data[2] = 0x01;		/* reg 1 */
	h_size = gspca_dev->width;
	v_size = gspca_dev->height;
	data[3] = h_size / 8;	/* h_size , reg 2 */
	data[4] = v_size / 8;	/* v_size , reg 3 */
	data[5] = 0x30;		/* reg 4, MI, PAS5101 :
				 *	0x30 for 24mhz , 0x28 for 12mhz */
	data[6] = 4;		/* reg 5, H start */
	data[7] = 0xc0;		/* reg 6, gamma 1.5 */
	data[8] = 3;		/* reg 7, V start */
195
/*	if (h_size == 320 ) */
196 197 198 199 200 201
/*		data[9]= 0x56;	 * reg 8, 24MHz, 2:1 scale down */
/*	else */
	data[9] = 0x52;		/* reg 8, 24MHz, no scale down */
	data[10] = 0x5d;	/* reg 9, I2C device address
				 *	[for PAS5101 (0x40)] [for MI (0x5d)] */

202 203
	err_code = reg_w_buf(gspca_dev, data[0], data, 11);
	kfree(data);
204 205 206
	if (err_code < 0)
		return;

207
	data = gspca_dev->usb_buf;
208 209 210
	data[0] = 0x23;		/* address */
	data[1] = 0x09;		/* reg 35, append frame header */

211 212
	err_code = reg_w(gspca_dev, data[0], 2);
	if (err_code < 0)
213 214
		return;

215 216
	data[0] = 0x3c;		/* address */
/*	if (gspca_dev->width == 1280) */
217 218 219 220 221
/*		data[1] = 200;	 * reg 60, pc-cam frame size
				 *	(unit: 4KB) 800KB */
/*	else */
	data[1] = 50;		/* 50 reg 60, pc-cam frame size
				 *	(unit: 4KB) 200KB */
222
	err_code = reg_w(gspca_dev, data[0], 2);
223 224 225 226 227 228
	if (err_code < 0)
		return;

	if (0) {			/* fixed dark-gain */
		data[1] = 0;		/* reg 94, Y Gain (1.75) */
		data[2] = 0;		/* reg 95, UV Gain (1.75) */
229 230
		data[3] = 0x3f;		/* reg 96, Y Gain/UV Gain/disable
					 *	auto dark-gain */
231 232 233 234 235
		data[4] = 0;		/* reg 97, set fixed dark level */
		data[5] = 0;		/* reg 98, don't care */
	} else {			/* auto dark-gain */
		data[1] = 0;		/* reg 94, Y Gain (auto) */
		data[2] = 0;		/* reg 95, UV Gain (1.75) */
236 237
		data[3] = 0x78;		/* reg 96, Y Gain/UV Gain/disable
					 *	auto dark-gain */
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
		switch (gspca_dev->width) {
/*		case 1280: */
/*			data[4] = 154;
				 * reg 97, %3 shadow point (unit: 256 pixel) */
/*			data[5] = 51;
				 * reg 98, %1 highlight point
				 *	(uint: 256 pixel) */
/*			break; */
		default:
/*		case 640: */
			data[4] = 36;	/* reg 97, %3 shadow point
					 *	(unit: 256 pixel) */
			data[5] = 12;	/* reg 98, %1 highlight point
					 *	(uint: 256 pixel) */
			break;
		case 320:
			data[4] = 9;	/* reg 97, %3 shadow point
					 *	(unit: 256 pixel) */
			data[5] = 3;	/* reg 98, %1 highlight point
					 *	(uint: 256 pixel) */
			break;
		}
	}
	/* auto dark-gain */
	data[0] = 0x5e;		/* address */

264
	err_code = reg_w(gspca_dev, data[0], 6);
265 266 267 268 269
	if (err_code < 0)
		return;

	data[0] = 0x67;
	data[1] = 0x13;		/* reg 103, first pixel B, disable sharpness */
270
	err_code = reg_w(gspca_dev, data[0], 2);
271 272 273 274 275 276
	if (err_code < 0)
		return;

	/*
	 * initialize the value of MI sensor...
	 */
277
	MI_buf = kzalloc(ATTR_TOTAL_MI_REG * sizeof *MI_buf, GFP_KERNEL);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
	MI_buf[REG_HW_MI_1] = 0x000a;
	MI_buf[REG_HW_MI_2] = 0x000c;
	MI_buf[REG_HW_MI_3] = 0x0405;
	MI_buf[REG_HW_MI_4] = 0x0507;
	/* mi_Attr_Reg_[REG_HW_MI_5]	 = 0x01ff;//13 */
	MI_buf[REG_HW_MI_5] = 0x0013;	/* 13 */
	MI_buf[REG_HW_MI_6] = 0x001f;	/* vertical blanking */
	/* mi_Attr_Reg_[REG_HW_MI_6]	 = 0x0400;  // vertical blanking */
	MI_buf[REG_HW_MI_7] = 0x0002;
	/* mi_Attr_Reg_[REG_HW_MI_9]	 = 0x015f; */
	/* mi_Attr_Reg_[REG_HW_MI_9]	 = 0x030f; */
	MI_buf[REG_HW_MI_9] = 0x0374;
	MI_buf[REG_HW_MI_B] = 0x0000;
	MI_buf[REG_HW_MI_C] = 0x0000;
	MI_buf[REG_HW_MI_D] = 0x0000;
	MI_buf[REG_HW_MI_1E] = 0x8000;
/* mi_Attr_Reg_[REG_HW_MI_20]	  = 0x1104; */
	MI_buf[REG_HW_MI_20] = 0x1104;	/* 0x111c; */
	MI_buf[REG_HW_MI_2B] = 0x0008;
/* mi_Attr_Reg_[REG_HW_MI_2C]	  = 0x000f; */
	MI_buf[REG_HW_MI_2C] = 0x001f;	/* lita suggest */
	MI_buf[REG_HW_MI_2D] = 0x0008;
	MI_buf[REG_HW_MI_2E] = 0x0008;
	MI_buf[REG_HW_MI_35] = 0x0051;
	MI_buf[REG_HW_MI_5F] = 0x0904;	/* fail to write */
	MI_buf[REG_HW_MI_60] = 0x0000;
	MI_buf[REG_HW_MI_61] = 0x0000;
	MI_buf[REG_HW_MI_62] = 0x0498;
	MI_buf[REG_HW_MI_63] = 0x0000;
	MI_buf[REG_HW_MI_64] = 0x0000;
	MI_buf[REG_HW_MI_F1] = 0x0001;
	/* changing while setting up the different value of dx/dy */

	if (gspca_dev->width != 1280) {
		MI_buf[0x01] = 0x010a;
		MI_buf[0x02] = 0x014c;
		MI_buf[0x03] = 0x01e5;
		MI_buf[0x04] = 0x0287;
	}
	MI_buf[0x20] = 0x1104;

319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
	bulk_w(gspca_dev, MI_buf + 1, 1);
	bulk_w(gspca_dev, MI_buf + 2, 2);
	bulk_w(gspca_dev, MI_buf + 3, 3);
	bulk_w(gspca_dev, MI_buf + 4, 4);
	bulk_w(gspca_dev, MI_buf + 5, 5);
	bulk_w(gspca_dev, MI_buf + 6, 6);
	bulk_w(gspca_dev, MI_buf + 7, 7);
	bulk_w(gspca_dev, MI_buf + 9, 9);
	bulk_w(gspca_dev, MI_buf + 0x0b, 0x0b);
	bulk_w(gspca_dev, MI_buf + 0x0c, 0x0c);
	bulk_w(gspca_dev, MI_buf + 0x0d, 0x0d);
	bulk_w(gspca_dev, MI_buf + 0x1e, 0x1e);
	bulk_w(gspca_dev, MI_buf + 0x20, 0x20);
	bulk_w(gspca_dev, MI_buf + 0x2b, 0x2b);
	bulk_w(gspca_dev, MI_buf + 0x2c, 0x2c);
	bulk_w(gspca_dev, MI_buf + 0x2d, 0x2d);
	bulk_w(gspca_dev, MI_buf + 0x2e, 0x2e);
	bulk_w(gspca_dev, MI_buf + 0x35, 0x35);
	bulk_w(gspca_dev, MI_buf + 0x5f, 0x5f);
	bulk_w(gspca_dev, MI_buf + 0x60, 0x60);
	bulk_w(gspca_dev, MI_buf + 0x61, 0x61);
	bulk_w(gspca_dev, MI_buf + 0x62, 0x62);
	bulk_w(gspca_dev, MI_buf + 0x63, 0x63);
	bulk_w(gspca_dev, MI_buf + 0x64, 0x64);
	bulk_w(gspca_dev, MI_buf + 0xf1, 0xf1);
	kfree(MI_buf);

	intpipe = usb_sndintpipe(gspca_dev->dev, 0);
	err_code = usb_clear_halt(gspca_dev->dev, intpipe);
348 349 350

	data[0] = 0x00;
	data[1] = 0x4d;		/* ISOC transfering enable... */
351
	reg_w(gspca_dev, data[0], 2);
352 353 354 355 356 357
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
	int result;

358 359 360
	gspca_dev->usb_buf[0] = 1;
	gspca_dev->usb_buf[1] = 0;
	result = reg_w(gspca_dev, gspca_dev->usb_buf[0], 2);
361 362 363 364 365 366 367 368 369 370 371 372 373 374
	if (result < 0)
		PDEBUG(D_ERR, "Camera Stop failed");
}

static void sd_stop0(struct gspca_dev *gspca_dev)
{
}

static void sd_close(struct gspca_dev *gspca_dev)
{
}

static void sd_pkt_scan(struct gspca_dev *gspca_dev,
			struct gspca_frame *frame,	/* target */
375
			__u8 *data,			/* isoc packet */
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
			int len)			/* iso packet length */
{
	struct sd *sd = (struct sd *) gspca_dev;
	int p;

	if (len < 6) {
/*		gspca_dev->last_packet_type = DISCARD_PACKET; */
		return;
	}
	for (p = 0; p < len - 6; p++) {
		if (data[0 + p] == 0xff
		    && data[1 + p] == 0xff
		    && data[2 + p] == 0x00
		    && data[3 + p] == 0xff
		    && data[4 + p] == 0x96) {
			if (data[5 + p] == 0x64
			    || data[5 + p] == 0x65
			    || data[5 + p] == 0x66
			    || data[5 + p] == 0x67) {
				PDEBUG(D_PACK, "sof offset: %d leng: %d",
					p, len);
				frame = gspca_frame_add(gspca_dev, LAST_PACKET,
							frame, data, 0);

				/* put the JPEG header */
				jpeg_put_header(gspca_dev, frame,
						sd->qindex, 0x21);
				data += 16;
				len -= 16;
				break;
			}
		}
	}
	gspca_frame_add(gspca_dev, INTER_PACKET, frame, data, len);
}

/* sub-driver description */
413
static const struct sd_desc sd_desc = {
414 415 416 417 418 419 420 421 422 423 424 425 426 427
	.name = MODULE_NAME,
	.ctrls = sd_ctrls,
	.nctrls = ARRAY_SIZE(sd_ctrls),
	.config = sd_config,
	.open = sd_open,
	.start = sd_start,
	.stopN = sd_stopN,
	.stop0 = sd_stop0,
	.close = sd_close,
	.pkt_scan = sd_pkt_scan,
};

/* -- module initialisation -- */
#define DVNM(name) .driver_info = (kernel_ulong_t) name
428
static const __devinitdata struct usb_device_id device_table[] = {
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	{USB_DEVICE(0x093a, 0x050f), DVNM("Mars-Semi Pc-Camera")},
	{}
};
MODULE_DEVICE_TABLE(usb, device_table);

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
				THIS_MODULE);
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
	if (usb_register(&sd_driver) < 0)
		return -1;
	PDEBUG(D_PROBE, "v%s registered", version);
	return 0;
}
static void __exit sd_mod_exit(void)
{
	usb_deregister(&sd_driver);
	PDEBUG(D_PROBE, "deregistered");
}

module_init(sd_mod_init);
module_exit(sd_mod_exit);