mars.c 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 *		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
 */

22 23
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

24 25 26 27 28 29 30 31 32 33 34 35
#define MODULE_NAME "mars"

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

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

37 38 39 40 41 42 43 44 45 46
	struct v4l2_ctrl_handler ctrl_handler;
	struct v4l2_ctrl *brightness;
	struct v4l2_ctrl *saturation;
	struct v4l2_ctrl *sharpness;
	struct v4l2_ctrl *gamma;
	struct { /* illuminator control cluster */
		struct v4l2_ctrl *illum_top;
		struct v4l2_ctrl *illum_bottom;
	};
	struct v4l2_ctrl *jpegqual;
47

48
	u8 quality;
49 50 51
#define QUALITY_MIN 40
#define QUALITY_MAX 70
#define QUALITY_DEF 50
52

53
	u8 jpeg_hdr[JPEG_HDR_SZ];
54 55 56
};

/* V4L2 controls supported by the driver */
57 58 59 60
static void setbrightness(struct gspca_dev *gspca_dev, s32 val);
static void setcolors(struct gspca_dev *gspca_dev, s32 val);
static void setgamma(struct gspca_dev *gspca_dev, s32 val);
static void setsharpness(struct gspca_dev *gspca_dev, s32 val);
61

62
static const struct v4l2_pix_format vga_mode[] = {
63 64
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
65
		.sizeimage = 320 * 240 * 3 / 8 + 590,
66 67 68 69 70 71 72
		.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},
73 74
};

75 76 77 78 79 80 81 82 83
static const __u8 mi_data[0x20] = {
/*	 01    02   03     04    05    06    07    08 */
	0x48, 0x22, 0x01, 0x47, 0x10, 0x00, 0x00, 0x00,
/*	 09    0a   0b     0c    0d    0e    0f    10 */
	0x00, 0x01, 0x30, 0x01, 0x30, 0x01, 0x30, 0x01,
/*	 11    12   13     14    15    16    17    18 */
	0x30, 0x00, 0x04, 0x00, 0x06, 0x01, 0xe2, 0x02,
/*	 19    1a   1b     1c    1d    1e    1f    20 */
	0x82, 0x00, 0x20, 0x17, 0x80, 0x08, 0x0c, 0x00
84 85
};

86
/* write <len> bytes from gspca_dev->usb_buf */
87
static void reg_w(struct gspca_dev *gspca_dev,
88
		 int len)
89
{
90 91
	int alen, ret;

92 93 94
	if (gspca_dev->usb_err < 0)
		return;

95 96 97 98 99 100
	ret = usb_bulk_msg(gspca_dev->dev,
			usb_sndbulkpipe(gspca_dev->dev, 4),
			gspca_dev->usb_buf,
			len,
			&alen,
			500);	/* timeout in milliseconds */
101
	if (ret < 0) {
102 103
		pr_err("reg write [%02x] error %d\n",
		       gspca_dev->usb_buf[0], ret);
104 105
		gspca_dev->usb_err = ret;
	}
106 107
}

108 109 110
static void mi_w(struct gspca_dev *gspca_dev,
		 u8 addr,
		 u8 value)
111 112 113
{
	gspca_dev->usb_buf[0] = 0x1f;
	gspca_dev->usb_buf[1] = 0;			/* control byte */
114 115
	gspca_dev->usb_buf[2] = addr;
	gspca_dev->usb_buf[3] = value;
116

117
	reg_w(gspca_dev, 4);
118 119
}

120
static void setbrightness(struct gspca_dev *gspca_dev, s32 val)
121 122
{
	gspca_dev->usb_buf[0] = 0x61;
123
	gspca_dev->usb_buf[1] = val;
124 125 126
	reg_w(gspca_dev, 2);
}

127
static void setcolors(struct gspca_dev *gspca_dev, s32 val)
128 129 130 131 132 133 134
{
	gspca_dev->usb_buf[0] = 0x5f;
	gspca_dev->usb_buf[1] = val << 3;
	gspca_dev->usb_buf[2] = ((val >> 2) & 0xf8) | 0x04;
	reg_w(gspca_dev, 3);
}

135
static void setgamma(struct gspca_dev *gspca_dev, s32 val)
136 137
{
	gspca_dev->usb_buf[0] = 0x06;
138
	gspca_dev->usb_buf[1] = val * 0x40;
139 140 141
	reg_w(gspca_dev, 2);
}

142
static void setsharpness(struct gspca_dev *gspca_dev, s32 val)
143 144
{
	gspca_dev->usb_buf[0] = 0x67;
145
	gspca_dev->usb_buf[1] = val * 4 + 3;
146 147 148
	reg_w(gspca_dev, 2);
}

149
static void setilluminators(struct gspca_dev *gspca_dev, bool top, bool bottom)
150
{
151
	/* both are off if not streaming */
152
	gspca_dev->usb_buf[0] = 0x22;
153
	if (top)
154
		gspca_dev->usb_buf[1] = 0x76;
155
	else if (bottom)
156 157 158 159 160 161
		gspca_dev->usb_buf[1] = 0x7a;
	else
		gspca_dev->usb_buf[1] = 0x7e;
	reg_w(gspca_dev, 2);
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
static int mars_s_ctrl(struct v4l2_ctrl *ctrl)
{
	struct sd *sd = container_of(ctrl->handler, struct sd, ctrl_handler);
	struct gspca_dev *gspca_dev = &sd->gspca_dev;

	gspca_dev->usb_err = 0;

	if (ctrl->id == V4L2_CID_ILLUMINATORS_1) {
		/* only one can be on at a time */
		if (ctrl->is_new && ctrl->val)
			sd->illum_bottom->val = 0;
		if (sd->illum_bottom->is_new && sd->illum_bottom->val)
			sd->illum_top->val = 0;
	}

	if (!gspca_dev->streaming)
		return 0;

	switch (ctrl->id) {
	case V4L2_CID_BRIGHTNESS:
		setbrightness(&sd->gspca_dev, ctrl->val);
		break;
	case V4L2_CID_SATURATION:
		setcolors(&sd->gspca_dev, ctrl->val);
		break;
	case V4L2_CID_GAMMA:
		setgamma(&sd->gspca_dev, ctrl->val);
		break;
	case V4L2_CID_ILLUMINATORS_1:
		setilluminators(&sd->gspca_dev, sd->illum_top->val,
						sd->illum_bottom->val);
		break;
	case V4L2_CID_SHARPNESS:
		setsharpness(&sd->gspca_dev, ctrl->val);
		break;
	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
		jpeg_set_qual(sd->jpeg_hdr, ctrl->val);
		break;
	default:
		return -EINVAL;
	}
	return gspca_dev->usb_err;
}

static const struct v4l2_ctrl_ops mars_ctrl_ops = {
	.s_ctrl = mars_s_ctrl,
};

/* this function is called at probe time */
static int sd_init_controls(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	struct v4l2_ctrl_handler *hdl = &sd->ctrl_handler;

	gspca_dev->vdev.ctrl_handler = hdl;
	v4l2_ctrl_handler_init(hdl, 7);
	sd->brightness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_BRIGHTNESS, 0, 30, 1, 15);
	sd->saturation = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_SATURATION, 0, 255, 1, 200);
	sd->gamma = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_GAMMA, 0, 3, 1, 1);
	sd->sharpness = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_SHARPNESS, 0, 2, 1, 1);
	sd->illum_top = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
	sd->illum_top->flags |= V4L2_CTRL_FLAG_UPDATE;
	sd->illum_bottom = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
	sd->illum_bottom->flags |= V4L2_CTRL_FLAG_UPDATE;
	sd->jpegqual = v4l2_ctrl_new_std(hdl, &mars_ctrl_ops,
			V4L2_CID_JPEG_COMPRESSION_QUALITY,
			QUALITY_MIN, QUALITY_MAX, 1, QUALITY_DEF);
	if (hdl->error) {
		pr_err("Could not initialize controls\n");
		return hdl->error;
	}
	v4l2_ctrl_cluster(2, &sd->illum_top);
	return 0;
}

243 244 245 246
/* this function is called at probe time */
static int sd_config(struct gspca_dev *gspca_dev,
			const struct usb_device_id *id)
{
247
	struct sd *sd = (struct sd *) gspca_dev;
248 249 250 251
	struct cam *cam;

	cam = &gspca_dev->cam;
	cam->cam_mode = vga_mode;
252
	cam->nmodes = ARRAY_SIZE(vga_mode);
253
	sd->quality = QUALITY_DEF;
254 255 256
	return 0;
}

257 258
/* this function is called at probe and resume time */
static int sd_init(struct gspca_dev *gspca_dev)
259 260 261 262
{
	return 0;
}

263
static int sd_start(struct gspca_dev *gspca_dev)
264
{
265 266
	struct sd *sd = (struct sd *) gspca_dev;
	u8 *data;
267
	int i;
268

269 270 271
	/* create the JPEG header */
	jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
			0x21);		/* JPEG 422 */
272
	jpeg_set_qual(sd->jpeg_hdr, v4l2_ctrl_g_ctrl(sd->jpegqual));
273

274
	data = gspca_dev->usb_buf;
275

276 277
	data[0] = 0x01;		/* address */
	data[1] = 0x01;
278
	reg_w(gspca_dev, 2);
279 280 281 282 283 284 285

	/*
	   Initialize the MR97113 chip register
	 */
	data[0] = 0x00;		/* address */
	data[1] = 0x0c | 0x01;	/* reg 0 */
	data[2] = 0x01;		/* reg 1 */
286 287
	data[3] = gspca_dev->width / 8;		/* h_size , reg 2 */
	data[4] = gspca_dev->height / 8;	/* v_size , reg 3 */
288 289
	data[5] = 0x30;		/* reg 4, MI, PAS5101 :
				 *	0x30 for 24mhz , 0x28 for 12mhz */
290
	data[6] = 0x02;		/* reg 5, H start - was 0x04 */
291
	data[7] = v4l2_ctrl_g_ctrl(sd->gamma) * 0x40;	/* reg 0x06: gamma */
292
	data[8] = 0x01;		/* reg 7, V start - was 0x03 */
293
/*	if (h_size == 320 ) */
294 295 296
/*		data[9]= 0x56;	 * reg 8, 24MHz, 2:1 scale down */
/*	else */
	data[9] = 0x52;		/* reg 8, 24MHz, no scale down */
297 298
/*jfm: from win trace*/
	data[10] = 0x18;
299

300
	reg_w(gspca_dev, 11);
301 302 303 304

	data[0] = 0x23;		/* address */
	data[1] = 0x09;		/* reg 35, append frame header */

305
	reg_w(gspca_dev, 2);
306

307 308
	data[0] = 0x3c;		/* address */
/*	if (gspca_dev->width == 1280) */
309 310 311 312 313
/*		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 */
314
	reg_w(gspca_dev, 2);
315 316 317

	/* auto dark-gain */
	data[0] = 0x5e;		/* address */
318 319
	data[1] = 0;		/* reg 94, Y Gain (auto) */
/*jfm: from win trace*/
320 321 322
				/* reg 0x5f/0x60 (LE) = saturation */
				/* h (60): xxxx x100
				 * l (5f): xxxx x000 */
323 324 325
	data[2] = v4l2_ctrl_g_ctrl(sd->saturation) << 3;
	data[3] = ((v4l2_ctrl_g_ctrl(sd->saturation) >> 2) & 0xf8) | 0x04;
	data[4] = v4l2_ctrl_g_ctrl(sd->brightness); /* reg 0x61 = brightness */
326 327
	data[5] = 0x00;

328
	reg_w(gspca_dev, 6);
329 330

	data[0] = 0x67;
331
/*jfm: from win trace*/
332
	data[1] = v4l2_ctrl_g_ctrl(sd->sharpness) * 4 + 3;
333
	data[2] = 0x14;
334
	reg_w(gspca_dev, 3);
335

336 337 338 339
	data[0] = 0x69;
	data[1] = 0x2f;
	data[2] = 0x28;
	data[3] = 0x42;
340
	reg_w(gspca_dev, 4);
341 342 343

	data[0] = 0x63;
	data[1] = 0x07;
344
	reg_w(gspca_dev, 2);
345 346 347 348 349
/*jfm: win trace - many writes here to reg 0x64*/

	/* initialize the MI sensor */
	for (i = 0; i < sizeof mi_data; i++)
		mi_w(gspca_dev, i + 1, mi_data[i]);
350 351

	data[0] = 0x00;
L
Lucas De Marchi 已提交
352
	data[1] = 0x4d;		/* ISOC transferring enable... */
353
	reg_w(gspca_dev, 2);
354

355 356 357
	setilluminators(gspca_dev, v4l2_ctrl_g_ctrl(sd->illum_top),
				   v4l2_ctrl_g_ctrl(sd->illum_bottom));

358
	return gspca_dev->usb_err;
359 360 361 362
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
363 364
	struct sd *sd = (struct sd *) gspca_dev;

365 366 367
	if (v4l2_ctrl_g_ctrl(sd->illum_top) ||
	    v4l2_ctrl_g_ctrl(sd->illum_bottom)) {
		setilluminators(gspca_dev, false, false);
368 369 370
		msleep(20);
	}

371 372
	gspca_dev->usb_buf[0] = 1;
	gspca_dev->usb_buf[1] = 0;
373
	reg_w(gspca_dev, 2);
374 375 376
}

static void sd_pkt_scan(struct gspca_dev *gspca_dev,
377
			u8 *data,			/* isoc packet */
378 379
			int len)			/* iso packet length */
{
380
	struct sd *sd = (struct sd *) gspca_dev;
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
	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) {
397
				PDEBUG(D_PACK, "sof offset: %d len: %d",
398
					p, len);
399 400
				gspca_frame_add(gspca_dev, LAST_PACKET,
						data, p);
401 402

				/* put the JPEG header */
403
				gspca_frame_add(gspca_dev, FIRST_PACKET,
404
					sd->jpeg_hdr, JPEG_HDR_SZ);
405 406
				data += p + 16;
				len -= p + 16;
407 408 409 410
				break;
			}
		}
	}
411
	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
412 413
}

414 415 416 417
static int sd_set_jcomp(struct gspca_dev *gspca_dev,
			struct v4l2_jpegcompression *jcomp)
{
	struct sd *sd = (struct sd *) gspca_dev;
418
	int ret;
419

420 421 422 423
	ret = v4l2_ctrl_s_ctrl(sd->jpegqual, jcomp->quality);
	if (ret)
		return ret;
	jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
424 425 426 427 428 429 430 431 432
	return 0;
}

static int sd_get_jcomp(struct gspca_dev *gspca_dev,
			struct v4l2_jpegcompression *jcomp)
{
	struct sd *sd = (struct sd *) gspca_dev;

	memset(jcomp, 0, sizeof *jcomp);
433
	jcomp->quality = v4l2_ctrl_g_ctrl(sd->jpegqual);
434 435 436 437 438
	jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
			| V4L2_JPEG_MARKER_DQT;
	return 0;
}

439
/* sub-driver description */
440
static const struct sd_desc sd_desc = {
441 442
	.name = MODULE_NAME,
	.config = sd_config,
443
	.init = sd_init,
444
	.init_controls = sd_init_controls,
445 446 447
	.start = sd_start,
	.stopN = sd_stopN,
	.pkt_scan = sd_pkt_scan,
448 449
	.get_jcomp = sd_get_jcomp,
	.set_jcomp = sd_set_jcomp,
450 451 452
};

/* -- module initialisation -- */
453
static const struct usb_device_id device_table[] = {
454
	{USB_DEVICE(0x093a, 0x050f)},
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	{}
};
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,
472 473 474
#ifdef CONFIG_PM
	.suspend = gspca_suspend,
	.resume = gspca_resume,
475
	.reset_resume = gspca_resume,
476
#endif
477 478
};

479
module_usb_driver(sd_driver);