cpia2_v4l.c 39.9 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 27 28
/****************************************************************************
 *
 *  Filename: cpia2_v4l.c
 *
 *  Copyright 2001, STMicrolectronics, Inc.
 *      Contact:  steve.miller@st.com
 *  Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
 *
 *  Description:
 *     This is a USB driver for CPia2 based video cameras.
 *     The infrastructure of this driver is based on the cpia usb driver by
 *     Jochen Scharrlach and Johannes Erdfeldt.
 *
 *  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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Stripped of 2.4 stuff ready for main kernel submit by
29
 *		Alan Cox <alan@lxorguk.ukuu.org.uk>
30 31 32 33 34 35 36 37 38 39
 ****************************************************************************/

#include <linux/version.h>


#include <linux/module.h>
#include <linux/time.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/init.h>
40
#include <linux/videodev2.h>
41
#include <linux/stringify.h>
42
#include <media/v4l2-ioctl.h>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

#include "cpia2.h"
#include "cpia2dev.h"

static int video_nr = -1;
module_param(video_nr, int, 0);
MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");

static int buffer_size = 68*1024;
module_param(buffer_size, int, 0);
MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");

static int num_buffers = 3;
module_param(num_buffers, int, 0);
MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
58
		 __stringify(VIDEO_MAX_FRAME) ", default 3)");
59 60 61

static int alternate = DEFAULT_ALT;
module_param(alternate, int, 0);
62 63 64
MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
		 __stringify(USBIF_ISO_6) ", default "
		 __stringify(DEFAULT_ALT) ")");
65 66 67

static int flicker_freq = 60;
module_param(flicker_freq, int, 0);
68 69 70
MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
		 __stringify(60) ", default "
		 __stringify(60) ")");
71 72 73 74

static int flicker_mode = NEVER_FLICKER;
module_param(flicker_mode, int, 0);
MODULE_PARM_DESC(flicker_mode,
75 76 77
		 "Flicker supression (" __stringify(NEVER_FLICKER) "or"
		 __stringify(ANTI_FLICKER_ON) ", default "
		 __stringify(NEVER_FLICKER) ")");
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
MODULE_SUPPORTED_DEVICE("video");
MODULE_LICENSE("GPL");

#define ABOUT "V4L-Driver for Vision CPiA2 based cameras"

struct control_menu_info {
	int value;
	char name[32];
};

static struct control_menu_info framerate_controls[] =
{
	{ CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
	{ CPIA2_VP_FRAMERATE_7_5,  "7.5 fps"  },
	{ CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
	{ CPIA2_VP_FRAMERATE_15,   "15 fps"   },
	{ CPIA2_VP_FRAMERATE_25,   "25 fps"   },
	{ CPIA2_VP_FRAMERATE_30,   "30 fps"   },
};
100
#define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
101 102 103 104 105 106 107

static struct control_menu_info flicker_controls[] =
{
	{ NEVER_FLICKER, "Off" },
	{ FLICKER_50,    "50 Hz" },
	{ FLICKER_60,    "60 Hz"  },
};
108
#define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
109 110 111 112 113 114 115 116

static struct control_menu_info lights_controls[] =
{
	{ 0,   "Off" },
	{ 64,  "Top" },
	{ 128, "Bottom"  },
	{ 192, "Both"  },
};
117
#define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 160 161 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
#define GPIO_LIGHTS_MASK 192

static struct v4l2_queryctrl controls[] = {
	{
		.id            = V4L2_CID_BRIGHTNESS,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "Brightness",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = DEFAULT_BRIGHTNESS,
	},
	{
		.id            = V4L2_CID_CONTRAST,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "Contrast",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = DEFAULT_CONTRAST,
	},
	{
		.id            = V4L2_CID_SATURATION,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "Saturation",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = DEFAULT_SATURATION,
	},
	{
		.id            = V4L2_CID_HFLIP,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
		.name          = "Mirror Horizontally",
		.minimum       = 0,
		.maximum       = 1,
		.step          = 1,
		.default_value = 0,
	},
	{
		.id            = V4L2_CID_VFLIP,
		.type          = V4L2_CTRL_TYPE_BOOLEAN,
		.name          = "Flip Vertically",
		.minimum       = 0,
		.maximum       = 1,
		.step          = 1,
		.default_value = 0,
	},
	{
		.id            = CPIA2_CID_TARGET_KB,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "Target KB",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = DEFAULT_TARGET_KB,
	},
	{
		.id            = CPIA2_CID_GPIO,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "GPIO",
		.minimum       = 0,
		.maximum       = 255,
		.step          = 1,
		.default_value = 0,
	},
	{
		.id            = CPIA2_CID_FLICKER_MODE,
		.type          = V4L2_CTRL_TYPE_MENU,
		.name          = "Flicker Reduction",
		.minimum       = 0,
		.maximum       = NUM_FLICKER_CONTROLS-1,
		.step          = 1,
		.default_value = 0,
	},
	{
		.id            = CPIA2_CID_FRAMERATE,
		.type          = V4L2_CTRL_TYPE_MENU,
		.name          = "Framerate",
		.minimum       = 0,
		.maximum       = NUM_FRAMERATE_CONTROLS-1,
		.step          = 1,
		.default_value = NUM_FRAMERATE_CONTROLS-1,
	},
	{
		.id            = CPIA2_CID_USB_ALT,
		.type          = V4L2_CTRL_TYPE_INTEGER,
		.name          = "USB Alternate",
		.minimum       = USBIF_ISO_1,
		.maximum       = USBIF_ISO_6,
		.step          = 1,
		.default_value = DEFAULT_ALT,
	},
	{
		.id            = CPIA2_CID_LIGHTS,
		.type          = V4L2_CTRL_TYPE_MENU,
		.name          = "Lights",
		.minimum       = 0,
		.maximum       = NUM_LIGHTS_CONTROLS-1,
		.step          = 1,
		.default_value = 0,
	},
	{
		.id            = CPIA2_CID_RESET_CAMERA,
		.type          = V4L2_CTRL_TYPE_BUTTON,
		.name          = "Reset Camera",
		.minimum       = 0,
		.maximum       = 0,
		.step          = 0,
		.default_value = 0,
	},
};
230
#define NUM_CONTROLS (ARRAY_SIZE(controls))
231 232 233 234 235 236 237


/******************************************************************************
 *
 *  cpia2_open
 *
 *****************************************************************************/
238
static int cpia2_open(struct file *file)
239
{
240
	struct camera_data *cam = video_drvdata(file);
241
	struct cpia2_fh *fh;
242 243 244 245 246 247

	if (!cam) {
		ERR("Internal error, camera_data not found!\n");
		return -ENODEV;
	}

248 249
	if (!cam->present)
		return -ENODEV;
250

251 252 253
	if (cam->open_count == 0) {
		if (cpia2_allocate_buffers(cam))
			return -ENOMEM;
254

255 256 257
		/* reset the camera */
		if (cpia2_reset_camera(cam) < 0)
			return -EIO;
258

259 260
		cam->APP_len = 0;
		cam->COM_len = 0;
261 262
	}

263 264 265 266 267 268 269
	fh = kmalloc(sizeof(*fh), GFP_KERNEL);
	if (!fh)
		return -ENOMEM;
	file->private_data = fh;
	fh->prio = V4L2_PRIORITY_UNSET;
	v4l2_prio_open(&cam->prio, &fh->prio);
	fh->mmapped = 0;
270 271 272 273

	++cam->open_count;

	cpia2_dbg_dump_registers(cam);
274
	return 0;
275 276 277 278 279 280 281
}

/******************************************************************************
 *
 *  cpia2_close
 *
 *****************************************************************************/
282
static int cpia2_close(struct file *file)
283 284 285 286 287 288
{
	struct video_device *dev = video_devdata(file);
	struct camera_data *cam = video_get_drvdata(dev);
	struct cpia2_fh *fh = file->private_data;

	if (cam->present &&
289
	    (cam->open_count == 1 || fh->prio == V4L2_PRIORITY_RECORD)) {
290 291
		cpia2_usb_stream_stop(cam);

292
		if (cam->open_count == 1) {
293 294 295 296 297 298 299 300
			/* save camera state for later open */
			cpia2_save_camera_state(cam);

			cpia2_set_low_power(cam);
			cpia2_free_buffers(cam);
		}
	}

301 302 303 304 305
	if (fh->mmapped)
		cam->mmapped = 0;
	v4l2_prio_close(&cam->prio, fh->prio);
	file->private_data = NULL;
	kfree(fh);
306 307 308 309 310 311

	if (--cam->open_count == 0) {
		cpia2_free_buffers(cam);
		if (!cam->present) {
			video_unregister_device(dev);
			kfree(cam);
312
			return 0;
313 314 315 316 317 318 319 320 321 322 323 324 325 326
		}
	}

	return 0;
}

/******************************************************************************
 *
 *  cpia2_v4l_read
 *
 *****************************************************************************/
static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
			      loff_t *off)
{
327
	struct camera_data *cam = video_drvdata(file);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	int noblock = file->f_flags&O_NONBLOCK;

	struct cpia2_fh *fh = file->private_data;

	if(!cam)
		return -EINVAL;

	/* Priority check */
	if(fh->prio != V4L2_PRIORITY_RECORD) {
		return -EBUSY;
	}

	return cpia2_read(cam, buf, count, noblock);
}


/******************************************************************************
 *
 *  cpia2_v4l_poll
 *
 *****************************************************************************/
static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
{
351
	struct camera_data *cam = video_drvdata(filp);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	struct cpia2_fh *fh = filp->private_data;

	if(!cam)
		return POLLERR;

	/* Priority check */
	if(fh->prio != V4L2_PRIORITY_RECORD) {
		return POLLERR;
	}

	return cpia2_poll(cam, filp, wait);
}


static int sync(struct camera_data *cam, int frame_nr)
{
	struct framebuf *frame = &cam->buffers[frame_nr];

	while (1) {
		if (frame->status == FRAME_READY)
			return 0;

		if (!cam->streaming) {
			frame->status = FRAME_READY;
			frame->length = 0;
			return 0;
		}

380
		mutex_unlock(&cam->v4l2_lock);
381 382 383
		wait_event_interruptible(cam->wq_stream,
					 !cam->streaming ||
					 frame->status == FRAME_READY);
384
		mutex_lock(&cam->v4l2_lock);
385 386 387 388 389 390 391 392 393 394 395 396 397
		if (signal_pending(current))
			return -ERESTARTSYS;
		if(!cam->present)
			return -ENOTTY;
	}
}

/******************************************************************************
 *
 *  ioctl_set_gpio
 *
 *****************************************************************************/

398 399
static long cpia2_default(struct file *file, void *fh, bool valid_prio,
			  int cmd, void *arg)
400
{
401
	struct camera_data *cam = video_drvdata(file);
402 403
	__u32 gpio_val;

404 405 406
	if (cmd != CPIA2_CID_GPIO)
		return -EINVAL;

407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	gpio_val = *(__u32*) arg;

	if (gpio_val &~ 0xFFU)
		return -EINVAL;

	return cpia2_set_gpio(cam, (unsigned char)gpio_val);
}

/******************************************************************************
 *
 *  ioctl_querycap
 *
 *  V4L2 device capabilities
 *
 *****************************************************************************/

423
static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
424
{
425
	struct camera_data *cam = video_drvdata(file);
426 427 428 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 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485

	strcpy(vc->driver, "cpia2");

	if (cam->params.pnp_id.product == 0x151)
		strcpy(vc->card, "QX5 Microscope");
	else
		strcpy(vc->card, "CPiA2 Camera");
	switch (cam->params.pnp_id.device_type) {
	case DEVICE_STV_672:
		strcat(vc->card, " (672/");
		break;
	case DEVICE_STV_676:
		strcat(vc->card, " (676/");
		break;
	default:
		strcat(vc->card, " (???/");
		break;
	}
	switch (cam->params.version.sensor_flags) {
	case CPIA2_VP_SENSOR_FLAGS_404:
		strcat(vc->card, "404)");
		break;
	case CPIA2_VP_SENSOR_FLAGS_407:
		strcat(vc->card, "407)");
		break;
	case CPIA2_VP_SENSOR_FLAGS_409:
		strcat(vc->card, "409)");
		break;
	case CPIA2_VP_SENSOR_FLAGS_410:
		strcat(vc->card, "410)");
		break;
	case CPIA2_VP_SENSOR_FLAGS_500:
		strcat(vc->card, "500)");
		break;
	default:
		strcat(vc->card, "???)");
		break;
	}

	if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
		memset(vc->bus_info,0, sizeof(vc->bus_info));

	vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
				     CPIA2_PATCH_VER);

	vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
			   V4L2_CAP_READWRITE |
			   V4L2_CAP_STREAMING;

	return 0;
}

/******************************************************************************
 *
 *  ioctl_input
 *
 *  V4L2 input get/set/enumerate
 *
 *****************************************************************************/

486
static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
487
{
488 489
	if (i->index)
		return -EINVAL;
490 491
	strcpy(i->name, "Camera");
	i->type = V4L2_INPUT_TYPE_CAMERA;
492 493
	return 0;
}
494

495 496 497
static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
{
	*i = 0;
498 499 500
	return 0;
}

501 502 503 504 505
static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
{
	return i ? -EINVAL : 0;
}

506 507 508 509 510 511 512 513
/******************************************************************************
 *
 *  ioctl_enum_fmt
 *
 *  V4L2 format enumerate
 *
 *****************************************************************************/

514 515
static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
					    struct v4l2_fmtdesc *f)
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
{
	int index = f->index;

	if (index < 0 || index > 1)
	       return -EINVAL;

	memset(f, 0, sizeof(*f));
	f->index = index;
	f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
	f->flags = V4L2_FMT_FLAG_COMPRESSED;
	switch(index) {
	case 0:
		strcpy(f->description, "MJPEG");
		f->pixelformat = V4L2_PIX_FMT_MJPEG;
		break;
	case 1:
		strcpy(f->description, "JPEG");
		f->pixelformat = V4L2_PIX_FMT_JPEG;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_try_fmt
 *
 *  V4L2 format try
 *
 *****************************************************************************/

550 551
static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
					  struct v4l2_format *f)
552
{
553
	struct camera_data *cam = video_drvdata(file);
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

	if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
	    f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
	       return -EINVAL;

	f->fmt.pix.field = V4L2_FIELD_NONE;
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.sizeimage = cam->frame_size;
	f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
	f->fmt.pix.priv = 0;

	switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
	case VIDEOSIZE_VGA:
		f->fmt.pix.width = 640;
		f->fmt.pix.height = 480;
		break;
	case VIDEOSIZE_CIF:
		f->fmt.pix.width = 352;
		f->fmt.pix.height = 288;
		break;
	case VIDEOSIZE_QVGA:
		f->fmt.pix.width = 320;
		f->fmt.pix.height = 240;
		break;
	case VIDEOSIZE_288_216:
		f->fmt.pix.width = 288;
		f->fmt.pix.height = 216;
		break;
	case VIDEOSIZE_256_192:
		f->fmt.pix.width = 256;
		f->fmt.pix.height = 192;
		break;
	case VIDEOSIZE_224_168:
		f->fmt.pix.width = 224;
		f->fmt.pix.height = 168;
		break;
	case VIDEOSIZE_192_144:
		f->fmt.pix.width = 192;
		f->fmt.pix.height = 144;
		break;
	case VIDEOSIZE_QCIF:
	default:
		f->fmt.pix.width = 176;
		f->fmt.pix.height = 144;
		break;
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_set_fmt
 *
 *  V4L2 format set
 *
 *****************************************************************************/

612 613
static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
					struct v4l2_format *f)
614
{
615 616
	struct camera_data *cam = video_drvdata(file);
	struct cpia2_fh *fh = _fh;
617 618
	int err, frame;

619 620 621 622
	err = v4l2_prio_check(&cam->prio, fh->prio);
	if (err)
		return err;
	err = cpia2_try_fmt_vid_cap(file, _fh, f);
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	if(err != 0)
		return err;

	/* Ensure that only this process can change the format. */
	err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
	if(err != 0) {
		return err;
	}

	cam->pixelformat = f->fmt.pix.pixelformat;

	/* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
	 * the missing Huffman table properly. */
	cam->params.compression.inhibit_htables = 0;
		/*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/

	/* we set the video window to something smaller or equal to what
	 * is requested by the user???
	 */
	DBG("Requested width = %d, height = %d\n",
	    f->fmt.pix.width, f->fmt.pix.height);
644 645 646 647
	if (f->fmt.pix.width != cam->width ||
	    f->fmt.pix.height != cam->height) {
		cam->width = f->fmt.pix.width;
		cam->height = f->fmt.pix.height;
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
		cam->params.roi.width = f->fmt.pix.width;
		cam->params.roi.height = f->fmt.pix.height;
		cpia2_set_format(cam);
	}

	for (frame = 0; frame < cam->num_frames; ++frame) {
		if (cam->buffers[frame].status == FRAME_READING)
			if ((err = sync(cam, frame)) < 0)
				return err;

		cam->buffers[frame].status = FRAME_EMPTY;
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_get_fmt
 *
 *  V4L2 format get
 *
 *****************************************************************************/

672 673
static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
					struct v4l2_format *f)
674
{
675
	struct camera_data *cam = video_drvdata(file);
676

677 678
	f->fmt.pix.width = cam->width;
	f->fmt.pix.height = cam->height;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	f->fmt.pix.pixelformat = cam->pixelformat;
	f->fmt.pix.field = V4L2_FIELD_NONE;
	f->fmt.pix.bytesperline = 0;
	f->fmt.pix.sizeimage = cam->frame_size;
	f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
	f->fmt.pix.priv = 0;

	return 0;
}

/******************************************************************************
 *
 *  ioctl_cropcap
 *
 *  V4L2 query cropping capabilities
 *  NOTE: cropping is currently disabled
 *
 *****************************************************************************/

698
static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
699
{
700
	struct camera_data *cam = video_drvdata(file);
701 702 703 704 705 706

	if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
	       return -EINVAL;

	c->bounds.left = 0;
	c->bounds.top = 0;
707 708
	c->bounds.width = cam->width;
	c->bounds.height = cam->height;
709 710
	c->defrect.left = 0;
	c->defrect.top = 0;
711 712
	c->defrect.width = cam->width;
	c->defrect.height = cam->height;
713 714 715 716 717 718 719 720 721 722 723 724 725 726
	c->pixelaspect.numerator = 1;
	c->pixelaspect.denominator = 1;

	return 0;
}

/******************************************************************************
 *
 *  ioctl_queryctrl
 *
 *  V4L2 query possible control variables
 *
 *****************************************************************************/

727
static int cpia2_queryctrl(struct file *file, void *fh, struct v4l2_queryctrl *c)
728
{
729
	struct camera_data *cam = video_drvdata(file);
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
	int i;

	for(i=0; i<NUM_CONTROLS; ++i) {
		if(c->id == controls[i].id) {
			memcpy(c, controls+i, sizeof(*c));
			break;
		}
	}

	if(i == NUM_CONTROLS)
		return -EINVAL;

	/* Some devices have additional limitations */
	switch(c->id) {
	case V4L2_CID_BRIGHTNESS:
		/***
		 * Don't let the register be set to zero - bug in VP4
		 * flash of full brightness
		 ***/
		if (cam->params.pnp_id.device_type == DEVICE_STV_672)
			c->minimum = 1;
		break;
	case V4L2_CID_VFLIP:
		// VP5 Only
		if(cam->params.pnp_id.device_type == DEVICE_STV_672)
			c->flags |= V4L2_CTRL_FLAG_DISABLED;
		break;
	case CPIA2_CID_FRAMERATE:
		if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
		   cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
			// Maximum 15fps
			for(i=0; i<c->maximum; ++i) {
				if(framerate_controls[i].value ==
				   CPIA2_VP_FRAMERATE_15) {
					c->maximum = i;
					c->default_value = i;
				}
			}
		}
		break;
	case CPIA2_CID_FLICKER_MODE:
		// Flicker control only valid for 672.
		if(cam->params.pnp_id.device_type != DEVICE_STV_672)
			c->flags |= V4L2_CTRL_FLAG_DISABLED;
		break;
	case CPIA2_CID_LIGHTS:
		// Light control only valid for the QX5 Microscope.
		if(cam->params.pnp_id.product != 0x151)
			c->flags |= V4L2_CTRL_FLAG_DISABLED;
		break;
	default:
		break;
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_querymenu
 *
 *  V4L2 query possible control variables
 *
 *****************************************************************************/

795
static int cpia2_querymenu(struct file *file, void *fh, struct v4l2_querymenu *m)
796
{
797
	struct camera_data *cam = video_drvdata(file);
798 799 800

	switch(m->id) {
	case CPIA2_CID_FLICKER_MODE:
801
		if (m->index >= NUM_FLICKER_CONTROLS)
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
			return -EINVAL;

		strcpy(m->name, flicker_controls[m->index].name);
		break;
	case CPIA2_CID_FRAMERATE:
	    {
		int maximum = NUM_FRAMERATE_CONTROLS - 1;
		if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
		   cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
			// Maximum 15fps
			int i;
			for(i=0; i<maximum; ++i) {
				if(framerate_controls[i].value ==
				   CPIA2_VP_FRAMERATE_15)
					maximum = i;
			}
		}
819
		if (m->index > maximum)
820 821 822 823 824 825
			return -EINVAL;

		strcpy(m->name, framerate_controls[m->index].name);
		break;
	    }
	case CPIA2_CID_LIGHTS:
826
		if (m->index >= NUM_LIGHTS_CONTROLS)
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
			return -EINVAL;

		strcpy(m->name, lights_controls[m->index].name);
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_g_ctrl
 *
 *  V4L2 get the value of a control variable
 *
 *****************************************************************************/

846
static int cpia2_g_ctrl(struct file *file, void *fh, struct v4l2_control *c)
847
{
848
	struct camera_data *cam = video_drvdata(file);
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

	switch(c->id) {
	case V4L2_CID_BRIGHTNESS:
		cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
				 TRANSFER_READ, 0);
		c->value = cam->params.color_params.brightness;
		break;
	case V4L2_CID_CONTRAST:
		cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
				 TRANSFER_READ, 0);
		c->value = cam->params.color_params.contrast;
		break;
	case V4L2_CID_SATURATION:
		cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
				 TRANSFER_READ, 0);
		c->value = cam->params.color_params.saturation;
		break;
	case V4L2_CID_HFLIP:
		cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
				 TRANSFER_READ, 0);
		c->value = (cam->params.vp_params.user_effects &
			    CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
		break;
	case V4L2_CID_VFLIP:
		cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
				 TRANSFER_READ, 0);
		c->value = (cam->params.vp_params.user_effects &
			    CPIA2_VP_USER_EFFECTS_FLIP) != 0;
		break;
	case CPIA2_CID_TARGET_KB:
		c->value = cam->params.vc_params.target_kb;
		break;
	case CPIA2_CID_GPIO:
		cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
				 TRANSFER_READ, 0);
		c->value = cam->params.vp_params.gpio_data;
		break;
	case CPIA2_CID_FLICKER_MODE:
	{
		int i, mode;
		cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
				 TRANSFER_READ, 0);
		if(cam->params.flicker_control.cam_register &
		   CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
			mode = NEVER_FLICKER;
		} else {
		    if(cam->params.flicker_control.cam_register &
		       CPIA2_VP_FLICKER_MODES_50HZ) {
897
			mode = FLICKER_50;
898
		    } else {
899
			mode = FLICKER_60;
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
		    }
		}
		for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
			if(flicker_controls[i].value == mode) {
				c->value = i;
				break;
			}
		}
		if(i == NUM_FLICKER_CONTROLS)
			return -EINVAL;
		break;
	}
	case CPIA2_CID_FRAMERATE:
	{
		int maximum = NUM_FRAMERATE_CONTROLS - 1;
		int i;
		for(i=0; i<= maximum; i++) {
			if(cam->params.vp_params.frame_rate ==
			   framerate_controls[i].value)
				break;
		}
		if(i > maximum)
			return -EINVAL;
		c->value = i;
		break;
	}
	case CPIA2_CID_USB_ALT:
		c->value = cam->params.camera_state.stream_mode;
		break;
	case CPIA2_CID_LIGHTS:
	{
		int i;
		cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
				 TRANSFER_READ, 0);
		for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
			if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
			   lights_controls[i].value) {
				break;
			}
		}
		if(i == NUM_LIGHTS_CONTROLS)
			return -EINVAL;
		c->value = i;
		break;
	}
	case CPIA2_CID_RESET_CAMERA:
		return -EINVAL;
	default:
		return -EINVAL;
	}

	DBG("Get control id:%d, value:%d\n", c->id, c->value);

	return 0;
}

/******************************************************************************
 *
 *  ioctl_s_ctrl
 *
 *  V4L2 set the value of a control variable
 *
 *****************************************************************************/

964
static int cpia2_s_ctrl(struct file *file, void *fh, struct v4l2_control *c)
965
{
966
	struct camera_data *cam = video_drvdata(file);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	int i;
	int retval = 0;

	DBG("Set control id:%d, value:%d\n", c->id, c->value);

	/* Check that the value is in range */
	for(i=0; i<NUM_CONTROLS; i++) {
		if(c->id == controls[i].id) {
			if(c->value < controls[i].minimum ||
			   c->value > controls[i].maximum) {
				return -EINVAL;
			}
			break;
		}
	}
	if(i == NUM_CONTROLS)
		return -EINVAL;

	switch(c->id) {
	case V4L2_CID_BRIGHTNESS:
		cpia2_set_brightness(cam, c->value);
		break;
	case V4L2_CID_CONTRAST:
		cpia2_set_contrast(cam, c->value);
		break;
	case V4L2_CID_SATURATION:
		cpia2_set_saturation(cam, c->value);
		break;
	case V4L2_CID_HFLIP:
		cpia2_set_property_mirror(cam, c->value);
		break;
	case V4L2_CID_VFLIP:
		cpia2_set_property_flip(cam, c->value);
		break;
	case CPIA2_CID_TARGET_KB:
		retval = cpia2_set_target_kb(cam, c->value);
		break;
	case CPIA2_CID_GPIO:
		retval = cpia2_set_gpio(cam, c->value);
		break;
	case CPIA2_CID_FLICKER_MODE:
		retval = cpia2_set_flicker_mode(cam,
					      flicker_controls[c->value].value);
		break;
	case CPIA2_CID_FRAMERATE:
		retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
		break;
	case CPIA2_CID_USB_ALT:
		retval = cpia2_usb_change_streaming_alternate(cam, c->value);
		break;
	case CPIA2_CID_LIGHTS:
		retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
		break;
	case CPIA2_CID_RESET_CAMERA:
		cpia2_usb_stream_pause(cam);
		cpia2_reset_camera(cam);
		cpia2_usb_stream_resume(cam);
		break;
	default:
		retval = -EINVAL;
	}

	return retval;
}

/******************************************************************************
 *
 *  ioctl_g_jpegcomp
 *
 *  V4L2 get the JPEG compression parameters
 *
 *****************************************************************************/

1040
static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
1041
{
1042
	struct camera_data *cam = video_drvdata(file);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

	memset(parms, 0, sizeof(*parms));

	parms->quality = 80; // TODO: Can this be made meaningful?

	parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
	if(!cam->params.compression.inhibit_htables) {
		parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
	}

	parms->APPn = cam->APPn;
	parms->APP_len = cam->APP_len;
	if(cam->APP_len > 0) {
		memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
		parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
	}

	parms->COM_len = cam->COM_len;
	if(cam->COM_len > 0) {
		memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
		parms->jpeg_markers |= JPEG_MARKER_COM;
	}

	DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
	    parms->APP_len, parms->COM_len);

	return 0;
}

/******************************************************************************
 *
 *  ioctl_s_jpegcomp
 *
 *  V4L2 set the JPEG compression parameters
 *  NOTE: quality and some jpeg_markers are ignored.
 *
 *****************************************************************************/

1081
static int cpia2_s_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
1082
{
1083
	struct camera_data *cam = video_drvdata(file);
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

	DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
	    parms->APP_len, parms->COM_len);

	cam->params.compression.inhibit_htables =
		!(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);

	if(parms->APP_len != 0) {
		if(parms->APP_len > 0 &&
		   parms->APP_len <= sizeof(cam->APP_data) &&
		   parms->APPn >= 0 && parms->APPn <= 15) {
			cam->APPn = parms->APPn;
			cam->APP_len = parms->APP_len;
			memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
		} else {
			LOG("Bad APPn Params n=%d len=%d\n",
			    parms->APPn, parms->APP_len);
			return -EINVAL;
		}
	} else {
		cam->APP_len = 0;
	}

	if(parms->COM_len != 0) {
		if(parms->COM_len > 0 &&
		   parms->COM_len <= sizeof(cam->COM_data)) {
			cam->COM_len = parms->COM_len;
			memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
		} else {
			LOG("Bad COM_len=%d\n", parms->COM_len);
			return -EINVAL;
		}
	}

	return 0;
}

/******************************************************************************
 *
 *  ioctl_reqbufs
 *
 *  V4L2 Initiate memory mapping.
 *  NOTE: The user's request is ignored. For now the buffers are fixed.
 *
 *****************************************************************************/

1130
static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
1131
{
1132
	struct camera_data *cam = video_drvdata(file);
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	   req->memory != V4L2_MEMORY_MMAP)
		return -EINVAL;

	DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
	req->count = cam->num_frames;
	memset(&req->reserved, 0, sizeof(req->reserved));

	return 0;
}

/******************************************************************************
 *
 *  ioctl_querybuf
 *
 *  V4L2 Query memory buffer status.
 *
 *****************************************************************************/

1153
static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1154
{
1155
	struct camera_data *cam = video_drvdata(file);
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	   buf->index > cam->num_frames)
		return -EINVAL;

	buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
	buf->length = cam->frame_size;

	buf->memory = V4L2_MEMORY_MMAP;

	if(cam->mmapped)
		buf->flags = V4L2_BUF_FLAG_MAPPED;
	else
		buf->flags = 0;

	switch (cam->buffers[buf->index].status) {
	case FRAME_EMPTY:
	case FRAME_ERROR:
	case FRAME_READING:
		buf->bytesused = 0;
		buf->flags = V4L2_BUF_FLAG_QUEUED;
		break;
	case FRAME_READY:
		buf->bytesused = cam->buffers[buf->index].length;
		buf->timestamp = cam->buffers[buf->index].timestamp;
		buf->sequence = cam->buffers[buf->index].seq;
		buf->flags = V4L2_BUF_FLAG_DONE;
		break;
	}

	DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
	     buf->index, buf->m.offset, buf->flags, buf->sequence,
	     buf->bytesused);

	return 0;
}

/******************************************************************************
 *
 *  ioctl_qbuf
 *
 *  V4L2 User is freeing buffer
 *
 *****************************************************************************/

1201
static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1202
{
1203
	struct camera_data *cam = video_drvdata(file);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256

	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	   buf->memory != V4L2_MEMORY_MMAP ||
	   buf->index > cam->num_frames)
		return -EINVAL;

	DBG("QBUF #%d\n", buf->index);

	if(cam->buffers[buf->index].status == FRAME_READY)
		cam->buffers[buf->index].status = FRAME_EMPTY;

	return 0;
}

/******************************************************************************
 *
 *  find_earliest_filled_buffer
 *
 *  Helper for ioctl_dqbuf. Find the next ready buffer.
 *
 *****************************************************************************/

static int find_earliest_filled_buffer(struct camera_data *cam)
{
	int i;
	int found = -1;
	for (i=0; i<cam->num_frames; i++) {
		if(cam->buffers[i].status == FRAME_READY) {
			if(found < 0) {
				found = i;
			} else {
				/* find which buffer is earlier */
				struct timeval *tv1, *tv2;
				tv1 = &cam->buffers[i].timestamp;
				tv2 = &cam->buffers[found].timestamp;
				if(tv1->tv_sec < tv2->tv_sec ||
				   (tv1->tv_sec == tv2->tv_sec &&
				    tv1->tv_usec < tv2->tv_usec))
					found = i;
			}
		}
	}
	return found;
}

/******************************************************************************
 *
 *  ioctl_dqbuf
 *
 *  V4L2 User is asking for a filled buffer.
 *
 *****************************************************************************/

1257
static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
1258
{
1259
	struct camera_data *cam = video_drvdata(file);
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273
	int frame;

	if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
	   buf->memory != V4L2_MEMORY_MMAP)
		return -EINVAL;

	frame = find_earliest_filled_buffer(cam);

	if(frame < 0 && file->f_flags&O_NONBLOCK)
		return -EAGAIN;

	if(frame < 0) {
		/* Wait for a frame to become available */
		struct framebuf *cb=cam->curbuff;
1274
		mutex_unlock(&cam->v4l2_lock);
1275 1276 1277
		wait_event_interruptible(cam->wq_stream,
					 !cam->present ||
					 (cb=cam->curbuff)->status == FRAME_READY);
1278
		mutex_lock(&cam->v4l2_lock);
1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
		if (signal_pending(current))
			return -ERESTARTSYS;
		if(!cam->present)
			return -ENOTTY;
		frame = cb->num;
	}


	buf->index = frame;
	buf->bytesused = cam->buffers[buf->index].length;
	buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
	buf->field = V4L2_FIELD_NONE;
	buf->timestamp = cam->buffers[buf->index].timestamp;
	buf->sequence = cam->buffers[buf->index].seq;
	buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
	buf->length = cam->frame_size;
	buf->input = 0;
	buf->reserved = 0;
	memset(&buf->timecode, 0, sizeof(buf->timecode));

	DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
	    cam->buffers[buf->index].status, buf->sequence, buf->bytesused);

	return 0;
}

1305
static int cpia2_g_priority(struct file *file, void *_fh, enum v4l2_priority *p)
1306
{
1307
	struct cpia2_fh *fh = _fh;
1308

1309 1310 1311
	*p = fh->prio;
	return 0;
}
1312

1313 1314 1315 1316
static int cpia2_s_priority(struct file *file, void *_fh, enum v4l2_priority prio)
{
	struct camera_data *cam = video_drvdata(file);
	struct cpia2_fh *fh = fh;
1317

1318 1319 1320 1321
	if (cam->streaming && prio != fh->prio &&
			fh->prio == V4L2_PRIORITY_RECORD)
		/* Can't drop record priority while streaming */
		return -EBUSY;
1322

1323 1324 1325 1326 1327 1328
	if (prio == V4L2_PRIORITY_RECORD && prio != fh->prio &&
			v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD)
		/* Only one program can record at a time */
		return -EBUSY;
	return v4l2_prio_change(&cam->prio, &fh->prio, prio);
}
1329

1330 1331 1332
static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
{
	struct camera_data *cam = video_drvdata(file);
1333

1334 1335 1336
	DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
	if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;
1337

1338 1339 1340 1341
	if (!cam->streaming)
		return cpia2_usb_stream_start(cam,
				cam->params.camera_state.stream_mode);
	return -EINVAL;
1342 1343
}

1344
static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
1345
{
1346 1347 1348 1349 1350 1351 1352 1353 1354
	struct camera_data *cam = video_drvdata(file);

	DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
	if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		return -EINVAL;

	if (cam->streaming)
		return cpia2_usb_stream_stop(cam);
	return -EINVAL;
1355 1356 1357 1358 1359 1360 1361 1362 1363
}

/******************************************************************************
 *
 *  cpia2_mmap
 *
 *****************************************************************************/
static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
{
1364
	struct camera_data *cam = video_drvdata(file);
1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387
	int retval;

	/* Priority check */
	struct cpia2_fh *fh = file->private_data;
	if(fh->prio != V4L2_PRIORITY_RECORD) {
		return -EBUSY;
	}

	retval = cpia2_remap_buffer(cam, area);

	if(!retval)
		fh->mmapped = 1;
	return retval;
}

/******************************************************************************
 *
 *  reset_camera_struct_v4l
 *
 *  Sets all values to the defaults
 *****************************************************************************/
static void reset_camera_struct_v4l(struct camera_data *cam)
{
1388 1389
	cam->width = cam->params.roi.width;
	cam->height = cam->params.roi.height;
1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404

	cam->frame_size = buffer_size;
	cam->num_frames = num_buffers;

	/* FlickerModes */
	cam->params.flicker_control.flicker_mode_req = flicker_mode;
	cam->params.flicker_control.mains_frequency = flicker_freq;

	/* streamMode */
	cam->params.camera_state.stream_mode = alternate;

	cam->pixelformat = V4L2_PIX_FMT_JPEG;
	v4l2_prio_init(&cam->prio);
}

1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
	.vidioc_querycap		    = cpia2_querycap,
	.vidioc_enum_input		    = cpia2_enum_input,
	.vidioc_g_input			    = cpia2_g_input,
	.vidioc_s_input			    = cpia2_s_input,
	.vidioc_enum_fmt_vid_cap	    = cpia2_enum_fmt_vid_cap,
	.vidioc_g_fmt_vid_cap		    = cpia2_g_fmt_vid_cap,
	.vidioc_s_fmt_vid_cap		    = cpia2_s_fmt_vid_cap,
	.vidioc_try_fmt_vid_cap		    = cpia2_try_fmt_vid_cap,
	.vidioc_queryctrl		    = cpia2_queryctrl,
	.vidioc_querymenu		    = cpia2_querymenu,
	.vidioc_g_ctrl			    = cpia2_g_ctrl,
	.vidioc_s_ctrl			    = cpia2_s_ctrl,
	.vidioc_g_jpegcomp		    = cpia2_g_jpegcomp,
	.vidioc_s_jpegcomp		    = cpia2_s_jpegcomp,
	.vidioc_cropcap			    = cpia2_cropcap,
	.vidioc_reqbufs			    = cpia2_reqbufs,
	.vidioc_querybuf		    = cpia2_querybuf,
	.vidioc_qbuf			    = cpia2_qbuf,
	.vidioc_dqbuf			    = cpia2_dqbuf,
	.vidioc_streamon		    = cpia2_streamon,
	.vidioc_streamoff		    = cpia2_streamoff,
	.vidioc_g_priority		    = cpia2_g_priority,
	.vidioc_s_priority		    = cpia2_s_priority,
	.vidioc_default			    = cpia2_default,
};

1432 1433 1434
/***
 * The v4l video device structure initialized for this device
 ***/
1435
static const struct v4l2_file_operations cpia2_fops = {
1436 1437 1438 1439 1440
	.owner		= THIS_MODULE,
	.open		= cpia2_open,
	.release	= cpia2_close,
	.read		= cpia2_v4l_read,
	.poll		= cpia2_v4l_poll,
1441
	.unlocked_ioctl	= video_ioctl2,
1442
	.mmap		= cpia2_mmap,
1443 1444 1445 1446
};

static struct video_device cpia2_template = {
	/* I could not find any place for the old .initialize initializer?? */
1447 1448
	.name =		"CPiA2 Camera",
	.fops =		&cpia2_fops,
1449
	.ioctl_ops =	&cpia2_ioctl_ops,
1450
	.release =	video_device_release,
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
};

/******************************************************************************
 *
 *  cpia2_register_camera
 *
 *****************************************************************************/
int cpia2_register_camera(struct camera_data *cam)
{
	cam->vdev = video_device_alloc();
	if(!cam->vdev)
		return -ENOMEM;

	memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
	video_set_drvdata(cam->vdev, cam);
1466
	cam->vdev->lock = &cam->v4l2_lock;
1467 1468 1469 1470

	reset_camera_struct_v4l(cam);

	/* register v4l device */
1471
	if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
		ERR("video_register_device failed\n");
		video_device_release(cam->vdev);
		return -ENODEV;
	}

	return 0;
}

/******************************************************************************
 *
 *  cpia2_unregister_camera
 *
 *****************************************************************************/
void cpia2_unregister_camera(struct camera_data *cam)
{
	if (!cam->open_count) {
		video_unregister_device(cam->vdev);
	} else {
1490 1491 1492
		LOG("%s removed while open, deferring "
		    "video_unregister_device\n",
		    video_device_node_name(cam->vdev));
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
	}
}

/******************************************************************************
 *
 *  check_parameters
 *
 *  Make sure that all user-supplied parameters are sensible
 *****************************************************************************/
static void __init check_parameters(void)
{
	if(buffer_size < PAGE_SIZE) {
		buffer_size = PAGE_SIZE;
		LOG("buffer_size too small, setting to %d\n", buffer_size);
	} else if(buffer_size > 1024*1024) {
		/* arbitrary upper limiit */
		buffer_size = 1024*1024;
		LOG("buffer_size ridiculously large, setting to %d\n",
		    buffer_size);
	} else {
		buffer_size += PAGE_SIZE-1;
		buffer_size &= ~(PAGE_SIZE-1);
	}

	if(num_buffers < 1) {
		num_buffers = 1;
		LOG("num_buffers too small, setting to %d\n", num_buffers);
	} else if(num_buffers > VIDEO_MAX_FRAME) {
		num_buffers = VIDEO_MAX_FRAME;
		LOG("num_buffers too large, setting to %d\n", num_buffers);
	}

	if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
		alternate = DEFAULT_ALT;
		LOG("alternate specified is invalid, using %d\n", alternate);
	}

	if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
		flicker_mode = NEVER_FLICKER;
		LOG("Flicker mode specified is invalid, using %d\n",
		    flicker_mode);
	}

	if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
		flicker_freq = FLICKER_60;
		LOG("Flicker mode specified is invalid, using %d\n",
		    flicker_freq);
	}

	if(video_nr < -1 || video_nr > 64) {
		video_nr = -1;
		LOG("invalid video_nr specified, must be -1 to 64\n");
	}

	DBG("Using %d buffers, each %d bytes, alternate=%d\n",
	    num_buffers, buffer_size, alternate);
}

/************   Module Stuff ***************/


/******************************************************************************
 *
 * cpia2_init/module_init
 *
 *****************************************************************************/
1559
static int __init cpia2_init(void)
1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
{
	LOG("%s v%d.%d.%d\n",
	    ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
	check_parameters();
	cpia2_usb_init();
	return 0;
}


/******************************************************************************
 *
 * cpia2_exit/module_exit
 *
 *****************************************************************************/
1574
static void __exit cpia2_exit(void)
1575 1576 1577 1578 1579 1580 1581 1582
{
	cpia2_usb_cleanup();
	schedule_timeout(2 * HZ);
}

module_init(cpia2_init);
module_exit(cpia2_exit);