stk014.c 14.8 KB
Newer Older
1 2 3
/*
 * Syntek DV4000 (STK014) subdriver
 *
4
 * Copyright (C) 2008 Jean-Francois Moine (http://moinejf.free.fr)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * 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 "stk014"

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

MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>");
MODULE_DESCRIPTION("Syntek DV4000 (STK014) USB Camera Driver");
MODULE_LICENSE("GPL");

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

	unsigned char brightness;
	unsigned char contrast;
	unsigned char colors;
37
	unsigned char lightfreq;
38
	u8 quality;
39
#define QUALITY_MIN 70
40
#define QUALITY_MAX 95
41
#define QUALITY_DEF 88
42

43
	u8 jpeg_hdr[JPEG_HDR_SZ];
44 45 46 47 48 49 50 51 52
};

/* V4L2 controls supported by the driver */
static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val);
static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val);
53 54
static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val);
static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val);
55

56
static const struct ctrl sd_ctrls[] = {
57 58 59 60 61 62 63 64
	{
	    {
		.id      = V4L2_CID_BRIGHTNESS,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Brightness",
		.minimum = 0,
		.maximum = 255,
		.step    = 1,
65 66
#define BRIGHTNESS_DEF 127
		.default_value = BRIGHTNESS_DEF,
67 68 69 70 71 72 73 74 75 76 77 78
	    },
	    .set = sd_setbrightness,
	    .get = sd_getbrightness,
	},
	{
	    {
		.id      = V4L2_CID_CONTRAST,
		.type    = V4L2_CTRL_TYPE_INTEGER,
		.name    = "Contrast",
		.minimum = 0,
		.maximum = 255,
		.step    = 1,
79 80
#define CONTRAST_DEF 127
		.default_value = CONTRAST_DEF,
81 82 83 84 85 86 87 88
	    },
	    .set = sd_setcontrast,
	    .get = sd_getcontrast,
	},
	{
	    {
		.id      = V4L2_CID_SATURATION,
		.type    = V4L2_CTRL_TYPE_INTEGER,
89
		.name    = "Color",
90 91 92
		.minimum = 0,
		.maximum = 255,
		.step    = 1,
93 94
#define COLOR_DEF 127
		.default_value = COLOR_DEF,
95 96 97 98
	    },
	    .set = sd_setcolors,
	    .get = sd_getcolors,
	},
99 100 101 102 103 104 105 106
	{
	    {
		.id	 = V4L2_CID_POWER_LINE_FREQUENCY,
		.type    = V4L2_CTRL_TYPE_MENU,
		.name    = "Light frequency filter",
		.minimum = 1,
		.maximum = 2,	/* 0: 0, 1: 50Hz, 2:60Hz */
		.step    = 1,
107 108
#define FREQ_DEF 1
		.default_value = FREQ_DEF,
109 110 111 112
	    },
	    .set = sd_setfreq,
	    .get = sd_getfreq,
	},
113 114
};

115
static const struct v4l2_pix_format vga_mode[] = {
116 117 118 119 120 121 122 123 124 125
	{320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 320,
		.sizeimage = 320 * 240 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 1},
	{640, 480, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
		.bytesperline = 640,
		.sizeimage = 640 * 480 * 3 / 8 + 590,
		.colorspace = V4L2_COLORSPACE_JPEG,
		.priv = 0},
126 127 128
};

/* -- read a register -- */
129
static u8 reg_r(struct gspca_dev *gspca_dev,
130
			__u16 index)
131 132
{
	struct usb_device *dev = gspca_dev->dev;
133
	int ret;
134

135 136
	if (gspca_dev->usb_err < 0)
		return 0;
137 138 139 140 141
	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
			0x00,
			USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			0x00,
			index,
142
			gspca_dev->usb_buf, 1,
143
			500);
144
	if (ret < 0) {
145
		PDEBUG(D_ERR, "reg_r err %d", ret);
146 147
		gspca_dev->usb_err = ret;
		return 0;
148 149
	}
	return gspca_dev->usb_buf[0];
150 151 152
}

/* -- write a register -- */
153
static void reg_w(struct gspca_dev *gspca_dev,
154 155 156 157 158
			__u16 index, __u16 value)
{
	struct usb_device *dev = gspca_dev->dev;
	int ret;

159 160
	if (gspca_dev->usb_err < 0)
		return;
161 162 163 164 165 166 167 168
	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
			0x01,
			USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
			value,
			index,
			NULL,
			0,
			500);
169
	if (ret < 0) {
170
		PDEBUG(D_ERR, "reg_w err %d", ret);
171 172
		gspca_dev->usb_err = ret;
	}
173 174
}

175
/* -- get a bulk value (4 bytes) -- */
176
static void rcv_val(struct gspca_dev *gspca_dev,
177
			int ads)
178 179 180 181
{
	struct usb_device *dev = gspca_dev->dev;
	int alen, ret;

182 183 184 185
	reg_w(gspca_dev, 0x634, (ads >> 16) & 0xff);
	reg_w(gspca_dev, 0x635, (ads >> 8) & 0xff);
	reg_w(gspca_dev, 0x636, ads & 0xff);
	reg_w(gspca_dev, 0x637, 0);
186 187
	reg_w(gspca_dev, 0x638, 4);	/* len & 0xff */
	reg_w(gspca_dev, 0x639, 0);	/* len >> 8 */
188 189 190
	reg_w(gspca_dev, 0x63a, 0);
	reg_w(gspca_dev, 0x63b, 0);
	reg_w(gspca_dev, 0x630, 5);
191 192
	if (gspca_dev->usb_err < 0)
		return;
193
	ret = usb_bulk_msg(dev,
194
			usb_rcvbulkpipe(dev, 0x05),
195 196
			gspca_dev->usb_buf,
			4,		/* length */
197
			&alen,
198
			500);		/* timeout in milliseconds */
199 200 201 202
	if (ret < 0) {
		PDEBUG(D_ERR, "rcv_val err %d", ret);
		gspca_dev->usb_err = ret;
	}
203 204
}

205
/* -- send a bulk value -- */
206
static void snd_val(struct gspca_dev *gspca_dev,
207 208 209 210 211
			int ads,
			unsigned int val)
{
	struct usb_device *dev = gspca_dev->dev;
	int alen, ret;
212
	__u8 seq = 0;
213 214

	if (ads == 0x003f08) {
215 216 217
		reg_r(gspca_dev, 0x0704);
		seq = reg_r(gspca_dev, 0x0705);
		reg_r(gspca_dev, 0x0650);
218
		reg_w(gspca_dev, 0x654, seq);
219
	} else {
220
		reg_w(gspca_dev, 0x654, (ads >> 16) & 0xff);
221
	}
222 223 224 225 226 227 228 229
	reg_w(gspca_dev, 0x655, (ads >> 8) & 0xff);
	reg_w(gspca_dev, 0x656, ads & 0xff);
	reg_w(gspca_dev, 0x657, 0);
	reg_w(gspca_dev, 0x658, 0x04);	/* size */
	reg_w(gspca_dev, 0x659, 0);
	reg_w(gspca_dev, 0x65a, 0);
	reg_w(gspca_dev, 0x65b, 0);
	reg_w(gspca_dev, 0x650, 5);
230 231
	if (gspca_dev->usb_err < 0)
		return;
232 233 234 235
	gspca_dev->usb_buf[0] = val >> 24;
	gspca_dev->usb_buf[1] = val >> 16;
	gspca_dev->usb_buf[2] = val >> 8;
	gspca_dev->usb_buf[3] = val;
236 237
	ret = usb_bulk_msg(dev,
			usb_sndbulkpipe(dev, 6),
238
			gspca_dev->usb_buf,
239 240 241
			4,
			&alen,
			500);	/* timeout in milliseconds */
242 243 244 245 246 247 248 249 250
	if (ret < 0) {
		PDEBUG(D_ERR, "snd_val err %d", ret);
		gspca_dev->usb_err = ret;
	} else {
		if (ads == 0x003f08) {
			seq += 4;
			seq &= 0x3f;
			reg_w(gspca_dev, 0x705, seq);
		}
251 252 253 254
	}
}

/* set a camera parameter */
255
static void set_par(struct gspca_dev *gspca_dev,
256 257
		   int parval)
{
258
	snd_val(gspca_dev, 0x003f08, parval);
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
}

static void setbrightness(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	int parval;

	parval = 0x06000000		/* whiteness */
		+ (sd->brightness << 16);
	set_par(gspca_dev, parval);
}

static void setcontrast(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	int parval;

	parval = 0x07000000		/* contrast */
		+ (sd->contrast << 16);
	set_par(gspca_dev, parval);
}

static void setcolors(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;
	int parval;

	parval = 0x08000000		/* saturation */
		+ (sd->colors << 16);
	set_par(gspca_dev, parval);
}

291 292 293 294 295 296 297 298 299
static void setfreq(struct gspca_dev *gspca_dev)
{
	struct sd *sd = (struct sd *) gspca_dev;

	set_par(gspca_dev, sd->lightfreq == 1
			? 0x33640000		/* 50 Hz */
			: 0x33780000);		/* 60 Hz */
}

300 301 302 303 304 305 306
/* 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;

	gspca_dev->cam.cam_mode = vga_mode;
307
	gspca_dev->cam.nmodes = ARRAY_SIZE(vga_mode);
308 309 310 311
	sd->brightness = BRIGHTNESS_DEF;
	sd->contrast = CONTRAST_DEF;
	sd->colors = COLOR_DEF;
	sd->lightfreq = FREQ_DEF;
312
	sd->quality = QUALITY_DEF;
313 314 315
	return 0;
}

316 317
/* this function is called at probe and resume time */
static int sd_init(struct gspca_dev *gspca_dev)
318
{
319
	u8 ret;
320 321 322

	/* check if the device responds */
	usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);
323
	ret = reg_r(gspca_dev, 0x0740);
324 325 326 327 328
	if (gspca_dev->usb_err >= 0) {
		if (ret != 0xff) {
			PDEBUG(D_ERR|D_STREAM, "init reg: 0x%02x", ret);
			gspca_dev->usb_err = -EIO;
		}
329
	}
330
	return gspca_dev->usb_err;
331 332 333
}

/* -- start the camera -- */
334
static int sd_start(struct gspca_dev *gspca_dev)
335
{
336
	struct sd *sd = (struct sd *) gspca_dev;
337 338
	int ret, value;

339 340 341 342 343
	/* create the JPEG header */
	jpeg_define(sd->jpeg_hdr, gspca_dev->height, gspca_dev->width,
			0x22);		/* JPEG 411 */
	jpeg_set_qual(sd->jpeg_hdr, sd->quality);

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	/* work on alternate 1 */
	usb_set_interface(gspca_dev->dev, gspca_dev->iface, 1);

	set_par(gspca_dev, 0x10000000);
	set_par(gspca_dev, 0x00000000);
	set_par(gspca_dev, 0x8002e001);
	set_par(gspca_dev, 0x14000000);
	if (gspca_dev->width > 320)
		value = 0x8002e001;		/* 640x480 */
	else
		value = 0x4001f000;		/* 320x240 */
	set_par(gspca_dev, value);
	ret = usb_set_interface(gspca_dev->dev,
					gspca_dev->iface,
					gspca_dev->alt);
359 360 361
	if (ret < 0) {
		PDEBUG(D_ERR|D_STREAM, "set intf %d %d failed",
			gspca_dev->iface, gspca_dev->alt);
362
		gspca_dev->usb_err = ret;
363
		goto out;
364
	}
365
	 reg_r(gspca_dev, 0x0630);
366
	rcv_val(gspca_dev, 0x000020);	/* << (value ff ff ff ff) */
367
	reg_r(gspca_dev, 0x0650);
368
	snd_val(gspca_dev, 0x000020, 0xffffffff);
369 370 371 372 373
	reg_w(gspca_dev, 0x0620, 0);
	reg_w(gspca_dev, 0x0630, 0);
	reg_w(gspca_dev, 0x0640, 0);
	reg_w(gspca_dev, 0x0650, 0);
	reg_w(gspca_dev, 0x0660, 0);
374 375 376 377 378 379 380
	setbrightness(gspca_dev);		/* whiteness */
	setcontrast(gspca_dev);			/* contrast */
	setcolors(gspca_dev);			/* saturation */
	set_par(gspca_dev, 0x09800000);		/* Red ? */
	set_par(gspca_dev, 0x0a800000);		/* Green ? */
	set_par(gspca_dev, 0x0b800000);		/* Blue ? */
	set_par(gspca_dev, 0x0d030000);		/* Gamma ? */
381
	setfreq(gspca_dev);			/* light frequency */
382 383 384 385

	/* start the video flow */
	set_par(gspca_dev, 0x01000000);
	set_par(gspca_dev, 0x01000000);
386 387 388
	if (gspca_dev->usb_err >= 0)
		PDEBUG(D_STREAM, "camera started alt: 0x%02x",
				gspca_dev->alt);
389
out:
390
	return gspca_dev->usb_err;
391 392 393 394 395 396 397 398 399
}

static void sd_stopN(struct gspca_dev *gspca_dev)
{
	struct usb_device *dev = gspca_dev->dev;

	set_par(gspca_dev, 0x02000000);
	set_par(gspca_dev, 0x02000000);
	usb_set_interface(dev, gspca_dev->iface, 1);
400 401 402
	reg_r(gspca_dev, 0x0630);
	rcv_val(gspca_dev, 0x000020);	/* << (value ff ff ff ff) */
	reg_r(gspca_dev, 0x0650);
403
	snd_val(gspca_dev, 0x000020, 0xffffffff);
404 405 406 407 408
	reg_w(gspca_dev, 0x0620, 0);
	reg_w(gspca_dev, 0x0630, 0);
	reg_w(gspca_dev, 0x0640, 0);
	reg_w(gspca_dev, 0x0650, 0);
	reg_w(gspca_dev, 0x0660, 0);
409 410 411 412
	PDEBUG(D_STREAM, "camera stopped");
}

static void sd_pkt_scan(struct gspca_dev *gspca_dev,
413
			u8 *data,			/* isoc packet */
414 415
			int len)			/* iso packet length */
{
416
	struct sd *sd = (struct sd *) gspca_dev;
417
	static unsigned char ffd9[] = {0xff, 0xd9};
418 419 420

	/* a frame starts with:
	 *	- 0xff 0xfe
421 422
	 *	- 0x08 0x00	- length (little endian ?!)
	 *	- 4 bytes = size of whole frame (BE - including header)
423 424 425
	 *	- 0x00 0x0c
	 *	- 0xff 0xd8
	 *	- ..	JPEG image with escape sequences (ff 00)
426
	 *		(without ending - ff d9)
427 428
	 */
	if (data[0] == 0xff && data[1] == 0xfe) {
429 430
		gspca_frame_add(gspca_dev, LAST_PACKET,
				ffd9, 2);
431

432
		/* put the JPEG 411 header */
433
		gspca_frame_add(gspca_dev, FIRST_PACKET,
434
			sd->jpeg_hdr, JPEG_HDR_SZ);
435 436 437

		/* beginning of the frame */
#define STKHDRSZ 12
438 439
		data += STKHDRSZ;
		len -= STKHDRSZ;
440
	}
441
	gspca_frame_add(gspca_dev, INTER_PACKET, data, len);
442 443 444 445 446 447 448 449 450
}

static int sd_setbrightness(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->brightness = val;
	if (gspca_dev->streaming)
		setbrightness(gspca_dev);
451
	return gspca_dev->usb_err;
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
}

static int sd_getbrightness(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->brightness;
	return 0;
}

static int sd_setcontrast(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->contrast = val;
	if (gspca_dev->streaming)
		setcontrast(gspca_dev);
469
	return gspca_dev->usb_err;
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
}

static int sd_getcontrast(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->contrast;
	return 0;
}

static int sd_setcolors(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->colors = val;
	if (gspca_dev->streaming)
		setcolors(gspca_dev);
487
	return gspca_dev->usb_err;
488 489 490 491 492 493 494 495 496 497
}

static int sd_getcolors(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->colors;
	return 0;
}

498 499 500 501 502 503 504
static int sd_setfreq(struct gspca_dev *gspca_dev, __s32 val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	sd->lightfreq = val;
	if (gspca_dev->streaming)
		setfreq(gspca_dev);
505
	return gspca_dev->usb_err;
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
}

static int sd_getfreq(struct gspca_dev *gspca_dev, __s32 *val)
{
	struct sd *sd = (struct sd *) gspca_dev;

	*val = sd->lightfreq;
	return 0;
}

static int sd_querymenu(struct gspca_dev *gspca_dev,
			struct v4l2_querymenu *menu)
{
	switch (menu->id) {
	case V4L2_CID_POWER_LINE_FREQUENCY:
		switch (menu->index) {
		case 1:		/* V4L2_CID_POWER_LINE_FREQUENCY_50HZ */
523
			strcpy((char *) menu->name, "50 Hz");
524 525
			return 0;
		case 2:		/* V4L2_CID_POWER_LINE_FREQUENCY_60HZ */
526
			strcpy((char *) menu->name, "60 Hz");
527 528 529 530 531 532 533
			return 0;
		}
		break;
	}
	return -EINVAL;
}

534 535 536 537 538 539 540 541 542 543 544 545 546
static int sd_set_jcomp(struct gspca_dev *gspca_dev,
			struct v4l2_jpegcompression *jcomp)
{
	struct sd *sd = (struct sd *) gspca_dev;

	if (jcomp->quality < QUALITY_MIN)
		sd->quality = QUALITY_MIN;
	else if (jcomp->quality > QUALITY_MAX)
		sd->quality = QUALITY_MAX;
	else
		sd->quality = jcomp->quality;
	if (gspca_dev->streaming)
		jpeg_set_qual(sd->jpeg_hdr, sd->quality);
547
	return gspca_dev->usb_err;
548 549 550 551 552 553 554 555 556 557 558 559 560 561
}

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);
	jcomp->quality = sd->quality;
	jcomp->jpeg_markers = V4L2_JPEG_MARKER_DHT
			| V4L2_JPEG_MARKER_DQT;
	return 0;
}

562
/* sub-driver description */
563
static const struct sd_desc sd_desc = {
564 565
	.name = MODULE_NAME,
	.ctrls = sd_ctrls,
566
	.nctrls = ARRAY_SIZE(sd_ctrls),
567
	.config = sd_config,
568
	.init = sd_init,
569 570 571
	.start = sd_start,
	.stopN = sd_stopN,
	.pkt_scan = sd_pkt_scan,
572
	.querymenu = sd_querymenu,
573 574
	.get_jcomp = sd_get_jcomp,
	.set_jcomp = sd_set_jcomp,
575 576 577
};

/* -- module initialisation -- */
578
static const __devinitdata struct usb_device_id device_table[] = {
579
	{USB_DEVICE(0x05e1, 0x0893)},
580 581 582 583 584 585 586 587
	{}
};
MODULE_DEVICE_TABLE(usb, device_table);

/* -- device connect -- */
static int sd_probe(struct usb_interface *intf,
			const struct usb_device_id *id)
{
588 589
	return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
				THIS_MODULE);
590 591 592 593 594 595 596
}

static struct usb_driver sd_driver = {
	.name = MODULE_NAME,
	.id_table = device_table,
	.probe = sd_probe,
	.disconnect = gspca_disconnect,
597 598 599 600
#ifdef CONFIG_PM
	.suspend = gspca_suspend,
	.resume = gspca_resume,
#endif
601 602 603 604 605
};

/* -- module insert / remove -- */
static int __init sd_mod_init(void)
{
606 607 608
	int ret;
	ret = usb_register(&sd_driver);
	if (ret < 0)
609
		return ret;
610
	info("registered");
611 612 613 614 615 616 617 618 619 620
	return 0;
}
static void __exit sd_mod_exit(void)
{
	usb_deregister(&sd_driver);
	info("deregistered");
}

module_init(sd_mod_init);
module_exit(sd_mod_exit);
反馈
建议
客服 返回
顶部