pwc-ctrl.c 39.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4
/* Driver for Philips webcam
   Functions that send various control messages to the webcam, including
   video modes.
   (C) 1999-2003 Nemosoft Unv.
5
   (C) 2004-2006 Luc Saillard (luc@saillard.org)
L
Linus Torvalds 已提交
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

   NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
   driver and thus may have bugs that are not present in the original version.
   Please send bug reports and support requests to <luc@saillard.org>.

   NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
   driver and thus may have bugs that are not present in the original version.
   Please send bug reports and support requests to <luc@saillard.org>.
   The decompression routines have been implemented by reverse-engineering the
   Nemosoft binary pwcx module. Caveat emptor.

   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
   (at your option) 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
*/

/*
   Changes
34 35
   2001/08/03  Alvarado   Added methods for changing white balance and
			  red/green gains
L
Linus Torvalds 已提交
36 37 38 39 40
 */

/* Control functions for the cam; brightness, contrast, video mode, etc. */

#ifdef __KERNEL__
41
#include <asm/uaccess.h>
L
Linus Torvalds 已提交
42 43
#endif
#include <asm/errno.h>
44

L
Linus Torvalds 已提交
45 46 47 48
#include "pwc.h"
#include "pwc-uncompress.h"
#include "pwc-kiara.h"
#include "pwc-timon.h"
49 50
#include "pwc-dec1.h"
#include "pwc-dec23.h"
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60

/* Request types: video */
#define SET_LUM_CTL			0x01
#define GET_LUM_CTL			0x02
#define SET_CHROM_CTL			0x03
#define GET_CHROM_CTL			0x04
#define SET_STATUS_CTL			0x05
#define GET_STATUS_CTL			0x06
#define SET_EP_STREAM_CTL		0x07
#define GET_EP_STREAM_CTL		0x08
61 62 63 64
#define GET_XX_CTL			0x09
#define SET_XX_CTL			0x0A
#define GET_XY_CTL			0x0B
#define SET_XY_CTL			0x0C
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
#define SET_MPT_CTL			0x0D
#define GET_MPT_CTL			0x0E

/* Selectors for the Luminance controls [GS]ET_LUM_CTL */
#define AGC_MODE_FORMATTER			0x2000
#define PRESET_AGC_FORMATTER			0x2100
#define SHUTTER_MODE_FORMATTER			0x2200
#define PRESET_SHUTTER_FORMATTER		0x2300
#define PRESET_CONTOUR_FORMATTER		0x2400
#define AUTO_CONTOUR_FORMATTER			0x2500
#define BACK_LIGHT_COMPENSATION_FORMATTER	0x2600
#define CONTRAST_FORMATTER			0x2700
#define DYNAMIC_NOISE_CONTROL_FORMATTER		0x2800
#define FLICKERLESS_MODE_FORMATTER		0x2900
#define AE_CONTROL_SPEED			0x2A00
#define BRIGHTNESS_FORMATTER			0x2B00
#define GAMMA_FORMATTER				0x2C00

/* Selectors for the Chrominance controls [GS]ET_CHROM_CTL */
#define WB_MODE_FORMATTER			0x1000
#define AWB_CONTROL_SPEED_FORMATTER		0x1100
#define AWB_CONTROL_DELAY_FORMATTER		0x1200
#define PRESET_MANUAL_RED_GAIN_FORMATTER	0x1300
#define PRESET_MANUAL_BLUE_GAIN_FORMATTER	0x1400
#define COLOUR_MODE_FORMATTER			0x1500
#define SATURATION_MODE_FORMATTER1		0x1600
#define SATURATION_MODE_FORMATTER2		0x1700

/* Selectors for the Status controls [GS]ET_STATUS_CTL */
#define SAVE_USER_DEFAULTS_FORMATTER		0x0200
#define RESTORE_USER_DEFAULTS_FORMATTER		0x0300
#define RESTORE_FACTORY_DEFAULTS_FORMATTER	0x0400
#define READ_AGC_FORMATTER			0x0500
#define READ_SHUTTER_FORMATTER			0x0600
#define READ_RED_GAIN_FORMATTER			0x0700
#define READ_BLUE_GAIN_FORMATTER		0x0800
101
#define GET_STATUS_B00				0x0B00
L
Linus Torvalds 已提交
102
#define SENSOR_TYPE_FORMATTER1			0x0C00
103
#define GET_STATUS_3000				0x3000
L
Linus Torvalds 已提交
104 105 106 107
#define READ_RAW_Y_MEAN_FORMATTER		0x3100
#define SET_POWER_SAVE_MODE_FORMATTER		0x3200
#define MIRROR_IMAGE_FORMATTER			0x3300
#define LED_FORMATTER				0x3400
108 109
#define LOWLIGHT				0x3500
#define GET_STATUS_3600				0x3600
L
Linus Torvalds 已提交
110
#define SENSOR_TYPE_FORMATTER2			0x3700
111 112 113 114
#define GET_STATUS_3800				0x3800
#define GET_STATUS_4000				0x4000
#define GET_STATUS_4100				0x4100	/* Get */
#define CTL_STATUS_4200				0x4200	/* [GS] 1 */
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123

/* Formatters for the Video Endpoint controls [GS]ET_EP_STREAM_CTL */
#define VIDEO_OUTPUT_CONTROL_FORMATTER		0x0100

/* Formatters for the motorized pan & tilt [GS]ET_MPT_CTL */
#define PT_RELATIVE_CONTROL_FORMATTER		0x01
#define PT_RESET_CONTROL_FORMATTER		0x02
#define PT_STATUS_FORMATTER			0x03

124
static const char *size2name[PSZ_MAX] =
L
Linus Torvalds 已提交
125 126 127 128 129 130 131
{
	"subQCIF",
	"QSIF",
	"QCIF",
	"SIF",
	"CIF",
	"VGA",
132
};
L
Linus Torvalds 已提交
133 134 135

/********/

136
/* Entries for the Nala (645/646) camera; the Nala doesn't have compression
L
Linus Torvalds 已提交
137
   preferences, so you either get compressed or non-compressed streams.
138

L
Linus Torvalds 已提交
139 140 141
   An alternate value of 0 means this mode is not available at all.
 */

142 143
#define PWC_FPS_MAX_NALA 8

L
Linus Torvalds 已提交
144 145 146 147 148 149 150
struct Nala_table_entry {
	char alternate;			/* USB alternate setting */
	int compressed;			/* Compressed yes/no */

	unsigned char mode[3];		/* precomputed mode table */
};

151 152 153
static unsigned int Nala_fps_vector[PWC_FPS_MAX_NALA] = { 4, 5, 7, 10, 12, 15, 20, 24 };

static struct Nala_table_entry Nala_table[PSZ_MAX][PWC_FPS_MAX_NALA] =
L
Linus Torvalds 已提交
154 155 156 157
{
#include "pwc-nala.h"
};

158
static void pwc_set_image_buffer_size(struct pwc_device *pdev);
L
Linus Torvalds 已提交
159 160 161

/****************************************************************************/

162 163 164 165 166 167 168 169 170 171 172 173
static int _send_control_msg(struct pwc_device *pdev,
	u8 request, u16 value, int index, void *buf, int buflen, int timeout)
{
	int rc;
	void *kbuf = NULL;

	if (buflen) {
		kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */
		if (kbuf == NULL)
			return -ENOMEM;
		memcpy(kbuf, buf, buflen);
	}
L
Linus Torvalds 已提交
174

175 176 177 178 179 180
	rc = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
		request,
		USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		index,
		kbuf, buflen, timeout);
L
Linus Torvalds 已提交
181

182 183 184
	kfree(kbuf);
	return rc;
}
L
Linus Torvalds 已提交
185

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
static int recv_control_msg(struct pwc_device *pdev,
	u8 request, u16 value, void *buf, int buflen)
{
	int rc;
	void *kbuf = kmalloc(buflen, GFP_KERNEL); /* not allowed on stack */

	if (kbuf == NULL)
		return -ENOMEM;

	rc = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
		request,
		USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
		value,
		pdev->vcinterface,
		kbuf, buflen, 500);
	memcpy(buf, kbuf, buflen);
	kfree(kbuf);
	return rc;
}
L
Linus Torvalds 已提交
205

206 207
static inline int send_video_command(struct pwc_device *pdev,
	int index, void *buf, int buflen)
L
Linus Torvalds 已提交
208
{
209
	return _send_control_msg(pdev,
L
Linus Torvalds 已提交
210 211 212 213 214 215
		SET_EP_STREAM_CTL,
		VIDEO_OUTPUT_CONTROL_FORMATTER,
		index,
		buf, buflen, 1000);
}

216 217 218 219 220 221 222
static inline int send_control_msg(struct pwc_device *pdev,
	u8 request, u16 value, void *buf, int buflen)
{
	return _send_control_msg(pdev,
		request, value, pdev->vcinterface, buf, buflen, 500);
}

L
Linus Torvalds 已提交
223 224


225
static int set_video_mode_Nala(struct pwc_device *pdev, int size, int frames)
L
Linus Torvalds 已提交
226 227 228 229 230 231 232 233
{
	unsigned char buf[3];
	int ret, fps;
	struct Nala_table_entry *pEntry;
	int frames2frames[31] =
	{ /* closest match of framerate */
	   0,  0,  0,  0,  4,  /*  0-4  */
	   5,  5,  7,  7, 10,  /*  5-9  */
234 235 236 237 238
	  10, 10, 12, 12, 15,  /* 10-14 */
	  15, 15, 15, 20, 20,  /* 15-19 */
	  20, 20, 20, 24, 24,  /* 20-24 */
	  24, 24, 24, 24, 24,  /* 25-29 */
	  24                   /* 30    */
L
Linus Torvalds 已提交
239
	};
240
	int frames2table[31] =
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248
	{ 0, 0, 0, 0, 0, /*  0-4  */
	  1, 1, 1, 2, 2, /*  5-9  */
	  3, 3, 4, 4, 4, /* 10-14 */
	  5, 5, 5, 5, 5, /* 15-19 */
	  6, 6, 6, 6, 7, /* 20-24 */
	  7, 7, 7, 7, 7, /* 25-29 */
	  7              /* 30    */
	};
249

L
Linus Torvalds 已提交
250 251 252 253 254 255 256 257
	if (size < 0 || size > PSZ_CIF || frames < 4 || frames > 25)
		return -EINVAL;
	frames = frames2frames[frames];
	fps = frames2table[frames];
	pEntry = &Nala_table[size][fps];
	if (pEntry->alternate == 0)
		return -EINVAL;

258
	memcpy(buf, pEntry->mode, 3);
259
	ret = send_video_command(pdev, pdev->vendpoint, buf, 3);
L
Linus Torvalds 已提交
260
	if (ret < 0) {
261
		PWC_DEBUG_MODULE("Failed to send video command... %d\n", ret);
L
Linus Torvalds 已提交
262 263
		return ret;
	}
264
	if (pEntry->compressed && pdev->pixfmt == V4L2_PIX_FMT_YUV420)
265
		pwc_dec1_init(pdev->type, pdev->release, buf, pdev->decompress_data);
266

L
Linus Torvalds 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	pdev->cmd_len = 3;
	memcpy(pdev->cmd_buf, buf, 3);

	/* Set various parameters */
	pdev->vframes = frames;
	pdev->vsize = size;
	pdev->valternate = pEntry->alternate;
	pdev->image = pwc_image_sizes[size];
	pdev->frame_size = (pdev->image.x * pdev->image.y * 3) / 2;
	if (pEntry->compressed) {
		if (pdev->release < 5) { /* 4 fold compression */
			pdev->vbandlength = 528;
			pdev->frame_size /= 4;
		}
		else {
			pdev->vbandlength = 704;
			pdev->frame_size /= 3;
		}
	}
	else
		pdev->vbandlength = 0;
	return 0;
}


292
static int set_video_mode_Timon(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
L
Linus Torvalds 已提交
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 319
{
	unsigned char buf[13];
	const struct Timon_table_entry *pChoose;
	int ret, fps;

	if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
		return -EINVAL;
	if (size == PSZ_VGA && frames > 15)
		return -EINVAL;
	fps = (frames / 5) - 1;

	/* Find a supported framerate with progressively higher compression ratios
	   if the preferred ratio is not available.
	*/
	pChoose = NULL;
	while (compression <= 3) {
	   pChoose = &Timon_table[size][fps][compression];
	   if (pChoose->alternate != 0)
	     break;
	   compression++;
	}
	if (pChoose == NULL || pChoose->alternate == 0)
		return -ENOENT; /* Not supported. */

	memcpy(buf, pChoose->mode, 13);
	if (snapshot)
		buf[0] |= 0x80;
320
	ret = send_video_command(pdev, pdev->vendpoint, buf, 13);
L
Linus Torvalds 已提交
321 322 323
	if (ret < 0)
		return ret;

324
	if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420)
325
		pwc_dec23_init(pdev, pdev->type, buf);
L
Linus Torvalds 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

	pdev->cmd_len = 13;
	memcpy(pdev->cmd_buf, buf, 13);

	/* Set various parameters */
	pdev->vframes = frames;
	pdev->vsize = size;
	pdev->vsnapshot = snapshot;
	pdev->valternate = pChoose->alternate;
	pdev->image = pwc_image_sizes[size];
	pdev->vbandlength = pChoose->bandlength;
	if (pChoose->bandlength > 0)
		pdev->frame_size = (pChoose->bandlength * pdev->image.y) / 4;
	else
		pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
	return 0;
}


345
static int set_video_mode_Kiara(struct pwc_device *pdev, int size, int frames, int compression, int snapshot)
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358
{
	const struct Kiara_table_entry *pChoose = NULL;
	int fps, ret;
	unsigned char buf[12];
	struct Kiara_table_entry RawEntry = {6, 773, 1272, {0xAD, 0xF4, 0x10, 0x27, 0xB6, 0x24, 0x96, 0x02, 0x30, 0x05, 0x03, 0x80}};

	if (size >= PSZ_MAX || frames < 5 || frames > 30 || compression < 0 || compression > 3)
		return -EINVAL;
	if (size == PSZ_VGA && frames > 15)
		return -EINVAL;
	fps = (frames / 5) - 1;

	/* special case: VGA @ 5 fps and snapshot is raw bayer mode */
359
	if (size == PSZ_VGA && frames == 5 && snapshot && pdev->pixfmt != V4L2_PIX_FMT_YUV420)
L
Linus Torvalds 已提交
360
	{
361 362 363
		/* Only available in case the raw palette is selected or
		   we have the decompressor available. This mode is
		   only available in compressed form
L
Linus Torvalds 已提交
364
		*/
365 366
		PWC_DEBUG_SIZE("Choosing VGA/5 BAYER mode.\n");
		pChoose = &RawEntry;
L
Linus Torvalds 已提交
367 368 369
	}
	else
	{
370
		/* Find a supported framerate with progressively higher compression ratios
L
Linus Torvalds 已提交
371
		   if the preferred ratio is not available.
372
		   Skip this step when using RAW modes.
L
Linus Torvalds 已提交
373
		*/
374
		snapshot = 0;
L
Linus Torvalds 已提交
375 376 377 378 379 380 381 382 383 384
		while (compression <= 3) {
			pChoose = &Kiara_table[size][fps][compression];
			if (pChoose->alternate != 0)
				break;
			compression++;
		}
	}
	if (pChoose == NULL || pChoose->alternate == 0)
		return -ENOENT; /* Not supported. */

385
	PWC_TRACE("Using alternate setting %d.\n", pChoose->alternate);
386

L
Linus Torvalds 已提交
387 388 389 390 391 392
	/* usb_control_msg won't take staticly allocated arrays as argument?? */
	memcpy(buf, pChoose->mode, 12);
	if (snapshot)
		buf[0] |= 0x80;

	/* Firmware bug: video endpoint is 5, but commands are sent to endpoint 4 */
393
	ret = send_video_command(pdev, 4 /* pdev->vendpoint */, buf, 12);
L
Linus Torvalds 已提交
394 395 396
	if (ret < 0)
		return ret;

397
	if (pChoose->bandlength > 0 && pdev->pixfmt == V4L2_PIX_FMT_YUV420)
398
		pwc_dec23_init(pdev, pdev->type, buf);
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412

	pdev->cmd_len = 12;
	memcpy(pdev->cmd_buf, buf, 12);
	/* All set and go */
	pdev->vframes = frames;
	pdev->vsize = size;
	pdev->vsnapshot = snapshot;
	pdev->valternate = pChoose->alternate;
	pdev->image = pwc_image_sizes[size];
	pdev->vbandlength = pChoose->bandlength;
	if (pdev->vbandlength > 0)
		pdev->frame_size = (pdev->vbandlength * pdev->image.y) / 4;
	else
		pdev->frame_size = (pdev->image.x * pdev->image.y * 12) / 8;
413 414
	PWC_TRACE("frame_size=%d, vframes=%d, vsize=%d, vsnapshot=%d, vbandlength=%d\n",
	    pdev->frame_size,pdev->vframes,pdev->vsize,pdev->vsnapshot,pdev->vbandlength);
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	return 0;
}



/**
   @pdev: device structure
   @width: viewport width
   @height: viewport height
   @frame: framerate, in fps
   @compression: preferred compression ratio
   @snapshot: snapshot mode or streaming
 */
int pwc_set_video_mode(struct pwc_device *pdev, int width, int height, int frames, int compression, int snapshot)
{
430
	int ret, size;
L
Linus Torvalds 已提交
431

432
	PWC_DEBUG_FLOW("set_video_mode(%dx%d @ %d, pixfmt %08x).\n", width, height, frames, pdev->pixfmt);
L
Linus Torvalds 已提交
433 434
	size = pwc_decode_size(pdev, width, height);
	if (size < 0) {
435
		PWC_DEBUG_MODULE("Could not find suitable size.\n");
L
Linus Torvalds 已提交
436 437
		return -ERANGE;
	}
438
	PWC_TRACE("decode_size = %d.\n", size);
L
Linus Torvalds 已提交
439

440
	if (DEVICE_USE_CODEC1(pdev->type)) {
L
Linus Torvalds 已提交
441
		ret = set_video_mode_Nala(pdev, size, frames);
442

443
	} else if (DEVICE_USE_CODEC3(pdev->type)) {
L
Linus Torvalds 已提交
444
		ret = set_video_mode_Kiara(pdev, size, frames, compression, snapshot);
445 446 447

	} else {
		ret = set_video_mode_Timon(pdev, size, frames, compression, snapshot);
L
Linus Torvalds 已提交
448 449
	}
	if (ret < 0) {
450
		PWC_ERROR("Failed to set video mode %s@%d fps; return code = %d\n", size2name[size], frames, ret);
L
Linus Torvalds 已提交
451 452 453 454 455 456
		return ret;
	}
	pdev->view.x = width;
	pdev->view.y = height;
	pdev->frame_total_size = pdev->frame_size + pdev->frame_header_size + pdev->frame_trailer_size;
	pwc_set_image_buffer_size(pdev);
457
	PWC_DEBUG_SIZE("Set viewport to %dx%d, image size is %dx%d.\n", width, height, pwc_image_sizes[size].x, pwc_image_sizes[size].y);
L
Linus Torvalds 已提交
458 459 460
	return 0;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
static unsigned int pwc_get_fps_Nala(struct pwc_device *pdev, unsigned int index, unsigned int size)
{
	unsigned int i;

	for (i = 0; i < PWC_FPS_MAX_NALA; i++) {
		if (Nala_table[size][i].alternate) {
			if (index--==0) return Nala_fps_vector[i];
		}
	}
	return 0;
}

static unsigned int pwc_get_fps_Kiara(struct pwc_device *pdev, unsigned int index, unsigned int size)
{
	unsigned int i;

	for (i = 0; i < PWC_FPS_MAX_KIARA; i++) {
		if (Kiara_table[size][i][3].alternate) {
			if (index--==0) return Kiara_fps_vector[i];
		}
	}
	return 0;
}

static unsigned int pwc_get_fps_Timon(struct pwc_device *pdev, unsigned int index, unsigned int size)
{
	unsigned int i;

	for (i=0; i < PWC_FPS_MAX_TIMON; i++) {
		if (Timon_table[size][i][3].alternate) {
			if (index--==0) return Timon_fps_vector[i];
		}
	}
	return 0;
}

unsigned int pwc_get_fps(struct pwc_device *pdev, unsigned int index, unsigned int size)
{
	unsigned int ret;

	if (DEVICE_USE_CODEC1(pdev->type)) {
		ret = pwc_get_fps_Nala(pdev, index, size);

	} else if (DEVICE_USE_CODEC3(pdev->type)) {
		ret = pwc_get_fps_Kiara(pdev, index, size);

	} else {
		ret = pwc_get_fps_Timon(pdev, index, size);
	}

	return ret;
}

514 515 516 517 518 519 520 521
#define BLACK_Y 0
#define BLACK_U 128
#define BLACK_V 128

static void pwc_set_image_buffer_size(struct pwc_device *pdev)
{
	int i, factor = 0;

522 523 524
	/* for V4L2_PIX_FMT_YUV420 */
	switch (pdev->pixfmt) {
	case V4L2_PIX_FMT_YUV420:
525 526
		factor = 6;
		break;
527 528
	case V4L2_PIX_FMT_PWC1:
	case V4L2_PIX_FMT_PWC2:
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
		factor = 6; /* can be uncompressed YUV420P */
		break;
	}

	/* Set sizes in bytes */
	pdev->image.size = pdev->image.x * pdev->image.y * factor / 4;
	pdev->view.size  = pdev->view.x  * pdev->view.y  * factor / 4;

	/* Align offset, or you'll get some very weird results in
	   YUV420 mode... x must be multiple of 4 (to get the Y's in
	   place), and y even (or you'll mixup U & V). This is less of a
	   problem for YUV420P.
	 */
	pdev->offset.x = ((pdev->view.x - pdev->image.x) / 2) & 0xFFFC;
	pdev->offset.y = ((pdev->view.y - pdev->image.y) / 2) & 0xFFFE;

	/* Fill buffers with black colors */
	for (i = 0; i < pwc_mbufs; i++) {
		unsigned char *p = pdev->image_data + pdev->images[i].offset;
		memset(p, BLACK_Y, pdev->view.x * pdev->view.y);
		p += pdev->view.x * pdev->view.y;
		memset(p, BLACK_U, pdev->view.x * pdev->view.y/4);
		p += pdev->view.x * pdev->view.y/4;
		memset(p, BLACK_V, pdev->view.x * pdev->view.y/4);
	}
}


L
Linus Torvalds 已提交
557 558 559 560 561 562 563 564

/* BRIGHTNESS */

int pwc_get_brightness(struct pwc_device *pdev)
{
	char buf;
	int ret;

565 566
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, BRIGHTNESS_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
567 568
	if (ret < 0)
		return ret;
569
	return buf;
L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578 579 580
}

int pwc_set_brightness(struct pwc_device *pdev, int value)
{
	char buf;

	if (value < 0)
		value = 0;
	if (value > 0xffff)
		value = 0xffff;
	buf = (value >> 9) & 0x7f;
581 582
	return send_control_msg(pdev,
		SET_LUM_CTL, BRIGHTNESS_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591
}

/* CONTRAST */

int pwc_get_contrast(struct pwc_device *pdev)
{
	char buf;
	int ret;

592 593
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, CONTRAST_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
594 595
	if (ret < 0)
		return ret;
596
	return buf;
L
Linus Torvalds 已提交
597 598 599 600 601 602 603 604 605 606 607
}

int pwc_set_contrast(struct pwc_device *pdev, int value)
{
	char buf;

	if (value < 0)
		value = 0;
	if (value > 0xffff)
		value = 0xffff;
	buf = (value >> 10) & 0x3f;
608 609
	return send_control_msg(pdev,
		SET_LUM_CTL, CONTRAST_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
610 611 612 613 614 615 616 617
}

/* GAMMA */

int pwc_get_gamma(struct pwc_device *pdev)
{
	char buf;
	int ret;
618

619 620
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, GAMMA_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
621 622
	if (ret < 0)
		return ret;
623
	return buf;
L
Linus Torvalds 已提交
624 625 626 627 628 629 630 631 632 633 634
}

int pwc_set_gamma(struct pwc_device *pdev, int value)
{
	char buf;

	if (value < 0)
		value = 0;
	if (value > 0xffff)
		value = 0xffff;
	buf = (value >> 11) & 0x1f;
635 636
	return send_control_msg(pdev,
		SET_LUM_CTL, GAMMA_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
637 638 639 640 641
}


/* SATURATION */

642 643
/* return a value between [-100 , 100] */
int pwc_get_saturation(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
644 645
{
	char buf;
646
	int ret, saturation_register;
L
Linus Torvalds 已提交
647 648

	if (pdev->type < 675)
649 650 651 652 653
		return -EINVAL;
	if (pdev->type < 730)
		saturation_register = SATURATION_MODE_FORMATTER2;
	else
		saturation_register = SATURATION_MODE_FORMATTER1;
654 655
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, saturation_register, &buf, sizeof(buf));
L
Linus Torvalds 已提交
656 657
	if (ret < 0)
		return ret;
658 659
	*value = (signed)buf;
	return 0;
L
Linus Torvalds 已提交
660 661
}

662
/* @param value saturation color between [-100 , 100] */
L
Linus Torvalds 已提交
663 664 665
int pwc_set_saturation(struct pwc_device *pdev, int value)
{
	char buf;
666
	int saturation_register;
L
Linus Torvalds 已提交
667 668 669

	if (pdev->type < 675)
		return -EINVAL;
670 671 672 673 674 675 676 677
	if (value < -100)
		value = -100;
	if (value > 100)
		value = 100;
	if (pdev->type < 730)
		saturation_register = SATURATION_MODE_FORMATTER2;
	else
		saturation_register = SATURATION_MODE_FORMATTER1;
678 679
	return send_control_msg(pdev,
		SET_CHROM_CTL, saturation_register, &buf, sizeof(buf));
L
Linus Torvalds 已提交
680 681 682 683
}

/* AGC */

684
int pwc_set_agc(struct pwc_device *pdev, int mode, int value)
L
Linus Torvalds 已提交
685 686 687
{
	char buf;
	int ret;
688

L
Linus Torvalds 已提交
689 690 691 692 693
	if (mode)
		buf = 0x0; /* auto */
	else
		buf = 0xff; /* fixed */

694 695
	ret = send_control_msg(pdev,
		SET_LUM_CTL, AGC_MODE_FORMATTER, &buf, sizeof(buf));
696

L
Linus Torvalds 已提交
697 698 699 700 701 702
	if (!mode && ret >= 0) {
		if (value < 0)
			value = 0;
		if (value > 0xffff)
			value = 0xffff;
		buf = (value >> 10) & 0x3F;
703 704
		ret = send_control_msg(pdev,
			SET_LUM_CTL, PRESET_AGC_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
705 706 707 708 709 710
	}
	if (ret < 0)
		return ret;
	return 0;
}

711
int pwc_get_agc(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
712 713 714
{
	unsigned char buf;
	int ret;
715

716 717
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, AGC_MODE_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
718 719 720 721
	if (ret < 0)
		return ret;

	if (buf != 0) { /* fixed */
722 723
		ret = recv_control_msg(pdev,
			GET_LUM_CTL, PRESET_AGC_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
724 725 726 727
		if (ret < 0)
			return ret;
		if (buf > 0x3F)
			buf = 0x3F;
728
		*value = (buf << 10);
L
Linus Torvalds 已提交
729 730
	}
	else { /* auto */
731 732
		ret = recv_control_msg(pdev,
			GET_STATUS_CTL, READ_AGC_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
733 734 735 736 737 738 739 740 741 742 743
		if (ret < 0)
			return ret;
		/* Gah... this value ranges from 0x00 ... 0x9F */
		if (buf > 0x9F)
			buf = 0x9F;
		*value = -(48 + buf * 409);
	}

	return 0;
}

744
int pwc_set_shutter_speed(struct pwc_device *pdev, int mode, int value)
L
Linus Torvalds 已提交
745 746 747 748 749 750 751 752 753
{
	char buf[2];
	int speed, ret;


	if (mode)
		buf[0] = 0x0;	/* auto */
	else
		buf[0] = 0xff; /* fixed */
754

755
	ret = send_control_msg(pdev,
756
		SET_LUM_CTL, SHUTTER_MODE_FORMATTER, &buf, 1);
L
Linus Torvalds 已提交
757 758 759 760 761 762

	if (!mode && ret >= 0) {
		if (value < 0)
			value = 0;
		if (value > 0xffff)
			value = 0xffff;
763 764

		if (DEVICE_USE_CODEC2(pdev->type)) {
L
Linus Torvalds 已提交
765 766 767 768
			/* speed ranges from 0x0 to 0x290 (656) */
			speed = (value / 100);
			buf[1] = speed >> 8;
			buf[0] = speed & 0xff;
769
		} else if (DEVICE_USE_CODEC3(pdev->type)) {
L
Linus Torvalds 已提交
770 771 772 773 774
			/* speed seems to range from 0x0 to 0xff */
			buf[1] = 0;
			buf[0] = value >> 8;
		}

775 776 777
		ret = send_control_msg(pdev,
			SET_LUM_CTL, PRESET_SHUTTER_FORMATTER,
			&buf, sizeof(buf));
L
Linus Torvalds 已提交
778 779
	}
	return ret;
780
}
L
Linus Torvalds 已提交
781

782 783 784 785 786 787
/* This function is not exported to v4l1, so output values between 0 -> 256 */
int pwc_get_shutter_speed(struct pwc_device *pdev, int *value)
{
	unsigned char buf[2];
	int ret;

788 789
	ret = recv_control_msg(pdev,
		GET_STATUS_CTL, READ_SHUTTER_FORMATTER, &buf, sizeof(buf));
790 791 792 793 794 795 796 797 798 799 800 801
	if (ret < 0)
		return ret;
	*value = buf[0] + (buf[1] << 8);
	if (DEVICE_USE_CODEC2(pdev->type)) {
		/* speed ranges from 0x0 to 0x290 (656) */
		*value *= 256/656;
	} else if (DEVICE_USE_CODEC3(pdev->type)) {
		/* speed seems to range from 0x0 to 0xff */
	}
	return 0;
}

L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813 814 815

/* POWER */

int pwc_camera_power(struct pwc_device *pdev, int power)
{
	char buf;

	if (pdev->type < 675 || (pdev->type < 730 && pdev->release < 6))
		return 0;	/* Not supported by Nala or Timon < release 6 */

	if (power)
		buf = 0x00; /* active */
	else
		buf = 0xFF; /* power save */
816 817 818
	return send_control_msg(pdev,
		SET_STATUS_CTL, SET_POWER_SAVE_MODE_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
819 820 821 822 823 824
}



/* private calls */

825
int pwc_restore_user(struct pwc_device *pdev)
L
Linus Torvalds 已提交
826
{
827 828
	return send_control_msg(pdev,
		SET_STATUS_CTL, RESTORE_USER_DEFAULTS_FORMATTER, NULL, 0);
L
Linus Torvalds 已提交
829 830
}

831
int pwc_save_user(struct pwc_device *pdev)
L
Linus Torvalds 已提交
832
{
833 834
	return send_control_msg(pdev,
		SET_STATUS_CTL, SAVE_USER_DEFAULTS_FORMATTER, NULL, 0);
L
Linus Torvalds 已提交
835 836
}

837
int pwc_restore_factory(struct pwc_device *pdev)
L
Linus Torvalds 已提交
838
{
839 840
	return send_control_msg(pdev,
		SET_STATUS_CTL, RESTORE_FACTORY_DEFAULTS_FORMATTER, NULL, 0);
L
Linus Torvalds 已提交
841 842 843 844 845 846 847 848 849 850 851 852 853
}

 /* ************************************************* */
 /* Patch by Alvarado: (not in the original version   */

 /*
  * the camera recognizes modes from 0 to 4:
  *
  * 00: indoor (incandescant lighting)
  * 01: outdoor (sunlight)
  * 02: fluorescent lighting
  * 03: manual
  * 04: auto
854
  */
855
int pwc_set_awb(struct pwc_device *pdev, int mode)
L
Linus Torvalds 已提交
856 857 858
{
	char buf;
	int ret;
859

L
Linus Torvalds 已提交
860 861
	if (mode < 0)
	    mode = 0;
862

L
Linus Torvalds 已提交
863 864
	if (mode > 4)
	    mode = 4;
865

L
Linus Torvalds 已提交
866
	buf = mode & 0x07; /* just the lowest three bits */
867

868 869
	ret = send_control_msg(pdev,
		SET_CHROM_CTL, WB_MODE_FORMATTER, &buf, sizeof(buf));
870

L
Linus Torvalds 已提交
871 872 873 874 875
	if (ret < 0)
		return ret;
	return 0;
}

876
int pwc_get_awb(struct pwc_device *pdev)
L
Linus Torvalds 已提交
877 878 879
{
	unsigned char buf;
	int ret;
880

881 882
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, WB_MODE_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
883

884
	if (ret < 0)
L
Linus Torvalds 已提交
885 886 887 888
		return ret;
	return buf;
}

889
int pwc_set_red_gain(struct pwc_device *pdev, int value)
L
Linus Torvalds 已提交
890
{
891
	unsigned char buf;
L
Linus Torvalds 已提交
892 893 894 895 896 897 898

	if (value < 0)
		value = 0;
	if (value > 0xffff)
		value = 0xffff;
	/* only the msb is considered */
	buf = value >> 8;
899 900 901
	return send_control_msg(pdev,
		SET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
902 903
}

904
int pwc_get_red_gain(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
905 906 907
{
	unsigned char buf;
	int ret;
908

909 910 911
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, PRESET_MANUAL_RED_GAIN_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
912 913 914 915 916 917 918
	if (ret < 0)
	    return ret;
	*value = buf << 8;
	return 0;
}


919
int pwc_set_blue_gain(struct pwc_device *pdev, int value)
L
Linus Torvalds 已提交
920 921 922 923 924 925 926 927 928
{
	unsigned char buf;

	if (value < 0)
		value = 0;
	if (value > 0xffff)
		value = 0xffff;
	/* only the msb is considered */
	buf = value >> 8;
929 930 931
	return send_control_msg(pdev,
		SET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
932 933
}

934
int pwc_get_blue_gain(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
935 936 937
{
	unsigned char buf;
	int ret;
938

939 940 941
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, PRESET_MANUAL_BLUE_GAIN_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
942 943 944 945 946 947 948 949
	if (ret < 0)
	    return ret;
	*value = buf << 8;
	return 0;
}


/* The following two functions are different, since they only read the
950
   internal red/blue gains, which may be different from the manual
L
Linus Torvalds 已提交
951
   gains set or read above.
952
 */
953
static int pwc_read_red_gain(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
954 955 956
{
	unsigned char buf;
	int ret;
957

958 959
	ret = recv_control_msg(pdev,
		GET_STATUS_CTL, READ_RED_GAIN_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
960 961 962 963 964 965
	if (ret < 0)
		return ret;
	*value = buf << 8;
	return 0;
}

966
static int pwc_read_blue_gain(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
967 968 969
{
	unsigned char buf;
	int ret;
970

971 972
	ret = recv_control_msg(pdev,
		GET_STATUS_CTL, READ_BLUE_GAIN_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
973 974 975 976 977 978 979
	if (ret < 0)
		return ret;
	*value = buf << 8;
	return 0;
}


980
static int pwc_set_wb_speed(struct pwc_device *pdev, int speed)
L
Linus Torvalds 已提交
981 982
{
	unsigned char buf;
983

L
Linus Torvalds 已提交
984 985
	/* useful range is 0x01..0x20 */
	buf = speed / 0x7f0;
986 987
	return send_control_msg(pdev,
		SET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
988 989
}

990
static int pwc_get_wb_speed(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
991 992 993
{
	unsigned char buf;
	int ret;
994

995 996
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, AWB_CONTROL_SPEED_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
997 998 999 1000 1001 1002 1003
	if (ret < 0)
		return ret;
	*value = buf * 0x7f0;
	return 0;
}


1004
static int pwc_set_wb_delay(struct pwc_device *pdev, int delay)
L
Linus Torvalds 已提交
1005 1006
{
	unsigned char buf;
1007

L
Linus Torvalds 已提交
1008 1009
	/* useful range is 0x01..0x3F */
	buf = (delay >> 10);
1010 1011
	return send_control_msg(pdev,
		SET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1012 1013
}

1014
static int pwc_get_wb_delay(struct pwc_device *pdev, int *value)
L
Linus Torvalds 已提交
1015 1016 1017
{
	unsigned char buf;
	int ret;
1018

1019 1020
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, AWB_CONTROL_DELAY_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
	if (ret < 0)
		return ret;
	*value = buf << 10;
	return 0;
}


int pwc_set_leds(struct pwc_device *pdev, int on_value, int off_value)
{
	unsigned char buf[2];

	if (pdev->type < 730)
		return 0;
	on_value /= 100;
	off_value /= 100;
	if (on_value < 0)
		on_value = 0;
	if (on_value > 0xff)
		on_value = 0xff;
	if (off_value < 0)
		off_value = 0;
	if (off_value > 0xff)
		off_value = 0xff;

	buf[0] = on_value;
	buf[1] = off_value;

1048 1049
	return send_control_msg(pdev,
		SET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1050 1051
}

1052
static int pwc_get_leds(struct pwc_device *pdev, int *on_value, int *off_value)
L
Linus Torvalds 已提交
1053 1054 1055
{
	unsigned char buf[2];
	int ret;
1056

L
Linus Torvalds 已提交
1057 1058 1059 1060 1061 1062
	if (pdev->type < 730) {
		*on_value = -1;
		*off_value = -1;
		return 0;
	}

1063 1064
	ret = recv_control_msg(pdev,
		GET_STATUS_CTL, LED_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1065 1066 1067 1068 1069 1070 1071
	if (ret < 0)
		return ret;
	*on_value = buf[0] * 100;
	*off_value = buf[1] * 100;
	return 0;
}

1072
int pwc_set_contour(struct pwc_device *pdev, int contour)
L
Linus Torvalds 已提交
1073 1074 1075
{
	unsigned char buf;
	int ret;
1076

L
Linus Torvalds 已提交
1077 1078 1079 1080
	if (contour < 0)
		buf = 0xff; /* auto contour on */
	else
		buf = 0x0; /* auto contour off */
1081 1082
	ret = send_control_msg(pdev,
		SET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1083 1084
	if (ret < 0)
		return ret;
1085

L
Linus Torvalds 已提交
1086 1087 1088 1089
	if (contour < 0)
		return 0;
	if (contour > 0xffff)
		contour = 0xffff;
1090

L
Linus Torvalds 已提交
1091
	buf = (contour >> 10); /* contour preset is [0..3f] */
1092 1093
	ret = send_control_msg(pdev,
		SET_LUM_CTL, PRESET_CONTOUR_FORMATTER, &buf, sizeof(buf));
1094 1095
	if (ret < 0)
		return ret;
L
Linus Torvalds 已提交
1096 1097 1098
	return 0;
}

1099
int pwc_get_contour(struct pwc_device *pdev, int *contour)
L
Linus Torvalds 已提交
1100 1101 1102
{
	unsigned char buf;
	int ret;
1103

1104 1105
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, AUTO_CONTOUR_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110
	if (ret < 0)
		return ret;

	if (buf == 0) {
		/* auto mode off, query current preset value */
1111 1112 1113
		ret = recv_control_msg(pdev,
			GET_LUM_CTL, PRESET_CONTOUR_FORMATTER,
			&buf, sizeof(buf));
1114
		if (ret < 0)
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123
			return ret;
		*contour = buf << 10;
	}
	else
		*contour = -1;
	return 0;
}


1124
int pwc_set_backlight(struct pwc_device *pdev, int backlight)
L
Linus Torvalds 已提交
1125 1126
{
	unsigned char buf;
1127

L
Linus Torvalds 已提交
1128 1129 1130 1131
	if (backlight)
		buf = 0xff;
	else
		buf = 0x0;
1132 1133 1134
	return send_control_msg(pdev,
		SET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
1135 1136
}

1137
int pwc_get_backlight(struct pwc_device *pdev, int *backlight)
L
Linus Torvalds 已提交
1138 1139 1140
{
	int ret;
	unsigned char buf;
1141

1142 1143 1144
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, BACK_LIGHT_COMPENSATION_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
1145 1146
	if (ret < 0)
		return ret;
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	*backlight = !!buf;
	return 0;
}

int pwc_set_colour_mode(struct pwc_device *pdev, int colour)
{
	unsigned char buf;

	if (colour)
		buf = 0xff;
	else
		buf = 0x0;
1159 1160
	return send_control_msg(pdev,
		SET_CHROM_CTL, COLOUR_MODE_FORMATTER, &buf, sizeof(buf));
1161 1162 1163 1164 1165 1166 1167
}

int pwc_get_colour_mode(struct pwc_device *pdev, int *colour)
{
	int ret;
	unsigned char buf;

1168 1169
	ret = recv_control_msg(pdev,
		GET_CHROM_CTL, COLOUR_MODE_FORMATTER, &buf, sizeof(buf));
1170 1171 1172
	if (ret < 0)
		return ret;
	*colour = !!buf;
L
Linus Torvalds 已提交
1173 1174 1175 1176
	return 0;
}


1177
int pwc_set_flicker(struct pwc_device *pdev, int flicker)
L
Linus Torvalds 已提交
1178 1179
{
	unsigned char buf;
1180

L
Linus Torvalds 已提交
1181 1182 1183 1184
	if (flicker)
		buf = 0xff;
	else
		buf = 0x0;
1185 1186
	return send_control_msg(pdev,
		SET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1187 1188
}

1189
int pwc_get_flicker(struct pwc_device *pdev, int *flicker)
L
Linus Torvalds 已提交
1190 1191 1192
{
	int ret;
	unsigned char buf;
1193

1194 1195
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, FLICKERLESS_MODE_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1196 1197
	if (ret < 0)
		return ret;
1198
	*flicker = !!buf;
L
Linus Torvalds 已提交
1199 1200 1201
	return 0;
}

1202
int pwc_set_dynamic_noise(struct pwc_device *pdev, int noise)
L
Linus Torvalds 已提交
1203 1204 1205 1206 1207 1208 1209 1210
{
	unsigned char buf;

	if (noise < 0)
		noise = 0;
	if (noise > 3)
		noise = 3;
	buf = noise;
1211 1212 1213
	return send_control_msg(pdev,
		SET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
1214 1215
}

1216
int pwc_get_dynamic_noise(struct pwc_device *pdev, int *noise)
L
Linus Torvalds 已提交
1217 1218 1219
{
	int ret;
	unsigned char buf;
1220

1221 1222 1223
	ret = recv_control_msg(pdev,
		GET_LUM_CTL, DYNAMIC_NOISE_CONTROL_FORMATTER,
		&buf, sizeof(buf));
L
Linus Torvalds 已提交
1224 1225 1226 1227 1228 1229
	if (ret < 0)
		return ret;
	*noise = buf;
	return 0;
}

1230
static int _pwc_mpt_reset(struct pwc_device *pdev, int flags)
L
Linus Torvalds 已提交
1231 1232
{
	unsigned char buf;
1233

L
Linus Torvalds 已提交
1234
	buf = flags & 0x03; // only lower two bits are currently used
1235 1236
	return send_control_msg(pdev,
		SET_MPT_CTL, PT_RESET_CONTROL_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1237 1238
}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
int pwc_mpt_reset(struct pwc_device *pdev, int flags)
{
	int ret;
	ret = _pwc_mpt_reset(pdev, flags);
	if (ret >= 0) {
		pdev->pan_angle = 0;
		pdev->tilt_angle = 0;
	}
	return ret;
}

static int _pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
L
Linus Torvalds 已提交
1251 1252
{
	unsigned char buf[4];
1253

L
Linus Torvalds 已提交
1254
	/* set new relative angle; angles are expressed in degrees * 100,
1255
	   but cam as .5 degree resolution, hence divide by 200. Also
L
Linus Torvalds 已提交
1256 1257 1258 1259 1260 1261 1262 1263 1264
	   the angle must be multiplied by 64 before it's send to
	   the cam (??)
	 */
	pan  =  64 * pan  / 100;
	tilt = -64 * tilt / 100; /* positive tilt is down, which is not what the user would expect */
	buf[0] = pan & 0xFF;
	buf[1] = (pan >> 8) & 0xFF;
	buf[2] = tilt & 0xFF;
	buf[3] = (tilt >> 8) & 0xFF;
1265 1266
	return send_control_msg(pdev,
		SET_MPT_CTL, PT_RELATIVE_CONTROL_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1267 1268
}

1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
int pwc_mpt_set_angle(struct pwc_device *pdev, int pan, int tilt)
{
	int ret;

	/* check absolute ranges */
	if (pan  < pdev->angle_range.pan_min  ||
	    pan  > pdev->angle_range.pan_max  ||
	    tilt < pdev->angle_range.tilt_min ||
	    tilt > pdev->angle_range.tilt_max)
		return -ERANGE;

	/* go to relative range, check again */
	pan  -= pdev->pan_angle;
	tilt -= pdev->tilt_angle;
	/* angles are specified in degrees * 100, thus the limit = 36000 */
	if (pan < -36000 || pan > 36000 || tilt < -36000 || tilt > 36000)
		return -ERANGE;

	ret = _pwc_mpt_set_angle(pdev, pan, tilt);
	if (ret >= 0) {
		pdev->pan_angle  += pan;
		pdev->tilt_angle += tilt;
	}
	if (ret == -EPIPE) /* stall -> out of range */
		ret = -ERANGE;
	return ret;
}

static int pwc_mpt_get_status(struct pwc_device *pdev, struct pwc_mpt_status *status)
L
Linus Torvalds 已提交
1298 1299 1300
{
	int ret;
	unsigned char buf[5];
1301

1302 1303
	ret = recv_control_msg(pdev,
		GET_MPT_CTL, PT_STATUS_FORMATTER, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
	if (ret < 0)
		return ret;
	status->status = buf[0] & 0x7; // 3 bits are used for reporting
	status->time_pan = (buf[1] << 8) + buf[2];
	status->time_tilt = (buf[3] << 8) + buf[4];
	return 0;
}


int pwc_get_cmos_sensor(struct pwc_device *pdev, int *sensor)
{
	unsigned char buf;
	int ret = -1, request;
1317

L
Linus Torvalds 已提交
1318 1319 1320 1321 1322 1323
	if (pdev->type < 675)
		request = SENSOR_TYPE_FORMATTER1;
	else if (pdev->type < 730)
		return -1; /* The Vesta series doesn't have this call */
	else
		request = SENSOR_TYPE_FORMATTER2;
1324

1325 1326
	ret = recv_control_msg(pdev,
		GET_STATUS_CTL, request, &buf, sizeof(buf));
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
	if (ret < 0)
		return ret;
	if (pdev->type < 675)
		*sensor = buf | 0x100;
	else
		*sensor = buf;
	return 0;
}


 /* End of Add-Ons                                    */
 /* ************************************************* */

1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
/* Linux 2.5.something and 2.6 pass direct pointers to arguments of
   ioctl() calls. With 2.4, you have to do tedious copy_from_user()
   and copy_to_user() calls. With these macros we circumvent this,
   and let me maintain only one source file. The functionality is
   exactly the same otherwise.
 */

/* define local variable for arg */
#define ARG_DEF(ARG_type, ARG_name)\
	ARG_type *ARG_name = arg;
/* copy arg to local variable */
#define ARG_IN(ARG_name) /* nothing */
/* argument itself (referenced) */
#define ARGR(ARG_name) (*ARG_name)
/* argument address */
#define ARGA(ARG_name) ARG_name
/* copy local variable to arg */
#define ARG_OUT(ARG_name) /* nothing */

1359
long pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
L
Linus Torvalds 已提交
1360
{
1361
	long ret = 0;
L
Linus Torvalds 已提交
1362 1363 1364 1365 1366 1367 1368 1369

	switch(cmd) {
	case VIDIOCPWCRUSER:
	{
		if (pwc_restore_user(pdev))
			ret = -EINVAL;
		break;
	}
1370

L
Linus Torvalds 已提交
1371 1372 1373 1374 1375 1376
	case VIDIOCPWCSUSER:
	{
		if (pwc_save_user(pdev))
			ret = -EINVAL;
		break;
	}
1377

L
Linus Torvalds 已提交
1378 1379 1380 1381 1382 1383
	case VIDIOCPWCFACTORY:
	{
		if (pwc_restore_factory(pdev))
			ret = -EINVAL;
		break;
	}
1384

L
Linus Torvalds 已提交
1385
	case VIDIOCPWCSCQUAL:
1386
	{
1387
		ARG_DEF(int, qual)
L
Linus Torvalds 已提交
1388

1389 1390 1391 1392 1393
		if (pdev->iso_init) {
			ret = -EBUSY;
			break;
		}

1394 1395
		ARG_IN(qual)
		if (ARGR(qual) < 0 || ARGR(qual) > 3)
L
Linus Torvalds 已提交
1396 1397
			ret = -EINVAL;
		else
1398
			ret = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, ARGR(qual), pdev->vsnapshot);
L
Linus Torvalds 已提交
1399
		if (ret >= 0)
1400
			pdev->vcompression = ARGR(qual);
L
Linus Torvalds 已提交
1401 1402
		break;
	}
1403

L
Linus Torvalds 已提交
1404 1405
	case VIDIOCPWCGCQUAL:
	{
1406 1407 1408 1409
		ARG_DEF(int, qual)

		ARGR(qual) = pdev->vcompression;
		ARG_OUT(qual)
L
Linus Torvalds 已提交
1410 1411
		break;
	}
1412

L
Linus Torvalds 已提交
1413 1414
	case VIDIOCPWCPROBE:
	{
1415 1416 1417 1418 1419
		ARG_DEF(struct pwc_probe, probe)

		strcpy(ARGR(probe).name, pdev->vdev->name);
		ARGR(probe).type = pdev->type;
		ARG_OUT(probe)
L
Linus Torvalds 已提交
1420 1421 1422 1423 1424
		break;
	}

	case VIDIOCPWCGSERIAL:
	{
1425 1426 1427 1428
		ARG_DEF(struct pwc_serial, serial)

		strcpy(ARGR(serial).serial, pdev->serial);
		ARG_OUT(serial)
L
Linus Torvalds 已提交
1429 1430 1431 1432 1433
		break;
	}

	case VIDIOCPWCSAGC:
	{
1434 1435 1436 1437
		ARG_DEF(int, agc)

		ARG_IN(agc)
		if (pwc_set_agc(pdev, ARGR(agc) < 0 ? 1 : 0, ARGR(agc)))
L
Linus Torvalds 已提交
1438 1439 1440
			ret = -EINVAL;
		break;
	}
1441

L
Linus Torvalds 已提交
1442 1443
	case VIDIOCPWCGAGC:
	{
1444
		ARG_DEF(int, agc)
1445

1446
		if (pwc_get_agc(pdev, ARGA(agc)))
L
Linus Torvalds 已提交
1447
			ret = -EINVAL;
1448
		ARG_OUT(agc)
L
Linus Torvalds 已提交
1449 1450
		break;
	}
1451

L
Linus Torvalds 已提交
1452 1453
	case VIDIOCPWCSSHUTTER:
	{
1454 1455 1456 1457
		ARG_DEF(int, shutter_speed)

		ARG_IN(shutter_speed)
		ret = pwc_set_shutter_speed(pdev, ARGR(shutter_speed) < 0 ? 1 : 0, ARGR(shutter_speed));
L
Linus Torvalds 已提交
1458 1459
		break;
	}
1460 1461

	case VIDIOCPWCSAWB:
L
Linus Torvalds 已提交
1462
	{
1463
		ARG_DEF(struct pwc_whitebalance, wb)
1464

1465 1466 1467 1468 1469
		ARG_IN(wb)
		ret = pwc_set_awb(pdev, ARGR(wb).mode);
		if (ret >= 0 && ARGR(wb).mode == PWC_WB_MANUAL) {
			pwc_set_red_gain(pdev, ARGR(wb).manual_red);
			pwc_set_blue_gain(pdev, ARGR(wb).manual_blue);
L
Linus Torvalds 已提交
1470 1471 1472 1473 1474 1475
		}
		break;
	}

	case VIDIOCPWCGAWB:
	{
1476
		ARG_DEF(struct pwc_whitebalance, wb)
L
Linus Torvalds 已提交
1477

1478 1479 1480
		memset(ARGA(wb), 0, sizeof(struct pwc_whitebalance));
		ARGR(wb).mode = pwc_get_awb(pdev);
		if (ARGR(wb).mode < 0)
L
Linus Torvalds 已提交
1481 1482
			ret = -EINVAL;
		else {
1483 1484
			if (ARGR(wb).mode == PWC_WB_MANUAL) {
				ret = pwc_get_red_gain(pdev, &ARGR(wb).manual_red);
L
Linus Torvalds 已提交
1485 1486
				if (ret < 0)
					break;
1487
				ret = pwc_get_blue_gain(pdev, &ARGR(wb).manual_blue);
L
Linus Torvalds 已提交
1488 1489 1490
				if (ret < 0)
					break;
			}
1491 1492
			if (ARGR(wb).mode == PWC_WB_AUTO) {
				ret = pwc_read_red_gain(pdev, &ARGR(wb).read_red);
L
Linus Torvalds 已提交
1493 1494
				if (ret < 0)
					break;
1495
				ret = pwc_read_blue_gain(pdev, &ARGR(wb).read_blue);
1496 1497
				if (ret < 0)
					break;
L
Linus Torvalds 已提交
1498 1499
			}
		}
1500
		ARG_OUT(wb)
L
Linus Torvalds 已提交
1501 1502
		break;
	}
1503

L
Linus Torvalds 已提交
1504 1505
	case VIDIOCPWCSAWBSPEED:
	{
1506
		ARG_DEF(struct pwc_wb_speed, wbs)
1507

1508 1509
		if (ARGR(wbs).control_speed > 0) {
			ret = pwc_set_wb_speed(pdev, ARGR(wbs).control_speed);
L
Linus Torvalds 已提交
1510
		}
1511 1512
		if (ARGR(wbs).control_delay > 0) {
			ret = pwc_set_wb_delay(pdev, ARGR(wbs).control_delay);
L
Linus Torvalds 已提交
1513 1514 1515
		}
		break;
	}
1516

L
Linus Torvalds 已提交
1517 1518
	case VIDIOCPWCGAWBSPEED:
	{
1519
		ARG_DEF(struct pwc_wb_speed, wbs)
1520

1521
		ret = pwc_get_wb_speed(pdev, &ARGR(wbs).control_speed);
L
Linus Torvalds 已提交
1522 1523
		if (ret < 0)
			break;
1524
		ret = pwc_get_wb_delay(pdev, &ARGR(wbs).control_delay);
L
Linus Torvalds 已提交
1525 1526
		if (ret < 0)
			break;
1527
		ARG_OUT(wbs)
L
Linus Torvalds 已提交
1528 1529 1530
		break;
	}

1531
	case VIDIOCPWCSLED:
L
Linus Torvalds 已提交
1532
	{
1533 1534 1535 1536
		ARG_DEF(struct pwc_leds, leds)

		ARG_IN(leds)
		ret = pwc_set_leds(pdev, ARGR(leds).led_on, ARGR(leds).led_off);
1537
		break;
L
Linus Torvalds 已提交
1538 1539 1540 1541 1542
	}


	case VIDIOCPWCGLED:
	{
1543 1544 1545 1546
		ARG_DEF(struct pwc_leds, leds)

		ret = pwc_get_leds(pdev, &ARGR(leds).led_on, &ARGR(leds).led_off);
		ARG_OUT(leds)
L
Linus Torvalds 已提交
1547 1548 1549 1550 1551
		break;
	}

	case VIDIOCPWCSCONTOUR:
	{
1552 1553 1554 1555
		ARG_DEF(int, contour)

		ARG_IN(contour)
		ret = pwc_set_contour(pdev, ARGR(contour));
L
Linus Torvalds 已提交
1556 1557
		break;
	}
1558

L
Linus Torvalds 已提交
1559 1560
	case VIDIOCPWCGCONTOUR:
	{
1561 1562 1563 1564
		ARG_DEF(int, contour)

		ret = pwc_get_contour(pdev, ARGA(contour));
		ARG_OUT(contour)
L
Linus Torvalds 已提交
1565 1566
		break;
	}
1567

L
Linus Torvalds 已提交
1568 1569
	case VIDIOCPWCSBACKLIGHT:
	{
1570 1571 1572 1573
		ARG_DEF(int, backlight)

		ARG_IN(backlight)
		ret = pwc_set_backlight(pdev, ARGR(backlight));
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578
		break;
	}

	case VIDIOCPWCGBACKLIGHT:
	{
1579 1580 1581 1582
		ARG_DEF(int, backlight)

		ret = pwc_get_backlight(pdev, ARGA(backlight));
		ARG_OUT(backlight)
L
Linus Torvalds 已提交
1583 1584
		break;
	}
1585

L
Linus Torvalds 已提交
1586 1587
	case VIDIOCPWCSFLICKER:
	{
1588 1589 1590 1591
		ARG_DEF(int, flicker)

		ARG_IN(flicker)
		ret = pwc_set_flicker(pdev, ARGR(flicker));
L
Linus Torvalds 已提交
1592 1593 1594 1595 1596
		break;
	}

	case VIDIOCPWCGFLICKER:
	{
1597 1598 1599 1600
		ARG_DEF(int, flicker)

		ret = pwc_get_flicker(pdev, ARGA(flicker));
		ARG_OUT(flicker)
L
Linus Torvalds 已提交
1601 1602
		break;
	}
1603

L
Linus Torvalds 已提交
1604 1605
	case VIDIOCPWCSDYNNOISE:
	{
1606 1607 1608 1609
		ARG_DEF(int, dynnoise)

		ARG_IN(dynnoise)
		ret = pwc_set_dynamic_noise(pdev, ARGR(dynnoise));
L
Linus Torvalds 已提交
1610 1611
		break;
	}
1612

L
Linus Torvalds 已提交
1613 1614
	case VIDIOCPWCGDYNNOISE:
	{
1615 1616 1617 1618
		ARG_DEF(int, dynnoise)

		ret = pwc_get_dynamic_noise(pdev, ARGA(dynnoise));
		ARG_OUT(dynnoise);
L
Linus Torvalds 已提交
1619 1620 1621 1622 1623
		break;
	}

	case VIDIOCPWCGREALSIZE:
	{
1624 1625 1626 1627 1628
		ARG_DEF(struct pwc_imagesize, size)

		ARGR(size).width = pdev->image.x;
		ARGR(size).height = pdev->image.y;
		ARG_OUT(size)
L
Linus Torvalds 已提交
1629
		break;
1630 1631 1632 1633 1634 1635
	}

	case VIDIOCPWCMPTRESET:
	{
		if (pdev->features & FEATURE_MOTOR_PANTILT)
		{
1636
			ARG_DEF(int, flags)
L
Linus Torvalds 已提交
1637

1638 1639
			ARG_IN(flags)
			ret = pwc_mpt_reset(pdev, ARGR(flags));
1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651
		}
		else
		{
			ret = -ENXIO;
		}
		break;
	}

	case VIDIOCPWCMPTGRANGE:
	{
		if (pdev->features & FEATURE_MOTOR_PANTILT)
		{
1652 1653 1654 1655
			ARG_DEF(struct pwc_mpt_range, range)

			ARGR(range) = pdev->angle_range;
			ARG_OUT(range)
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
		}
		else
		{
			ret = -ENXIO;
		}
		break;
	}

	case VIDIOCPWCMPTSANGLE:
	{
		int new_pan, new_tilt;

		if (pdev->features & FEATURE_MOTOR_PANTILT)
		{
1670 1671 1672
			ARG_DEF(struct pwc_mpt_angles, angles)

			ARG_IN(angles)
L
Linus Torvalds 已提交
1673 1674 1675
			/* The camera can only set relative angles, so
			   do some calculations when getting an absolute angle .
			 */
1676
			if (ARGR(angles).absolute)
L
Linus Torvalds 已提交
1677
			{
1678 1679
				new_pan  = ARGR(angles).pan;
				new_tilt = ARGR(angles).tilt;
1680 1681 1682
			}
			else
			{
1683 1684
				new_pan  = pdev->pan_angle  + ARGR(angles).pan;
				new_tilt = pdev->tilt_angle + ARGR(angles).tilt;
L
Linus Torvalds 已提交
1685
			}
1686
			ret = pwc_mpt_set_angle(pdev, new_pan, new_tilt);
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699
		}
		else
		{
			ret = -ENXIO;
		}
		break;
	}

	case VIDIOCPWCMPTGANGLE:
	{

		if (pdev->features & FEATURE_MOTOR_PANTILT)
		{
1700
			ARG_DEF(struct pwc_mpt_angles, angles)
1701

1702 1703 1704 1705
			ARGR(angles).absolute = 1;
			ARGR(angles).pan  = pdev->pan_angle;
			ARGR(angles).tilt = pdev->tilt_angle;
			ARG_OUT(angles)
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
		}
		else
		{
			ret = -ENXIO;
		}
		break;
	}

	case VIDIOCPWCMPTSTATUS:
	{
		if (pdev->features & FEATURE_MOTOR_PANTILT)
		{
1718 1719 1720 1721
			ARG_DEF(struct pwc_mpt_status, status)

			ret = pwc_mpt_get_status(pdev, ARGA(status));
			ARG_OUT(status)
1722 1723 1724 1725 1726 1727
		}
		else
		{
			ret = -ENXIO;
		}
		break;
L
Linus Torvalds 已提交
1728 1729 1730 1731
	}

	case VIDIOCPWCGVIDCMD:
	{
1732 1733 1734 1735 1736 1737 1738 1739 1740
		ARG_DEF(struct pwc_video_command, vcmd);

		ARGR(vcmd).type = pdev->type;
		ARGR(vcmd).release = pdev->release;
		ARGR(vcmd).command_len = pdev->cmd_len;
		memcpy(&ARGR(vcmd).command_buf, pdev->cmd_buf, pdev->cmd_len);
		ARGR(vcmd).bandlength = pdev->vbandlength;
		ARGR(vcmd).frame_size = pdev->frame_size;
		ARG_OUT(vcmd)
L
Linus Torvalds 已提交
1741 1742
		break;
	}
1743
	/*
L
Linus Torvalds 已提交
1744 1745
	case VIDIOCPWCGVIDTABLE:
	{
1746 1747 1748 1749
		ARG_DEF(struct pwc_table_init_buffer, table);
		ARGR(table).len = pdev->cmd_len;
		memcpy(&ARGR(table).buffer, pdev->decompress_data, pdev->decompressor->table_size);
		ARG_OUT(table)
L
Linus Torvalds 已提交
1750 1751 1752 1753 1754 1755 1756 1757
		break;
	}
	*/

	default:
		ret = -ENOIOCTLCMD;
		break;
	}
1758

L
Linus Torvalds 已提交
1759 1760 1761 1762 1763 1764
	if (ret > 0)
		return 0;
	return ret;
}


1765
/* vim: set cinoptions= formatoptions=croql cindent shiftwidth=8 tabstop=8: */