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@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/videodev2.h>
41 #include <linux/stringify.h>
42 #include <media/v4l2-ioctl.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 "
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 {
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[] =
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",
128 .default_value = DEFAULT_BRIGHTNESS,
131 .id = V4L2_CID_CONTRAST,
132 .type = V4L2_CTRL_TYPE_INTEGER,
137 .default_value = DEFAULT_CONTRAST,
140 .id = V4L2_CID_SATURATION,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Saturation",
146 .default_value = DEFAULT_SATURATION,
149 .id = V4L2_CID_HFLIP,
150 .type = V4L2_CTRL_TYPE_BOOLEAN,
151 .name = "Mirror Horizontally",
158 .id = V4L2_CID_VFLIP,
159 .type = V4L2_CTRL_TYPE_BOOLEAN,
160 .name = "Flip Vertically",
167 .id = CPIA2_CID_TARGET_KB,
168 .type = V4L2_CTRL_TYPE_INTEGER,
173 .default_value = DEFAULT_TARGET_KB,
176 .id = CPIA2_CID_GPIO,
177 .type = V4L2_CTRL_TYPE_INTEGER,
185 .id = CPIA2_CID_FLICKER_MODE,
186 .type = V4L2_CTRL_TYPE_MENU,
187 .name = "Flicker Reduction",
189 .maximum = NUM_FLICKER_CONTROLS-1,
194 .id = CPIA2_CID_FRAMERATE,
195 .type = V4L2_CTRL_TYPE_MENU,
198 .maximum = NUM_FRAMERATE_CONTROLS-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,
209 .default_value = DEFAULT_ALT,
212 .id = CPIA2_CID_LIGHTS,
213 .type = V4L2_CTRL_TYPE_MENU,
216 .maximum = NUM_LIGHTS_CONTROLS-1,
221 .id = CPIA2_CID_RESET_CAMERA,
222 .type = V4L2_CTRL_TYPE_BUTTON,
223 .name = "Reset Camera",
230 #define NUM_CONTROLS (ARRAY_SIZE(controls))
233 /******************************************************************************
237 *****************************************************************************/
238 static int cpia2_open(struct file *file)
240 struct camera_data *cam = video_drvdata(file);
244 ERR("Internal error, camera_data not found!\n");
248 if(mutex_lock_interruptible(&cam->busy_lock))
256 if (cam->open_count > 0) {
260 if (cpia2_allocate_buffers(cam)) {
265 /* reset the camera */
266 if (cpia2_reset_camera(cam) < 0) {
276 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
281 file->private_data = fh;
282 fh->prio = V4L2_PRIORITY_UNSET;
283 v4l2_prio_open(&cam->prio, &fh->prio);
289 cpia2_dbg_dump_registers(cam);
292 mutex_unlock(&cam->busy_lock);
296 /******************************************************************************
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);
310 (cam->open_count == 1
311 || fh->prio == V4L2_PRIORITY_RECORD
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);
327 v4l2_prio_close(&cam->prio, fh->prio);
328 file->private_data = NULL;
332 if (--cam->open_count == 0) {
333 cpia2_free_buffers(cam);
335 video_unregister_device(dev);
336 mutex_unlock(&cam->busy_lock);
342 mutex_unlock(&cam->busy_lock);
347 /******************************************************************************
351 *****************************************************************************/
352 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
355 struct camera_data *cam = video_drvdata(file);
356 int noblock = file->f_flags&O_NONBLOCK;
358 struct cpia2_fh *fh = file->private_data;
364 if(fh->prio != V4L2_PRIORITY_RECORD) {
368 return cpia2_read(cam, buf, count, noblock);
372 /******************************************************************************
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;
386 if(fh->prio != V4L2_PRIORITY_RECORD) {
390 return cpia2_poll(cam, filp, wait);
394 static int sync(struct camera_data *cam, int frame_nr)
396 struct framebuf *frame = &cam->buffers[frame_nr];
399 if (frame->status == FRAME_READY)
402 if (!cam->streaming) {
403 frame->status = FRAME_READY;
408 mutex_unlock(&cam->busy_lock);
409 wait_event_interruptible(cam->wq_stream,
411 frame->status == FRAME_READY);
412 mutex_lock(&cam->busy_lock);
413 if (signal_pending(current))
420 /******************************************************************************
424 *****************************************************************************/
426 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
430 gpio_val = *(__u32*) arg;
432 if (gpio_val &~ 0xFFU)
435 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
438 /******************************************************************************
442 * V4L2 device capabilities
444 *****************************************************************************/
446 static int ioctl_querycap(void *arg, struct camera_data *cam)
448 struct v4l2_capability *vc = arg;
450 memset(vc, 0, sizeof(*vc));
451 strcpy(vc->driver, "cpia2");
453 if (cam->params.pnp_id.product == 0x151)
454 strcpy(vc->card, "QX5 Microscope");
456 strcpy(vc->card, "CPiA2 Camera");
457 switch (cam->params.pnp_id.device_type) {
459 strcat(vc->card, " (672/");
462 strcat(vc->card, " (676/");
465 strcat(vc->card, " (???/");
468 switch (cam->params.version.sensor_flags) {
469 case CPIA2_VP_SENSOR_FLAGS_404:
470 strcat(vc->card, "404)");
472 case CPIA2_VP_SENSOR_FLAGS_407:
473 strcat(vc->card, "407)");
475 case CPIA2_VP_SENSOR_FLAGS_409:
476 strcat(vc->card, "409)");
478 case CPIA2_VP_SENSOR_FLAGS_410:
479 strcat(vc->card, "410)");
481 case CPIA2_VP_SENSOR_FLAGS_500:
482 strcat(vc->card, "500)");
485 strcat(vc->card, "???)");
489 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
490 memset(vc->bus_info,0, sizeof(vc->bus_info));
492 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
495 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
502 /******************************************************************************
506 * V4L2 input get/set/enumerate
508 *****************************************************************************/
510 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
512 struct v4l2_input *i = arg;
514 if(ioclt_nr != VIDIOC_G_INPUT) {
519 memset(i, 0, sizeof(*i));
520 strcpy(i->name, "Camera");
521 i->type = V4L2_INPUT_TYPE_CAMERA;
526 /******************************************************************************
530 * V4L2 format enumerate
532 *****************************************************************************/
534 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
536 struct v4l2_fmtdesc *f = arg;
537 int index = f->index;
539 if (index < 0 || index > 1)
542 memset(f, 0, sizeof(*f));
544 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
545 f->flags = V4L2_FMT_FLAG_COMPRESSED;
548 strcpy(f->description, "MJPEG");
549 f->pixelformat = V4L2_PIX_FMT_MJPEG;
552 strcpy(f->description, "JPEG");
553 f->pixelformat = V4L2_PIX_FMT_JPEG;
562 /******************************************************************************
568 *****************************************************************************/
570 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
572 struct v4l2_format *f = arg;
574 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
577 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
578 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
581 f->fmt.pix.field = V4L2_FIELD_NONE;
582 f->fmt.pix.bytesperline = 0;
583 f->fmt.pix.sizeimage = cam->frame_size;
584 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
587 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
589 f->fmt.pix.width = 640;
590 f->fmt.pix.height = 480;
593 f->fmt.pix.width = 352;
594 f->fmt.pix.height = 288;
597 f->fmt.pix.width = 320;
598 f->fmt.pix.height = 240;
600 case VIDEOSIZE_288_216:
601 f->fmt.pix.width = 288;
602 f->fmt.pix.height = 216;
604 case VIDEOSIZE_256_192:
605 f->fmt.pix.width = 256;
606 f->fmt.pix.height = 192;
608 case VIDEOSIZE_224_168:
609 f->fmt.pix.width = 224;
610 f->fmt.pix.height = 168;
612 case VIDEOSIZE_192_144:
613 f->fmt.pix.width = 192;
614 f->fmt.pix.height = 144;
618 f->fmt.pix.width = 176;
619 f->fmt.pix.height = 144;
626 /******************************************************************************
632 *****************************************************************************/
634 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
636 struct v4l2_format *f = arg;
639 err = ioctl_try_fmt(arg, cam);
643 /* Ensure that only this process can change the format. */
644 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
649 cam->pixelformat = f->fmt.pix.pixelformat;
651 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
652 * the missing Huffman table properly. */
653 cam->params.compression.inhibit_htables = 0;
654 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
656 /* we set the video window to something smaller or equal to what
657 * is requested by the user???
659 DBG("Requested width = %d, height = %d\n",
660 f->fmt.pix.width, f->fmt.pix.height);
661 if (f->fmt.pix.width != cam->width ||
662 f->fmt.pix.height != cam->height) {
663 cam->width = f->fmt.pix.width;
664 cam->height = f->fmt.pix.height;
665 cam->params.roi.width = f->fmt.pix.width;
666 cam->params.roi.height = f->fmt.pix.height;
667 cpia2_set_format(cam);
670 for (frame = 0; frame < cam->num_frames; ++frame) {
671 if (cam->buffers[frame].status == FRAME_READING)
672 if ((err = sync(cam, frame)) < 0)
675 cam->buffers[frame].status = FRAME_EMPTY;
681 /******************************************************************************
687 *****************************************************************************/
689 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
691 struct v4l2_format *f = arg;
693 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
696 f->fmt.pix.width = cam->width;
697 f->fmt.pix.height = cam->height;
698 f->fmt.pix.pixelformat = cam->pixelformat;
699 f->fmt.pix.field = V4L2_FIELD_NONE;
700 f->fmt.pix.bytesperline = 0;
701 f->fmt.pix.sizeimage = cam->frame_size;
702 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
708 /******************************************************************************
712 * V4L2 query cropping capabilities
713 * NOTE: cropping is currently disabled
715 *****************************************************************************/
717 static int ioctl_cropcap(void *arg,struct camera_data *cam)
719 struct v4l2_cropcap *c = arg;
721 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
726 c->bounds.width = cam->width;
727 c->bounds.height = cam->height;
730 c->defrect.width = cam->width;
731 c->defrect.height = cam->height;
732 c->pixelaspect.numerator = 1;
733 c->pixelaspect.denominator = 1;
738 /******************************************************************************
742 * V4L2 query possible control variables
744 *****************************************************************************/
746 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
748 struct v4l2_queryctrl *c = arg;
751 for(i=0; i<NUM_CONTROLS; ++i) {
752 if(c->id == controls[i].id) {
753 memcpy(c, controls+i, sizeof(*c));
758 if(i == NUM_CONTROLS)
761 /* Some devices have additional limitations */
763 case V4L2_CID_BRIGHTNESS:
765 * Don't let the register be set to zero - bug in VP4
766 * flash of full brightness
768 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
773 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
774 c->flags |= V4L2_CTRL_FLAG_DISABLED;
776 case CPIA2_CID_FRAMERATE:
777 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
778 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
780 for(i=0; i<c->maximum; ++i) {
781 if(framerate_controls[i].value ==
782 CPIA2_VP_FRAMERATE_15) {
784 c->default_value = i;
789 case CPIA2_CID_FLICKER_MODE:
790 // Flicker control only valid for 672.
791 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
792 c->flags |= V4L2_CTRL_FLAG_DISABLED;
794 case CPIA2_CID_LIGHTS:
795 // Light control only valid for the QX5 Microscope.
796 if(cam->params.pnp_id.product != 0x151)
797 c->flags |= V4L2_CTRL_FLAG_DISABLED;
806 /******************************************************************************
810 * V4L2 query possible control variables
812 *****************************************************************************/
814 static int ioctl_querymenu(void *arg,struct camera_data *cam)
816 struct v4l2_querymenu *m = arg;
818 memset(m->name, 0, sizeof(m->name));
822 case CPIA2_CID_FLICKER_MODE:
823 if (m->index >= NUM_FLICKER_CONTROLS)
826 strcpy(m->name, flicker_controls[m->index].name);
828 case CPIA2_CID_FRAMERATE:
830 int maximum = NUM_FRAMERATE_CONTROLS - 1;
831 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
832 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
835 for(i=0; i<maximum; ++i) {
836 if(framerate_controls[i].value ==
837 CPIA2_VP_FRAMERATE_15)
841 if (m->index > maximum)
844 strcpy(m->name, framerate_controls[m->index].name);
847 case CPIA2_CID_LIGHTS:
848 if (m->index >= NUM_LIGHTS_CONTROLS)
851 strcpy(m->name, lights_controls[m->index].name);
860 /******************************************************************************
864 * V4L2 get the value of a control variable
866 *****************************************************************************/
868 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
870 struct v4l2_control *c = arg;
873 case V4L2_CID_BRIGHTNESS:
874 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
876 c->value = cam->params.color_params.brightness;
878 case V4L2_CID_CONTRAST:
879 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
881 c->value = cam->params.color_params.contrast;
883 case V4L2_CID_SATURATION:
884 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
886 c->value = cam->params.color_params.saturation;
889 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
891 c->value = (cam->params.vp_params.user_effects &
892 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
895 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
897 c->value = (cam->params.vp_params.user_effects &
898 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
900 case CPIA2_CID_TARGET_KB:
901 c->value = cam->params.vc_params.target_kb;
904 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
906 c->value = cam->params.vp_params.gpio_data;
908 case CPIA2_CID_FLICKER_MODE:
911 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
913 if(cam->params.flicker_control.cam_register &
914 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
915 mode = NEVER_FLICKER;
917 if(cam->params.flicker_control.cam_register &
918 CPIA2_VP_FLICKER_MODES_50HZ) {
924 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
925 if(flicker_controls[i].value == mode) {
930 if(i == NUM_FLICKER_CONTROLS)
934 case CPIA2_CID_FRAMERATE:
936 int maximum = NUM_FRAMERATE_CONTROLS - 1;
938 for(i=0; i<= maximum; i++) {
939 if(cam->params.vp_params.frame_rate ==
940 framerate_controls[i].value)
948 case CPIA2_CID_USB_ALT:
949 c->value = cam->params.camera_state.stream_mode;
951 case CPIA2_CID_LIGHTS:
954 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
956 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
957 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
958 lights_controls[i].value) {
962 if(i == NUM_LIGHTS_CONTROLS)
967 case CPIA2_CID_RESET_CAMERA:
973 DBG("Get control id:%d, value:%d\n", c->id, c->value);
978 /******************************************************************************
982 * V4L2 set the value of a control variable
984 *****************************************************************************/
986 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
988 struct v4l2_control *c = arg;
992 DBG("Set control id:%d, value:%d\n", c->id, c->value);
994 /* Check that the value is in range */
995 for(i=0; i<NUM_CONTROLS; i++) {
996 if(c->id == controls[i].id) {
997 if(c->value < controls[i].minimum ||
998 c->value > controls[i].maximum) {
1004 if(i == NUM_CONTROLS)
1008 case V4L2_CID_BRIGHTNESS:
1009 cpia2_set_brightness(cam, c->value);
1011 case V4L2_CID_CONTRAST:
1012 cpia2_set_contrast(cam, c->value);
1014 case V4L2_CID_SATURATION:
1015 cpia2_set_saturation(cam, c->value);
1017 case V4L2_CID_HFLIP:
1018 cpia2_set_property_mirror(cam, c->value);
1020 case V4L2_CID_VFLIP:
1021 cpia2_set_property_flip(cam, c->value);
1023 case CPIA2_CID_TARGET_KB:
1024 retval = cpia2_set_target_kb(cam, c->value);
1026 case CPIA2_CID_GPIO:
1027 retval = cpia2_set_gpio(cam, c->value);
1029 case CPIA2_CID_FLICKER_MODE:
1030 retval = cpia2_set_flicker_mode(cam,
1031 flicker_controls[c->value].value);
1033 case CPIA2_CID_FRAMERATE:
1034 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1036 case CPIA2_CID_USB_ALT:
1037 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1039 case CPIA2_CID_LIGHTS:
1040 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1042 case CPIA2_CID_RESET_CAMERA:
1043 cpia2_usb_stream_pause(cam);
1044 cpia2_reset_camera(cam);
1045 cpia2_usb_stream_resume(cam);
1054 /******************************************************************************
1058 * V4L2 get the JPEG compression parameters
1060 *****************************************************************************/
1062 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1064 struct v4l2_jpegcompression *parms = arg;
1066 memset(parms, 0, sizeof(*parms));
1068 parms->quality = 80; // TODO: Can this be made meaningful?
1070 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1071 if(!cam->params.compression.inhibit_htables) {
1072 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1075 parms->APPn = cam->APPn;
1076 parms->APP_len = cam->APP_len;
1077 if(cam->APP_len > 0) {
1078 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1079 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1082 parms->COM_len = cam->COM_len;
1083 if(cam->COM_len > 0) {
1084 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1085 parms->jpeg_markers |= JPEG_MARKER_COM;
1088 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1089 parms->APP_len, parms->COM_len);
1094 /******************************************************************************
1098 * V4L2 set the JPEG compression parameters
1099 * NOTE: quality and some jpeg_markers are ignored.
1101 *****************************************************************************/
1103 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1105 struct v4l2_jpegcompression *parms = arg;
1107 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1108 parms->APP_len, parms->COM_len);
1110 cam->params.compression.inhibit_htables =
1111 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1113 if(parms->APP_len != 0) {
1114 if(parms->APP_len > 0 &&
1115 parms->APP_len <= sizeof(cam->APP_data) &&
1116 parms->APPn >= 0 && parms->APPn <= 15) {
1117 cam->APPn = parms->APPn;
1118 cam->APP_len = parms->APP_len;
1119 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1121 LOG("Bad APPn Params n=%d len=%d\n",
1122 parms->APPn, parms->APP_len);
1129 if(parms->COM_len != 0) {
1130 if(parms->COM_len > 0 &&
1131 parms->COM_len <= sizeof(cam->COM_data)) {
1132 cam->COM_len = parms->COM_len;
1133 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1135 LOG("Bad COM_len=%d\n", parms->COM_len);
1143 /******************************************************************************
1147 * V4L2 Initiate memory mapping.
1148 * NOTE: The user's request is ignored. For now the buffers are fixed.
1150 *****************************************************************************/
1152 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1154 struct v4l2_requestbuffers *req = arg;
1156 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1157 req->memory != V4L2_MEMORY_MMAP)
1160 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1161 req->count = cam->num_frames;
1162 memset(&req->reserved, 0, sizeof(req->reserved));
1167 /******************************************************************************
1171 * V4L2 Query memory buffer status.
1173 *****************************************************************************/
1175 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1177 struct v4l2_buffer *buf = arg;
1179 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1180 buf->index > cam->num_frames)
1183 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1184 buf->length = cam->frame_size;
1186 buf->memory = V4L2_MEMORY_MMAP;
1189 buf->flags = V4L2_BUF_FLAG_MAPPED;
1193 switch (cam->buffers[buf->index].status) {
1198 buf->flags = V4L2_BUF_FLAG_QUEUED;
1201 buf->bytesused = cam->buffers[buf->index].length;
1202 buf->timestamp = cam->buffers[buf->index].timestamp;
1203 buf->sequence = cam->buffers[buf->index].seq;
1204 buf->flags = V4L2_BUF_FLAG_DONE;
1208 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1209 buf->index, buf->m.offset, buf->flags, buf->sequence,
1215 /******************************************************************************
1219 * V4L2 User is freeing buffer
1221 *****************************************************************************/
1223 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1225 struct v4l2_buffer *buf = arg;
1227 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1228 buf->memory != V4L2_MEMORY_MMAP ||
1229 buf->index > cam->num_frames)
1232 DBG("QBUF #%d\n", buf->index);
1234 if(cam->buffers[buf->index].status == FRAME_READY)
1235 cam->buffers[buf->index].status = FRAME_EMPTY;
1240 /******************************************************************************
1242 * find_earliest_filled_buffer
1244 * Helper for ioctl_dqbuf. Find the next ready buffer.
1246 *****************************************************************************/
1248 static int find_earliest_filled_buffer(struct camera_data *cam)
1252 for (i=0; i<cam->num_frames; i++) {
1253 if(cam->buffers[i].status == FRAME_READY) {
1257 /* find which buffer is earlier */
1258 struct timeval *tv1, *tv2;
1259 tv1 = &cam->buffers[i].timestamp;
1260 tv2 = &cam->buffers[found].timestamp;
1261 if(tv1->tv_sec < tv2->tv_sec ||
1262 (tv1->tv_sec == tv2->tv_sec &&
1263 tv1->tv_usec < tv2->tv_usec))
1271 /******************************************************************************
1275 * V4L2 User is asking for a filled buffer.
1277 *****************************************************************************/
1279 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1281 struct v4l2_buffer *buf = arg;
1284 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1285 buf->memory != V4L2_MEMORY_MMAP)
1288 frame = find_earliest_filled_buffer(cam);
1290 if(frame < 0 && file->f_flags&O_NONBLOCK)
1294 /* Wait for a frame to become available */
1295 struct framebuf *cb=cam->curbuff;
1296 mutex_unlock(&cam->busy_lock);
1297 wait_event_interruptible(cam->wq_stream,
1299 (cb=cam->curbuff)->status == FRAME_READY);
1300 mutex_lock(&cam->busy_lock);
1301 if (signal_pending(current))
1302 return -ERESTARTSYS;
1310 buf->bytesused = cam->buffers[buf->index].length;
1311 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1312 buf->field = V4L2_FIELD_NONE;
1313 buf->timestamp = cam->buffers[buf->index].timestamp;
1314 buf->sequence = cam->buffers[buf->index].seq;
1315 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1316 buf->length = cam->frame_size;
1319 memset(&buf->timecode, 0, sizeof(buf->timecode));
1321 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1322 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1327 /******************************************************************************
1331 *****************************************************************************/
1332 static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1334 struct camera_data *cam = video_drvdata(file);
1340 /* make this _really_ smp-safe */
1341 if (mutex_lock_interruptible(&cam->busy_lock))
1342 return -ERESTARTSYS;
1344 if (!cam->present) {
1345 mutex_unlock(&cam->busy_lock);
1349 /* Priority check */
1353 struct cpia2_fh *fh = file->private_data;
1354 retval = v4l2_prio_check(&cam->prio, fh->prio);
1356 mutex_unlock(&cam->busy_lock);
1366 /* CPIA2 extension to Video4Linux API */
1367 case CPIA2_IOC_SET_GPIO:
1368 retval = ioctl_set_gpio(arg, cam);
1370 case VIDIOC_QUERYCAP:
1371 retval = ioctl_querycap(arg,cam);
1374 case VIDIOC_ENUMINPUT:
1375 case VIDIOC_G_INPUT:
1376 case VIDIOC_S_INPUT:
1377 retval = ioctl_input(cmd, arg, cam);
1380 case VIDIOC_ENUM_FMT:
1381 retval = ioctl_enum_fmt(arg,cam);
1383 case VIDIOC_TRY_FMT:
1384 retval = ioctl_try_fmt(arg,cam);
1387 retval = ioctl_get_fmt(arg,cam);
1390 retval = ioctl_set_fmt(arg,cam,file->private_data);
1393 case VIDIOC_CROPCAP:
1394 retval = ioctl_cropcap(arg,cam);
1398 // TODO: I think cropping can be implemented - SJB
1402 case VIDIOC_QUERYCTRL:
1403 retval = ioctl_queryctrl(arg,cam);
1405 case VIDIOC_QUERYMENU:
1406 retval = ioctl_querymenu(arg,cam);
1409 retval = ioctl_g_ctrl(arg,cam);
1412 retval = ioctl_s_ctrl(arg,cam);
1415 case VIDIOC_G_JPEGCOMP:
1416 retval = ioctl_g_jpegcomp(arg,cam);
1418 case VIDIOC_S_JPEGCOMP:
1419 retval = ioctl_s_jpegcomp(arg,cam);
1422 case VIDIOC_G_PRIORITY:
1424 struct cpia2_fh *fh = file->private_data;
1425 *(enum v4l2_priority*)arg = fh->prio;
1428 case VIDIOC_S_PRIORITY:
1430 struct cpia2_fh *fh = file->private_data;
1431 enum v4l2_priority prio;
1432 prio = *(enum v4l2_priority*)arg;
1433 if(cam->streaming &&
1435 fh->prio == V4L2_PRIORITY_RECORD) {
1436 /* Can't drop record priority while streaming */
1438 } else if(prio == V4L2_PRIORITY_RECORD &&
1440 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1441 /* Only one program can record at a time */
1444 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1449 case VIDIOC_REQBUFS:
1450 retval = ioctl_reqbufs(arg,cam);
1452 case VIDIOC_QUERYBUF:
1453 retval = ioctl_querybuf(arg,cam);
1456 retval = ioctl_qbuf(arg,cam);
1459 retval = ioctl_dqbuf(arg,cam,file);
1461 case VIDIOC_STREAMON:
1464 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1466 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1469 if(!cam->streaming) {
1470 retval = cpia2_usb_stream_start(cam,
1471 cam->params.camera_state.stream_mode);
1478 case VIDIOC_STREAMOFF:
1481 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1483 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1486 if(cam->streaming) {
1487 retval = cpia2_usb_stream_stop(cam);
1495 case VIDIOC_ENUMOUTPUT:
1496 case VIDIOC_G_OUTPUT:
1497 case VIDIOC_S_OUTPUT:
1498 case VIDIOC_G_MODULATOR:
1499 case VIDIOC_S_MODULATOR:
1501 case VIDIOC_ENUMAUDIO:
1502 case VIDIOC_G_AUDIO:
1503 case VIDIOC_S_AUDIO:
1505 case VIDIOC_ENUMAUDOUT:
1506 case VIDIOC_G_AUDOUT:
1507 case VIDIOC_S_AUDOUT:
1509 case VIDIOC_ENUMSTD:
1510 case VIDIOC_QUERYSTD:
1514 case VIDIOC_G_TUNER:
1515 case VIDIOC_S_TUNER:
1516 case VIDIOC_G_FREQUENCY:
1517 case VIDIOC_S_FREQUENCY:
1519 case VIDIOC_OVERLAY:
1528 retval = -ENOIOCTLCMD;
1532 mutex_unlock(&cam->busy_lock);
1536 static long cpia2_ioctl(struct file *file,
1537 unsigned int cmd, unsigned long arg)
1539 return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
1542 /******************************************************************************
1546 *****************************************************************************/
1547 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1549 struct camera_data *cam = video_drvdata(file);
1552 /* Priority check */
1553 struct cpia2_fh *fh = file->private_data;
1554 if(fh->prio != V4L2_PRIORITY_RECORD) {
1558 retval = cpia2_remap_buffer(cam, area);
1565 /******************************************************************************
1567 * reset_camera_struct_v4l
1569 * Sets all values to the defaults
1570 *****************************************************************************/
1571 static void reset_camera_struct_v4l(struct camera_data *cam)
1573 cam->width = cam->params.roi.width;
1574 cam->height = cam->params.roi.height;
1576 cam->frame_size = buffer_size;
1577 cam->num_frames = num_buffers;
1580 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1581 cam->params.flicker_control.mains_frequency = flicker_freq;
1584 cam->params.camera_state.stream_mode = alternate;
1586 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1587 v4l2_prio_init(&cam->prio);
1591 * The v4l video device structure initialized for this device
1593 static const struct v4l2_file_operations cpia2_fops = {
1594 .owner = THIS_MODULE,
1596 .release = cpia2_close,
1597 .read = cpia2_v4l_read,
1598 .poll = cpia2_v4l_poll,
1599 .ioctl = cpia2_ioctl,
1603 static struct video_device cpia2_template = {
1604 /* I could not find any place for the old .initialize initializer?? */
1605 .name = "CPiA2 Camera",
1606 .fops = &cpia2_fops,
1607 .release = video_device_release,
1610 /******************************************************************************
1612 * cpia2_register_camera
1614 *****************************************************************************/
1615 int cpia2_register_camera(struct camera_data *cam)
1617 cam->vdev = video_device_alloc();
1621 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1622 video_set_drvdata(cam->vdev, cam);
1624 reset_camera_struct_v4l(cam);
1626 /* register v4l device */
1627 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1628 ERR("video_register_device failed\n");
1629 video_device_release(cam->vdev);
1636 /******************************************************************************
1638 * cpia2_unregister_camera
1640 *****************************************************************************/
1641 void cpia2_unregister_camera(struct camera_data *cam)
1643 if (!cam->open_count) {
1644 video_unregister_device(cam->vdev);
1646 LOG("%s removed while open, deferring "
1647 "video_unregister_device\n",
1648 video_device_node_name(cam->vdev));
1652 /******************************************************************************
1656 * Make sure that all user-supplied parameters are sensible
1657 *****************************************************************************/
1658 static void __init check_parameters(void)
1660 if(buffer_size < PAGE_SIZE) {
1661 buffer_size = PAGE_SIZE;
1662 LOG("buffer_size too small, setting to %d\n", buffer_size);
1663 } else if(buffer_size > 1024*1024) {
1664 /* arbitrary upper limiit */
1665 buffer_size = 1024*1024;
1666 LOG("buffer_size ridiculously large, setting to %d\n",
1669 buffer_size += PAGE_SIZE-1;
1670 buffer_size &= ~(PAGE_SIZE-1);
1673 if(num_buffers < 1) {
1675 LOG("num_buffers too small, setting to %d\n", num_buffers);
1676 } else if(num_buffers > VIDEO_MAX_FRAME) {
1677 num_buffers = VIDEO_MAX_FRAME;
1678 LOG("num_buffers too large, setting to %d\n", num_buffers);
1681 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1682 alternate = DEFAULT_ALT;
1683 LOG("alternate specified is invalid, using %d\n", alternate);
1686 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1687 flicker_mode = NEVER_FLICKER;
1688 LOG("Flicker mode specified is invalid, using %d\n",
1692 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1693 flicker_freq = FLICKER_60;
1694 LOG("Flicker mode specified is invalid, using %d\n",
1698 if(video_nr < -1 || video_nr > 64) {
1700 LOG("invalid video_nr specified, must be -1 to 64\n");
1703 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1704 num_buffers, buffer_size, alternate);
1707 /************ Module Stuff ***************/
1710 /******************************************************************************
1712 * cpia2_init/module_init
1714 *****************************************************************************/
1715 static int __init cpia2_init(void)
1717 LOG("%s v%d.%d.%d\n",
1718 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
1725 /******************************************************************************
1727 * cpia2_exit/module_exit
1729 *****************************************************************************/
1730 static void __exit cpia2_exit(void)
1732 cpia2_usb_cleanup();
1733 schedule_timeout(2 * HZ);
1736 module_init(cpia2_init);
1737 module_exit(cpia2_exit);