Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / drivers / media / video / cpia2 / cpia2_v4l.c
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/videodev2.h>
41 #include <linux/stringify.h>
42 #include <media/v4l2-ioctl.h>
43
44 #include "cpia2.h"
45 #include "cpia2dev.h"
46
47 static int video_nr = -1;
48 module_param(video_nr, int, 0);
49 MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
50
51 static int buffer_size = 68*1024;
52 module_param(buffer_size, int, 0);
53 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
54
55 static int num_buffers = 3;
56 module_param(num_buffers, int, 0);
57 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
58                  __stringify(VIDEO_MAX_FRAME) ", default 3)");
59
60 static int alternate = DEFAULT_ALT;
61 module_param(alternate, int, 0);
62 MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
63                  __stringify(USBIF_ISO_6) ", default "
64                  __stringify(DEFAULT_ALT) ")");
65
66 static int flicker_freq = 60;
67 module_param(flicker_freq, int, 0);
68 MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" __stringify(50) "or"
69                  __stringify(60) ", default "
70                  __stringify(60) ")");
71
72 static int flicker_mode = NEVER_FLICKER;
73 module_param(flicker_mode, int, 0);
74 MODULE_PARM_DESC(flicker_mode,
75                  "Flicker supression (" __stringify(NEVER_FLICKER) "or"
76                  __stringify(ANTI_FLICKER_ON) ", default "
77                  __stringify(NEVER_FLICKER) ")");
78
79 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
80 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
81 MODULE_SUPPORTED_DEVICE("video");
82 MODULE_LICENSE("GPL");
83
84 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
85
86 struct control_menu_info {
87         int value;
88         char name[32];
89 };
90
91 static 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
102 static 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
110 static 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
120 static 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  *****************************************************************************/
238 static 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
274 skip_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
291 err_return:
292         mutex_unlock(&cam->busy_lock);
293         return retval;
294 }
295
296 /******************************************************************************
297  *
298  *  cpia2_close
299  *
300  *****************************************************************************/
301 static 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  *****************************************************************************/
352 static 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  *****************************************************************************/
377 static 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 static int sync(struct camera_data *cam, int frame_nr)
395 {
396         struct framebuf *frame = &cam->buffers[frame_nr];
397
398         while (1) {
399                 if (frame->status == FRAME_READY)
400                         return 0;
401
402                 if (!cam->streaming) {
403                         frame->status = FRAME_READY;
404                         frame->length = 0;
405                         return 0;
406                 }
407
408                 mutex_unlock(&cam->busy_lock);
409                 wait_event_interruptible(cam->wq_stream,
410                                          !cam->streaming ||
411                                          frame->status == FRAME_READY);
412                 mutex_lock(&cam->busy_lock);
413                 if (signal_pending(current))
414                         return -ERESTARTSYS;
415                 if(!cam->present)
416                         return -ENOTTY;
417         }
418 }
419
420 /******************************************************************************
421  *
422  *  ioctl_get_mbuf
423  *
424  *****************************************************************************/
425 #ifdef CONFIG_VIDEO_V4L1_COMPAT
426 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
427 {
428         struct video_mbuf *vm;
429         int i;
430         vm = arg;
431
432         memset(vm, 0, sizeof(*vm));
433         vm->size = cam->frame_size*cam->num_frames;
434         vm->frames = cam->num_frames;
435         for (i = 0; i < cam->num_frames; i++)
436                 vm->offsets[i] = cam->frame_size * i;
437
438         return 0;
439 }
440 #endif
441
442 /******************************************************************************
443  *
444  *  ioctl_set_gpio
445  *
446  *****************************************************************************/
447
448 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
449 {
450         __u32 gpio_val;
451
452         gpio_val = *(__u32*) arg;
453
454         if (gpio_val &~ 0xFFU)
455                 return -EINVAL;
456
457         return cpia2_set_gpio(cam, (unsigned char)gpio_val);
458 }
459
460 /******************************************************************************
461  *
462  *  ioctl_querycap
463  *
464  *  V4L2 device capabilities
465  *
466  *****************************************************************************/
467
468 static int ioctl_querycap(void *arg, struct camera_data *cam)
469 {
470         struct v4l2_capability *vc = arg;
471
472         memset(vc, 0, sizeof(*vc));
473         strcpy(vc->driver, "cpia2");
474
475         if (cam->params.pnp_id.product == 0x151)
476                 strcpy(vc->card, "QX5 Microscope");
477         else
478                 strcpy(vc->card, "CPiA2 Camera");
479         switch (cam->params.pnp_id.device_type) {
480         case DEVICE_STV_672:
481                 strcat(vc->card, " (672/");
482                 break;
483         case DEVICE_STV_676:
484                 strcat(vc->card, " (676/");
485                 break;
486         default:
487                 strcat(vc->card, " (???/");
488                 break;
489         }
490         switch (cam->params.version.sensor_flags) {
491         case CPIA2_VP_SENSOR_FLAGS_404:
492                 strcat(vc->card, "404)");
493                 break;
494         case CPIA2_VP_SENSOR_FLAGS_407:
495                 strcat(vc->card, "407)");
496                 break;
497         case CPIA2_VP_SENSOR_FLAGS_409:
498                 strcat(vc->card, "409)");
499                 break;
500         case CPIA2_VP_SENSOR_FLAGS_410:
501                 strcat(vc->card, "410)");
502                 break;
503         case CPIA2_VP_SENSOR_FLAGS_500:
504                 strcat(vc->card, "500)");
505                 break;
506         default:
507                 strcat(vc->card, "???)");
508                 break;
509         }
510
511         if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
512                 memset(vc->bus_info,0, sizeof(vc->bus_info));
513
514         vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
515                                      CPIA2_PATCH_VER);
516
517         vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
518                            V4L2_CAP_READWRITE |
519                            V4L2_CAP_STREAMING;
520
521         return 0;
522 }
523
524 /******************************************************************************
525  *
526  *  ioctl_input
527  *
528  *  V4L2 input get/set/enumerate
529  *
530  *****************************************************************************/
531
532 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
533 {
534         struct v4l2_input *i = arg;
535
536         if(ioclt_nr  != VIDIOC_G_INPUT) {
537                 if (i->index != 0)
538                        return -EINVAL;
539         }
540
541         memset(i, 0, sizeof(*i));
542         strcpy(i->name, "Camera");
543         i->type = V4L2_INPUT_TYPE_CAMERA;
544
545         return 0;
546 }
547
548 /******************************************************************************
549  *
550  *  ioctl_enum_fmt
551  *
552  *  V4L2 format enumerate
553  *
554  *****************************************************************************/
555
556 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
557 {
558         struct v4l2_fmtdesc *f = arg;
559         int index = f->index;
560
561         if (index < 0 || index > 1)
562                return -EINVAL;
563
564         memset(f, 0, sizeof(*f));
565         f->index = index;
566         f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
567         f->flags = V4L2_FMT_FLAG_COMPRESSED;
568         switch(index) {
569         case 0:
570                 strcpy(f->description, "MJPEG");
571                 f->pixelformat = V4L2_PIX_FMT_MJPEG;
572                 break;
573         case 1:
574                 strcpy(f->description, "JPEG");
575                 f->pixelformat = V4L2_PIX_FMT_JPEG;
576                 break;
577         default:
578                 return -EINVAL;
579         }
580
581         return 0;
582 }
583
584 /******************************************************************************
585  *
586  *  ioctl_try_fmt
587  *
588  *  V4L2 format try
589  *
590  *****************************************************************************/
591
592 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
593 {
594         struct v4l2_format *f = arg;
595
596         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
597                return -EINVAL;
598
599         if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
600             f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
601                return -EINVAL;
602
603         f->fmt.pix.field = V4L2_FIELD_NONE;
604         f->fmt.pix.bytesperline = 0;
605         f->fmt.pix.sizeimage = cam->frame_size;
606         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
607         f->fmt.pix.priv = 0;
608
609         switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
610         case VIDEOSIZE_VGA:
611                 f->fmt.pix.width = 640;
612                 f->fmt.pix.height = 480;
613                 break;
614         case VIDEOSIZE_CIF:
615                 f->fmt.pix.width = 352;
616                 f->fmt.pix.height = 288;
617                 break;
618         case VIDEOSIZE_QVGA:
619                 f->fmt.pix.width = 320;
620                 f->fmt.pix.height = 240;
621                 break;
622         case VIDEOSIZE_288_216:
623                 f->fmt.pix.width = 288;
624                 f->fmt.pix.height = 216;
625                 break;
626         case VIDEOSIZE_256_192:
627                 f->fmt.pix.width = 256;
628                 f->fmt.pix.height = 192;
629                 break;
630         case VIDEOSIZE_224_168:
631                 f->fmt.pix.width = 224;
632                 f->fmt.pix.height = 168;
633                 break;
634         case VIDEOSIZE_192_144:
635                 f->fmt.pix.width = 192;
636                 f->fmt.pix.height = 144;
637                 break;
638         case VIDEOSIZE_QCIF:
639         default:
640                 f->fmt.pix.width = 176;
641                 f->fmt.pix.height = 144;
642                 break;
643         }
644
645         return 0;
646 }
647
648 /******************************************************************************
649  *
650  *  ioctl_set_fmt
651  *
652  *  V4L2 format set
653  *
654  *****************************************************************************/
655
656 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
657 {
658         struct v4l2_format *f = arg;
659         int err, frame;
660
661         err = ioctl_try_fmt(arg, cam);
662         if(err != 0)
663                 return err;
664
665         /* Ensure that only this process can change the format. */
666         err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
667         if(err != 0) {
668                 return err;
669         }
670
671         cam->pixelformat = f->fmt.pix.pixelformat;
672
673         /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
674          * the missing Huffman table properly. */
675         cam->params.compression.inhibit_htables = 0;
676                 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
677
678         /* we set the video window to something smaller or equal to what
679          * is requested by the user???
680          */
681         DBG("Requested width = %d, height = %d\n",
682             f->fmt.pix.width, f->fmt.pix.height);
683         if (f->fmt.pix.width != cam->width ||
684             f->fmt.pix.height != cam->height) {
685                 cam->width = f->fmt.pix.width;
686                 cam->height = f->fmt.pix.height;
687                 cam->params.roi.width = f->fmt.pix.width;
688                 cam->params.roi.height = f->fmt.pix.height;
689                 cpia2_set_format(cam);
690         }
691
692         for (frame = 0; frame < cam->num_frames; ++frame) {
693                 if (cam->buffers[frame].status == FRAME_READING)
694                         if ((err = sync(cam, frame)) < 0)
695                                 return err;
696
697                 cam->buffers[frame].status = FRAME_EMPTY;
698         }
699
700         return 0;
701 }
702
703 /******************************************************************************
704  *
705  *  ioctl_get_fmt
706  *
707  *  V4L2 format get
708  *
709  *****************************************************************************/
710
711 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
712 {
713         struct v4l2_format *f = arg;
714
715         if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
716                return -EINVAL;
717
718         f->fmt.pix.width = cam->width;
719         f->fmt.pix.height = cam->height;
720         f->fmt.pix.pixelformat = cam->pixelformat;
721         f->fmt.pix.field = V4L2_FIELD_NONE;
722         f->fmt.pix.bytesperline = 0;
723         f->fmt.pix.sizeimage = cam->frame_size;
724         f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
725         f->fmt.pix.priv = 0;
726
727         return 0;
728 }
729
730 /******************************************************************************
731  *
732  *  ioctl_cropcap
733  *
734  *  V4L2 query cropping capabilities
735  *  NOTE: cropping is currently disabled
736  *
737  *****************************************************************************/
738
739 static int ioctl_cropcap(void *arg,struct camera_data *cam)
740 {
741         struct v4l2_cropcap *c = arg;
742
743         if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
744                return -EINVAL;
745
746         c->bounds.left = 0;
747         c->bounds.top = 0;
748         c->bounds.width = cam->width;
749         c->bounds.height = cam->height;
750         c->defrect.left = 0;
751         c->defrect.top = 0;
752         c->defrect.width = cam->width;
753         c->defrect.height = cam->height;
754         c->pixelaspect.numerator = 1;
755         c->pixelaspect.denominator = 1;
756
757         return 0;
758 }
759
760 /******************************************************************************
761  *
762  *  ioctl_queryctrl
763  *
764  *  V4L2 query possible control variables
765  *
766  *****************************************************************************/
767
768 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
769 {
770         struct v4l2_queryctrl *c = arg;
771         int i;
772
773         for(i=0; i<NUM_CONTROLS; ++i) {
774                 if(c->id == controls[i].id) {
775                         memcpy(c, controls+i, sizeof(*c));
776                         break;
777                 }
778         }
779
780         if(i == NUM_CONTROLS)
781                 return -EINVAL;
782
783         /* Some devices have additional limitations */
784         switch(c->id) {
785         case V4L2_CID_BRIGHTNESS:
786                 /***
787                  * Don't let the register be set to zero - bug in VP4
788                  * flash of full brightness
789                  ***/
790                 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
791                         c->minimum = 1;
792                 break;
793         case V4L2_CID_VFLIP:
794                 // VP5 Only
795                 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
796                         c->flags |= V4L2_CTRL_FLAG_DISABLED;
797                 break;
798         case CPIA2_CID_FRAMERATE:
799                 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
800                    cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
801                         // Maximum 15fps
802                         for(i=0; i<c->maximum; ++i) {
803                                 if(framerate_controls[i].value ==
804                                    CPIA2_VP_FRAMERATE_15) {
805                                         c->maximum = i;
806                                         c->default_value = i;
807                                 }
808                         }
809                 }
810                 break;
811         case CPIA2_CID_FLICKER_MODE:
812                 // Flicker control only valid for 672.
813                 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
814                         c->flags |= V4L2_CTRL_FLAG_DISABLED;
815                 break;
816         case CPIA2_CID_LIGHTS:
817                 // Light control only valid for the QX5 Microscope.
818                 if(cam->params.pnp_id.product != 0x151)
819                         c->flags |= V4L2_CTRL_FLAG_DISABLED;
820                 break;
821         default:
822                 break;
823         }
824
825         return 0;
826 }
827
828 /******************************************************************************
829  *
830  *  ioctl_querymenu
831  *
832  *  V4L2 query possible control variables
833  *
834  *****************************************************************************/
835
836 static int ioctl_querymenu(void *arg,struct camera_data *cam)
837 {
838         struct v4l2_querymenu *m = arg;
839
840         memset(m->name, 0, sizeof(m->name));
841         m->reserved = 0;
842
843         switch(m->id) {
844         case CPIA2_CID_FLICKER_MODE:
845                 if (m->index >= NUM_FLICKER_CONTROLS)
846                         return -EINVAL;
847
848                 strcpy(m->name, flicker_controls[m->index].name);
849                 break;
850         case CPIA2_CID_FRAMERATE:
851             {
852                 int maximum = NUM_FRAMERATE_CONTROLS - 1;
853                 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
854                    cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
855                         // Maximum 15fps
856                         int i;
857                         for(i=0; i<maximum; ++i) {
858                                 if(framerate_controls[i].value ==
859                                    CPIA2_VP_FRAMERATE_15)
860                                         maximum = i;
861                         }
862                 }
863                 if (m->index > maximum)
864                         return -EINVAL;
865
866                 strcpy(m->name, framerate_controls[m->index].name);
867                 break;
868             }
869         case CPIA2_CID_LIGHTS:
870                 if (m->index >= NUM_LIGHTS_CONTROLS)
871                         return -EINVAL;
872
873                 strcpy(m->name, lights_controls[m->index].name);
874                 break;
875         default:
876                 return -EINVAL;
877         }
878
879         return 0;
880 }
881
882 /******************************************************************************
883  *
884  *  ioctl_g_ctrl
885  *
886  *  V4L2 get the value of a control variable
887  *
888  *****************************************************************************/
889
890 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
891 {
892         struct v4l2_control *c = arg;
893
894         switch(c->id) {
895         case V4L2_CID_BRIGHTNESS:
896                 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
897                                  TRANSFER_READ, 0);
898                 c->value = cam->params.color_params.brightness;
899                 break;
900         case V4L2_CID_CONTRAST:
901                 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
902                                  TRANSFER_READ, 0);
903                 c->value = cam->params.color_params.contrast;
904                 break;
905         case V4L2_CID_SATURATION:
906                 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
907                                  TRANSFER_READ, 0);
908                 c->value = cam->params.color_params.saturation;
909                 break;
910         case V4L2_CID_HFLIP:
911                 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
912                                  TRANSFER_READ, 0);
913                 c->value = (cam->params.vp_params.user_effects &
914                             CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
915                 break;
916         case V4L2_CID_VFLIP:
917                 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
918                                  TRANSFER_READ, 0);
919                 c->value = (cam->params.vp_params.user_effects &
920                             CPIA2_VP_USER_EFFECTS_FLIP) != 0;
921                 break;
922         case CPIA2_CID_TARGET_KB:
923                 c->value = cam->params.vc_params.target_kb;
924                 break;
925         case CPIA2_CID_GPIO:
926                 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
927                                  TRANSFER_READ, 0);
928                 c->value = cam->params.vp_params.gpio_data;
929                 break;
930         case CPIA2_CID_FLICKER_MODE:
931         {
932                 int i, mode;
933                 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
934                                  TRANSFER_READ, 0);
935                 if(cam->params.flicker_control.cam_register &
936                    CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
937                         mode = NEVER_FLICKER;
938                 } else {
939                     if(cam->params.flicker_control.cam_register &
940                        CPIA2_VP_FLICKER_MODES_50HZ) {
941                         mode = FLICKER_50;
942                     } else {
943                         mode = FLICKER_60;
944                     }
945                 }
946                 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
947                         if(flicker_controls[i].value == mode) {
948                                 c->value = i;
949                                 break;
950                         }
951                 }
952                 if(i == NUM_FLICKER_CONTROLS)
953                         return -EINVAL;
954                 break;
955         }
956         case CPIA2_CID_FRAMERATE:
957         {
958                 int maximum = NUM_FRAMERATE_CONTROLS - 1;
959                 int i;
960                 for(i=0; i<= maximum; i++) {
961                         if(cam->params.vp_params.frame_rate ==
962                            framerate_controls[i].value)
963                                 break;
964                 }
965                 if(i > maximum)
966                         return -EINVAL;
967                 c->value = i;
968                 break;
969         }
970         case CPIA2_CID_USB_ALT:
971                 c->value = cam->params.camera_state.stream_mode;
972                 break;
973         case CPIA2_CID_LIGHTS:
974         {
975                 int i;
976                 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
977                                  TRANSFER_READ, 0);
978                 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
979                         if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
980                            lights_controls[i].value) {
981                                 break;
982                         }
983                 }
984                 if(i == NUM_LIGHTS_CONTROLS)
985                         return -EINVAL;
986                 c->value = i;
987                 break;
988         }
989         case CPIA2_CID_RESET_CAMERA:
990                 return -EINVAL;
991         default:
992                 return -EINVAL;
993         }
994
995         DBG("Get control id:%d, value:%d\n", c->id, c->value);
996
997         return 0;
998 }
999
1000 /******************************************************************************
1001  *
1002  *  ioctl_s_ctrl
1003  *
1004  *  V4L2 set the value of a control variable
1005  *
1006  *****************************************************************************/
1007
1008 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1009 {
1010         struct v4l2_control *c = arg;
1011         int i;
1012         int retval = 0;
1013
1014         DBG("Set control id:%d, value:%d\n", c->id, c->value);
1015
1016         /* Check that the value is in range */
1017         for(i=0; i<NUM_CONTROLS; i++) {
1018                 if(c->id == controls[i].id) {
1019                         if(c->value < controls[i].minimum ||
1020                            c->value > controls[i].maximum) {
1021                                 return -EINVAL;
1022                         }
1023                         break;
1024                 }
1025         }
1026         if(i == NUM_CONTROLS)
1027                 return -EINVAL;
1028
1029         switch(c->id) {
1030         case V4L2_CID_BRIGHTNESS:
1031                 cpia2_set_brightness(cam, c->value);
1032                 break;
1033         case V4L2_CID_CONTRAST:
1034                 cpia2_set_contrast(cam, c->value);
1035                 break;
1036         case V4L2_CID_SATURATION:
1037                 cpia2_set_saturation(cam, c->value);
1038                 break;
1039         case V4L2_CID_HFLIP:
1040                 cpia2_set_property_mirror(cam, c->value);
1041                 break;
1042         case V4L2_CID_VFLIP:
1043                 cpia2_set_property_flip(cam, c->value);
1044                 break;
1045         case CPIA2_CID_TARGET_KB:
1046                 retval = cpia2_set_target_kb(cam, c->value);
1047                 break;
1048         case CPIA2_CID_GPIO:
1049                 retval = cpia2_set_gpio(cam, c->value);
1050                 break;
1051         case CPIA2_CID_FLICKER_MODE:
1052                 retval = cpia2_set_flicker_mode(cam,
1053                                               flicker_controls[c->value].value);
1054                 break;
1055         case CPIA2_CID_FRAMERATE:
1056                 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1057                 break;
1058         case CPIA2_CID_USB_ALT:
1059                 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1060                 break;
1061         case CPIA2_CID_LIGHTS:
1062                 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1063                 break;
1064         case CPIA2_CID_RESET_CAMERA:
1065                 cpia2_usb_stream_pause(cam);
1066                 cpia2_reset_camera(cam);
1067                 cpia2_usb_stream_resume(cam);
1068                 break;
1069         default:
1070                 retval = -EINVAL;
1071         }
1072
1073         return retval;
1074 }
1075
1076 /******************************************************************************
1077  *
1078  *  ioctl_g_jpegcomp
1079  *
1080  *  V4L2 get the JPEG compression parameters
1081  *
1082  *****************************************************************************/
1083
1084 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1085 {
1086         struct v4l2_jpegcompression *parms = arg;
1087
1088         memset(parms, 0, sizeof(*parms));
1089
1090         parms->quality = 80; // TODO: Can this be made meaningful?
1091
1092         parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1093         if(!cam->params.compression.inhibit_htables) {
1094                 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1095         }
1096
1097         parms->APPn = cam->APPn;
1098         parms->APP_len = cam->APP_len;
1099         if(cam->APP_len > 0) {
1100                 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1101                 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1102         }
1103
1104         parms->COM_len = cam->COM_len;
1105         if(cam->COM_len > 0) {
1106                 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1107                 parms->jpeg_markers |= JPEG_MARKER_COM;
1108         }
1109
1110         DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1111             parms->APP_len, parms->COM_len);
1112
1113         return 0;
1114 }
1115
1116 /******************************************************************************
1117  *
1118  *  ioctl_s_jpegcomp
1119  *
1120  *  V4L2 set the JPEG compression parameters
1121  *  NOTE: quality and some jpeg_markers are ignored.
1122  *
1123  *****************************************************************************/
1124
1125 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1126 {
1127         struct v4l2_jpegcompression *parms = arg;
1128
1129         DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1130             parms->APP_len, parms->COM_len);
1131
1132         cam->params.compression.inhibit_htables =
1133                 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1134
1135         if(parms->APP_len != 0) {
1136                 if(parms->APP_len > 0 &&
1137                    parms->APP_len <= sizeof(cam->APP_data) &&
1138                    parms->APPn >= 0 && parms->APPn <= 15) {
1139                         cam->APPn = parms->APPn;
1140                         cam->APP_len = parms->APP_len;
1141                         memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1142                 } else {
1143                         LOG("Bad APPn Params n=%d len=%d\n",
1144                             parms->APPn, parms->APP_len);
1145                         return -EINVAL;
1146                 }
1147         } else {
1148                 cam->APP_len = 0;
1149         }
1150
1151         if(parms->COM_len != 0) {
1152                 if(parms->COM_len > 0 &&
1153                    parms->COM_len <= sizeof(cam->COM_data)) {
1154                         cam->COM_len = parms->COM_len;
1155                         memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1156                 } else {
1157                         LOG("Bad COM_len=%d\n", parms->COM_len);
1158                         return -EINVAL;
1159                 }
1160         }
1161
1162         return 0;
1163 }
1164
1165 /******************************************************************************
1166  *
1167  *  ioctl_reqbufs
1168  *
1169  *  V4L2 Initiate memory mapping.
1170  *  NOTE: The user's request is ignored. For now the buffers are fixed.
1171  *
1172  *****************************************************************************/
1173
1174 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1175 {
1176         struct v4l2_requestbuffers *req = arg;
1177
1178         if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1179            req->memory != V4L2_MEMORY_MMAP)
1180                 return -EINVAL;
1181
1182         DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1183         req->count = cam->num_frames;
1184         memset(&req->reserved, 0, sizeof(req->reserved));
1185
1186         return 0;
1187 }
1188
1189 /******************************************************************************
1190  *
1191  *  ioctl_querybuf
1192  *
1193  *  V4L2 Query memory buffer status.
1194  *
1195  *****************************************************************************/
1196
1197 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1198 {
1199         struct v4l2_buffer *buf = arg;
1200
1201         if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1202            buf->index > cam->num_frames)
1203                 return -EINVAL;
1204
1205         buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1206         buf->length = cam->frame_size;
1207
1208         buf->memory = V4L2_MEMORY_MMAP;
1209
1210         if(cam->mmapped)
1211                 buf->flags = V4L2_BUF_FLAG_MAPPED;
1212         else
1213                 buf->flags = 0;
1214
1215         switch (cam->buffers[buf->index].status) {
1216         case FRAME_EMPTY:
1217         case FRAME_ERROR:
1218         case FRAME_READING:
1219                 buf->bytesused = 0;
1220                 buf->flags = V4L2_BUF_FLAG_QUEUED;
1221                 break;
1222         case FRAME_READY:
1223                 buf->bytesused = cam->buffers[buf->index].length;
1224                 buf->timestamp = cam->buffers[buf->index].timestamp;
1225                 buf->sequence = cam->buffers[buf->index].seq;
1226                 buf->flags = V4L2_BUF_FLAG_DONE;
1227                 break;
1228         }
1229
1230         DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1231              buf->index, buf->m.offset, buf->flags, buf->sequence,
1232              buf->bytesused);
1233
1234         return 0;
1235 }
1236
1237 /******************************************************************************
1238  *
1239  *  ioctl_qbuf
1240  *
1241  *  V4L2 User is freeing buffer
1242  *
1243  *****************************************************************************/
1244
1245 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1246 {
1247         struct v4l2_buffer *buf = arg;
1248
1249         if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1250            buf->memory != V4L2_MEMORY_MMAP ||
1251            buf->index > cam->num_frames)
1252                 return -EINVAL;
1253
1254         DBG("QBUF #%d\n", buf->index);
1255
1256         if(cam->buffers[buf->index].status == FRAME_READY)
1257                 cam->buffers[buf->index].status = FRAME_EMPTY;
1258
1259         return 0;
1260 }
1261
1262 /******************************************************************************
1263  *
1264  *  find_earliest_filled_buffer
1265  *
1266  *  Helper for ioctl_dqbuf. Find the next ready buffer.
1267  *
1268  *****************************************************************************/
1269
1270 static int find_earliest_filled_buffer(struct camera_data *cam)
1271 {
1272         int i;
1273         int found = -1;
1274         for (i=0; i<cam->num_frames; i++) {
1275                 if(cam->buffers[i].status == FRAME_READY) {
1276                         if(found < 0) {
1277                                 found = i;
1278                         } else {
1279                                 /* find which buffer is earlier */
1280                                 struct timeval *tv1, *tv2;
1281                                 tv1 = &cam->buffers[i].timestamp;
1282                                 tv2 = &cam->buffers[found].timestamp;
1283                                 if(tv1->tv_sec < tv2->tv_sec ||
1284                                    (tv1->tv_sec == tv2->tv_sec &&
1285                                     tv1->tv_usec < tv2->tv_usec))
1286                                         found = i;
1287                         }
1288                 }
1289         }
1290         return found;
1291 }
1292
1293 /******************************************************************************
1294  *
1295  *  ioctl_dqbuf
1296  *
1297  *  V4L2 User is asking for a filled buffer.
1298  *
1299  *****************************************************************************/
1300
1301 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1302 {
1303         struct v4l2_buffer *buf = arg;
1304         int frame;
1305
1306         if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1307            buf->memory != V4L2_MEMORY_MMAP)
1308                 return -EINVAL;
1309
1310         frame = find_earliest_filled_buffer(cam);
1311
1312         if(frame < 0 && file->f_flags&O_NONBLOCK)
1313                 return -EAGAIN;
1314
1315         if(frame < 0) {
1316                 /* Wait for a frame to become available */
1317                 struct framebuf *cb=cam->curbuff;
1318                 mutex_unlock(&cam->busy_lock);
1319                 wait_event_interruptible(cam->wq_stream,
1320                                          !cam->present ||
1321                                          (cb=cam->curbuff)->status == FRAME_READY);
1322                 mutex_lock(&cam->busy_lock);
1323                 if (signal_pending(current))
1324                         return -ERESTARTSYS;
1325                 if(!cam->present)
1326                         return -ENOTTY;
1327                 frame = cb->num;
1328         }
1329
1330
1331         buf->index = frame;
1332         buf->bytesused = cam->buffers[buf->index].length;
1333         buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1334         buf->field = V4L2_FIELD_NONE;
1335         buf->timestamp = cam->buffers[buf->index].timestamp;
1336         buf->sequence = cam->buffers[buf->index].seq;
1337         buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1338         buf->length = cam->frame_size;
1339         buf->input = 0;
1340         buf->reserved = 0;
1341         memset(&buf->timecode, 0, sizeof(buf->timecode));
1342
1343         DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1344             cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1345
1346         return 0;
1347 }
1348
1349 /******************************************************************************
1350  *
1351  *  cpia2_ioctl
1352  *
1353  *****************************************************************************/
1354 static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1355 {
1356         struct camera_data *cam = video_drvdata(file);
1357         long retval = 0;
1358
1359         if (!cam)
1360                 return -ENOTTY;
1361
1362         /* make this _really_ smp-safe */
1363         if (mutex_lock_interruptible(&cam->busy_lock))
1364                 return -ERESTARTSYS;
1365
1366         if (!cam->present) {
1367                 mutex_unlock(&cam->busy_lock);
1368                 return -ENODEV;
1369         }
1370
1371         /* Priority check */
1372         switch (cmd) {
1373         case VIDIOC_S_FMT:
1374         {
1375                 struct cpia2_fh *fh = file->private_data;
1376                 retval = v4l2_prio_check(&cam->prio, fh->prio);
1377                 if(retval) {
1378                         mutex_unlock(&cam->busy_lock);
1379                         return retval;
1380                 }
1381                 break;
1382         }
1383 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1384         case VIDIOCGMBUF:
1385         {
1386                 struct cpia2_fh *fh = file->private_data;
1387                 if(fh->prio != V4L2_PRIORITY_RECORD) {
1388                         mutex_unlock(&cam->busy_lock);
1389                         return -EBUSY;
1390                 }
1391                 break;
1392         }
1393 #endif
1394         default:
1395                 break;
1396         }
1397
1398         switch (cmd) {
1399         /* CPIA2 extension to Video4Linux API */
1400         case CPIA2_IOC_SET_GPIO:
1401                 retval = ioctl_set_gpio(arg, cam);
1402                 break;
1403 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1404         case VIDIOCGMBUF:       /* mmap interface */
1405                 retval = ioctl_get_mbuf(arg, cam);
1406                 break;
1407 #endif
1408         case VIDIOC_QUERYCAP:
1409                 retval = ioctl_querycap(arg,cam);
1410                 break;
1411
1412         case VIDIOC_ENUMINPUT:
1413         case VIDIOC_G_INPUT:
1414         case VIDIOC_S_INPUT:
1415                 retval = ioctl_input(cmd, arg, cam);
1416                 break;
1417
1418         case VIDIOC_ENUM_FMT:
1419                 retval = ioctl_enum_fmt(arg,cam);
1420                 break;
1421         case VIDIOC_TRY_FMT:
1422                 retval = ioctl_try_fmt(arg,cam);
1423                 break;
1424         case VIDIOC_G_FMT:
1425                 retval = ioctl_get_fmt(arg,cam);
1426                 break;
1427         case VIDIOC_S_FMT:
1428                 retval = ioctl_set_fmt(arg,cam,file->private_data);
1429                 break;
1430
1431         case VIDIOC_CROPCAP:
1432                 retval = ioctl_cropcap(arg,cam);
1433                 break;
1434         case VIDIOC_G_CROP:
1435         case VIDIOC_S_CROP:
1436                 // TODO: I think cropping can be implemented - SJB
1437                 retval = -EINVAL;
1438                 break;
1439
1440         case VIDIOC_QUERYCTRL:
1441                 retval = ioctl_queryctrl(arg,cam);
1442                 break;
1443         case VIDIOC_QUERYMENU:
1444                 retval = ioctl_querymenu(arg,cam);
1445                 break;
1446         case VIDIOC_G_CTRL:
1447                 retval = ioctl_g_ctrl(arg,cam);
1448                 break;
1449         case VIDIOC_S_CTRL:
1450                 retval = ioctl_s_ctrl(arg,cam);
1451                 break;
1452
1453         case VIDIOC_G_JPEGCOMP:
1454                 retval = ioctl_g_jpegcomp(arg,cam);
1455                 break;
1456         case VIDIOC_S_JPEGCOMP:
1457                 retval = ioctl_s_jpegcomp(arg,cam);
1458                 break;
1459
1460         case VIDIOC_G_PRIORITY:
1461         {
1462                 struct cpia2_fh *fh = file->private_data;
1463                 *(enum v4l2_priority*)arg = fh->prio;
1464                 break;
1465         }
1466         case VIDIOC_S_PRIORITY:
1467         {
1468                 struct cpia2_fh *fh = file->private_data;
1469                 enum v4l2_priority prio;
1470                 prio = *(enum v4l2_priority*)arg;
1471                 if(cam->streaming &&
1472                    prio != fh->prio &&
1473                    fh->prio == V4L2_PRIORITY_RECORD) {
1474                         /* Can't drop record priority while streaming */
1475                         retval = -EBUSY;
1476                 } else if(prio == V4L2_PRIORITY_RECORD &&
1477                    prio != fh->prio &&
1478                    v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1479                         /* Only one program can record at a time */
1480                         retval = -EBUSY;
1481                 } else {
1482                         retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1483                 }
1484                 break;
1485         }
1486
1487         case VIDIOC_REQBUFS:
1488                 retval = ioctl_reqbufs(arg,cam);
1489                 break;
1490         case VIDIOC_QUERYBUF:
1491                 retval = ioctl_querybuf(arg,cam);
1492                 break;
1493         case VIDIOC_QBUF:
1494                 retval = ioctl_qbuf(arg,cam);
1495                 break;
1496         case VIDIOC_DQBUF:
1497                 retval = ioctl_dqbuf(arg,cam,file);
1498                 break;
1499         case VIDIOC_STREAMON:
1500         {
1501                 int type;
1502                 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1503                 type = *(int*)arg;
1504                 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1505                         retval = -EINVAL;
1506
1507                 if(!cam->streaming) {
1508                         retval = cpia2_usb_stream_start(cam,
1509                                           cam->params.camera_state.stream_mode);
1510                 } else {
1511                         retval = -EINVAL;
1512                 }
1513
1514                 break;
1515         }
1516         case VIDIOC_STREAMOFF:
1517         {
1518                 int type;
1519                 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1520                 type = *(int*)arg;
1521                 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1522                         retval = -EINVAL;
1523
1524                 if(cam->streaming) {
1525                         retval = cpia2_usb_stream_stop(cam);
1526                 } else {
1527                         retval = -EINVAL;
1528                 }
1529
1530                 break;
1531         }
1532
1533         case VIDIOC_ENUMOUTPUT:
1534         case VIDIOC_G_OUTPUT:
1535         case VIDIOC_S_OUTPUT:
1536         case VIDIOC_G_MODULATOR:
1537         case VIDIOC_S_MODULATOR:
1538
1539         case VIDIOC_ENUMAUDIO:
1540         case VIDIOC_G_AUDIO:
1541         case VIDIOC_S_AUDIO:
1542
1543         case VIDIOC_ENUMAUDOUT:
1544         case VIDIOC_G_AUDOUT:
1545         case VIDIOC_S_AUDOUT:
1546
1547         case VIDIOC_ENUMSTD:
1548         case VIDIOC_QUERYSTD:
1549         case VIDIOC_G_STD:
1550         case VIDIOC_S_STD:
1551
1552         case VIDIOC_G_TUNER:
1553         case VIDIOC_S_TUNER:
1554         case VIDIOC_G_FREQUENCY:
1555         case VIDIOC_S_FREQUENCY:
1556
1557         case VIDIOC_OVERLAY:
1558         case VIDIOC_G_FBUF:
1559         case VIDIOC_S_FBUF:
1560
1561         case VIDIOC_G_PARM:
1562         case VIDIOC_S_PARM:
1563                 retval = -EINVAL;
1564                 break;
1565         default:
1566                 retval = -ENOIOCTLCMD;
1567                 break;
1568         }
1569
1570         mutex_unlock(&cam->busy_lock);
1571         return retval;
1572 }
1573
1574 static long cpia2_ioctl(struct file *file,
1575                        unsigned int cmd, unsigned long arg)
1576 {
1577         return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
1578 }
1579
1580 /******************************************************************************
1581  *
1582  *  cpia2_mmap
1583  *
1584  *****************************************************************************/
1585 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1586 {
1587         struct camera_data *cam = video_drvdata(file);
1588         int retval;
1589
1590         /* Priority check */
1591         struct cpia2_fh *fh = file->private_data;
1592         if(fh->prio != V4L2_PRIORITY_RECORD) {
1593                 return -EBUSY;
1594         }
1595
1596         retval = cpia2_remap_buffer(cam, area);
1597
1598         if(!retval)
1599                 fh->mmapped = 1;
1600         return retval;
1601 }
1602
1603 /******************************************************************************
1604  *
1605  *  reset_camera_struct_v4l
1606  *
1607  *  Sets all values to the defaults
1608  *****************************************************************************/
1609 static void reset_camera_struct_v4l(struct camera_data *cam)
1610 {
1611         cam->width = cam->params.roi.width;
1612         cam->height = cam->params.roi.height;
1613
1614         cam->frame_size = buffer_size;
1615         cam->num_frames = num_buffers;
1616
1617         /* FlickerModes */
1618         cam->params.flicker_control.flicker_mode_req = flicker_mode;
1619         cam->params.flicker_control.mains_frequency = flicker_freq;
1620
1621         /* streamMode */
1622         cam->params.camera_state.stream_mode = alternate;
1623
1624         cam->pixelformat = V4L2_PIX_FMT_JPEG;
1625         v4l2_prio_init(&cam->prio);
1626 }
1627
1628 /***
1629  * The v4l video device structure initialized for this device
1630  ***/
1631 static const struct v4l2_file_operations cpia2_fops = {
1632         .owner          = THIS_MODULE,
1633         .open           = cpia2_open,
1634         .release        = cpia2_close,
1635         .read           = cpia2_v4l_read,
1636         .poll           = cpia2_v4l_poll,
1637         .ioctl          = cpia2_ioctl,
1638         .mmap           = cpia2_mmap,
1639 };
1640
1641 static struct video_device cpia2_template = {
1642         /* I could not find any place for the old .initialize initializer?? */
1643         .name =         "CPiA2 Camera",
1644         .fops =         &cpia2_fops,
1645         .release =      video_device_release,
1646 };
1647
1648 /******************************************************************************
1649  *
1650  *  cpia2_register_camera
1651  *
1652  *****************************************************************************/
1653 int cpia2_register_camera(struct camera_data *cam)
1654 {
1655         cam->vdev = video_device_alloc();
1656         if(!cam->vdev)
1657                 return -ENOMEM;
1658
1659         memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1660         video_set_drvdata(cam->vdev, cam);
1661
1662         reset_camera_struct_v4l(cam);
1663
1664         /* register v4l device */
1665         if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1666                 ERR("video_register_device failed\n");
1667                 video_device_release(cam->vdev);
1668                 return -ENODEV;
1669         }
1670
1671         return 0;
1672 }
1673
1674 /******************************************************************************
1675  *
1676  *  cpia2_unregister_camera
1677  *
1678  *****************************************************************************/
1679 void cpia2_unregister_camera(struct camera_data *cam)
1680 {
1681         if (!cam->open_count) {
1682                 video_unregister_device(cam->vdev);
1683         } else {
1684                 LOG("%s removed while open, deferring "
1685                     "video_unregister_device\n",
1686                     video_device_node_name(cam->vdev));
1687         }
1688 }
1689
1690 /******************************************************************************
1691  *
1692  *  check_parameters
1693  *
1694  *  Make sure that all user-supplied parameters are sensible
1695  *****************************************************************************/
1696 static void __init check_parameters(void)
1697 {
1698         if(buffer_size < PAGE_SIZE) {
1699                 buffer_size = PAGE_SIZE;
1700                 LOG("buffer_size too small, setting to %d\n", buffer_size);
1701         } else if(buffer_size > 1024*1024) {
1702                 /* arbitrary upper limiit */
1703                 buffer_size = 1024*1024;
1704                 LOG("buffer_size ridiculously large, setting to %d\n",
1705                     buffer_size);
1706         } else {
1707                 buffer_size += PAGE_SIZE-1;
1708                 buffer_size &= ~(PAGE_SIZE-1);
1709         }
1710
1711         if(num_buffers < 1) {
1712                 num_buffers = 1;
1713                 LOG("num_buffers too small, setting to %d\n", num_buffers);
1714         } else if(num_buffers > VIDEO_MAX_FRAME) {
1715                 num_buffers = VIDEO_MAX_FRAME;
1716                 LOG("num_buffers too large, setting to %d\n", num_buffers);
1717         }
1718
1719         if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1720                 alternate = DEFAULT_ALT;
1721                 LOG("alternate specified is invalid, using %d\n", alternate);
1722         }
1723
1724         if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1725                 flicker_mode = NEVER_FLICKER;
1726                 LOG("Flicker mode specified is invalid, using %d\n",
1727                     flicker_mode);
1728         }
1729
1730         if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1731                 flicker_freq = FLICKER_60;
1732                 LOG("Flicker mode specified is invalid, using %d\n",
1733                     flicker_freq);
1734         }
1735
1736         if(video_nr < -1 || video_nr > 64) {
1737                 video_nr = -1;
1738                 LOG("invalid video_nr specified, must be -1 to 64\n");
1739         }
1740
1741         DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1742             num_buffers, buffer_size, alternate);
1743 }
1744
1745 /************   Module Stuff ***************/
1746
1747
1748 /******************************************************************************
1749  *
1750  * cpia2_init/module_init
1751  *
1752  *****************************************************************************/
1753 static int __init cpia2_init(void)
1754 {
1755         LOG("%s v%d.%d.%d\n",
1756             ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
1757         check_parameters();
1758         cpia2_usb_init();
1759         return 0;
1760 }
1761
1762
1763 /******************************************************************************
1764  *
1765  * cpia2_exit/module_exit
1766  *
1767  *****************************************************************************/
1768 static void __exit cpia2_exit(void)
1769 {
1770         cpia2_usb_cleanup();
1771         schedule_timeout(2 * HZ);
1772 }
1773
1774 module_init(cpia2_init);
1775 module_exit(cpia2_exit);
1776