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>
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@redhat.com>
30 ****************************************************************************/
32 #include <linux/version.h>
34 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/time.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/moduleparam.h>
47 //#define _CPIA2_DEBUG_
49 #define MAKE_STRING_1(x) #x
50 #define MAKE_STRING(x) MAKE_STRING_1(x)
52 static int video_nr = -1;
53 module_param(video_nr, int, 0);
54 MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
56 static int buffer_size = 68*1024;
57 module_param(buffer_size, int, 0);
58 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
60 static int num_buffers = 3;
61 module_param(num_buffers, int, 0);
62 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
63 MAKE_STRING(VIDEO_MAX_FRAME) ", default 3)");
65 static int alternate = DEFAULT_ALT;
66 module_param(alternate, int, 0);
67 MODULE_PARM_DESC(alternate, "USB Alternate (" MAKE_STRING(USBIF_ISO_1) "-"
68 MAKE_STRING(USBIF_ISO_6) ", default "
69 MAKE_STRING(DEFAULT_ALT) ")");
71 static int flicker_freq = 60;
72 module_param(flicker_freq, int, 0);
73 MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" MAKE_STRING(50) "or"
74 MAKE_STRING(60) ", default "
77 static int flicker_mode = NEVER_FLICKER;
78 module_param(flicker_mode, int, 0);
79 MODULE_PARM_DESC(flicker_mode,
80 "Flicker supression (" MAKE_STRING(NEVER_FLICKER) "or"
81 MAKE_STRING(ANTI_FLICKER_ON) ", default "
82 MAKE_STRING(NEVER_FLICKER) ")");
84 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
85 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
86 MODULE_SUPPORTED_DEVICE("video");
87 MODULE_LICENSE("GPL");
89 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
91 #ifndef VID_HARDWARE_CPIA2
92 #error "VID_HARDWARE_CPIA2 should have been defined in linux/videodev.h"
95 struct control_menu_info {
100 static struct control_menu_info framerate_controls[] =
102 { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
103 { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
104 { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
105 { CPIA2_VP_FRAMERATE_15, "15 fps" },
106 { CPIA2_VP_FRAMERATE_25, "25 fps" },
107 { CPIA2_VP_FRAMERATE_30, "30 fps" },
109 #define NUM_FRAMERATE_CONTROLS (sizeof(framerate_controls)/sizeof(framerate_controls[0]))
111 static struct control_menu_info flicker_controls[] =
113 { NEVER_FLICKER, "Off" },
114 { FLICKER_50, "50 Hz" },
115 { FLICKER_60, "60 Hz" },
117 #define NUM_FLICKER_CONTROLS (sizeof(flicker_controls)/sizeof(flicker_controls[0]))
119 static struct control_menu_info lights_controls[] =
126 #define NUM_LIGHTS_CONTROLS (sizeof(lights_controls)/sizeof(lights_controls[0]))
127 #define GPIO_LIGHTS_MASK 192
129 static struct v4l2_queryctrl controls[] = {
131 .id = V4L2_CID_BRIGHTNESS,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Brightness",
137 .default_value = DEFAULT_BRIGHTNESS,
140 .id = V4L2_CID_CONTRAST,
141 .type = V4L2_CTRL_TYPE_INTEGER,
146 .default_value = DEFAULT_CONTRAST,
149 .id = V4L2_CID_SATURATION,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "Saturation",
155 .default_value = DEFAULT_SATURATION,
158 .id = V4L2_CID_HFLIP,
159 .type = V4L2_CTRL_TYPE_BOOLEAN,
160 .name = "Mirror Horizontally",
167 .id = V4L2_CID_VFLIP,
168 .type = V4L2_CTRL_TYPE_BOOLEAN,
169 .name = "Flip Vertically",
176 .id = CPIA2_CID_TARGET_KB,
177 .type = V4L2_CTRL_TYPE_INTEGER,
182 .default_value = DEFAULT_TARGET_KB,
185 .id = CPIA2_CID_GPIO,
186 .type = V4L2_CTRL_TYPE_INTEGER,
194 .id = CPIA2_CID_FLICKER_MODE,
195 .type = V4L2_CTRL_TYPE_MENU,
196 .name = "Flicker Reduction",
198 .maximum = NUM_FLICKER_CONTROLS-1,
203 .id = CPIA2_CID_FRAMERATE,
204 .type = V4L2_CTRL_TYPE_MENU,
207 .maximum = NUM_FRAMERATE_CONTROLS-1,
209 .default_value = NUM_FRAMERATE_CONTROLS-1,
212 .id = CPIA2_CID_USB_ALT,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .name = "USB Alternate",
215 .minimum = USBIF_ISO_1,
216 .maximum = USBIF_ISO_6,
218 .default_value = DEFAULT_ALT,
221 .id = CPIA2_CID_LIGHTS,
222 .type = V4L2_CTRL_TYPE_MENU,
225 .maximum = NUM_LIGHTS_CONTROLS-1,
230 .id = CPIA2_CID_RESET_CAMERA,
231 .type = V4L2_CTRL_TYPE_BUTTON,
232 .name = "Reset Camera",
239 #define NUM_CONTROLS (sizeof(controls)/sizeof(controls[0]))
242 /******************************************************************************
246 *****************************************************************************/
247 static int cpia2_open(struct inode *inode, struct file *file)
249 struct video_device *dev = video_devdata(file);
250 struct camera_data *cam = video_get_drvdata(dev);
254 ERR("Internal error, camera_data not found!\n");
258 if(down_interruptible(&cam->busy_lock))
266 if (cam->open_count > 0) {
270 if (cpia2_allocate_buffers(cam)) {
275 /* reset the camera */
276 if (cpia2_reset_camera(cam) < 0) {
286 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
291 file->private_data = fh;
292 fh->prio = V4L2_PRIORITY_UNSET;
293 v4l2_prio_open(&cam->prio, &fh->prio);
299 cpia2_dbg_dump_registers(cam);
306 /******************************************************************************
310 *****************************************************************************/
311 static int cpia2_close(struct inode *inode, struct file *file)
313 struct video_device *dev = video_devdata(file);
314 struct camera_data *cam = video_get_drvdata(dev);
315 struct cpia2_fh *fh = file->private_data;
317 down(&cam->busy_lock);
320 (cam->open_count == 1
321 || fh->prio == V4L2_PRIORITY_RECORD
323 cpia2_usb_stream_stop(cam);
325 if(cam->open_count == 1) {
326 /* save camera state for later open */
327 cpia2_save_camera_state(cam);
329 cpia2_set_low_power(cam);
330 cpia2_free_buffers(cam);
337 v4l2_prio_close(&cam->prio,&fh->prio);
338 file->private_data = NULL;
342 if (--cam->open_count == 0) {
343 cpia2_free_buffers(cam);
345 video_unregister_device(dev);
355 /******************************************************************************
359 *****************************************************************************/
360 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
363 struct video_device *dev = video_devdata(file);
364 struct camera_data *cam = video_get_drvdata(dev);
365 int noblock = file->f_flags&O_NONBLOCK;
367 struct cpia2_fh *fh = file->private_data;
373 if(fh->prio != V4L2_PRIORITY_RECORD) {
377 return cpia2_read(cam, buf, count, noblock);
381 /******************************************************************************
385 *****************************************************************************/
386 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
388 struct video_device *dev = video_devdata(filp);
389 struct camera_data *cam = video_get_drvdata(dev);
391 struct cpia2_fh *fh = filp->private_data;
397 if(fh->prio != V4L2_PRIORITY_RECORD) {
401 return cpia2_poll(cam, filp, wait);
405 /******************************************************************************
409 *****************************************************************************/
410 static int ioctl_cap_query(void *arg, struct camera_data *cam)
412 struct video_capability *vc;
416 if (cam->params.pnp_id.product == 0x151)
417 strcpy(vc->name, "QX5 Microscope");
419 strcpy(vc->name, "CPiA2 Camera");
421 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
424 vc->minwidth = 176; /* VIDEOSIZE_QCIF */
426 switch (cam->params.version.sensor_flags) {
427 case CPIA2_VP_SENSOR_FLAGS_500:
428 vc->maxwidth = STV_IMAGE_VGA_COLS;
429 vc->maxheight = STV_IMAGE_VGA_ROWS;
431 case CPIA2_VP_SENSOR_FLAGS_410:
432 vc->maxwidth = STV_IMAGE_CIF_COLS;
433 vc->maxheight = STV_IMAGE_CIF_ROWS;
442 /******************************************************************************
446 *****************************************************************************/
447 static int ioctl_get_channel(void *arg)
450 struct video_channel *v;
457 strcpy(v->name, "Camera");
460 v->type = VIDEO_TYPE_CAMERA;
466 /******************************************************************************
470 *****************************************************************************/
471 static int ioctl_set_channel(void *arg)
473 struct video_channel *v;
477 if (retval == 0 && v->channel != 0)
483 /******************************************************************************
485 * ioctl_set_image_prop
487 *****************************************************************************/
488 static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
490 struct video_picture *vp;
494 /* brightness, color, contrast need no check 0-65535 */
495 memcpy(&cam->vp, vp, sizeof(*vp));
497 /* update cam->params.colorParams */
498 cam->params.color_params.brightness = vp->brightness / 256;
499 cam->params.color_params.saturation = vp->colour / 256;
500 cam->params.color_params.contrast = vp->contrast / 256;
502 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
503 cam->params.color_params.brightness,
504 cam->params.color_params.saturation,
505 cam->params.color_params.contrast);
507 cpia2_set_color_params(cam);
512 static int sync(struct camera_data *cam, int frame_nr)
514 struct framebuf *frame = &cam->buffers[frame_nr];
517 if (frame->status == FRAME_READY)
520 if (!cam->streaming) {
521 frame->status = FRAME_READY;
527 wait_event_interruptible(cam->wq_stream,
529 frame->status == FRAME_READY);
530 down(&cam->busy_lock);
531 if (signal_pending(current))
538 /******************************************************************************
540 * ioctl_set_window_size
542 *****************************************************************************/
543 static int ioctl_set_window_size(void *arg, struct camera_data *cam,
546 /* copy_from_user, check validity, copy to internal structure */
547 struct video_window *vw;
551 if (vw->clipcount != 0) /* clipping not supported */
554 if (vw->clips != NULL) /* clipping not supported */
557 /* Ensure that only this process can change the format. */
558 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
562 cam->pixelformat = V4L2_PIX_FMT_JPEG;
564 /* Be sure to supply the Huffman tables, this isn't MJPEG */
565 cam->params.compression.inhibit_htables = 0;
567 /* we set the video window to something smaller or equal to what
568 * is requested by the user???
570 DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
571 if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
572 cam->vw.width = vw->width;
573 cam->vw.height = vw->height;
574 cam->params.roi.width = vw->width;
575 cam->params.roi.height = vw->height;
576 cpia2_set_format(cam);
579 for (frame = 0; frame < cam->num_frames; ++frame) {
580 if (cam->buffers[frame].status == FRAME_READING)
581 if ((err = sync(cam, frame)) < 0)
584 cam->buffers[frame].status = FRAME_EMPTY;
590 /******************************************************************************
594 *****************************************************************************/
595 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
597 struct video_mbuf *vm;
601 memset(vm, 0, sizeof(*vm));
602 vm->size = cam->frame_size*cam->num_frames;
603 vm->frames = cam->num_frames;
604 for (i = 0; i < cam->num_frames; i++)
605 vm->offsets[i] = cam->frame_size * i;
610 /******************************************************************************
614 *****************************************************************************/
615 static int ioctl_mcapture(void *arg, struct camera_data *cam,
618 struct video_mmap *vm;
622 if (vm->frame < 0 || vm->frame >= cam->num_frames)
626 video_size = cpia2_match_video_size(vm->width, vm->height);
627 if (cam->video_size < 0) {
631 /* Ensure that only this process can change the format. */
632 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
636 if (video_size != cam->video_size) {
637 cam->video_size = video_size;
638 cam->params.roi.width = vm->width;
639 cam->params.roi.height = vm->height;
640 cpia2_set_format(cam);
643 if (cam->buffers[vm->frame].status == FRAME_READING)
644 if ((err=sync(cam, vm->frame)) < 0)
647 cam->buffers[vm->frame].status = FRAME_EMPTY;
649 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
652 /******************************************************************************
656 *****************************************************************************/
657 static int ioctl_sync(void *arg, struct camera_data *cam)
663 if (frame < 0 || frame >= cam->num_frames)
666 return sync(cam, frame);
670 /******************************************************************************
674 *****************************************************************************/
676 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
680 gpio_val = *(__u32*) arg;
682 if (gpio_val &~ 0xFFU)
685 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
688 /******************************************************************************
692 * V4L2 device capabilities
694 *****************************************************************************/
696 static int ioctl_querycap(void *arg, struct camera_data *cam)
698 struct v4l2_capability *vc = arg;
700 memset(vc, 0, sizeof(*vc));
701 strcpy(vc->driver, "cpia2");
703 if (cam->params.pnp_id.product == 0x151)
704 strcpy(vc->card, "QX5 Microscope");
706 strcpy(vc->card, "CPiA2 Camera");
707 switch (cam->params.pnp_id.device_type) {
709 strcat(vc->card, " (672/");
712 strcat(vc->card, " (676/");
715 strcat(vc->card, " (???/");
718 switch (cam->params.version.sensor_flags) {
719 case CPIA2_VP_SENSOR_FLAGS_404:
720 strcat(vc->card, "404)");
722 case CPIA2_VP_SENSOR_FLAGS_407:
723 strcat(vc->card, "407)");
725 case CPIA2_VP_SENSOR_FLAGS_409:
726 strcat(vc->card, "409)");
728 case CPIA2_VP_SENSOR_FLAGS_410:
729 strcat(vc->card, "410)");
731 case CPIA2_VP_SENSOR_FLAGS_500:
732 strcat(vc->card, "500)");
735 strcat(vc->card, "???)");
739 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
740 memset(vc->bus_info,0, sizeof(vc->bus_info));
742 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
745 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
752 /******************************************************************************
756 * V4L2 input get/set/enumerate
758 *****************************************************************************/
760 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
762 struct v4l2_input *i = arg;
764 if(ioclt_nr != VIDIOC_G_INPUT) {
769 memset(i, 0, sizeof(*i));
770 strcpy(i->name, "Camera");
771 i->type = V4L2_INPUT_TYPE_CAMERA;
776 /******************************************************************************
780 * V4L2 format enumerate
782 *****************************************************************************/
784 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
786 struct v4l2_fmtdesc *f = arg;
787 int index = f->index;
789 if (index < 0 || index > 1)
792 memset(f, 0, sizeof(*f));
794 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
795 f->flags = V4L2_FMT_FLAG_COMPRESSED;
798 strcpy(f->description, "MJPEG");
799 f->pixelformat = V4L2_PIX_FMT_MJPEG;
802 strcpy(f->description, "JPEG");
803 f->pixelformat = V4L2_PIX_FMT_JPEG;
812 /******************************************************************************
818 *****************************************************************************/
820 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
822 struct v4l2_format *f = arg;
824 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
827 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
828 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
831 f->fmt.pix.field = V4L2_FIELD_NONE;
832 f->fmt.pix.bytesperline = 0;
833 f->fmt.pix.sizeimage = cam->frame_size;
834 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
837 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
839 f->fmt.pix.width = 640;
840 f->fmt.pix.height = 480;
843 f->fmt.pix.width = 352;
844 f->fmt.pix.height = 288;
847 f->fmt.pix.width = 320;
848 f->fmt.pix.height = 240;
850 case VIDEOSIZE_288_216:
851 f->fmt.pix.width = 288;
852 f->fmt.pix.height = 216;
854 case VIDEOSIZE_256_192:
855 f->fmt.pix.width = 256;
856 f->fmt.pix.height = 192;
858 case VIDEOSIZE_224_168:
859 f->fmt.pix.width = 224;
860 f->fmt.pix.height = 168;
862 case VIDEOSIZE_192_144:
863 f->fmt.pix.width = 192;
864 f->fmt.pix.height = 144;
868 f->fmt.pix.width = 176;
869 f->fmt.pix.height = 144;
876 /******************************************************************************
882 *****************************************************************************/
884 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
886 struct v4l2_format *f = arg;
889 err = ioctl_try_fmt(arg, cam);
893 /* Ensure that only this process can change the format. */
894 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
899 cam->pixelformat = f->fmt.pix.pixelformat;
901 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
902 * the missing Huffman table properly. */
903 cam->params.compression.inhibit_htables = 0;
904 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
906 /* we set the video window to something smaller or equal to what
907 * is requested by the user???
909 DBG("Requested width = %d, height = %d\n",
910 f->fmt.pix.width, f->fmt.pix.height);
911 if (f->fmt.pix.width != cam->vw.width ||
912 f->fmt.pix.height != cam->vw.height) {
913 cam->vw.width = f->fmt.pix.width;
914 cam->vw.height = f->fmt.pix.height;
915 cam->params.roi.width = f->fmt.pix.width;
916 cam->params.roi.height = f->fmt.pix.height;
917 cpia2_set_format(cam);
920 for (frame = 0; frame < cam->num_frames; ++frame) {
921 if (cam->buffers[frame].status == FRAME_READING)
922 if ((err = sync(cam, frame)) < 0)
925 cam->buffers[frame].status = FRAME_EMPTY;
931 /******************************************************************************
937 *****************************************************************************/
939 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
941 struct v4l2_format *f = arg;
943 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
946 f->fmt.pix.width = cam->vw.width;
947 f->fmt.pix.height = cam->vw.height;
948 f->fmt.pix.pixelformat = cam->pixelformat;
949 f->fmt.pix.field = V4L2_FIELD_NONE;
950 f->fmt.pix.bytesperline = 0;
951 f->fmt.pix.sizeimage = cam->frame_size;
952 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
958 /******************************************************************************
962 * V4L2 query cropping capabilities
963 * NOTE: cropping is currently disabled
965 *****************************************************************************/
967 static int ioctl_cropcap(void *arg,struct camera_data *cam)
969 struct v4l2_cropcap *c = arg;
971 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
976 c->bounds.width = cam->vw.width;
977 c->bounds.height = cam->vw.height;
980 c->defrect.width = cam->vw.width;
981 c->defrect.height = cam->vw.height;
982 c->pixelaspect.numerator = 1;
983 c->pixelaspect.denominator = 1;
988 /******************************************************************************
992 * V4L2 query possible control variables
994 *****************************************************************************/
996 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
998 struct v4l2_queryctrl *c = arg;
1001 for(i=0; i<NUM_CONTROLS; ++i) {
1002 if(c->id == controls[i].id) {
1003 memcpy(c, controls+i, sizeof(*c));
1008 if(i == NUM_CONTROLS)
1011 /* Some devices have additional limitations */
1013 case V4L2_CID_BRIGHTNESS:
1015 * Don't let the register be set to zero - bug in VP4
1016 * flash of full brightness
1018 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1021 case V4L2_CID_VFLIP:
1023 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
1024 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1026 case CPIA2_CID_FRAMERATE:
1027 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1028 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1031 for(i=0; i<c->maximum; ++i) {
1032 if(framerate_controls[i].value ==
1033 CPIA2_VP_FRAMERATE_15) {
1035 c->default_value = i;
1040 case CPIA2_CID_FLICKER_MODE:
1041 // Flicker control only valid for 672.
1042 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1043 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1045 case CPIA2_CID_LIGHTS:
1046 // Light control only valid for the QX5 Microscope.
1047 if(cam->params.pnp_id.product != 0x151)
1048 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1057 /******************************************************************************
1061 * V4L2 query possible control variables
1063 *****************************************************************************/
1065 static int ioctl_querymenu(void *arg,struct camera_data *cam)
1067 struct v4l2_querymenu *m = arg;
1069 memset(m->name, 0, sizeof(m->name));
1073 case CPIA2_CID_FLICKER_MODE:
1074 if(m->index < 0 || m->index >= NUM_FLICKER_CONTROLS)
1077 strcpy(m->name, flicker_controls[m->index].name);
1079 case CPIA2_CID_FRAMERATE:
1081 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1082 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1083 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1086 for(i=0; i<maximum; ++i) {
1087 if(framerate_controls[i].value ==
1088 CPIA2_VP_FRAMERATE_15)
1092 if(m->index < 0 || m->index > maximum)
1095 strcpy(m->name, framerate_controls[m->index].name);
1098 case CPIA2_CID_LIGHTS:
1099 if(m->index < 0 || m->index >= NUM_LIGHTS_CONTROLS)
1102 strcpy(m->name, lights_controls[m->index].name);
1111 /******************************************************************************
1115 * V4L2 get the value of a control variable
1117 *****************************************************************************/
1119 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
1121 struct v4l2_control *c = arg;
1124 case V4L2_CID_BRIGHTNESS:
1125 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
1127 c->value = cam->params.color_params.brightness;
1129 case V4L2_CID_CONTRAST:
1130 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
1132 c->value = cam->params.color_params.contrast;
1134 case V4L2_CID_SATURATION:
1135 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
1137 c->value = cam->params.color_params.saturation;
1139 case V4L2_CID_HFLIP:
1140 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1142 c->value = (cam->params.vp_params.user_effects &
1143 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
1145 case V4L2_CID_VFLIP:
1146 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1148 c->value = (cam->params.vp_params.user_effects &
1149 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
1151 case CPIA2_CID_TARGET_KB:
1152 c->value = cam->params.vc_params.target_kb;
1154 case CPIA2_CID_GPIO:
1155 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1157 c->value = cam->params.vp_params.gpio_data;
1159 case CPIA2_CID_FLICKER_MODE:
1162 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1164 if(cam->params.flicker_control.cam_register &
1165 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
1166 mode = NEVER_FLICKER;
1168 if(cam->params.flicker_control.cam_register &
1169 CPIA2_VP_FLICKER_MODES_50HZ) {
1175 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
1176 if(flicker_controls[i].value == mode) {
1181 if(i == NUM_FLICKER_CONTROLS)
1185 case CPIA2_CID_FRAMERATE:
1187 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1189 for(i=0; i<= maximum; i++) {
1190 if(cam->params.vp_params.frame_rate ==
1191 framerate_controls[i].value)
1199 case CPIA2_CID_USB_ALT:
1200 c->value = cam->params.camera_state.stream_mode;
1202 case CPIA2_CID_LIGHTS:
1205 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1207 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
1208 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
1209 lights_controls[i].value) {
1213 if(i == NUM_LIGHTS_CONTROLS)
1218 case CPIA2_CID_RESET_CAMERA:
1224 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1229 /******************************************************************************
1233 * V4L2 set the value of a control variable
1235 *****************************************************************************/
1237 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1239 struct v4l2_control *c = arg;
1243 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1245 /* Check that the value is in range */
1246 for(i=0; i<NUM_CONTROLS; i++) {
1247 if(c->id == controls[i].id) {
1248 if(c->value < controls[i].minimum ||
1249 c->value > controls[i].maximum) {
1255 if(i == NUM_CONTROLS)
1259 case V4L2_CID_BRIGHTNESS:
1260 cpia2_set_brightness(cam, c->value);
1262 case V4L2_CID_CONTRAST:
1263 cpia2_set_contrast(cam, c->value);
1265 case V4L2_CID_SATURATION:
1266 cpia2_set_saturation(cam, c->value);
1268 case V4L2_CID_HFLIP:
1269 cpia2_set_property_mirror(cam, c->value);
1271 case V4L2_CID_VFLIP:
1272 cpia2_set_property_flip(cam, c->value);
1274 case CPIA2_CID_TARGET_KB:
1275 retval = cpia2_set_target_kb(cam, c->value);
1277 case CPIA2_CID_GPIO:
1278 retval = cpia2_set_gpio(cam, c->value);
1280 case CPIA2_CID_FLICKER_MODE:
1281 retval = cpia2_set_flicker_mode(cam,
1282 flicker_controls[c->value].value);
1284 case CPIA2_CID_FRAMERATE:
1285 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1287 case CPIA2_CID_USB_ALT:
1288 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1290 case CPIA2_CID_LIGHTS:
1291 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1293 case CPIA2_CID_RESET_CAMERA:
1294 cpia2_usb_stream_pause(cam);
1295 cpia2_reset_camera(cam);
1296 cpia2_usb_stream_resume(cam);
1305 /******************************************************************************
1309 * V4L2 get the JPEG compression parameters
1311 *****************************************************************************/
1313 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1315 struct v4l2_jpegcompression *parms = arg;
1317 memset(parms, 0, sizeof(*parms));
1319 parms->quality = 80; // TODO: Can this be made meaningful?
1321 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1322 if(!cam->params.compression.inhibit_htables) {
1323 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1326 parms->APPn = cam->APPn;
1327 parms->APP_len = cam->APP_len;
1328 if(cam->APP_len > 0) {
1329 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1330 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1333 parms->COM_len = cam->COM_len;
1334 if(cam->COM_len > 0) {
1335 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1336 parms->jpeg_markers |= JPEG_MARKER_COM;
1339 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1340 parms->APP_len, parms->COM_len);
1345 /******************************************************************************
1349 * V4L2 set the JPEG compression parameters
1350 * NOTE: quality and some jpeg_markers are ignored.
1352 *****************************************************************************/
1354 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1356 struct v4l2_jpegcompression *parms = arg;
1358 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1359 parms->APP_len, parms->COM_len);
1361 cam->params.compression.inhibit_htables =
1362 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1364 if(parms->APP_len != 0) {
1365 if(parms->APP_len > 0 &&
1366 parms->APP_len <= sizeof(cam->APP_data) &&
1367 parms->APPn >= 0 && parms->APPn <= 15) {
1368 cam->APPn = parms->APPn;
1369 cam->APP_len = parms->APP_len;
1370 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1372 LOG("Bad APPn Params n=%d len=%d\n",
1373 parms->APPn, parms->APP_len);
1380 if(parms->COM_len != 0) {
1381 if(parms->COM_len > 0 &&
1382 parms->COM_len <= sizeof(cam->COM_data)) {
1383 cam->COM_len = parms->COM_len;
1384 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1386 LOG("Bad COM_len=%d\n", parms->COM_len);
1394 /******************************************************************************
1398 * V4L2 Initiate memory mapping.
1399 * NOTE: The user's request is ignored. For now the buffers are fixed.
1401 *****************************************************************************/
1403 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1405 struct v4l2_requestbuffers *req = arg;
1407 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1408 req->memory != V4L2_MEMORY_MMAP)
1411 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1412 req->count = cam->num_frames;
1413 memset(&req->reserved, 0, sizeof(req->reserved));
1418 /******************************************************************************
1422 * V4L2 Query memory buffer status.
1424 *****************************************************************************/
1426 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1428 struct v4l2_buffer *buf = arg;
1430 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1431 buf->index > cam->num_frames)
1434 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1435 buf->length = cam->frame_size;
1437 buf->memory = V4L2_MEMORY_MMAP;
1440 buf->flags = V4L2_BUF_FLAG_MAPPED;
1444 switch (cam->buffers[buf->index].status) {
1449 buf->flags = V4L2_BUF_FLAG_QUEUED;
1452 buf->bytesused = cam->buffers[buf->index].length;
1453 buf->timestamp = cam->buffers[buf->index].timestamp;
1454 buf->sequence = cam->buffers[buf->index].seq;
1455 buf->flags = V4L2_BUF_FLAG_DONE;
1459 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1460 buf->index, buf->m.offset, buf->flags, buf->sequence,
1466 /******************************************************************************
1470 * V4L2 User is freeing buffer
1472 *****************************************************************************/
1474 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1476 struct v4l2_buffer *buf = arg;
1478 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1479 buf->memory != V4L2_MEMORY_MMAP ||
1480 buf->index > cam->num_frames)
1483 DBG("QBUF #%d\n", buf->index);
1485 if(cam->buffers[buf->index].status == FRAME_READY)
1486 cam->buffers[buf->index].status = FRAME_EMPTY;
1491 /******************************************************************************
1493 * find_earliest_filled_buffer
1495 * Helper for ioctl_dqbuf. Find the next ready buffer.
1497 *****************************************************************************/
1499 static int find_earliest_filled_buffer(struct camera_data *cam)
1503 for (i=0; i<cam->num_frames; i++) {
1504 if(cam->buffers[i].status == FRAME_READY) {
1508 /* find which buffer is earlier */
1509 struct timeval *tv1, *tv2;
1510 tv1 = &cam->buffers[i].timestamp;
1511 tv2 = &cam->buffers[found].timestamp;
1512 if(tv1->tv_sec < tv2->tv_sec ||
1513 (tv1->tv_sec == tv2->tv_sec &&
1514 tv1->tv_usec < tv2->tv_usec))
1522 /******************************************************************************
1526 * V4L2 User is asking for a filled buffer.
1528 *****************************************************************************/
1530 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1532 struct v4l2_buffer *buf = arg;
1535 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1536 buf->memory != V4L2_MEMORY_MMAP)
1539 frame = find_earliest_filled_buffer(cam);
1541 if(frame < 0 && file->f_flags&O_NONBLOCK)
1545 /* Wait for a frame to become available */
1546 struct framebuf *cb=cam->curbuff;
1547 up(&cam->busy_lock);
1548 wait_event_interruptible(cam->wq_stream,
1550 (cb=cam->curbuff)->status == FRAME_READY);
1551 down(&cam->busy_lock);
1552 if (signal_pending(current))
1553 return -ERESTARTSYS;
1561 buf->bytesused = cam->buffers[buf->index].length;
1562 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1563 buf->field = V4L2_FIELD_NONE;
1564 buf->timestamp = cam->buffers[buf->index].timestamp;
1565 buf->sequence = cam->buffers[buf->index].seq;
1566 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1567 buf->length = cam->frame_size;
1570 memset(&buf->timecode, 0, sizeof(buf->timecode));
1572 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1573 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1578 /******************************************************************************
1582 *****************************************************************************/
1583 static int cpia2_do_ioctl(struct inode *inode, struct file *file,
1584 unsigned int ioctl_nr, void *arg)
1586 struct video_device *dev = video_devdata(file);
1587 struct camera_data *cam = video_get_drvdata(dev);
1593 /* make this _really_ smp-safe */
1594 if (down_interruptible(&cam->busy_lock))
1595 return -ERESTARTSYS;
1597 if (!cam->present) {
1598 up(&cam->busy_lock);
1602 /* Priority check */
1605 case VIDIOCMCAPTURE:
1608 struct cpia2_fh *fh = file->private_data;
1609 retval = v4l2_prio_check(&cam->prio, &fh->prio);
1611 up(&cam->busy_lock);
1619 struct cpia2_fh *fh = file->private_data;
1620 if(fh->prio != V4L2_PRIORITY_RECORD) {
1621 up(&cam->busy_lock);
1631 case VIDIOCGCAP: /* query capabilities */
1632 retval = ioctl_cap_query(arg, cam);
1635 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
1636 retval = ioctl_get_channel(arg);
1638 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
1639 retval = ioctl_set_channel(arg);
1641 case VIDIOCGPICT: /* image properties */
1642 memcpy(arg, &cam->vp, sizeof(struct video_picture));
1645 retval = ioctl_set_image_prop(arg, cam);
1647 case VIDIOCGWIN: /* get/set capture window */
1648 memcpy(arg, &cam->vw, sizeof(struct video_window));
1651 retval = ioctl_set_window_size(arg, cam, file->private_data);
1653 case VIDIOCGMBUF: /* mmap interface */
1654 retval = ioctl_get_mbuf(arg, cam);
1656 case VIDIOCMCAPTURE:
1657 retval = ioctl_mcapture(arg, cam, file->private_data);
1660 retval = ioctl_sync(arg, cam);
1662 /* pointless to implement overlay with this camera */
1670 /* tuner interface - we have none */
1678 /* audio interface - we have none */
1684 /* CPIA2 extension to Video4Linux API */
1685 case CPIA2_IOC_SET_GPIO:
1686 retval = ioctl_set_gpio(arg, cam);
1688 case VIDIOC_QUERYCAP:
1689 retval = ioctl_querycap(arg,cam);
1692 case VIDIOC_ENUMINPUT:
1693 case VIDIOC_G_INPUT:
1694 case VIDIOC_S_INPUT:
1695 retval = ioctl_input(ioctl_nr, arg,cam);
1698 case VIDIOC_ENUM_FMT:
1699 retval = ioctl_enum_fmt(arg,cam);
1701 case VIDIOC_TRY_FMT:
1702 retval = ioctl_try_fmt(arg,cam);
1705 retval = ioctl_get_fmt(arg,cam);
1708 retval = ioctl_set_fmt(arg,cam,file->private_data);
1711 case VIDIOC_CROPCAP:
1712 retval = ioctl_cropcap(arg,cam);
1716 // TODO: I think cropping can be implemented - SJB
1720 case VIDIOC_QUERYCTRL:
1721 retval = ioctl_queryctrl(arg,cam);
1723 case VIDIOC_QUERYMENU:
1724 retval = ioctl_querymenu(arg,cam);
1727 retval = ioctl_g_ctrl(arg,cam);
1730 retval = ioctl_s_ctrl(arg,cam);
1733 case VIDIOC_G_JPEGCOMP:
1734 retval = ioctl_g_jpegcomp(arg,cam);
1736 case VIDIOC_S_JPEGCOMP:
1737 retval = ioctl_s_jpegcomp(arg,cam);
1740 case VIDIOC_G_PRIORITY:
1742 struct cpia2_fh *fh = file->private_data;
1743 *(enum v4l2_priority*)arg = fh->prio;
1746 case VIDIOC_S_PRIORITY:
1748 struct cpia2_fh *fh = file->private_data;
1749 enum v4l2_priority prio;
1750 prio = *(enum v4l2_priority*)arg;
1751 if(cam->streaming &&
1753 fh->prio == V4L2_PRIORITY_RECORD) {
1754 /* Can't drop record priority while streaming */
1756 } else if(prio == V4L2_PRIORITY_RECORD &&
1758 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1759 /* Only one program can record at a time */
1762 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1767 case VIDIOC_REQBUFS:
1768 retval = ioctl_reqbufs(arg,cam);
1770 case VIDIOC_QUERYBUF:
1771 retval = ioctl_querybuf(arg,cam);
1774 retval = ioctl_qbuf(arg,cam);
1777 retval = ioctl_dqbuf(arg,cam,file);
1779 case VIDIOC_STREAMON:
1782 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1784 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1787 if(!cam->streaming) {
1788 retval = cpia2_usb_stream_start(cam,
1789 cam->params.camera_state.stream_mode);
1796 case VIDIOC_STREAMOFF:
1799 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1801 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1804 if(cam->streaming) {
1805 retval = cpia2_usb_stream_stop(cam);
1813 case VIDIOC_ENUMOUTPUT:
1814 case VIDIOC_G_OUTPUT:
1815 case VIDIOC_S_OUTPUT:
1816 case VIDIOC_G_MODULATOR:
1817 case VIDIOC_S_MODULATOR:
1819 case VIDIOC_ENUMAUDIO:
1820 case VIDIOC_G_AUDIO:
1821 case VIDIOC_S_AUDIO:
1823 case VIDIOC_ENUMAUDOUT:
1824 case VIDIOC_G_AUDOUT:
1825 case VIDIOC_S_AUDOUT:
1827 case VIDIOC_ENUMSTD:
1828 case VIDIOC_QUERYSTD:
1832 case VIDIOC_G_TUNER:
1833 case VIDIOC_S_TUNER:
1834 case VIDIOC_G_FREQUENCY:
1835 case VIDIOC_S_FREQUENCY:
1837 case VIDIOC_OVERLAY:
1846 retval = -ENOIOCTLCMD;
1850 up(&cam->busy_lock);
1854 static int cpia2_ioctl(struct inode *inode, struct file *file,
1855 unsigned int ioctl_nr, unsigned long iarg)
1857 return video_usercopy(inode, file, ioctl_nr, iarg, cpia2_do_ioctl);
1860 /******************************************************************************
1864 *****************************************************************************/
1865 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1868 struct video_device *dev = video_devdata(file);
1869 struct camera_data *cam = video_get_drvdata(dev);
1871 /* Priority check */
1872 struct cpia2_fh *fh = file->private_data;
1873 if(fh->prio != V4L2_PRIORITY_RECORD) {
1877 retval = cpia2_remap_buffer(cam, area);
1884 /******************************************************************************
1886 * reset_camera_struct_v4l
1888 * Sets all values to the defaults
1889 *****************************************************************************/
1890 static void reset_camera_struct_v4l(struct camera_data *cam)
1893 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1894 * Ioctl. Here, just do the window and picture stucts.
1896 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
1897 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
1898 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
1899 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
1903 cam->vw.width = cam->params.roi.width;
1904 cam->vw.height = cam->params.roi.height;
1906 cam->vw.clipcount = 0;
1908 cam->frame_size = buffer_size;
1909 cam->num_frames = num_buffers;
1912 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1913 cam->params.flicker_control.mains_frequency = flicker_freq;
1916 cam->params.camera_state.stream_mode = alternate;
1918 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1919 v4l2_prio_init(&cam->prio);
1924 * The v4l video device structure initialized for this device
1926 static struct file_operations fops_template = {
1927 .owner= THIS_MODULE,
1929 .release= cpia2_close,
1930 .read= cpia2_v4l_read,
1931 .poll= cpia2_v4l_poll,
1932 .ioctl= cpia2_ioctl,
1937 static struct video_device cpia2_template = {
1938 /* I could not find any place for the old .initialize initializer?? */
1939 .owner= THIS_MODULE,
1940 .name= "CPiA2 Camera",
1941 .type= VID_TYPE_CAPTURE,
1942 .type2 = V4L2_CAP_VIDEO_CAPTURE |
1944 .hardware= VID_HARDWARE_CPIA2,
1946 .fops= &fops_template,
1947 .release= video_device_release,
1950 /******************************************************************************
1952 * cpia2_register_camera
1954 *****************************************************************************/
1955 int cpia2_register_camera(struct camera_data *cam)
1957 cam->vdev = video_device_alloc();
1961 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1962 video_set_drvdata(cam->vdev, cam);
1964 reset_camera_struct_v4l(cam);
1966 /* register v4l device */
1967 if (video_register_device
1968 (cam->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1969 ERR("video_register_device failed\n");
1970 video_device_release(cam->vdev);
1977 /******************************************************************************
1979 * cpia2_unregister_camera
1981 *****************************************************************************/
1982 void cpia2_unregister_camera(struct camera_data *cam)
1984 if (!cam->open_count) {
1985 video_unregister_device(cam->vdev);
1987 LOG("/dev/video%d removed while open, "
1988 "deferring video_unregister_device\n",
1993 /******************************************************************************
1997 * Make sure that all user-supplied parameters are sensible
1998 *****************************************************************************/
1999 static void __init check_parameters(void)
2001 if(buffer_size < PAGE_SIZE) {
2002 buffer_size = PAGE_SIZE;
2003 LOG("buffer_size too small, setting to %d\n", buffer_size);
2004 } else if(buffer_size > 1024*1024) {
2005 /* arbitrary upper limiit */
2006 buffer_size = 1024*1024;
2007 LOG("buffer_size ridiculously large, setting to %d\n",
2010 buffer_size += PAGE_SIZE-1;
2011 buffer_size &= ~(PAGE_SIZE-1);
2014 if(num_buffers < 1) {
2016 LOG("num_buffers too small, setting to %d\n", num_buffers);
2017 } else if(num_buffers > VIDEO_MAX_FRAME) {
2018 num_buffers = VIDEO_MAX_FRAME;
2019 LOG("num_buffers too large, setting to %d\n", num_buffers);
2022 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
2023 alternate = DEFAULT_ALT;
2024 LOG("alternate specified is invalid, using %d\n", alternate);
2027 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
2028 flicker_mode = NEVER_FLICKER;
2029 LOG("Flicker mode specified is invalid, using %d\n",
2033 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
2034 flicker_freq = FLICKER_60;
2035 LOG("Flicker mode specified is invalid, using %d\n",
2039 if(video_nr < -1 || video_nr > 64) {
2041 LOG("invalid video_nr specified, must be -1 to 64\n");
2044 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2045 num_buffers, buffer_size, alternate);
2048 /************ Module Stuff ***************/
2051 /******************************************************************************
2053 * cpia2_init/module_init
2055 *****************************************************************************/
2056 static int __init cpia2_init(void)
2058 LOG("%s v%d.%d.%d\n",
2059 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
2066 /******************************************************************************
2068 * cpia2_exit/module_exit
2070 *****************************************************************************/
2071 static void __exit cpia2_exit(void)
2073 cpia2_usb_cleanup();
2074 schedule_timeout(2 * HZ);
2077 module_init(cpia2_init);
2078 module_exit(cpia2_exit);