Linux 2.6.34-rc3
[pohmelfs.git] / drivers / media / video / cpia2 / cpia2_v4l.c
blob6f91415eb7b4c8afcf9a641549e064033ef304cb
1 /****************************************************************************
3 * Filename: cpia2_v4l.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
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.
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.
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.
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.
28 * Stripped of 2.4 stuff ready for main kernel submit by
29 * Alan Cox <alan@lxorguk.ukuu.org.uk>
30 ****************************************************************************/
32 #include <linux/version.h>
35 #include <linux/module.h>
36 #include <linux/time.h>
37 #include <linux/sched.h>
38 #include <linux/slab.h>
39 #include <linux/init.h>
40 #include <linux/videodev.h>
41 #include <linux/stringify.h>
42 #include <media/v4l2-ioctl.h>
44 #include "cpia2.h"
45 #include "cpia2dev.h"
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)");
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)");
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)");
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) ")");
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) ")");
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) ")");
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");
84 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
86 struct control_menu_info {
87 int value;
88 char name[32];
91 static struct control_menu_info framerate_controls[] =
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" },
100 #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
102 static struct control_menu_info flicker_controls[] =
104 { NEVER_FLICKER, "Off" },
105 { FLICKER_50, "50 Hz" },
106 { FLICKER_60, "60 Hz" },
108 #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
110 static struct control_menu_info lights_controls[] =
112 { 0, "Off" },
113 { 64, "Top" },
114 { 128, "Bottom" },
115 { 192, "Both" },
117 #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
118 #define GPIO_LIGHTS_MASK 192
120 static struct v4l2_queryctrl controls[] = {
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
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,
230 #define NUM_CONTROLS (ARRAY_SIZE(controls))
233 /******************************************************************************
235 * cpia2_open
237 *****************************************************************************/
238 static int cpia2_open(struct file *file)
240 struct camera_data *cam = video_drvdata(file);
241 int retval = 0;
243 if (!cam) {
244 ERR("Internal error, camera_data not found!\n");
245 return -ENODEV;
248 if(mutex_lock_interruptible(&cam->busy_lock))
249 return -ERESTARTSYS;
251 if(!cam->present) {
252 retval = -ENODEV;
253 goto err_return;
256 if (cam->open_count > 0) {
257 goto skip_init;
260 if (cpia2_allocate_buffers(cam)) {
261 retval = -ENOMEM;
262 goto err_return;
265 /* reset the camera */
266 if (cpia2_reset_camera(cam) < 0) {
267 retval = -EIO;
268 goto err_return;
271 cam->APP_len = 0;
272 cam->COM_len = 0;
274 skip_init:
276 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
277 if(!fh) {
278 retval = -ENOMEM;
279 goto err_return;
281 file->private_data = fh;
282 fh->prio = V4L2_PRIORITY_UNSET;
283 v4l2_prio_open(&cam->prio, &fh->prio);
284 fh->mmapped = 0;
287 ++cam->open_count;
289 cpia2_dbg_dump_registers(cam);
291 err_return:
292 mutex_unlock(&cam->busy_lock);
293 return retval;
296 /******************************************************************************
298 * cpia2_close
300 *****************************************************************************/
301 static int cpia2_close(struct file *file)
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;
307 mutex_lock(&cam->busy_lock);
309 if (cam->present &&
310 (cam->open_count == 1
311 || fh->prio == V4L2_PRIORITY_RECORD
312 )) {
313 cpia2_usb_stream_stop(cam);
315 if(cam->open_count == 1) {
316 /* save camera state for later open */
317 cpia2_save_camera_state(cam);
319 cpia2_set_low_power(cam);
320 cpia2_free_buffers(cam);
325 if(fh->mmapped)
326 cam->mmapped = 0;
327 v4l2_prio_close(&cam->prio,&fh->prio);
328 file->private_data = NULL;
329 kfree(fh);
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;
342 mutex_unlock(&cam->busy_lock);
344 return 0;
347 /******************************************************************************
349 * cpia2_v4l_read
351 *****************************************************************************/
352 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
353 loff_t *off)
355 struct camera_data *cam = video_drvdata(file);
356 int noblock = file->f_flags&O_NONBLOCK;
358 struct cpia2_fh *fh = file->private_data;
360 if(!cam)
361 return -EINVAL;
363 /* Priority check */
364 if(fh->prio != V4L2_PRIORITY_RECORD) {
365 return -EBUSY;
368 return cpia2_read(cam, buf, count, noblock);
372 /******************************************************************************
374 * cpia2_v4l_poll
376 *****************************************************************************/
377 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
379 struct camera_data *cam = video_drvdata(filp);
380 struct cpia2_fh *fh = filp->private_data;
382 if(!cam)
383 return POLLERR;
385 /* Priority check */
386 if(fh->prio != V4L2_PRIORITY_RECORD) {
387 return POLLERR;
390 return cpia2_poll(cam, filp, wait);
394 /******************************************************************************
396 * ioctl_cap_query
398 *****************************************************************************/
399 static int ioctl_cap_query(void *arg, struct camera_data *cam)
401 struct video_capability *vc;
402 int retval = 0;
403 vc = arg;
405 if (cam->params.pnp_id.product == 0x151)
406 strcpy(vc->name, "QX5 Microscope");
407 else
408 strcpy(vc->name, "CPiA2 Camera");
410 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
411 vc->channels = 1;
412 vc->audios = 0;
413 vc->minwidth = 176; /* VIDEOSIZE_QCIF */
414 vc->minheight = 144;
415 switch (cam->params.version.sensor_flags) {
416 case CPIA2_VP_SENSOR_FLAGS_500:
417 vc->maxwidth = STV_IMAGE_VGA_COLS;
418 vc->maxheight = STV_IMAGE_VGA_ROWS;
419 break;
420 case CPIA2_VP_SENSOR_FLAGS_410:
421 vc->maxwidth = STV_IMAGE_CIF_COLS;
422 vc->maxheight = STV_IMAGE_CIF_ROWS;
423 break;
424 default:
425 return -EINVAL;
428 return retval;
431 /******************************************************************************
433 * ioctl_get_channel
435 *****************************************************************************/
436 static int ioctl_get_channel(void *arg)
438 int retval = 0;
439 struct video_channel *v;
440 v = arg;
442 if (v->channel != 0)
443 return -EINVAL;
445 v->channel = 0;
446 strcpy(v->name, "Camera");
447 v->tuners = 0;
448 v->flags = 0;
449 v->type = VIDEO_TYPE_CAMERA;
450 v->norm = 0;
452 return retval;
455 /******************************************************************************
457 * ioctl_set_channel
459 *****************************************************************************/
460 static int ioctl_set_channel(void *arg)
462 struct video_channel *v;
463 int retval = 0;
464 v = arg;
466 if (retval == 0 && v->channel != 0)
467 retval = -EINVAL;
469 return retval;
472 /******************************************************************************
474 * ioctl_set_image_prop
476 *****************************************************************************/
477 static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
479 struct video_picture *vp;
480 int retval = 0;
481 vp = arg;
483 /* brightness, color, contrast need no check 0-65535 */
484 memcpy(&cam->vp, vp, sizeof(*vp));
486 /* update cam->params.colorParams */
487 cam->params.color_params.brightness = vp->brightness / 256;
488 cam->params.color_params.saturation = vp->colour / 256;
489 cam->params.color_params.contrast = vp->contrast / 256;
491 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
492 cam->params.color_params.brightness,
493 cam->params.color_params.saturation,
494 cam->params.color_params.contrast);
496 cpia2_set_color_params(cam);
498 return retval;
501 static int sync(struct camera_data *cam, int frame_nr)
503 struct framebuf *frame = &cam->buffers[frame_nr];
505 while (1) {
506 if (frame->status == FRAME_READY)
507 return 0;
509 if (!cam->streaming) {
510 frame->status = FRAME_READY;
511 frame->length = 0;
512 return 0;
515 mutex_unlock(&cam->busy_lock);
516 wait_event_interruptible(cam->wq_stream,
517 !cam->streaming ||
518 frame->status == FRAME_READY);
519 mutex_lock(&cam->busy_lock);
520 if (signal_pending(current))
521 return -ERESTARTSYS;
522 if(!cam->present)
523 return -ENOTTY;
527 /******************************************************************************
529 * ioctl_set_window_size
531 *****************************************************************************/
532 static int ioctl_set_window_size(void *arg, struct camera_data *cam,
533 struct cpia2_fh *fh)
535 /* copy_from_user, check validity, copy to internal structure */
536 struct video_window *vw;
537 int frame, err;
538 vw = arg;
540 if (vw->clipcount != 0) /* clipping not supported */
541 return -EINVAL;
543 if (vw->clips != NULL) /* clipping not supported */
544 return -EINVAL;
546 /* Ensure that only this process can change the format. */
547 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
548 if(err != 0)
549 return err;
551 cam->pixelformat = V4L2_PIX_FMT_JPEG;
553 /* Be sure to supply the Huffman tables, this isn't MJPEG */
554 cam->params.compression.inhibit_htables = 0;
556 /* we set the video window to something smaller or equal to what
557 * is requested by the user???
559 DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
560 if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
561 cam->vw.width = vw->width;
562 cam->vw.height = vw->height;
563 cam->params.roi.width = vw->width;
564 cam->params.roi.height = vw->height;
565 cpia2_set_format(cam);
568 for (frame = 0; frame < cam->num_frames; ++frame) {
569 if (cam->buffers[frame].status == FRAME_READING)
570 if ((err = sync(cam, frame)) < 0)
571 return err;
573 cam->buffers[frame].status = FRAME_EMPTY;
576 return 0;
579 /******************************************************************************
581 * ioctl_get_mbuf
583 *****************************************************************************/
584 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
586 struct video_mbuf *vm;
587 int i;
588 vm = arg;
590 memset(vm, 0, sizeof(*vm));
591 vm->size = cam->frame_size*cam->num_frames;
592 vm->frames = cam->num_frames;
593 for (i = 0; i < cam->num_frames; i++)
594 vm->offsets[i] = cam->frame_size * i;
596 return 0;
599 /******************************************************************************
601 * ioctl_mcapture
603 *****************************************************************************/
604 static int ioctl_mcapture(void *arg, struct camera_data *cam,
605 struct cpia2_fh *fh)
607 struct video_mmap *vm;
608 int video_size, err;
609 vm = arg;
611 if (vm->frame < 0 || vm->frame >= cam->num_frames)
612 return -EINVAL;
614 /* set video size */
615 video_size = cpia2_match_video_size(vm->width, vm->height);
616 if (cam->video_size < 0) {
617 return -EINVAL;
620 /* Ensure that only this process can change the format. */
621 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
622 if(err != 0)
623 return err;
625 if (video_size != cam->video_size) {
626 cam->video_size = video_size;
627 cam->params.roi.width = vm->width;
628 cam->params.roi.height = vm->height;
629 cpia2_set_format(cam);
632 if (cam->buffers[vm->frame].status == FRAME_READING)
633 if ((err=sync(cam, vm->frame)) < 0)
634 return err;
636 cam->buffers[vm->frame].status = FRAME_EMPTY;
638 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
641 /******************************************************************************
643 * ioctl_sync
645 *****************************************************************************/
646 static int ioctl_sync(void *arg, struct camera_data *cam)
648 int frame;
650 frame = *(int*)arg;
652 if (frame < 0 || frame >= cam->num_frames)
653 return -EINVAL;
655 return sync(cam, frame);
659 /******************************************************************************
661 * ioctl_set_gpio
663 *****************************************************************************/
665 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
667 __u32 gpio_val;
669 gpio_val = *(__u32*) arg;
671 if (gpio_val &~ 0xFFU)
672 return -EINVAL;
674 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
677 /******************************************************************************
679 * ioctl_querycap
681 * V4L2 device capabilities
683 *****************************************************************************/
685 static int ioctl_querycap(void *arg, struct camera_data *cam)
687 struct v4l2_capability *vc = arg;
689 memset(vc, 0, sizeof(*vc));
690 strcpy(vc->driver, "cpia2");
692 if (cam->params.pnp_id.product == 0x151)
693 strcpy(vc->card, "QX5 Microscope");
694 else
695 strcpy(vc->card, "CPiA2 Camera");
696 switch (cam->params.pnp_id.device_type) {
697 case DEVICE_STV_672:
698 strcat(vc->card, " (672/");
699 break;
700 case DEVICE_STV_676:
701 strcat(vc->card, " (676/");
702 break;
703 default:
704 strcat(vc->card, " (???/");
705 break;
707 switch (cam->params.version.sensor_flags) {
708 case CPIA2_VP_SENSOR_FLAGS_404:
709 strcat(vc->card, "404)");
710 break;
711 case CPIA2_VP_SENSOR_FLAGS_407:
712 strcat(vc->card, "407)");
713 break;
714 case CPIA2_VP_SENSOR_FLAGS_409:
715 strcat(vc->card, "409)");
716 break;
717 case CPIA2_VP_SENSOR_FLAGS_410:
718 strcat(vc->card, "410)");
719 break;
720 case CPIA2_VP_SENSOR_FLAGS_500:
721 strcat(vc->card, "500)");
722 break;
723 default:
724 strcat(vc->card, "???)");
725 break;
728 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
729 memset(vc->bus_info,0, sizeof(vc->bus_info));
731 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
732 CPIA2_PATCH_VER);
734 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
735 V4L2_CAP_READWRITE |
736 V4L2_CAP_STREAMING;
738 return 0;
741 /******************************************************************************
743 * ioctl_input
745 * V4L2 input get/set/enumerate
747 *****************************************************************************/
749 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
751 struct v4l2_input *i = arg;
753 if(ioclt_nr != VIDIOC_G_INPUT) {
754 if (i->index != 0)
755 return -EINVAL;
758 memset(i, 0, sizeof(*i));
759 strcpy(i->name, "Camera");
760 i->type = V4L2_INPUT_TYPE_CAMERA;
762 return 0;
765 /******************************************************************************
767 * ioctl_enum_fmt
769 * V4L2 format enumerate
771 *****************************************************************************/
773 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
775 struct v4l2_fmtdesc *f = arg;
776 int index = f->index;
778 if (index < 0 || index > 1)
779 return -EINVAL;
781 memset(f, 0, sizeof(*f));
782 f->index = index;
783 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
784 f->flags = V4L2_FMT_FLAG_COMPRESSED;
785 switch(index) {
786 case 0:
787 strcpy(f->description, "MJPEG");
788 f->pixelformat = V4L2_PIX_FMT_MJPEG;
789 break;
790 case 1:
791 strcpy(f->description, "JPEG");
792 f->pixelformat = V4L2_PIX_FMT_JPEG;
793 break;
794 default:
795 return -EINVAL;
798 return 0;
801 /******************************************************************************
803 * ioctl_try_fmt
805 * V4L2 format try
807 *****************************************************************************/
809 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
811 struct v4l2_format *f = arg;
813 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
814 return -EINVAL;
816 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
817 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
818 return -EINVAL;
820 f->fmt.pix.field = V4L2_FIELD_NONE;
821 f->fmt.pix.bytesperline = 0;
822 f->fmt.pix.sizeimage = cam->frame_size;
823 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
824 f->fmt.pix.priv = 0;
826 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
827 case VIDEOSIZE_VGA:
828 f->fmt.pix.width = 640;
829 f->fmt.pix.height = 480;
830 break;
831 case VIDEOSIZE_CIF:
832 f->fmt.pix.width = 352;
833 f->fmt.pix.height = 288;
834 break;
835 case VIDEOSIZE_QVGA:
836 f->fmt.pix.width = 320;
837 f->fmt.pix.height = 240;
838 break;
839 case VIDEOSIZE_288_216:
840 f->fmt.pix.width = 288;
841 f->fmt.pix.height = 216;
842 break;
843 case VIDEOSIZE_256_192:
844 f->fmt.pix.width = 256;
845 f->fmt.pix.height = 192;
846 break;
847 case VIDEOSIZE_224_168:
848 f->fmt.pix.width = 224;
849 f->fmt.pix.height = 168;
850 break;
851 case VIDEOSIZE_192_144:
852 f->fmt.pix.width = 192;
853 f->fmt.pix.height = 144;
854 break;
855 case VIDEOSIZE_QCIF:
856 default:
857 f->fmt.pix.width = 176;
858 f->fmt.pix.height = 144;
859 break;
862 return 0;
865 /******************************************************************************
867 * ioctl_set_fmt
869 * V4L2 format set
871 *****************************************************************************/
873 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
875 struct v4l2_format *f = arg;
876 int err, frame;
878 err = ioctl_try_fmt(arg, cam);
879 if(err != 0)
880 return err;
882 /* Ensure that only this process can change the format. */
883 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
884 if(err != 0) {
885 return err;
888 cam->pixelformat = f->fmt.pix.pixelformat;
890 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
891 * the missing Huffman table properly. */
892 cam->params.compression.inhibit_htables = 0;
893 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
895 /* we set the video window to something smaller or equal to what
896 * is requested by the user???
898 DBG("Requested width = %d, height = %d\n",
899 f->fmt.pix.width, f->fmt.pix.height);
900 if (f->fmt.pix.width != cam->vw.width ||
901 f->fmt.pix.height != cam->vw.height) {
902 cam->vw.width = f->fmt.pix.width;
903 cam->vw.height = f->fmt.pix.height;
904 cam->params.roi.width = f->fmt.pix.width;
905 cam->params.roi.height = f->fmt.pix.height;
906 cpia2_set_format(cam);
909 for (frame = 0; frame < cam->num_frames; ++frame) {
910 if (cam->buffers[frame].status == FRAME_READING)
911 if ((err = sync(cam, frame)) < 0)
912 return err;
914 cam->buffers[frame].status = FRAME_EMPTY;
917 return 0;
920 /******************************************************************************
922 * ioctl_get_fmt
924 * V4L2 format get
926 *****************************************************************************/
928 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
930 struct v4l2_format *f = arg;
932 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
933 return -EINVAL;
935 f->fmt.pix.width = cam->vw.width;
936 f->fmt.pix.height = cam->vw.height;
937 f->fmt.pix.pixelformat = cam->pixelformat;
938 f->fmt.pix.field = V4L2_FIELD_NONE;
939 f->fmt.pix.bytesperline = 0;
940 f->fmt.pix.sizeimage = cam->frame_size;
941 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
942 f->fmt.pix.priv = 0;
944 return 0;
947 /******************************************************************************
949 * ioctl_cropcap
951 * V4L2 query cropping capabilities
952 * NOTE: cropping is currently disabled
954 *****************************************************************************/
956 static int ioctl_cropcap(void *arg,struct camera_data *cam)
958 struct v4l2_cropcap *c = arg;
960 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
961 return -EINVAL;
963 c->bounds.left = 0;
964 c->bounds.top = 0;
965 c->bounds.width = cam->vw.width;
966 c->bounds.height = cam->vw.height;
967 c->defrect.left = 0;
968 c->defrect.top = 0;
969 c->defrect.width = cam->vw.width;
970 c->defrect.height = cam->vw.height;
971 c->pixelaspect.numerator = 1;
972 c->pixelaspect.denominator = 1;
974 return 0;
977 /******************************************************************************
979 * ioctl_queryctrl
981 * V4L2 query possible control variables
983 *****************************************************************************/
985 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
987 struct v4l2_queryctrl *c = arg;
988 int i;
990 for(i=0; i<NUM_CONTROLS; ++i) {
991 if(c->id == controls[i].id) {
992 memcpy(c, controls+i, sizeof(*c));
993 break;
997 if(i == NUM_CONTROLS)
998 return -EINVAL;
1000 /* Some devices have additional limitations */
1001 switch(c->id) {
1002 case V4L2_CID_BRIGHTNESS:
1003 /***
1004 * Don't let the register be set to zero - bug in VP4
1005 * flash of full brightness
1006 ***/
1007 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1008 c->minimum = 1;
1009 break;
1010 case V4L2_CID_VFLIP:
1011 // VP5 Only
1012 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
1013 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1014 break;
1015 case CPIA2_CID_FRAMERATE:
1016 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1017 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1018 // Maximum 15fps
1019 for(i=0; i<c->maximum; ++i) {
1020 if(framerate_controls[i].value ==
1021 CPIA2_VP_FRAMERATE_15) {
1022 c->maximum = i;
1023 c->default_value = i;
1027 break;
1028 case CPIA2_CID_FLICKER_MODE:
1029 // Flicker control only valid for 672.
1030 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1031 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1032 break;
1033 case CPIA2_CID_LIGHTS:
1034 // Light control only valid for the QX5 Microscope.
1035 if(cam->params.pnp_id.product != 0x151)
1036 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1037 break;
1038 default:
1039 break;
1042 return 0;
1045 /******************************************************************************
1047 * ioctl_querymenu
1049 * V4L2 query possible control variables
1051 *****************************************************************************/
1053 static int ioctl_querymenu(void *arg,struct camera_data *cam)
1055 struct v4l2_querymenu *m = arg;
1057 memset(m->name, 0, sizeof(m->name));
1058 m->reserved = 0;
1060 switch(m->id) {
1061 case CPIA2_CID_FLICKER_MODE:
1062 if (m->index >= NUM_FLICKER_CONTROLS)
1063 return -EINVAL;
1065 strcpy(m->name, flicker_controls[m->index].name);
1066 break;
1067 case CPIA2_CID_FRAMERATE:
1069 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1070 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1071 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1072 // Maximum 15fps
1073 int i;
1074 for(i=0; i<maximum; ++i) {
1075 if(framerate_controls[i].value ==
1076 CPIA2_VP_FRAMERATE_15)
1077 maximum = i;
1080 if (m->index > maximum)
1081 return -EINVAL;
1083 strcpy(m->name, framerate_controls[m->index].name);
1084 break;
1086 case CPIA2_CID_LIGHTS:
1087 if (m->index >= NUM_LIGHTS_CONTROLS)
1088 return -EINVAL;
1090 strcpy(m->name, lights_controls[m->index].name);
1091 break;
1092 default:
1093 return -EINVAL;
1096 return 0;
1099 /******************************************************************************
1101 * ioctl_g_ctrl
1103 * V4L2 get the value of a control variable
1105 *****************************************************************************/
1107 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
1109 struct v4l2_control *c = arg;
1111 switch(c->id) {
1112 case V4L2_CID_BRIGHTNESS:
1113 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
1114 TRANSFER_READ, 0);
1115 c->value = cam->params.color_params.brightness;
1116 break;
1117 case V4L2_CID_CONTRAST:
1118 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
1119 TRANSFER_READ, 0);
1120 c->value = cam->params.color_params.contrast;
1121 break;
1122 case V4L2_CID_SATURATION:
1123 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
1124 TRANSFER_READ, 0);
1125 c->value = cam->params.color_params.saturation;
1126 break;
1127 case V4L2_CID_HFLIP:
1128 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1129 TRANSFER_READ, 0);
1130 c->value = (cam->params.vp_params.user_effects &
1131 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
1132 break;
1133 case V4L2_CID_VFLIP:
1134 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1135 TRANSFER_READ, 0);
1136 c->value = (cam->params.vp_params.user_effects &
1137 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
1138 break;
1139 case CPIA2_CID_TARGET_KB:
1140 c->value = cam->params.vc_params.target_kb;
1141 break;
1142 case CPIA2_CID_GPIO:
1143 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1144 TRANSFER_READ, 0);
1145 c->value = cam->params.vp_params.gpio_data;
1146 break;
1147 case CPIA2_CID_FLICKER_MODE:
1149 int i, mode;
1150 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1151 TRANSFER_READ, 0);
1152 if(cam->params.flicker_control.cam_register &
1153 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
1154 mode = NEVER_FLICKER;
1155 } else {
1156 if(cam->params.flicker_control.cam_register &
1157 CPIA2_VP_FLICKER_MODES_50HZ) {
1158 mode = FLICKER_50;
1159 } else {
1160 mode = FLICKER_60;
1163 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
1164 if(flicker_controls[i].value == mode) {
1165 c->value = i;
1166 break;
1169 if(i == NUM_FLICKER_CONTROLS)
1170 return -EINVAL;
1171 break;
1173 case CPIA2_CID_FRAMERATE:
1175 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1176 int i;
1177 for(i=0; i<= maximum; i++) {
1178 if(cam->params.vp_params.frame_rate ==
1179 framerate_controls[i].value)
1180 break;
1182 if(i > maximum)
1183 return -EINVAL;
1184 c->value = i;
1185 break;
1187 case CPIA2_CID_USB_ALT:
1188 c->value = cam->params.camera_state.stream_mode;
1189 break;
1190 case CPIA2_CID_LIGHTS:
1192 int i;
1193 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1194 TRANSFER_READ, 0);
1195 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
1196 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
1197 lights_controls[i].value) {
1198 break;
1201 if(i == NUM_LIGHTS_CONTROLS)
1202 return -EINVAL;
1203 c->value = i;
1204 break;
1206 case CPIA2_CID_RESET_CAMERA:
1207 return -EINVAL;
1208 default:
1209 return -EINVAL;
1212 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1214 return 0;
1217 /******************************************************************************
1219 * ioctl_s_ctrl
1221 * V4L2 set the value of a control variable
1223 *****************************************************************************/
1225 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1227 struct v4l2_control *c = arg;
1228 int i;
1229 int retval = 0;
1231 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1233 /* Check that the value is in range */
1234 for(i=0; i<NUM_CONTROLS; i++) {
1235 if(c->id == controls[i].id) {
1236 if(c->value < controls[i].minimum ||
1237 c->value > controls[i].maximum) {
1238 return -EINVAL;
1240 break;
1243 if(i == NUM_CONTROLS)
1244 return -EINVAL;
1246 switch(c->id) {
1247 case V4L2_CID_BRIGHTNESS:
1248 cpia2_set_brightness(cam, c->value);
1249 break;
1250 case V4L2_CID_CONTRAST:
1251 cpia2_set_contrast(cam, c->value);
1252 break;
1253 case V4L2_CID_SATURATION:
1254 cpia2_set_saturation(cam, c->value);
1255 break;
1256 case V4L2_CID_HFLIP:
1257 cpia2_set_property_mirror(cam, c->value);
1258 break;
1259 case V4L2_CID_VFLIP:
1260 cpia2_set_property_flip(cam, c->value);
1261 break;
1262 case CPIA2_CID_TARGET_KB:
1263 retval = cpia2_set_target_kb(cam, c->value);
1264 break;
1265 case CPIA2_CID_GPIO:
1266 retval = cpia2_set_gpio(cam, c->value);
1267 break;
1268 case CPIA2_CID_FLICKER_MODE:
1269 retval = cpia2_set_flicker_mode(cam,
1270 flicker_controls[c->value].value);
1271 break;
1272 case CPIA2_CID_FRAMERATE:
1273 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1274 break;
1275 case CPIA2_CID_USB_ALT:
1276 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1277 break;
1278 case CPIA2_CID_LIGHTS:
1279 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1280 break;
1281 case CPIA2_CID_RESET_CAMERA:
1282 cpia2_usb_stream_pause(cam);
1283 cpia2_reset_camera(cam);
1284 cpia2_usb_stream_resume(cam);
1285 break;
1286 default:
1287 retval = -EINVAL;
1290 return retval;
1293 /******************************************************************************
1295 * ioctl_g_jpegcomp
1297 * V4L2 get the JPEG compression parameters
1299 *****************************************************************************/
1301 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1303 struct v4l2_jpegcompression *parms = arg;
1305 memset(parms, 0, sizeof(*parms));
1307 parms->quality = 80; // TODO: Can this be made meaningful?
1309 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1310 if(!cam->params.compression.inhibit_htables) {
1311 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1314 parms->APPn = cam->APPn;
1315 parms->APP_len = cam->APP_len;
1316 if(cam->APP_len > 0) {
1317 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1318 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1321 parms->COM_len = cam->COM_len;
1322 if(cam->COM_len > 0) {
1323 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1324 parms->jpeg_markers |= JPEG_MARKER_COM;
1327 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1328 parms->APP_len, parms->COM_len);
1330 return 0;
1333 /******************************************************************************
1335 * ioctl_s_jpegcomp
1337 * V4L2 set the JPEG compression parameters
1338 * NOTE: quality and some jpeg_markers are ignored.
1340 *****************************************************************************/
1342 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1344 struct v4l2_jpegcompression *parms = arg;
1346 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1347 parms->APP_len, parms->COM_len);
1349 cam->params.compression.inhibit_htables =
1350 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1352 if(parms->APP_len != 0) {
1353 if(parms->APP_len > 0 &&
1354 parms->APP_len <= sizeof(cam->APP_data) &&
1355 parms->APPn >= 0 && parms->APPn <= 15) {
1356 cam->APPn = parms->APPn;
1357 cam->APP_len = parms->APP_len;
1358 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1359 } else {
1360 LOG("Bad APPn Params n=%d len=%d\n",
1361 parms->APPn, parms->APP_len);
1362 return -EINVAL;
1364 } else {
1365 cam->APP_len = 0;
1368 if(parms->COM_len != 0) {
1369 if(parms->COM_len > 0 &&
1370 parms->COM_len <= sizeof(cam->COM_data)) {
1371 cam->COM_len = parms->COM_len;
1372 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1373 } else {
1374 LOG("Bad COM_len=%d\n", parms->COM_len);
1375 return -EINVAL;
1379 return 0;
1382 /******************************************************************************
1384 * ioctl_reqbufs
1386 * V4L2 Initiate memory mapping.
1387 * NOTE: The user's request is ignored. For now the buffers are fixed.
1389 *****************************************************************************/
1391 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1393 struct v4l2_requestbuffers *req = arg;
1395 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1396 req->memory != V4L2_MEMORY_MMAP)
1397 return -EINVAL;
1399 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1400 req->count = cam->num_frames;
1401 memset(&req->reserved, 0, sizeof(req->reserved));
1403 return 0;
1406 /******************************************************************************
1408 * ioctl_querybuf
1410 * V4L2 Query memory buffer status.
1412 *****************************************************************************/
1414 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1416 struct v4l2_buffer *buf = arg;
1418 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1419 buf->index > cam->num_frames)
1420 return -EINVAL;
1422 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1423 buf->length = cam->frame_size;
1425 buf->memory = V4L2_MEMORY_MMAP;
1427 if(cam->mmapped)
1428 buf->flags = V4L2_BUF_FLAG_MAPPED;
1429 else
1430 buf->flags = 0;
1432 switch (cam->buffers[buf->index].status) {
1433 case FRAME_EMPTY:
1434 case FRAME_ERROR:
1435 case FRAME_READING:
1436 buf->bytesused = 0;
1437 buf->flags = V4L2_BUF_FLAG_QUEUED;
1438 break;
1439 case FRAME_READY:
1440 buf->bytesused = cam->buffers[buf->index].length;
1441 buf->timestamp = cam->buffers[buf->index].timestamp;
1442 buf->sequence = cam->buffers[buf->index].seq;
1443 buf->flags = V4L2_BUF_FLAG_DONE;
1444 break;
1447 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1448 buf->index, buf->m.offset, buf->flags, buf->sequence,
1449 buf->bytesused);
1451 return 0;
1454 /******************************************************************************
1456 * ioctl_qbuf
1458 * V4L2 User is freeing buffer
1460 *****************************************************************************/
1462 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1464 struct v4l2_buffer *buf = arg;
1466 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1467 buf->memory != V4L2_MEMORY_MMAP ||
1468 buf->index > cam->num_frames)
1469 return -EINVAL;
1471 DBG("QBUF #%d\n", buf->index);
1473 if(cam->buffers[buf->index].status == FRAME_READY)
1474 cam->buffers[buf->index].status = FRAME_EMPTY;
1476 return 0;
1479 /******************************************************************************
1481 * find_earliest_filled_buffer
1483 * Helper for ioctl_dqbuf. Find the next ready buffer.
1485 *****************************************************************************/
1487 static int find_earliest_filled_buffer(struct camera_data *cam)
1489 int i;
1490 int found = -1;
1491 for (i=0; i<cam->num_frames; i++) {
1492 if(cam->buffers[i].status == FRAME_READY) {
1493 if(found < 0) {
1494 found = i;
1495 } else {
1496 /* find which buffer is earlier */
1497 struct timeval *tv1, *tv2;
1498 tv1 = &cam->buffers[i].timestamp;
1499 tv2 = &cam->buffers[found].timestamp;
1500 if(tv1->tv_sec < tv2->tv_sec ||
1501 (tv1->tv_sec == tv2->tv_sec &&
1502 tv1->tv_usec < tv2->tv_usec))
1503 found = i;
1507 return found;
1510 /******************************************************************************
1512 * ioctl_dqbuf
1514 * V4L2 User is asking for a filled buffer.
1516 *****************************************************************************/
1518 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1520 struct v4l2_buffer *buf = arg;
1521 int frame;
1523 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1524 buf->memory != V4L2_MEMORY_MMAP)
1525 return -EINVAL;
1527 frame = find_earliest_filled_buffer(cam);
1529 if(frame < 0 && file->f_flags&O_NONBLOCK)
1530 return -EAGAIN;
1532 if(frame < 0) {
1533 /* Wait for a frame to become available */
1534 struct framebuf *cb=cam->curbuff;
1535 mutex_unlock(&cam->busy_lock);
1536 wait_event_interruptible(cam->wq_stream,
1537 !cam->present ||
1538 (cb=cam->curbuff)->status == FRAME_READY);
1539 mutex_lock(&cam->busy_lock);
1540 if (signal_pending(current))
1541 return -ERESTARTSYS;
1542 if(!cam->present)
1543 return -ENOTTY;
1544 frame = cb->num;
1548 buf->index = frame;
1549 buf->bytesused = cam->buffers[buf->index].length;
1550 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1551 buf->field = V4L2_FIELD_NONE;
1552 buf->timestamp = cam->buffers[buf->index].timestamp;
1553 buf->sequence = cam->buffers[buf->index].seq;
1554 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1555 buf->length = cam->frame_size;
1556 buf->input = 0;
1557 buf->reserved = 0;
1558 memset(&buf->timecode, 0, sizeof(buf->timecode));
1560 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1561 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1563 return 0;
1566 /******************************************************************************
1568 * cpia2_ioctl
1570 *****************************************************************************/
1571 static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1573 struct camera_data *cam = video_drvdata(file);
1574 long retval = 0;
1576 if (!cam)
1577 return -ENOTTY;
1579 /* make this _really_ smp-safe */
1580 if (mutex_lock_interruptible(&cam->busy_lock))
1581 return -ERESTARTSYS;
1583 if (!cam->present) {
1584 mutex_unlock(&cam->busy_lock);
1585 return -ENODEV;
1588 /* Priority check */
1589 switch (cmd) {
1590 case VIDIOCSWIN:
1591 case VIDIOCMCAPTURE:
1592 case VIDIOC_S_FMT:
1594 struct cpia2_fh *fh = file->private_data;
1595 retval = v4l2_prio_check(&cam->prio, &fh->prio);
1596 if(retval) {
1597 mutex_unlock(&cam->busy_lock);
1598 return retval;
1600 break;
1602 case VIDIOCGMBUF:
1603 case VIDIOCSYNC:
1605 struct cpia2_fh *fh = file->private_data;
1606 if(fh->prio != V4L2_PRIORITY_RECORD) {
1607 mutex_unlock(&cam->busy_lock);
1608 return -EBUSY;
1610 break;
1612 default:
1613 break;
1616 switch (cmd) {
1617 case VIDIOCGCAP: /* query capabilities */
1618 retval = ioctl_cap_query(arg, cam);
1619 break;
1621 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
1622 retval = ioctl_get_channel(arg);
1623 break;
1624 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
1625 retval = ioctl_set_channel(arg);
1626 break;
1627 case VIDIOCGPICT: /* image properties */
1628 memcpy(arg, &cam->vp, sizeof(struct video_picture));
1629 break;
1630 case VIDIOCSPICT:
1631 retval = ioctl_set_image_prop(arg, cam);
1632 break;
1633 case VIDIOCGWIN: /* get/set capture window */
1634 memcpy(arg, &cam->vw, sizeof(struct video_window));
1635 break;
1636 case VIDIOCSWIN:
1637 retval = ioctl_set_window_size(arg, cam, file->private_data);
1638 break;
1639 case VIDIOCGMBUF: /* mmap interface */
1640 retval = ioctl_get_mbuf(arg, cam);
1641 break;
1642 case VIDIOCMCAPTURE:
1643 retval = ioctl_mcapture(arg, cam, file->private_data);
1644 break;
1645 case VIDIOCSYNC:
1646 retval = ioctl_sync(arg, cam);
1647 break;
1648 /* pointless to implement overlay with this camera */
1649 case VIDIOCCAPTURE:
1650 case VIDIOCGFBUF:
1651 case VIDIOCSFBUF:
1652 case VIDIOCKEY:
1653 retval = -EINVAL;
1654 break;
1656 /* tuner interface - we have none */
1657 case VIDIOCGTUNER:
1658 case VIDIOCSTUNER:
1659 case VIDIOCGFREQ:
1660 case VIDIOCSFREQ:
1661 retval = -EINVAL;
1662 break;
1664 /* audio interface - we have none */
1665 case VIDIOCGAUDIO:
1666 case VIDIOCSAUDIO:
1667 retval = -EINVAL;
1668 break;
1670 /* CPIA2 extension to Video4Linux API */
1671 case CPIA2_IOC_SET_GPIO:
1672 retval = ioctl_set_gpio(arg, cam);
1673 break;
1674 case VIDIOC_QUERYCAP:
1675 retval = ioctl_querycap(arg,cam);
1676 break;
1678 case VIDIOC_ENUMINPUT:
1679 case VIDIOC_G_INPUT:
1680 case VIDIOC_S_INPUT:
1681 retval = ioctl_input(cmd, arg, cam);
1682 break;
1684 case VIDIOC_ENUM_FMT:
1685 retval = ioctl_enum_fmt(arg,cam);
1686 break;
1687 case VIDIOC_TRY_FMT:
1688 retval = ioctl_try_fmt(arg,cam);
1689 break;
1690 case VIDIOC_G_FMT:
1691 retval = ioctl_get_fmt(arg,cam);
1692 break;
1693 case VIDIOC_S_FMT:
1694 retval = ioctl_set_fmt(arg,cam,file->private_data);
1695 break;
1697 case VIDIOC_CROPCAP:
1698 retval = ioctl_cropcap(arg,cam);
1699 break;
1700 case VIDIOC_G_CROP:
1701 case VIDIOC_S_CROP:
1702 // TODO: I think cropping can be implemented - SJB
1703 retval = -EINVAL;
1704 break;
1706 case VIDIOC_QUERYCTRL:
1707 retval = ioctl_queryctrl(arg,cam);
1708 break;
1709 case VIDIOC_QUERYMENU:
1710 retval = ioctl_querymenu(arg,cam);
1711 break;
1712 case VIDIOC_G_CTRL:
1713 retval = ioctl_g_ctrl(arg,cam);
1714 break;
1715 case VIDIOC_S_CTRL:
1716 retval = ioctl_s_ctrl(arg,cam);
1717 break;
1719 case VIDIOC_G_JPEGCOMP:
1720 retval = ioctl_g_jpegcomp(arg,cam);
1721 break;
1722 case VIDIOC_S_JPEGCOMP:
1723 retval = ioctl_s_jpegcomp(arg,cam);
1724 break;
1726 case VIDIOC_G_PRIORITY:
1728 struct cpia2_fh *fh = file->private_data;
1729 *(enum v4l2_priority*)arg = fh->prio;
1730 break;
1732 case VIDIOC_S_PRIORITY:
1734 struct cpia2_fh *fh = file->private_data;
1735 enum v4l2_priority prio;
1736 prio = *(enum v4l2_priority*)arg;
1737 if(cam->streaming &&
1738 prio != fh->prio &&
1739 fh->prio == V4L2_PRIORITY_RECORD) {
1740 /* Can't drop record priority while streaming */
1741 retval = -EBUSY;
1742 } else if(prio == V4L2_PRIORITY_RECORD &&
1743 prio != fh->prio &&
1744 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1745 /* Only one program can record at a time */
1746 retval = -EBUSY;
1747 } else {
1748 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1750 break;
1753 case VIDIOC_REQBUFS:
1754 retval = ioctl_reqbufs(arg,cam);
1755 break;
1756 case VIDIOC_QUERYBUF:
1757 retval = ioctl_querybuf(arg,cam);
1758 break;
1759 case VIDIOC_QBUF:
1760 retval = ioctl_qbuf(arg,cam);
1761 break;
1762 case VIDIOC_DQBUF:
1763 retval = ioctl_dqbuf(arg,cam,file);
1764 break;
1765 case VIDIOC_STREAMON:
1767 int type;
1768 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1769 type = *(int*)arg;
1770 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1771 retval = -EINVAL;
1773 if(!cam->streaming) {
1774 retval = cpia2_usb_stream_start(cam,
1775 cam->params.camera_state.stream_mode);
1776 } else {
1777 retval = -EINVAL;
1780 break;
1782 case VIDIOC_STREAMOFF:
1784 int type;
1785 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1786 type = *(int*)arg;
1787 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1788 retval = -EINVAL;
1790 if(cam->streaming) {
1791 retval = cpia2_usb_stream_stop(cam);
1792 } else {
1793 retval = -EINVAL;
1796 break;
1799 case VIDIOC_ENUMOUTPUT:
1800 case VIDIOC_G_OUTPUT:
1801 case VIDIOC_S_OUTPUT:
1802 case VIDIOC_G_MODULATOR:
1803 case VIDIOC_S_MODULATOR:
1805 case VIDIOC_ENUMAUDIO:
1806 case VIDIOC_G_AUDIO:
1807 case VIDIOC_S_AUDIO:
1809 case VIDIOC_ENUMAUDOUT:
1810 case VIDIOC_G_AUDOUT:
1811 case VIDIOC_S_AUDOUT:
1813 case VIDIOC_ENUMSTD:
1814 case VIDIOC_QUERYSTD:
1815 case VIDIOC_G_STD:
1816 case VIDIOC_S_STD:
1818 case VIDIOC_G_TUNER:
1819 case VIDIOC_S_TUNER:
1820 case VIDIOC_G_FREQUENCY:
1821 case VIDIOC_S_FREQUENCY:
1823 case VIDIOC_OVERLAY:
1824 case VIDIOC_G_FBUF:
1825 case VIDIOC_S_FBUF:
1827 case VIDIOC_G_PARM:
1828 case VIDIOC_S_PARM:
1829 retval = -EINVAL;
1830 break;
1831 default:
1832 retval = -ENOIOCTLCMD;
1833 break;
1836 mutex_unlock(&cam->busy_lock);
1837 return retval;
1840 static long cpia2_ioctl(struct file *file,
1841 unsigned int cmd, unsigned long arg)
1843 return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
1846 /******************************************************************************
1848 * cpia2_mmap
1850 *****************************************************************************/
1851 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1853 struct camera_data *cam = video_drvdata(file);
1854 int retval;
1856 /* Priority check */
1857 struct cpia2_fh *fh = file->private_data;
1858 if(fh->prio != V4L2_PRIORITY_RECORD) {
1859 return -EBUSY;
1862 retval = cpia2_remap_buffer(cam, area);
1864 if(!retval)
1865 fh->mmapped = 1;
1866 return retval;
1869 /******************************************************************************
1871 * reset_camera_struct_v4l
1873 * Sets all values to the defaults
1874 *****************************************************************************/
1875 static void reset_camera_struct_v4l(struct camera_data *cam)
1877 /***
1878 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1879 * Ioctl. Here, just do the window and picture stucts.
1880 ***/
1881 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
1882 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
1883 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
1884 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
1886 cam->vw.x = 0;
1887 cam->vw.y = 0;
1888 cam->vw.width = cam->params.roi.width;
1889 cam->vw.height = cam->params.roi.height;
1890 cam->vw.flags = 0;
1891 cam->vw.clipcount = 0;
1893 cam->frame_size = buffer_size;
1894 cam->num_frames = num_buffers;
1896 /* FlickerModes */
1897 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1898 cam->params.flicker_control.mains_frequency = flicker_freq;
1900 /* streamMode */
1901 cam->params.camera_state.stream_mode = alternate;
1903 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1904 v4l2_prio_init(&cam->prio);
1905 return;
1908 /***
1909 * The v4l video device structure initialized for this device
1910 ***/
1911 static const struct v4l2_file_operations fops_template = {
1912 .owner = THIS_MODULE,
1913 .open = cpia2_open,
1914 .release = cpia2_close,
1915 .read = cpia2_v4l_read,
1916 .poll = cpia2_v4l_poll,
1917 .ioctl = cpia2_ioctl,
1918 .mmap = cpia2_mmap,
1921 static struct video_device cpia2_template = {
1922 /* I could not find any place for the old .initialize initializer?? */
1923 .name= "CPiA2 Camera",
1924 .fops= &fops_template,
1925 .release= video_device_release,
1928 /******************************************************************************
1930 * cpia2_register_camera
1932 *****************************************************************************/
1933 int cpia2_register_camera(struct camera_data *cam)
1935 cam->vdev = video_device_alloc();
1936 if(!cam->vdev)
1937 return -ENOMEM;
1939 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1940 video_set_drvdata(cam->vdev, cam);
1942 reset_camera_struct_v4l(cam);
1944 /* register v4l device */
1945 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1946 ERR("video_register_device failed\n");
1947 video_device_release(cam->vdev);
1948 return -ENODEV;
1951 return 0;
1954 /******************************************************************************
1956 * cpia2_unregister_camera
1958 *****************************************************************************/
1959 void cpia2_unregister_camera(struct camera_data *cam)
1961 if (!cam->open_count) {
1962 video_unregister_device(cam->vdev);
1963 } else {
1964 LOG("%s removed while open, deferring "
1965 "video_unregister_device\n",
1966 video_device_node_name(cam->vdev));
1970 /******************************************************************************
1972 * check_parameters
1974 * Make sure that all user-supplied parameters are sensible
1975 *****************************************************************************/
1976 static void __init check_parameters(void)
1978 if(buffer_size < PAGE_SIZE) {
1979 buffer_size = PAGE_SIZE;
1980 LOG("buffer_size too small, setting to %d\n", buffer_size);
1981 } else if(buffer_size > 1024*1024) {
1982 /* arbitrary upper limiit */
1983 buffer_size = 1024*1024;
1984 LOG("buffer_size ridiculously large, setting to %d\n",
1985 buffer_size);
1986 } else {
1987 buffer_size += PAGE_SIZE-1;
1988 buffer_size &= ~(PAGE_SIZE-1);
1991 if(num_buffers < 1) {
1992 num_buffers = 1;
1993 LOG("num_buffers too small, setting to %d\n", num_buffers);
1994 } else if(num_buffers > VIDEO_MAX_FRAME) {
1995 num_buffers = VIDEO_MAX_FRAME;
1996 LOG("num_buffers too large, setting to %d\n", num_buffers);
1999 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
2000 alternate = DEFAULT_ALT;
2001 LOG("alternate specified is invalid, using %d\n", alternate);
2004 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
2005 flicker_mode = NEVER_FLICKER;
2006 LOG("Flicker mode specified is invalid, using %d\n",
2007 flicker_mode);
2010 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
2011 flicker_freq = FLICKER_60;
2012 LOG("Flicker mode specified is invalid, using %d\n",
2013 flicker_freq);
2016 if(video_nr < -1 || video_nr > 64) {
2017 video_nr = -1;
2018 LOG("invalid video_nr specified, must be -1 to 64\n");
2021 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2022 num_buffers, buffer_size, alternate);
2025 /************ Module Stuff ***************/
2028 /******************************************************************************
2030 * cpia2_init/module_init
2032 *****************************************************************************/
2033 static int __init cpia2_init(void)
2035 LOG("%s v%d.%d.%d\n",
2036 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
2037 check_parameters();
2038 cpia2_usb_init();
2039 return 0;
2043 /******************************************************************************
2045 * cpia2_exit/module_exit
2047 *****************************************************************************/
2048 static void __exit cpia2_exit(void)
2050 cpia2_usb_cleanup();
2051 schedule_timeout(2 * HZ);
2054 module_init(cpia2_init);
2055 module_exit(cpia2_exit);