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