1/**************************************************************************** 2 * 3 * Filename: cpia2_v4l.c 4 * 5 * Copyright 2001, STMicrolectronics, Inc. 6 * Contact: steve.miller@st.com 7 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com> 8 * 9 * Description: 10 * This is a USB driver for CPia2 based video cameras. 11 * The infrastructure of this driver is based on the cpia usb driver by 12 * Jochen Scharrlach and Johannes Erdfeldt. 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation; either version 2 of the License, or 17 * (at your option) any later version. 18 * 19 * This program is distributed in the hope that it will be useful, 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 * GNU General Public License for more details. 23 * 24 * You should have received a copy of the GNU General Public License 25 * along with this program; if not, write to the Free Software 26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 27 * 28 * Stripped of 2.4 stuff ready for main kernel submit by 29 * Alan Cox <alan@lxorguk.ukuu.org.uk> 30 ****************************************************************************/ 31 32#include <linux/version.h> 33 34 35#include <linux/module.h> 36#include <linux/time.h> 37#include <linux/sched.h> 38#include <linux/slab.h> 39#include <linux/init.h> 40#include <linux/videodev.h> 41#include <linux/stringify.h> 42#include <media/v4l2-ioctl.h> 43 44#include "cpia2.h" 45#include "cpia2dev.h" 46 47static int video_nr = -1; 48module_param(video_nr, int, 0); 49MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)"); 50 51static int buffer_size = 68*1024; 52module_param(buffer_size, int, 0); 53MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)"); 54 55static int num_buffers = 3; 56module_param(num_buffers, int, 0); 57MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-" 58 __stringify(VIDEO_MAX_FRAME) ", default 3)"); 59 60static int alternate = DEFAULT_ALT; 61module_param(alternate, int, 0); 62MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-" 63 __stringify(USBIF_ISO_6) ", default " 64 __stringify(DEFAULT_ALT) ")"); 65 66static int flicker_freq = 60; 67module_param(flicker_freq, int, 0); 68MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or" 69 __stringify(60) ", default " 70 __stringify(60) ")"); 71 72static int flicker_mode = NEVER_FLICKER; 73module_param(flicker_mode, int, 0); 74MODULE_PARM_DESC(flicker_mode, 75 "Flicker supression (" __stringify(NEVER_FLICKER) "or" 76 __stringify(ANTI_FLICKER_ON) ", default " 77 __stringify(NEVER_FLICKER) ")"); 78 79MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>"); 80MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras"); 81MODULE_SUPPORTED_DEVICE("video"); 82MODULE_LICENSE("GPL"); 83 84#define ABOUT "V4L-Driver for Vision CPiA2 based cameras" 85 86struct control_menu_info { 87 int value; 88 char name[32]; 89}; 90 91static struct control_menu_info framerate_controls[] = 92{ 93 { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" }, 94 { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" }, 95 { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" }, 96 { CPIA2_VP_FRAMERATE_15, "15 fps" }, 97 { CPIA2_VP_FRAMERATE_25, "25 fps" }, 98 { CPIA2_VP_FRAMERATE_30, "30 fps" }, 99}; 100#define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls)) 101 102static struct control_menu_info flicker_controls[] = 103{ 104 { NEVER_FLICKER, "Off" }, 105 { FLICKER_50, "50 Hz" }, 106 { FLICKER_60, "60 Hz" }, 107}; 108#define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls)) 109 110static struct control_menu_info lights_controls[] = 111{ 112 { 0, "Off" }, 113 { 64, "Top" }, 114 { 128, "Bottom" }, 115 { 192, "Both" }, 116}; 117#define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls)) 118#define GPIO_LIGHTS_MASK 192 119 120static struct v4l2_queryctrl controls[] = { 121 { 122 .id = V4L2_CID_BRIGHTNESS, 123 .type = V4L2_CTRL_TYPE_INTEGER, 124 .name = "Brightness", 125 .minimum = 0, 126 .maximum = 255, 127 .step = 1, 128 .default_value = DEFAULT_BRIGHTNESS, 129 }, 130 { 131 .id = V4L2_CID_CONTRAST, 132 .type = V4L2_CTRL_TYPE_INTEGER, 133 .name = "Contrast", 134 .minimum = 0, 135 .maximum = 255, 136 .step = 1, 137 .default_value = DEFAULT_CONTRAST, 138 }, 139 { 140 .id = V4L2_CID_SATURATION, 141 .type = V4L2_CTRL_TYPE_INTEGER, 142 .name = "Saturation", 143 .minimum = 0, 144 .maximum = 255, 145 .step = 1, 146 .default_value = DEFAULT_SATURATION, 147 }, 148 { 149 .id = V4L2_CID_HFLIP, 150 .type = V4L2_CTRL_TYPE_BOOLEAN, 151 .name = "Mirror Horizontally", 152 .minimum = 0, 153 .maximum = 1, 154 .step = 1, 155 .default_value = 0, 156 }, 157 { 158 .id = V4L2_CID_VFLIP, 159 .type = V4L2_CTRL_TYPE_BOOLEAN, 160 .name = "Flip Vertically", 161 .minimum = 0, 162 .maximum = 1, 163 .step = 1, 164 .default_value = 0, 165 }, 166 { 167 .id = CPIA2_CID_TARGET_KB, 168 .type = V4L2_CTRL_TYPE_INTEGER, 169 .name = "Target KB", 170 .minimum = 0, 171 .maximum = 255, 172 .step = 1, 173 .default_value = DEFAULT_TARGET_KB, 174 }, 175 { 176 .id = CPIA2_CID_GPIO, 177 .type = V4L2_CTRL_TYPE_INTEGER, 178 .name = "GPIO", 179 .minimum = 0, 180 .maximum = 255, 181 .step = 1, 182 .default_value = 0, 183 }, 184 { 185 .id = CPIA2_CID_FLICKER_MODE, 186 .type = V4L2_CTRL_TYPE_MENU, 187 .name = "Flicker Reduction", 188 .minimum = 0, 189 .maximum = NUM_FLICKER_CONTROLS-1, 190 .step = 1, 191 .default_value = 0, 192 }, 193 { 194 .id = CPIA2_CID_FRAMERATE, 195 .type = V4L2_CTRL_TYPE_MENU, 196 .name = "Framerate", 197 .minimum = 0, 198 .maximum = NUM_FRAMERATE_CONTROLS-1, 199 .step = 1, 200 .default_value = NUM_FRAMERATE_CONTROLS-1, 201 }, 202 { 203 .id = CPIA2_CID_USB_ALT, 204 .type = V4L2_CTRL_TYPE_INTEGER, 205 .name = "USB Alternate", 206 .minimum = USBIF_ISO_1, 207 .maximum = USBIF_ISO_6, 208 .step = 1, 209 .default_value = DEFAULT_ALT, 210 }, 211 { 212 .id = CPIA2_CID_LIGHTS, 213 .type = V4L2_CTRL_TYPE_MENU, 214 .name = "Lights", 215 .minimum = 0, 216 .maximum = NUM_LIGHTS_CONTROLS-1, 217 .step = 1, 218 .default_value = 0, 219 }, 220 { 221 .id = CPIA2_CID_RESET_CAMERA, 222 .type = V4L2_CTRL_TYPE_BUTTON, 223 .name = "Reset Camera", 224 .minimum = 0, 225 .maximum = 0, 226 .step = 0, 227 .default_value = 0, 228 }, 229}; 230#define NUM_CONTROLS (ARRAY_SIZE(controls)) 231 232 233/****************************************************************************** 234 * 235 * cpia2_open 236 * 237 *****************************************************************************/ 238static int cpia2_open(struct file *file) 239{ 240 struct camera_data *cam = video_drvdata(file); 241 int retval = 0; 242 243 if (!cam) { 244 ERR("Internal error, camera_data not found!\n"); 245 return -ENODEV; 246 } 247 248 if(mutex_lock_interruptible(&cam->busy_lock)) 249 return -ERESTARTSYS; 250 251 if(!cam->present) { 252 retval = -ENODEV; 253 goto err_return; 254 } 255 256 if (cam->open_count > 0) { 257 goto skip_init; 258 } 259 260 if (cpia2_allocate_buffers(cam)) { 261 retval = -ENOMEM; 262 goto err_return; 263 } 264 265 /* reset the camera */ 266 if (cpia2_reset_camera(cam) < 0) { 267 retval = -EIO; 268 goto err_return; 269 } 270 271 cam->APP_len = 0; 272 cam->COM_len = 0; 273 274skip_init: 275 { 276 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL); 277 if(!fh) { 278 retval = -ENOMEM; 279 goto err_return; 280 } 281 file->private_data = fh; 282 fh->prio = V4L2_PRIORITY_UNSET; 283 v4l2_prio_open(&cam->prio, &fh->prio); 284 fh->mmapped = 0; 285 } 286 287 ++cam->open_count; 288 289 cpia2_dbg_dump_registers(cam); 290 291err_return: 292 mutex_unlock(&cam->busy_lock); 293 return retval; 294} 295 296/****************************************************************************** 297 * 298 * cpia2_close 299 * 300 *****************************************************************************/ 301static int cpia2_close(struct file *file) 302{ 303 struct video_device *dev = video_devdata(file); 304 struct camera_data *cam = video_get_drvdata(dev); 305 struct cpia2_fh *fh = file->private_data; 306 307 mutex_lock(&cam->busy_lock); 308 309 if (cam->present && 310 (cam->open_count == 1 311 || fh->prio == V4L2_PRIORITY_RECORD 312 )) { 313 cpia2_usb_stream_stop(cam); 314 315 if(cam->open_count == 1) { 316 /* save camera state for later open */ 317 cpia2_save_camera_state(cam); 318 319 cpia2_set_low_power(cam); 320 cpia2_free_buffers(cam); 321 } 322 } 323 324 { 325 if(fh->mmapped) 326 cam->mmapped = 0; 327 v4l2_prio_close(&cam->prio, fh->prio); 328 file->private_data = NULL; 329 kfree(fh); 330 } 331 332 if (--cam->open_count == 0) { 333 cpia2_free_buffers(cam); 334 if (!cam->present) { 335 video_unregister_device(dev); 336 mutex_unlock(&cam->busy_lock); 337 kfree(cam); 338 return 0; 339 } 340 } 341 342 mutex_unlock(&cam->busy_lock); 343 344 return 0; 345} 346 347/****************************************************************************** 348 * 349 * cpia2_v4l_read 350 * 351 *****************************************************************************/ 352static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count, 353 loff_t *off) 354{ 355 struct camera_data *cam = video_drvdata(file); 356 int noblock = file->f_flags&O_NONBLOCK; 357 358 struct cpia2_fh *fh = file->private_data; 359 360 if(!cam) 361 return -EINVAL; 362 363 /* Priority check */ 364 if(fh->prio != V4L2_PRIORITY_RECORD) { 365 return -EBUSY; 366 } 367 368 return cpia2_read(cam, buf, count, noblock); 369} 370 371 372/****************************************************************************** 373 * 374 * cpia2_v4l_poll 375 * 376 *****************************************************************************/ 377static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait) 378{ 379 struct camera_data *cam = video_drvdata(filp); 380 struct cpia2_fh *fh = filp->private_data; 381 382 if(!cam) 383 return POLLERR; 384 385 /* Priority check */ 386 if(fh->prio != V4L2_PRIORITY_RECORD) { 387 return POLLERR; 388 } 389 390 return cpia2_poll(cam, filp, wait); 391} 392 393 394/****************************************************************************** 395 * 396 * ioctl_cap_query 397 * 398 *****************************************************************************/ 399static int ioctl_cap_query(void *arg, struct camera_data *cam) 400{ 401 struct video_capability *vc; 402 int retval = 0; 403 vc = arg; 404 405 if (cam->params.pnp_id.product == 0x151) 406 strcpy(vc->name, "QX5 Microscope"); 407 else 408 strcpy(vc->name, "CPiA2 Camera"); 409 410 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER; 411 vc->channels = 1; 412 vc->audios = 0; 413 vc->minwidth = 176; /* VIDEOSIZE_QCIF */ 414 vc->minheight = 144; 415 switch (cam->params.version.sensor_flags) { 416 case CPIA2_VP_SENSOR_FLAGS_500: 417 vc->maxwidth = STV_IMAGE_VGA_COLS; 418 vc->maxheight = STV_IMAGE_VGA_ROWS; 419 break; 420 case CPIA2_VP_SENSOR_FLAGS_410: 421 vc->maxwidth = STV_IMAGE_CIF_COLS; 422 vc->maxheight = STV_IMAGE_CIF_ROWS; 423 break; 424 default: 425 return -EINVAL; 426 } 427 428 return retval; 429} 430 431/****************************************************************************** 432 * 433 * ioctl_get_channel 434 * 435 *****************************************************************************/ 436static int ioctl_get_channel(void *arg) 437{ 438 int retval = 0; 439 struct video_channel *v; 440 v = arg; 441 442 if (v->channel != 0) 443 return -EINVAL; 444 445 v->channel = 0; 446 strcpy(v->name, "Camera"); 447 v->tuners = 0; 448 v->flags = 0; 449 v->type = VIDEO_TYPE_CAMERA; 450 v->norm = 0; 451 452 return retval; 453} 454 455/****************************************************************************** 456 * 457 * ioctl_set_channel 458 * 459 *****************************************************************************/ 460static int ioctl_set_channel(void *arg) 461{ 462 struct video_channel *v; 463 int retval = 0; 464 v = arg; 465 466 if (retval == 0 && v->channel != 0) 467 retval = -EINVAL; 468 469 return retval; 470} 471 472/****************************************************************************** 473 * 474 * ioctl_set_image_prop 475 * 476 *****************************************************************************/ 477static int ioctl_set_image_prop(void *arg, struct camera_data *cam) 478{ 479 struct video_picture *vp; 480 int retval = 0; 481 vp = arg; 482 483 /* brightness, color, contrast need no check 0-65535 */ 484 memcpy(&cam->vp, vp, sizeof(*vp)); 485 486 /* update cam->params.colorParams */ 487 cam->params.color_params.brightness = vp->brightness / 256; 488 cam->params.color_params.saturation = vp->colour / 256; 489 cam->params.color_params.contrast = vp->contrast / 256; 490 491 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n", 492 cam->params.color_params.brightness, 493 cam->params.color_params.saturation, 494 cam->params.color_params.contrast); 495 496 cpia2_set_color_params(cam); 497 498 return retval; 499} 500 501static int sync(struct camera_data *cam, int frame_nr) 502{ 503 struct framebuf *frame = &cam->buffers[frame_nr]; 504 505 while (1) { 506 if (frame->status == FRAME_READY) 507 return 0; 508 509 if (!cam->streaming) { 510 frame->status = FRAME_READY; 511 frame->length = 0; 512 return 0; 513 } 514 515 mutex_unlock(&cam->busy_lock); 516 wait_event_interruptible(cam->wq_stream, 517 !cam->streaming || 518 frame->status == FRAME_READY); 519 mutex_lock(&cam->busy_lock); 520 if (signal_pending(current)) 521 return -ERESTARTSYS; 522 if(!cam->present) 523 return -ENOTTY; 524 } 525} 526 527/****************************************************************************** 528 * 529 * ioctl_set_window_size 530 * 531 *****************************************************************************/ 532static int ioctl_set_window_size(void *arg, struct camera_data *cam, 533 struct cpia2_fh *fh) 534{ 535 /* copy_from_user, check validity, copy to internal structure */ 536 struct video_window *vw; 537 int frame, err; 538 vw = arg; 539 540 if (vw->clipcount != 0) /* clipping not supported */ 541 return -EINVAL; 542 543 if (vw->clips != NULL) /* clipping not supported */ 544 return -EINVAL; 545 546 /* Ensure that only this process can change the format. */ 547 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD); 548 if(err != 0) 549 return err; 550 551 cam->pixelformat = V4L2_PIX_FMT_JPEG; 552 553 /* Be sure to supply the Huffman tables, this isn't MJPEG */ 554 cam->params.compression.inhibit_htables = 0; 555 556 /* we set the video window to something smaller or equal to what 557 * is requested by the user??? 558 */ 559 DBG("Requested width = %d, height = %d\n", vw->width, vw->height); 560 if (vw->width != cam->vw.width || vw->height != cam->vw.height) { 561 cam->vw.width = vw->width; 562 cam->vw.height = vw->height; 563 cam->params.roi.width = vw->width; 564 cam->params.roi.height = vw->height; 565 cpia2_set_format(cam); 566 } 567 568 for (frame = 0; frame < cam->num_frames; ++frame) { 569 if (cam->buffers[frame].status == FRAME_READING) 570 if ((err = sync(cam, frame)) < 0) 571 return err; 572 573 cam->buffers[frame].status = FRAME_EMPTY; 574 } 575 576 return 0; 577} 578 579/****************************************************************************** 580 * 581 * ioctl_get_mbuf 582 * 583 *****************************************************************************/ 584static int ioctl_get_mbuf(void *arg, struct camera_data *cam) 585{ 586 struct video_mbuf *vm; 587 int i; 588 vm = arg; 589 590 memset(vm, 0, sizeof(*vm)); 591 vm->size = cam->frame_size*cam->num_frames; 592 vm->frames = cam->num_frames; 593 for (i = 0; i < cam->num_frames; i++) 594 vm->offsets[i] = cam->frame_size * i; 595 596 return 0; 597} 598 599/****************************************************************************** 600 * 601 * ioctl_mcapture 602 * 603 *****************************************************************************/ 604static int ioctl_mcapture(void *arg, struct camera_data *cam, 605 struct cpia2_fh *fh) 606{ 607 struct video_mmap *vm; 608 int video_size, err; 609 vm = arg; 610 611 if (vm->frame < 0 || vm->frame >= cam->num_frames) 612 return -EINVAL; 613 614 /* set video size */ 615 video_size = cpia2_match_video_size(vm->width, vm->height); 616 if (cam->video_size < 0) { 617 return -EINVAL; 618 } 619 620 /* Ensure that only this process can change the format. */ 621 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD); 622 if(err != 0) 623 return err; 624 625 if (video_size != cam->video_size) { 626 cam->video_size = video_size; 627 cam->params.roi.width = vm->width; 628 cam->params.roi.height = vm->height; 629 cpia2_set_format(cam); 630 } 631 632 if (cam->buffers[vm->frame].status == FRAME_READING) 633 if ((err=sync(cam, vm->frame)) < 0) 634 return err; 635 636 cam->buffers[vm->frame].status = FRAME_EMPTY; 637 638 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode); 639} 640 641/****************************************************************************** 642 * 643 * ioctl_sync 644 * 645 *****************************************************************************/ 646static int ioctl_sync(void *arg, struct camera_data *cam) 647{ 648 int frame; 649 650 frame = *(int*)arg; 651 652 if (frame < 0 || frame >= cam->num_frames) 653 return -EINVAL; 654 655 return sync(cam, frame); 656} 657 658 659/****************************************************************************** 660 * 661 * ioctl_set_gpio 662 * 663 *****************************************************************************/ 664 665static int ioctl_set_gpio(void *arg, struct camera_data *cam) 666{ 667 __u32 gpio_val; 668 669 gpio_val = *(__u32*) arg; 670 671 if (gpio_val &~ 0xFFU) 672 return -EINVAL; 673 674 return cpia2_set_gpio(cam, (unsigned char)gpio_val); 675} 676 677/****************************************************************************** 678 * 679 * ioctl_querycap 680 * 681 * V4L2 device capabilities 682 * 683 *****************************************************************************/ 684 685static int ioctl_querycap(void *arg, struct camera_data *cam) 686{ 687 struct v4l2_capability *vc = arg; 688 689 memset(vc, 0, sizeof(*vc)); 690 strcpy(vc->driver, "cpia2"); 691 692 if (cam->params.pnp_id.product == 0x151) 693 strcpy(vc->card, "QX5 Microscope"); 694 else 695 strcpy(vc->card, "CPiA2 Camera"); 696 switch (cam->params.pnp_id.device_type) { 697 case DEVICE_STV_672: 698 strcat(vc->card, " (672/"); 699 break; 700 case DEVICE_STV_676: 701 strcat(vc->card, " (676/"); 702 break; 703 default: 704 strcat(vc->card, " (???/"); 705 break; 706 } 707 switch (cam->params.version.sensor_flags) { 708 case CPIA2_VP_SENSOR_FLAGS_404: 709 strcat(vc->card, "404)"); 710 break; 711 case CPIA2_VP_SENSOR_FLAGS_407: 712 strcat(vc->card, "407)"); 713 break; 714 case CPIA2_VP_SENSOR_FLAGS_409: 715 strcat(vc->card, "409)"); 716 break; 717 case CPIA2_VP_SENSOR_FLAGS_410: 718 strcat(vc->card, "410)"); 719 break; 720 case CPIA2_VP_SENSOR_FLAGS_500: 721 strcat(vc->card, "500)"); 722 break; 723 default: 724 strcat(vc->card, "???)"); 725 break; 726 } 727 728 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0) 729 memset(vc->bus_info,0, sizeof(vc->bus_info)); 730 731 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER, 732 CPIA2_PATCH_VER); 733 734 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE | 735 V4L2_CAP_READWRITE | 736 V4L2_CAP_STREAMING; 737 738 return 0; 739} 740 741/****************************************************************************** 742 * 743 * ioctl_input 744 * 745 * V4L2 input get/set/enumerate 746 * 747 *****************************************************************************/ 748 749static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam) 750{ 751 struct v4l2_input *i = arg; 752 753 if(ioclt_nr != VIDIOC_G_INPUT) { 754 if (i->index != 0) 755 return -EINVAL; 756 } 757 758 memset(i, 0, sizeof(*i)); 759 strcpy(i->name, "Camera"); 760 i->type = V4L2_INPUT_TYPE_CAMERA; 761 762 return 0; 763} 764 765/****************************************************************************** 766 * 767 * ioctl_enum_fmt 768 * 769 * V4L2 format enumerate 770 * 771 *****************************************************************************/ 772 773static int ioctl_enum_fmt(void *arg,struct camera_data *cam) 774{ 775 struct v4l2_fmtdesc *f = arg; 776 int index = f->index; 777 778 if (index < 0 || index > 1) 779 return -EINVAL; 780 781 memset(f, 0, sizeof(*f)); 782 f->index = index; 783 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 784 f->flags = V4L2_FMT_FLAG_COMPRESSED; 785 switch(index) { 786 case 0: 787 strcpy(f->description, "MJPEG"); 788 f->pixelformat = V4L2_PIX_FMT_MJPEG; 789 break; 790 case 1: 791 strcpy(f->description, "JPEG"); 792 f->pixelformat = V4L2_PIX_FMT_JPEG; 793 break; 794 default: 795 return -EINVAL; 796 } 797 798 return 0; 799} 800 801/****************************************************************************** 802 * 803 * ioctl_try_fmt 804 * 805 * V4L2 format try 806 * 807 *****************************************************************************/ 808 809static int ioctl_try_fmt(void *arg,struct camera_data *cam) 810{ 811 struct v4l2_format *f = arg; 812 813 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 814 return -EINVAL; 815 816 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG && 817 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) 818 return -EINVAL; 819 820 f->fmt.pix.field = V4L2_FIELD_NONE; 821 f->fmt.pix.bytesperline = 0; 822 f->fmt.pix.sizeimage = cam->frame_size; 823 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; 824 f->fmt.pix.priv = 0; 825 826 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) { 827 case VIDEOSIZE_VGA: 828 f->fmt.pix.width = 640; 829 f->fmt.pix.height = 480; 830 break; 831 case VIDEOSIZE_CIF: 832 f->fmt.pix.width = 352; 833 f->fmt.pix.height = 288; 834 break; 835 case VIDEOSIZE_QVGA: 836 f->fmt.pix.width = 320; 837 f->fmt.pix.height = 240; 838 break; 839 case VIDEOSIZE_288_216: 840 f->fmt.pix.width = 288; 841 f->fmt.pix.height = 216; 842 break; 843 case VIDEOSIZE_256_192: 844 f->fmt.pix.width = 256; 845 f->fmt.pix.height = 192; 846 break; 847 case VIDEOSIZE_224_168: 848 f->fmt.pix.width = 224; 849 f->fmt.pix.height = 168; 850 break; 851 case VIDEOSIZE_192_144: 852 f->fmt.pix.width = 192; 853 f->fmt.pix.height = 144; 854 break; 855 case VIDEOSIZE_QCIF: 856 default: 857 f->fmt.pix.width = 176; 858 f->fmt.pix.height = 144; 859 break; 860 } 861 862 return 0; 863} 864 865/****************************************************************************** 866 * 867 * ioctl_set_fmt 868 * 869 * V4L2 format set 870 * 871 *****************************************************************************/ 872 873static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh) 874{ 875 struct v4l2_format *f = arg; 876 int err, frame; 877 878 err = ioctl_try_fmt(arg, cam); 879 if(err != 0) 880 return err; 881 882 /* Ensure that only this process can change the format. */ 883 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD); 884 if(err != 0) { 885 return err; 886 } 887 888 cam->pixelformat = f->fmt.pix.pixelformat; 889 890 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle 891 * the missing Huffman table properly. */ 892 cam->params.compression.inhibit_htables = 0; 893 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/ 894 895 /* we set the video window to something smaller or equal to what 896 * is requested by the user??? 897 */ 898 DBG("Requested width = %d, height = %d\n", 899 f->fmt.pix.width, f->fmt.pix.height); 900 if (f->fmt.pix.width != cam->vw.width || 901 f->fmt.pix.height != cam->vw.height) { 902 cam->vw.width = f->fmt.pix.width; 903 cam->vw.height = f->fmt.pix.height; 904 cam->params.roi.width = f->fmt.pix.width; 905 cam->params.roi.height = f->fmt.pix.height; 906 cpia2_set_format(cam); 907 } 908 909 for (frame = 0; frame < cam->num_frames; ++frame) { 910 if (cam->buffers[frame].status == FRAME_READING) 911 if ((err = sync(cam, frame)) < 0) 912 return err; 913 914 cam->buffers[frame].status = FRAME_EMPTY; 915 } 916 917 return 0; 918} 919 920/****************************************************************************** 921 * 922 * ioctl_get_fmt 923 * 924 * V4L2 format get 925 * 926 *****************************************************************************/ 927 928static int ioctl_get_fmt(void *arg,struct camera_data *cam) 929{ 930 struct v4l2_format *f = arg; 931 932 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 933 return -EINVAL; 934 935 f->fmt.pix.width = cam->vw.width; 936 f->fmt.pix.height = cam->vw.height; 937 f->fmt.pix.pixelformat = cam->pixelformat; 938 f->fmt.pix.field = V4L2_FIELD_NONE; 939 f->fmt.pix.bytesperline = 0; 940 f->fmt.pix.sizeimage = cam->frame_size; 941 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; 942 f->fmt.pix.priv = 0; 943 944 return 0; 945} 946 947/****************************************************************************** 948 * 949 * ioctl_cropcap 950 * 951 * V4L2 query cropping capabilities 952 * NOTE: cropping is currently disabled 953 * 954 *****************************************************************************/ 955 956static int ioctl_cropcap(void *arg,struct camera_data *cam) 957{ 958 struct v4l2_cropcap *c = arg; 959 960 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 961 return -EINVAL; 962 963 c->bounds.left = 0; 964 c->bounds.top = 0; 965 c->bounds.width = cam->vw.width; 966 c->bounds.height = cam->vw.height; 967 c->defrect.left = 0; 968 c->defrect.top = 0; 969 c->defrect.width = cam->vw.width; 970 c->defrect.height = cam->vw.height; 971 c->pixelaspect.numerator = 1; 972 c->pixelaspect.denominator = 1; 973 974 return 0; 975} 976 977/****************************************************************************** 978 * 979 * ioctl_queryctrl 980 * 981 * V4L2 query possible control variables 982 * 983 *****************************************************************************/ 984 985static int ioctl_queryctrl(void *arg,struct camera_data *cam) 986{ 987 struct v4l2_queryctrl *c = arg; 988 int i; 989 990 for(i=0; i<NUM_CONTROLS; ++i) { 991 if(c->id == controls[i].id) { 992 memcpy(c, controls+i, sizeof(*c)); 993 break; 994 } 995 } 996 997 if(i == NUM_CONTROLS) 998 return -EINVAL; 999 1000 /* Some devices have additional limitations */ 1001 switch(c->id) { 1002 case V4L2_CID_BRIGHTNESS: 1003 /*** 1004 * Don't let the register be set to zero - bug in VP4 1005 * flash of full brightness 1006 ***/ 1007 if (cam->params.pnp_id.device_type == DEVICE_STV_672) 1008 c->minimum = 1; 1009 break; 1010 case V4L2_CID_VFLIP: 1011 // VP5 Only 1012 if(cam->params.pnp_id.device_type == DEVICE_STV_672) 1013 c->flags |= V4L2_CTRL_FLAG_DISABLED; 1014 break; 1015 case CPIA2_CID_FRAMERATE: 1016 if(cam->params.pnp_id.device_type == DEVICE_STV_672 && 1017 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){ 1018 // Maximum 15fps 1019 for(i=0; i<c->maximum; ++i) { 1020 if(framerate_controls[i].value == 1021 CPIA2_VP_FRAMERATE_15) { 1022 c->maximum = i; 1023 c->default_value = i; 1024 } 1025 } 1026 } 1027 break; 1028 case CPIA2_CID_FLICKER_MODE: 1029 // Flicker control only valid for 672. 1030 if(cam->params.pnp_id.device_type != DEVICE_STV_672) 1031 c->flags |= V4L2_CTRL_FLAG_DISABLED; 1032 break; 1033 case CPIA2_CID_LIGHTS: 1034 // Light control only valid for the QX5 Microscope. 1035 if(cam->params.pnp_id.product != 0x151) 1036 c->flags |= V4L2_CTRL_FLAG_DISABLED; 1037 break; 1038 default: 1039 break; 1040 } 1041 1042 return 0; 1043} 1044 1045/****************************************************************************** 1046 * 1047 * ioctl_querymenu 1048 * 1049 * V4L2 query possible control variables 1050 * 1051 *****************************************************************************/ 1052 1053static int ioctl_querymenu(void *arg,struct camera_data *cam) 1054{ 1055 struct v4l2_querymenu *m = arg; 1056 1057 memset(m->name, 0, sizeof(m->name)); 1058 m->reserved = 0; 1059 1060 switch(m->id) { 1061 case CPIA2_CID_FLICKER_MODE: 1062 if (m->index >= NUM_FLICKER_CONTROLS) 1063 return -EINVAL; 1064 1065 strcpy(m->name, flicker_controls[m->index].name); 1066 break; 1067 case CPIA2_CID_FRAMERATE: 1068 { 1069 int maximum = NUM_FRAMERATE_CONTROLS - 1; 1070 if(cam->params.pnp_id.device_type == DEVICE_STV_672 && 1071 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){ 1072 // Maximum 15fps 1073 int i; 1074 for(i=0; i<maximum; ++i) { 1075 if(framerate_controls[i].value == 1076 CPIA2_VP_FRAMERATE_15) 1077 maximum = i; 1078 } 1079 } 1080 if (m->index > maximum) 1081 return -EINVAL; 1082 1083 strcpy(m->name, framerate_controls[m->index].name); 1084 break; 1085 } 1086 case CPIA2_CID_LIGHTS: 1087 if (m->index >= NUM_LIGHTS_CONTROLS) 1088 return -EINVAL; 1089 1090 strcpy(m->name, lights_controls[m->index].name); 1091 break; 1092 default: 1093 return -EINVAL; 1094 } 1095 1096 return 0; 1097} 1098 1099/****************************************************************************** 1100 * 1101 * ioctl_g_ctrl 1102 * 1103 * V4L2 get the value of a control variable 1104 * 1105 *****************************************************************************/ 1106 1107static int ioctl_g_ctrl(void *arg,struct camera_data *cam) 1108{ 1109 struct v4l2_control *c = arg; 1110 1111 switch(c->id) { 1112 case V4L2_CID_BRIGHTNESS: 1113 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS, 1114 TRANSFER_READ, 0); 1115 c->value = cam->params.color_params.brightness; 1116 break; 1117 case V4L2_CID_CONTRAST: 1118 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST, 1119 TRANSFER_READ, 0); 1120 c->value = cam->params.color_params.contrast; 1121 break; 1122 case V4L2_CID_SATURATION: 1123 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION, 1124 TRANSFER_READ, 0); 1125 c->value = cam->params.color_params.saturation; 1126 break; 1127 case V4L2_CID_HFLIP: 1128 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, 1129 TRANSFER_READ, 0); 1130 c->value = (cam->params.vp_params.user_effects & 1131 CPIA2_VP_USER_EFFECTS_MIRROR) != 0; 1132 break; 1133 case V4L2_CID_VFLIP: 1134 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS, 1135 TRANSFER_READ, 0); 1136 c->value = (cam->params.vp_params.user_effects & 1137 CPIA2_VP_USER_EFFECTS_FLIP) != 0; 1138 break; 1139 case CPIA2_CID_TARGET_KB: 1140 c->value = cam->params.vc_params.target_kb; 1141 break; 1142 case CPIA2_CID_GPIO: 1143 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA, 1144 TRANSFER_READ, 0); 1145 c->value = cam->params.vp_params.gpio_data; 1146 break; 1147 case CPIA2_CID_FLICKER_MODE: 1148 { 1149 int i, mode; 1150 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES, 1151 TRANSFER_READ, 0); 1152 if(cam->params.flicker_control.cam_register & 1153 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) { 1154 mode = NEVER_FLICKER; 1155 } else { 1156 if(cam->params.flicker_control.cam_register & 1157 CPIA2_VP_FLICKER_MODES_50HZ) { 1158 mode = FLICKER_50; 1159 } else { 1160 mode = FLICKER_60; 1161 } 1162 } 1163 for(i=0; i<NUM_FLICKER_CONTROLS; i++) { 1164 if(flicker_controls[i].value == mode) { 1165 c->value = i; 1166 break; 1167 } 1168 } 1169 if(i == NUM_FLICKER_CONTROLS) 1170 return -EINVAL; 1171 break; 1172 } 1173 case CPIA2_CID_FRAMERATE: 1174 { 1175 int maximum = NUM_FRAMERATE_CONTROLS - 1; 1176 int i; 1177 for(i=0; i<= maximum; i++) { 1178 if(cam->params.vp_params.frame_rate == 1179 framerate_controls[i].value) 1180 break; 1181 } 1182 if(i > maximum) 1183 return -EINVAL; 1184 c->value = i; 1185 break; 1186 } 1187 case CPIA2_CID_USB_ALT: 1188 c->value = cam->params.camera_state.stream_mode; 1189 break; 1190 case CPIA2_CID_LIGHTS: 1191 { 1192 int i; 1193 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA, 1194 TRANSFER_READ, 0); 1195 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) { 1196 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) == 1197 lights_controls[i].value) { 1198 break; 1199 } 1200 } 1201 if(i == NUM_LIGHTS_CONTROLS) 1202 return -EINVAL; 1203 c->value = i; 1204 break; 1205 } 1206 case CPIA2_CID_RESET_CAMERA: 1207 return -EINVAL; 1208 default: 1209 return -EINVAL; 1210 } 1211 1212 DBG("Get control id:%d, value:%d\n", c->id, c->value); 1213 1214 return 0; 1215} 1216 1217/****************************************************************************** 1218 * 1219 * ioctl_s_ctrl 1220 * 1221 * V4L2 set the value of a control variable 1222 * 1223 *****************************************************************************/ 1224 1225static int ioctl_s_ctrl(void *arg,struct camera_data *cam) 1226{ 1227 struct v4l2_control *c = arg; 1228 int i; 1229 int retval = 0; 1230 1231 DBG("Set control id:%d, value:%d\n", c->id, c->value); 1232 1233 /* Check that the value is in range */ 1234 for(i=0; i<NUM_CONTROLS; i++) { 1235 if(c->id == controls[i].id) { 1236 if(c->value < controls[i].minimum || 1237 c->value > controls[i].maximum) { 1238 return -EINVAL; 1239 } 1240 break; 1241 } 1242 } 1243 if(i == NUM_CONTROLS) 1244 return -EINVAL; 1245 1246 switch(c->id) { 1247 case V4L2_CID_BRIGHTNESS: 1248 cpia2_set_brightness(cam, c->value); 1249 break; 1250 case V4L2_CID_CONTRAST: 1251 cpia2_set_contrast(cam, c->value); 1252 break; 1253 case V4L2_CID_SATURATION: 1254 cpia2_set_saturation(cam, c->value); 1255 break; 1256 case V4L2_CID_HFLIP: 1257 cpia2_set_property_mirror(cam, c->value); 1258 break; 1259 case V4L2_CID_VFLIP: 1260 cpia2_set_property_flip(cam, c->value); 1261 break; 1262 case CPIA2_CID_TARGET_KB: 1263 retval = cpia2_set_target_kb(cam, c->value); 1264 break; 1265 case CPIA2_CID_GPIO: 1266 retval = cpia2_set_gpio(cam, c->value); 1267 break; 1268 case CPIA2_CID_FLICKER_MODE: 1269 retval = cpia2_set_flicker_mode(cam, 1270 flicker_controls[c->value].value); 1271 break; 1272 case CPIA2_CID_FRAMERATE: 1273 retval = cpia2_set_fps(cam, framerate_controls[c->value].value); 1274 break; 1275 case CPIA2_CID_USB_ALT: 1276 retval = cpia2_usb_change_streaming_alternate(cam, c->value); 1277 break; 1278 case CPIA2_CID_LIGHTS: 1279 retval = cpia2_set_gpio(cam, lights_controls[c->value].value); 1280 break; 1281 case CPIA2_CID_RESET_CAMERA: 1282 cpia2_usb_stream_pause(cam); 1283 cpia2_reset_camera(cam); 1284 cpia2_usb_stream_resume(cam); 1285 break; 1286 default: 1287 retval = -EINVAL; 1288 } 1289 1290 return retval; 1291} 1292 1293/****************************************************************************** 1294 * 1295 * ioctl_g_jpegcomp 1296 * 1297 * V4L2 get the JPEG compression parameters 1298 * 1299 *****************************************************************************/ 1300 1301static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam) 1302{ 1303 struct v4l2_jpegcompression *parms = arg; 1304 1305 memset(parms, 0, sizeof(*parms)); 1306 1307 parms->quality = 80; // TODO: Can this be made meaningful? 1308 1309 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI; 1310 if(!cam->params.compression.inhibit_htables) { 1311 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT; 1312 } 1313 1314 parms->APPn = cam->APPn; 1315 parms->APP_len = cam->APP_len; 1316 if(cam->APP_len > 0) { 1317 memcpy(parms->APP_data, cam->APP_data, cam->APP_len); 1318 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP; 1319 } 1320 1321 parms->COM_len = cam->COM_len; 1322 if(cam->COM_len > 0) { 1323 memcpy(parms->COM_data, cam->COM_data, cam->COM_len); 1324 parms->jpeg_markers |= JPEG_MARKER_COM; 1325 } 1326 1327 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n", 1328 parms->APP_len, parms->COM_len); 1329 1330 return 0; 1331} 1332 1333/****************************************************************************** 1334 * 1335 * ioctl_s_jpegcomp 1336 * 1337 * V4L2 set the JPEG compression parameters 1338 * NOTE: quality and some jpeg_markers are ignored. 1339 * 1340 *****************************************************************************/ 1341 1342static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam) 1343{ 1344 struct v4l2_jpegcompression *parms = arg; 1345 1346 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n", 1347 parms->APP_len, parms->COM_len); 1348 1349 cam->params.compression.inhibit_htables = 1350 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT); 1351 1352 if(parms->APP_len != 0) { 1353 if(parms->APP_len > 0 && 1354 parms->APP_len <= sizeof(cam->APP_data) && 1355 parms->APPn >= 0 && parms->APPn <= 15) { 1356 cam->APPn = parms->APPn; 1357 cam->APP_len = parms->APP_len; 1358 memcpy(cam->APP_data, parms->APP_data, parms->APP_len); 1359 } else { 1360 LOG("Bad APPn Params n=%d len=%d\n", 1361 parms->APPn, parms->APP_len); 1362 return -EINVAL; 1363 } 1364 } else { 1365 cam->APP_len = 0; 1366 } 1367 1368 if(parms->COM_len != 0) { 1369 if(parms->COM_len > 0 && 1370 parms->COM_len <= sizeof(cam->COM_data)) { 1371 cam->COM_len = parms->COM_len; 1372 memcpy(cam->COM_data, parms->COM_data, parms->COM_len); 1373 } else { 1374 LOG("Bad COM_len=%d\n", parms->COM_len); 1375 return -EINVAL; 1376 } 1377 } 1378 1379 return 0; 1380} 1381 1382/****************************************************************************** 1383 * 1384 * ioctl_reqbufs 1385 * 1386 * V4L2 Initiate memory mapping. 1387 * NOTE: The user's request is ignored. For now the buffers are fixed. 1388 * 1389 *****************************************************************************/ 1390 1391static int ioctl_reqbufs(void *arg,struct camera_data *cam) 1392{ 1393 struct v4l2_requestbuffers *req = arg; 1394 1395 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1396 req->memory != V4L2_MEMORY_MMAP) 1397 return -EINVAL; 1398 1399 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames); 1400 req->count = cam->num_frames; 1401 memset(&req->reserved, 0, sizeof(req->reserved)); 1402 1403 return 0; 1404} 1405 1406/****************************************************************************** 1407 * 1408 * ioctl_querybuf 1409 * 1410 * V4L2 Query memory buffer status. 1411 * 1412 *****************************************************************************/ 1413 1414static int ioctl_querybuf(void *arg,struct camera_data *cam) 1415{ 1416 struct v4l2_buffer *buf = arg; 1417 1418 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1419 buf->index > cam->num_frames) 1420 return -EINVAL; 1421 1422 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer; 1423 buf->length = cam->frame_size; 1424 1425 buf->memory = V4L2_MEMORY_MMAP; 1426 1427 if(cam->mmapped) 1428 buf->flags = V4L2_BUF_FLAG_MAPPED; 1429 else 1430 buf->flags = 0; 1431 1432 switch (cam->buffers[buf->index].status) { 1433 case FRAME_EMPTY: 1434 case FRAME_ERROR: 1435 case FRAME_READING: 1436 buf->bytesused = 0; 1437 buf->flags = V4L2_BUF_FLAG_QUEUED; 1438 break; 1439 case FRAME_READY: 1440 buf->bytesused = cam->buffers[buf->index].length; 1441 buf->timestamp = cam->buffers[buf->index].timestamp; 1442 buf->sequence = cam->buffers[buf->index].seq; 1443 buf->flags = V4L2_BUF_FLAG_DONE; 1444 break; 1445 } 1446 1447 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n", 1448 buf->index, buf->m.offset, buf->flags, buf->sequence, 1449 buf->bytesused); 1450 1451 return 0; 1452} 1453 1454/****************************************************************************** 1455 * 1456 * ioctl_qbuf 1457 * 1458 * V4L2 User is freeing buffer 1459 * 1460 *****************************************************************************/ 1461 1462static int ioctl_qbuf(void *arg,struct camera_data *cam) 1463{ 1464 struct v4l2_buffer *buf = arg; 1465 1466 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1467 buf->memory != V4L2_MEMORY_MMAP || 1468 buf->index > cam->num_frames) 1469 return -EINVAL; 1470 1471 DBG("QBUF #%d\n", buf->index); 1472 1473 if(cam->buffers[buf->index].status == FRAME_READY) 1474 cam->buffers[buf->index].status = FRAME_EMPTY; 1475 1476 return 0; 1477} 1478 1479/****************************************************************************** 1480 * 1481 * find_earliest_filled_buffer 1482 * 1483 * Helper for ioctl_dqbuf. Find the next ready buffer. 1484 * 1485 *****************************************************************************/ 1486 1487static int find_earliest_filled_buffer(struct camera_data *cam) 1488{ 1489 int i; 1490 int found = -1; 1491 for (i=0; i<cam->num_frames; i++) { 1492 if(cam->buffers[i].status == FRAME_READY) { 1493 if(found < 0) { 1494 found = i; 1495 } else { 1496 /* find which buffer is earlier */ 1497 struct timeval *tv1, *tv2; 1498 tv1 = &cam->buffers[i].timestamp; 1499 tv2 = &cam->buffers[found].timestamp; 1500 if(tv1->tv_sec < tv2->tv_sec || 1501 (tv1->tv_sec == tv2->tv_sec && 1502 tv1->tv_usec < tv2->tv_usec)) 1503 found = i; 1504 } 1505 } 1506 } 1507 return found; 1508} 1509 1510/****************************************************************************** 1511 * 1512 * ioctl_dqbuf 1513 * 1514 * V4L2 User is asking for a filled buffer. 1515 * 1516 *****************************************************************************/ 1517 1518static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file) 1519{ 1520 struct v4l2_buffer *buf = arg; 1521 int frame; 1522 1523 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 1524 buf->memory != V4L2_MEMORY_MMAP) 1525 return -EINVAL; 1526 1527 frame = find_earliest_filled_buffer(cam); 1528 1529 if(frame < 0 && file->f_flags&O_NONBLOCK) 1530 return -EAGAIN; 1531 1532 if(frame < 0) { 1533 /* Wait for a frame to become available */ 1534 struct framebuf *cb=cam->curbuff; 1535 mutex_unlock(&cam->busy_lock); 1536 wait_event_interruptible(cam->wq_stream, 1537 !cam->present || 1538 (cb=cam->curbuff)->status == FRAME_READY); 1539 mutex_lock(&cam->busy_lock); 1540 if (signal_pending(current)) 1541 return -ERESTARTSYS; 1542 if(!cam->present) 1543 return -ENOTTY; 1544 frame = cb->num; 1545 } 1546 1547 1548 buf->index = frame; 1549 buf->bytesused = cam->buffers[buf->index].length; 1550 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE; 1551 buf->field = V4L2_FIELD_NONE; 1552 buf->timestamp = cam->buffers[buf->index].timestamp; 1553 buf->sequence = cam->buffers[buf->index].seq; 1554 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer; 1555 buf->length = cam->frame_size; 1556 buf->input = 0; 1557 buf->reserved = 0; 1558 memset(&buf->timecode, 0, sizeof(buf->timecode)); 1559 1560 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index, 1561 cam->buffers[buf->index].status, buf->sequence, buf->bytesused); 1562 1563 return 0; 1564} 1565 1566/****************************************************************************** 1567 * 1568 * cpia2_ioctl 1569 * 1570 *****************************************************************************/ 1571static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg) 1572{ 1573 struct camera_data *cam = video_drvdata(file); 1574 long retval = 0; 1575 1576 if (!cam) 1577 return -ENOTTY; 1578 1579 /* make this _really_ smp-safe */ 1580 if (mutex_lock_interruptible(&cam->busy_lock)) 1581 return -ERESTARTSYS; 1582 1583 if (!cam->present) { 1584 mutex_unlock(&cam->busy_lock); 1585 return -ENODEV; 1586 } 1587 1588 /* Priority check */ 1589 switch (cmd) { 1590 case VIDIOCSWIN: 1591 case VIDIOCMCAPTURE: 1592 case VIDIOC_S_FMT: 1593 { 1594 struct cpia2_fh *fh = file->private_data; 1595 retval = v4l2_prio_check(&cam->prio, fh->prio); 1596 if(retval) { 1597 mutex_unlock(&cam->busy_lock); 1598 return retval; 1599 } 1600 break; 1601 } 1602 case VIDIOCGMBUF: 1603 case VIDIOCSYNC: 1604 { 1605 struct cpia2_fh *fh = file->private_data; 1606 if(fh->prio != V4L2_PRIORITY_RECORD) { 1607 mutex_unlock(&cam->busy_lock); 1608 return -EBUSY; 1609 } 1610 break; 1611 } 1612 default: 1613 break; 1614 } 1615 1616 switch (cmd) { 1617 case VIDIOCGCAP: /* query capabilities */ 1618 retval = ioctl_cap_query(arg, cam); 1619 break; 1620 1621 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */ 1622 retval = ioctl_get_channel(arg); 1623 break; 1624 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */ 1625 retval = ioctl_set_channel(arg); 1626 break; 1627 case VIDIOCGPICT: /* image properties */ 1628 memcpy(arg, &cam->vp, sizeof(struct video_picture)); 1629 break; 1630 case VIDIOCSPICT: 1631 retval = ioctl_set_image_prop(arg, cam); 1632 break; 1633 case VIDIOCGWIN: /* get/set capture window */ 1634 memcpy(arg, &cam->vw, sizeof(struct video_window)); 1635 break; 1636 case VIDIOCSWIN: 1637 retval = ioctl_set_window_size(arg, cam, file->private_data); 1638 break; 1639 case VIDIOCGMBUF: /* mmap interface */ 1640 retval = ioctl_get_mbuf(arg, cam); 1641 break; 1642 case VIDIOCMCAPTURE: 1643 retval = ioctl_mcapture(arg, cam, file->private_data); 1644 break; 1645 case VIDIOCSYNC: 1646 retval = ioctl_sync(arg, cam); 1647 break; 1648 /* pointless to implement overlay with this camera */ 1649 case VIDIOCCAPTURE: 1650 case VIDIOCGFBUF: 1651 case VIDIOCSFBUF: 1652 case VIDIOCKEY: 1653 retval = -EINVAL; 1654 break; 1655 1656 /* tuner interface - we have none */ 1657 case VIDIOCGTUNER: 1658 case VIDIOCSTUNER: 1659 case VIDIOCGFREQ: 1660 case VIDIOCSFREQ: 1661 retval = -EINVAL; 1662 break; 1663 1664 /* audio interface - we have none */ 1665 case VIDIOCGAUDIO: 1666 case VIDIOCSAUDIO: 1667 retval = -EINVAL; 1668 break; 1669 1670 /* CPIA2 extension to Video4Linux API */ 1671 case CPIA2_IOC_SET_GPIO: 1672 retval = ioctl_set_gpio(arg, cam); 1673 break; 1674 case VIDIOC_QUERYCAP: 1675 retval = ioctl_querycap(arg,cam); 1676 break; 1677 1678 case VIDIOC_ENUMINPUT: 1679 case VIDIOC_G_INPUT: 1680 case VIDIOC_S_INPUT: 1681 retval = ioctl_input(cmd, arg, cam); 1682 break; 1683 1684 case VIDIOC_ENUM_FMT: 1685 retval = ioctl_enum_fmt(arg,cam); 1686 break; 1687 case VIDIOC_TRY_FMT: 1688 retval = ioctl_try_fmt(arg,cam); 1689 break; 1690 case VIDIOC_G_FMT: 1691 retval = ioctl_get_fmt(arg,cam); 1692 break; 1693 case VIDIOC_S_FMT: 1694 retval = ioctl_set_fmt(arg,cam,file->private_data); 1695 break; 1696 1697 case VIDIOC_CROPCAP: 1698 retval = ioctl_cropcap(arg,cam); 1699 break; 1700 case VIDIOC_G_CROP: 1701 case VIDIOC_S_CROP: 1702 // TODO: I think cropping can be implemented - SJB 1703 retval = -EINVAL; 1704 break; 1705 1706 case VIDIOC_QUERYCTRL: 1707 retval = ioctl_queryctrl(arg,cam); 1708 break; 1709 case VIDIOC_QUERYMENU: 1710 retval = ioctl_querymenu(arg,cam); 1711 break; 1712 case VIDIOC_G_CTRL: 1713 retval = ioctl_g_ctrl(arg,cam); 1714 break; 1715 case VIDIOC_S_CTRL: 1716 retval = ioctl_s_ctrl(arg,cam); 1717 break; 1718 1719 case VIDIOC_G_JPEGCOMP: 1720 retval = ioctl_g_jpegcomp(arg,cam); 1721 break; 1722 case VIDIOC_S_JPEGCOMP: 1723 retval = ioctl_s_jpegcomp(arg,cam); 1724 break; 1725 1726 case VIDIOC_G_PRIORITY: 1727 { 1728 struct cpia2_fh *fh = file->private_data; 1729 *(enum v4l2_priority*)arg = fh->prio; 1730 break; 1731 } 1732 case VIDIOC_S_PRIORITY: 1733 { 1734 struct cpia2_fh *fh = file->private_data; 1735 enum v4l2_priority prio; 1736 prio = *(enum v4l2_priority*)arg; 1737 if(cam->streaming && 1738 prio != fh->prio && 1739 fh->prio == V4L2_PRIORITY_RECORD) { 1740 /* Can't drop record priority while streaming */ 1741 retval = -EBUSY; 1742 } else if(prio == V4L2_PRIORITY_RECORD && 1743 prio != fh->prio && 1744 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) { 1745 /* Only one program can record at a time */ 1746 retval = -EBUSY; 1747 } else { 1748 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio); 1749 } 1750 break; 1751 } 1752 1753 case VIDIOC_REQBUFS: 1754 retval = ioctl_reqbufs(arg,cam); 1755 break; 1756 case VIDIOC_QUERYBUF: 1757 retval = ioctl_querybuf(arg,cam); 1758 break; 1759 case VIDIOC_QBUF: 1760 retval = ioctl_qbuf(arg,cam); 1761 break; 1762 case VIDIOC_DQBUF: 1763 retval = ioctl_dqbuf(arg,cam,file); 1764 break; 1765 case VIDIOC_STREAMON: 1766 { 1767 int type; 1768 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming); 1769 type = *(int*)arg; 1770 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1771 retval = -EINVAL; 1772 1773 if(!cam->streaming) { 1774 retval = cpia2_usb_stream_start(cam, 1775 cam->params.camera_state.stream_mode); 1776 } else { 1777 retval = -EINVAL; 1778 } 1779 1780 break; 1781 } 1782 case VIDIOC_STREAMOFF: 1783 { 1784 int type; 1785 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming); 1786 type = *(int*)arg; 1787 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1788 retval = -EINVAL; 1789 1790 if(cam->streaming) { 1791 retval = cpia2_usb_stream_stop(cam); 1792 } else { 1793 retval = -EINVAL; 1794 } 1795 1796 break; 1797 } 1798 1799 case VIDIOC_ENUMOUTPUT: 1800 case VIDIOC_G_OUTPUT: 1801 case VIDIOC_S_OUTPUT: 1802 case VIDIOC_G_MODULATOR: 1803 case VIDIOC_S_MODULATOR: 1804 1805 case VIDIOC_ENUMAUDIO: 1806 case VIDIOC_G_AUDIO: 1807 case VIDIOC_S_AUDIO: 1808 1809 case VIDIOC_ENUMAUDOUT: 1810 case VIDIOC_G_AUDOUT: 1811 case VIDIOC_S_AUDOUT: 1812 1813 case VIDIOC_ENUMSTD: 1814 case VIDIOC_QUERYSTD: 1815 case VIDIOC_G_STD: 1816 case VIDIOC_S_STD: 1817 1818 case VIDIOC_G_TUNER: 1819 case VIDIOC_S_TUNER: 1820 case VIDIOC_G_FREQUENCY: 1821 case VIDIOC_S_FREQUENCY: 1822 1823 case VIDIOC_OVERLAY: 1824 case VIDIOC_G_FBUF: 1825 case VIDIOC_S_FBUF: 1826 1827 case VIDIOC_G_PARM: 1828 case VIDIOC_S_PARM: 1829 retval = -EINVAL; 1830 break; 1831 default: 1832 retval = -ENOIOCTLCMD; 1833 break; 1834 } 1835 1836 mutex_unlock(&cam->busy_lock); 1837 return retval; 1838} 1839 1840static long cpia2_ioctl(struct file *file, 1841 unsigned int cmd, unsigned long arg) 1842{ 1843 return video_usercopy(file, cmd, arg, cpia2_do_ioctl); 1844} 1845 1846/****************************************************************************** 1847 * 1848 * cpia2_mmap 1849 * 1850 *****************************************************************************/ 1851static int cpia2_mmap(struct file *file, struct vm_area_struct *area) 1852{ 1853 struct camera_data *cam = video_drvdata(file); 1854 int retval; 1855 1856 /* Priority check */ 1857 struct cpia2_fh *fh = file->private_data; 1858 if(fh->prio != V4L2_PRIORITY_RECORD) { 1859 return -EBUSY; 1860 } 1861 1862 retval = cpia2_remap_buffer(cam, area); 1863 1864 if(!retval) 1865 fh->mmapped = 1; 1866 return retval; 1867} 1868 1869/****************************************************************************** 1870 * 1871 * reset_camera_struct_v4l 1872 * 1873 * Sets all values to the defaults 1874 *****************************************************************************/ 1875static void reset_camera_struct_v4l(struct camera_data *cam) 1876{ 1877 /*** 1878 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP 1879 * Ioctl. Here, just do the window and picture stucts. 1880 ***/ 1881 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */ 1882 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256; 1883 cam->vp.colour = (u16) cam->params.color_params.saturation * 256; 1884 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256; 1885 1886 cam->vw.x = 0; 1887 cam->vw.y = 0; 1888 cam->vw.width = cam->params.roi.width; 1889 cam->vw.height = cam->params.roi.height; 1890 cam->vw.flags = 0; 1891 cam->vw.clipcount = 0; 1892 1893 cam->frame_size = buffer_size; 1894 cam->num_frames = num_buffers; 1895 1896 /* FlickerModes */ 1897 cam->params.flicker_control.flicker_mode_req = flicker_mode; 1898 cam->params.flicker_control.mains_frequency = flicker_freq; 1899 1900 /* streamMode */ 1901 cam->params.camera_state.stream_mode = alternate; 1902 1903 cam->pixelformat = V4L2_PIX_FMT_JPEG; 1904 v4l2_prio_init(&cam->prio); 1905 return; 1906} 1907 1908/*** 1909 * The v4l video device structure initialized for this device 1910 ***/ 1911static const struct v4l2_file_operations fops_template = { 1912 .owner = THIS_MODULE, 1913 .open = cpia2_open, 1914 .release = cpia2_close, 1915 .read = cpia2_v4l_read, 1916 .poll = cpia2_v4l_poll, 1917 .ioctl = cpia2_ioctl, 1918 .mmap = cpia2_mmap, 1919}; 1920 1921static struct video_device cpia2_template = { 1922 /* I could not find any place for the old .initialize initializer?? */ 1923 .name= "CPiA2 Camera", 1924 .fops= &fops_template, 1925 .release= video_device_release, 1926}; 1927 1928/****************************************************************************** 1929 * 1930 * cpia2_register_camera 1931 * 1932 *****************************************************************************/ 1933int cpia2_register_camera(struct camera_data *cam) 1934{ 1935 cam->vdev = video_device_alloc(); 1936 if(!cam->vdev) 1937 return -ENOMEM; 1938 1939 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template)); 1940 video_set_drvdata(cam->vdev, cam); 1941 1942 reset_camera_struct_v4l(cam); 1943 1944 /* register v4l device */ 1945 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) { 1946 ERR("video_register_device failed\n"); 1947 video_device_release(cam->vdev); 1948 return -ENODEV; 1949 } 1950 1951 return 0; 1952} 1953 1954/****************************************************************************** 1955 * 1956 * cpia2_unregister_camera 1957 * 1958 *****************************************************************************/ 1959void cpia2_unregister_camera(struct camera_data *cam) 1960{ 1961 if (!cam->open_count) { 1962 video_unregister_device(cam->vdev); 1963 } else { 1964 LOG("%s removed while open, deferring " 1965 "video_unregister_device\n", 1966 video_device_node_name(cam->vdev)); 1967 } 1968} 1969 1970/****************************************************************************** 1971 * 1972 * check_parameters 1973 * 1974 * Make sure that all user-supplied parameters are sensible 1975 *****************************************************************************/ 1976static void __init check_parameters(void) 1977{ 1978 if(buffer_size < PAGE_SIZE) { 1979 buffer_size = PAGE_SIZE; 1980 LOG("buffer_size too small, setting to %d\n", buffer_size); 1981 } else if(buffer_size > 1024*1024) { 1982 /* arbitrary upper limiit */ 1983 buffer_size = 1024*1024; 1984 LOG("buffer_size ridiculously large, setting to %d\n", 1985 buffer_size); 1986 } else { 1987 buffer_size += PAGE_SIZE-1; 1988 buffer_size &= ~(PAGE_SIZE-1); 1989 } 1990 1991 if(num_buffers < 1) { 1992 num_buffers = 1; 1993 LOG("num_buffers too small, setting to %d\n", num_buffers); 1994 } else if(num_buffers > VIDEO_MAX_FRAME) { 1995 num_buffers = VIDEO_MAX_FRAME; 1996 LOG("num_buffers too large, setting to %d\n", num_buffers); 1997 } 1998 1999 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) { 2000 alternate = DEFAULT_ALT; 2001 LOG("alternate specified is invalid, using %d\n", alternate); 2002 } 2003 2004 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) { 2005 flicker_mode = NEVER_FLICKER; 2006 LOG("Flicker mode specified is invalid, using %d\n", 2007 flicker_mode); 2008 } 2009 2010 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) { 2011 flicker_freq = FLICKER_60; 2012 LOG("Flicker mode specified is invalid, using %d\n", 2013 flicker_freq); 2014 } 2015 2016 if(video_nr < -1 || video_nr > 64) { 2017 video_nr = -1; 2018 LOG("invalid video_nr specified, must be -1 to 64\n"); 2019 } 2020 2021 DBG("Using %d buffers, each %d bytes, alternate=%d\n", 2022 num_buffers, buffer_size, alternate); 2023} 2024 2025/************ Module Stuff ***************/ 2026 2027 2028/****************************************************************************** 2029 * 2030 * cpia2_init/module_init 2031 * 2032 *****************************************************************************/ 2033static int __init cpia2_init(void) 2034{ 2035 LOG("%s v%d.%d.%d\n", 2036 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER); 2037 check_parameters(); 2038 cpia2_usb_init(); 2039 return 0; 2040} 2041 2042 2043/****************************************************************************** 2044 * 2045 * cpia2_exit/module_exit 2046 * 2047 *****************************************************************************/ 2048static void __exit cpia2_exit(void) 2049{ 2050 cpia2_usb_cleanup(); 2051 schedule_timeout(2 * HZ); 2052} 2053 2054module_init(cpia2_init); 2055module_exit(cpia2_exit); 2056