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");
251 if (cam->open_count == 0) {
252 if (cpia2_allocate_buffers(cam))
255 /* reset the camera */
256 if (cpia2_reset_camera(cam) < 0)
263 fh = kmalloc(sizeof(*fh), GFP_KERNEL);
266 file->private_data = fh;
267 fh->prio = V4L2_PRIORITY_UNSET;
268 v4l2_prio_open(&cam->prio, &fh->prio);
273 cpia2_dbg_dump_registers(cam);
277 /******************************************************************************
281 *****************************************************************************/
282 static int cpia2_close(struct file *file)
284 struct video_device *dev = video_devdata(file);
285 struct camera_data *cam = video_get_drvdata(dev);
286 struct cpia2_fh *fh = file->private_data;
289 (cam->open_count == 1 || fh->prio == V4L2_PRIORITY_RECORD)) {
290 cpia2_usb_stream_stop(cam);
292 if (cam->open_count == 1) {
293 /* save camera state for later open */
294 cpia2_save_camera_state(cam);
296 cpia2_set_low_power(cam);
297 cpia2_free_buffers(cam);
303 v4l2_prio_close(&cam->prio, fh->prio);
304 file->private_data = NULL;
307 if (--cam->open_count == 0) {
308 cpia2_free_buffers(cam);
310 video_unregister_device(dev);
319 /******************************************************************************
323 *****************************************************************************/
324 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
327 struct camera_data *cam = video_drvdata(file);
328 int noblock = file->f_flags&O_NONBLOCK;
330 struct cpia2_fh *fh = file->private_data;
336 if(fh->prio != V4L2_PRIORITY_RECORD) {
340 return cpia2_read(cam, buf, count, noblock);
344 /******************************************************************************
348 *****************************************************************************/
349 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
351 struct camera_data *cam = video_drvdata(filp);
352 struct cpia2_fh *fh = filp->private_data;
358 if(fh->prio != V4L2_PRIORITY_RECORD) {
362 return cpia2_poll(cam, filp, wait);
366 static int sync(struct camera_data *cam, int frame_nr)
368 struct framebuf *frame = &cam->buffers[frame_nr];
371 if (frame->status == FRAME_READY)
374 if (!cam->streaming) {
375 frame->status = FRAME_READY;
380 mutex_unlock(&cam->v4l2_lock);
381 wait_event_interruptible(cam->wq_stream,
383 frame->status == FRAME_READY);
384 mutex_lock(&cam->v4l2_lock);
385 if (signal_pending(current))
392 /******************************************************************************
396 *****************************************************************************/
398 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
402 gpio_val = *(__u32*) arg;
404 if (gpio_val &~ 0xFFU)
407 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
410 /******************************************************************************
414 * V4L2 device capabilities
416 *****************************************************************************/
418 static int ioctl_querycap(void *arg, struct camera_data *cam)
420 struct v4l2_capability *vc = arg;
422 memset(vc, 0, sizeof(*vc));
423 strcpy(vc->driver, "cpia2");
425 if (cam->params.pnp_id.product == 0x151)
426 strcpy(vc->card, "QX5 Microscope");
428 strcpy(vc->card, "CPiA2 Camera");
429 switch (cam->params.pnp_id.device_type) {
431 strcat(vc->card, " (672/");
434 strcat(vc->card, " (676/");
437 strcat(vc->card, " (???/");
440 switch (cam->params.version.sensor_flags) {
441 case CPIA2_VP_SENSOR_FLAGS_404:
442 strcat(vc->card, "404)");
444 case CPIA2_VP_SENSOR_FLAGS_407:
445 strcat(vc->card, "407)");
447 case CPIA2_VP_SENSOR_FLAGS_409:
448 strcat(vc->card, "409)");
450 case CPIA2_VP_SENSOR_FLAGS_410:
451 strcat(vc->card, "410)");
453 case CPIA2_VP_SENSOR_FLAGS_500:
454 strcat(vc->card, "500)");
457 strcat(vc->card, "???)");
461 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
462 memset(vc->bus_info,0, sizeof(vc->bus_info));
464 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
467 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
474 /******************************************************************************
478 * V4L2 input get/set/enumerate
480 *****************************************************************************/
482 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
484 struct v4l2_input *i = arg;
486 if(ioclt_nr != VIDIOC_G_INPUT) {
491 memset(i, 0, sizeof(*i));
492 strcpy(i->name, "Camera");
493 i->type = V4L2_INPUT_TYPE_CAMERA;
498 /******************************************************************************
502 * V4L2 format enumerate
504 *****************************************************************************/
506 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
508 struct v4l2_fmtdesc *f = arg;
509 int index = f->index;
511 if (index < 0 || index > 1)
514 memset(f, 0, sizeof(*f));
516 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
517 f->flags = V4L2_FMT_FLAG_COMPRESSED;
520 strcpy(f->description, "MJPEG");
521 f->pixelformat = V4L2_PIX_FMT_MJPEG;
524 strcpy(f->description, "JPEG");
525 f->pixelformat = V4L2_PIX_FMT_JPEG;
534 /******************************************************************************
540 *****************************************************************************/
542 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
544 struct v4l2_format *f = arg;
546 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
549 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
550 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
553 f->fmt.pix.field = V4L2_FIELD_NONE;
554 f->fmt.pix.bytesperline = 0;
555 f->fmt.pix.sizeimage = cam->frame_size;
556 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
559 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
561 f->fmt.pix.width = 640;
562 f->fmt.pix.height = 480;
565 f->fmt.pix.width = 352;
566 f->fmt.pix.height = 288;
569 f->fmt.pix.width = 320;
570 f->fmt.pix.height = 240;
572 case VIDEOSIZE_288_216:
573 f->fmt.pix.width = 288;
574 f->fmt.pix.height = 216;
576 case VIDEOSIZE_256_192:
577 f->fmt.pix.width = 256;
578 f->fmt.pix.height = 192;
580 case VIDEOSIZE_224_168:
581 f->fmt.pix.width = 224;
582 f->fmt.pix.height = 168;
584 case VIDEOSIZE_192_144:
585 f->fmt.pix.width = 192;
586 f->fmt.pix.height = 144;
590 f->fmt.pix.width = 176;
591 f->fmt.pix.height = 144;
598 /******************************************************************************
604 *****************************************************************************/
606 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
608 struct v4l2_format *f = arg;
611 err = ioctl_try_fmt(arg, cam);
615 /* Ensure that only this process can change the format. */
616 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
621 cam->pixelformat = f->fmt.pix.pixelformat;
623 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
624 * the missing Huffman table properly. */
625 cam->params.compression.inhibit_htables = 0;
626 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
628 /* we set the video window to something smaller or equal to what
629 * is requested by the user???
631 DBG("Requested width = %d, height = %d\n",
632 f->fmt.pix.width, f->fmt.pix.height);
633 if (f->fmt.pix.width != cam->width ||
634 f->fmt.pix.height != cam->height) {
635 cam->width = f->fmt.pix.width;
636 cam->height = f->fmt.pix.height;
637 cam->params.roi.width = f->fmt.pix.width;
638 cam->params.roi.height = f->fmt.pix.height;
639 cpia2_set_format(cam);
642 for (frame = 0; frame < cam->num_frames; ++frame) {
643 if (cam->buffers[frame].status == FRAME_READING)
644 if ((err = sync(cam, frame)) < 0)
647 cam->buffers[frame].status = FRAME_EMPTY;
653 /******************************************************************************
659 *****************************************************************************/
661 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
663 struct v4l2_format *f = arg;
665 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
668 f->fmt.pix.width = cam->width;
669 f->fmt.pix.height = cam->height;
670 f->fmt.pix.pixelformat = cam->pixelformat;
671 f->fmt.pix.field = V4L2_FIELD_NONE;
672 f->fmt.pix.bytesperline = 0;
673 f->fmt.pix.sizeimage = cam->frame_size;
674 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
680 /******************************************************************************
684 * V4L2 query cropping capabilities
685 * NOTE: cropping is currently disabled
687 *****************************************************************************/
689 static int ioctl_cropcap(void *arg,struct camera_data *cam)
691 struct v4l2_cropcap *c = arg;
693 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
698 c->bounds.width = cam->width;
699 c->bounds.height = cam->height;
702 c->defrect.width = cam->width;
703 c->defrect.height = cam->height;
704 c->pixelaspect.numerator = 1;
705 c->pixelaspect.denominator = 1;
710 /******************************************************************************
714 * V4L2 query possible control variables
716 *****************************************************************************/
718 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
720 struct v4l2_queryctrl *c = arg;
723 for(i=0; i<NUM_CONTROLS; ++i) {
724 if(c->id == controls[i].id) {
725 memcpy(c, controls+i, sizeof(*c));
730 if(i == NUM_CONTROLS)
733 /* Some devices have additional limitations */
735 case V4L2_CID_BRIGHTNESS:
737 * Don't let the register be set to zero - bug in VP4
738 * flash of full brightness
740 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
745 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
746 c->flags |= V4L2_CTRL_FLAG_DISABLED;
748 case CPIA2_CID_FRAMERATE:
749 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
750 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
752 for(i=0; i<c->maximum; ++i) {
753 if(framerate_controls[i].value ==
754 CPIA2_VP_FRAMERATE_15) {
756 c->default_value = i;
761 case CPIA2_CID_FLICKER_MODE:
762 // Flicker control only valid for 672.
763 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
764 c->flags |= V4L2_CTRL_FLAG_DISABLED;
766 case CPIA2_CID_LIGHTS:
767 // Light control only valid for the QX5 Microscope.
768 if(cam->params.pnp_id.product != 0x151)
769 c->flags |= V4L2_CTRL_FLAG_DISABLED;
778 /******************************************************************************
782 * V4L2 query possible control variables
784 *****************************************************************************/
786 static int ioctl_querymenu(void *arg,struct camera_data *cam)
788 struct v4l2_querymenu *m = arg;
790 memset(m->name, 0, sizeof(m->name));
794 case CPIA2_CID_FLICKER_MODE:
795 if (m->index >= NUM_FLICKER_CONTROLS)
798 strcpy(m->name, flicker_controls[m->index].name);
800 case CPIA2_CID_FRAMERATE:
802 int maximum = NUM_FRAMERATE_CONTROLS - 1;
803 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
804 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
807 for(i=0; i<maximum; ++i) {
808 if(framerate_controls[i].value ==
809 CPIA2_VP_FRAMERATE_15)
813 if (m->index > maximum)
816 strcpy(m->name, framerate_controls[m->index].name);
819 case CPIA2_CID_LIGHTS:
820 if (m->index >= NUM_LIGHTS_CONTROLS)
823 strcpy(m->name, lights_controls[m->index].name);
832 /******************************************************************************
836 * V4L2 get the value of a control variable
838 *****************************************************************************/
840 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
842 struct v4l2_control *c = arg;
845 case V4L2_CID_BRIGHTNESS:
846 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
848 c->value = cam->params.color_params.brightness;
850 case V4L2_CID_CONTRAST:
851 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
853 c->value = cam->params.color_params.contrast;
855 case V4L2_CID_SATURATION:
856 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
858 c->value = cam->params.color_params.saturation;
861 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
863 c->value = (cam->params.vp_params.user_effects &
864 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
867 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
869 c->value = (cam->params.vp_params.user_effects &
870 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
872 case CPIA2_CID_TARGET_KB:
873 c->value = cam->params.vc_params.target_kb;
876 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
878 c->value = cam->params.vp_params.gpio_data;
880 case CPIA2_CID_FLICKER_MODE:
883 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
885 if(cam->params.flicker_control.cam_register &
886 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
887 mode = NEVER_FLICKER;
889 if(cam->params.flicker_control.cam_register &
890 CPIA2_VP_FLICKER_MODES_50HZ) {
896 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
897 if(flicker_controls[i].value == mode) {
902 if(i == NUM_FLICKER_CONTROLS)
906 case CPIA2_CID_FRAMERATE:
908 int maximum = NUM_FRAMERATE_CONTROLS - 1;
910 for(i=0; i<= maximum; i++) {
911 if(cam->params.vp_params.frame_rate ==
912 framerate_controls[i].value)
920 case CPIA2_CID_USB_ALT:
921 c->value = cam->params.camera_state.stream_mode;
923 case CPIA2_CID_LIGHTS:
926 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
928 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
929 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
930 lights_controls[i].value) {
934 if(i == NUM_LIGHTS_CONTROLS)
939 case CPIA2_CID_RESET_CAMERA:
945 DBG("Get control id:%d, value:%d\n", c->id, c->value);
950 /******************************************************************************
954 * V4L2 set the value of a control variable
956 *****************************************************************************/
958 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
960 struct v4l2_control *c = arg;
964 DBG("Set control id:%d, value:%d\n", c->id, c->value);
966 /* Check that the value is in range */
967 for(i=0; i<NUM_CONTROLS; i++) {
968 if(c->id == controls[i].id) {
969 if(c->value < controls[i].minimum ||
970 c->value > controls[i].maximum) {
976 if(i == NUM_CONTROLS)
980 case V4L2_CID_BRIGHTNESS:
981 cpia2_set_brightness(cam, c->value);
983 case V4L2_CID_CONTRAST:
984 cpia2_set_contrast(cam, c->value);
986 case V4L2_CID_SATURATION:
987 cpia2_set_saturation(cam, c->value);
990 cpia2_set_property_mirror(cam, c->value);
993 cpia2_set_property_flip(cam, c->value);
995 case CPIA2_CID_TARGET_KB:
996 retval = cpia2_set_target_kb(cam, c->value);
999 retval = cpia2_set_gpio(cam, c->value);
1001 case CPIA2_CID_FLICKER_MODE:
1002 retval = cpia2_set_flicker_mode(cam,
1003 flicker_controls[c->value].value);
1005 case CPIA2_CID_FRAMERATE:
1006 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1008 case CPIA2_CID_USB_ALT:
1009 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1011 case CPIA2_CID_LIGHTS:
1012 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1014 case CPIA2_CID_RESET_CAMERA:
1015 cpia2_usb_stream_pause(cam);
1016 cpia2_reset_camera(cam);
1017 cpia2_usb_stream_resume(cam);
1026 /******************************************************************************
1030 * V4L2 get the JPEG compression parameters
1032 *****************************************************************************/
1034 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1036 struct v4l2_jpegcompression *parms = arg;
1038 memset(parms, 0, sizeof(*parms));
1040 parms->quality = 80; // TODO: Can this be made meaningful?
1042 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1043 if(!cam->params.compression.inhibit_htables) {
1044 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1047 parms->APPn = cam->APPn;
1048 parms->APP_len = cam->APP_len;
1049 if(cam->APP_len > 0) {
1050 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1051 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1054 parms->COM_len = cam->COM_len;
1055 if(cam->COM_len > 0) {
1056 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1057 parms->jpeg_markers |= JPEG_MARKER_COM;
1060 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1061 parms->APP_len, parms->COM_len);
1066 /******************************************************************************
1070 * V4L2 set the JPEG compression parameters
1071 * NOTE: quality and some jpeg_markers are ignored.
1073 *****************************************************************************/
1075 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1077 struct v4l2_jpegcompression *parms = arg;
1079 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1080 parms->APP_len, parms->COM_len);
1082 cam->params.compression.inhibit_htables =
1083 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1085 if(parms->APP_len != 0) {
1086 if(parms->APP_len > 0 &&
1087 parms->APP_len <= sizeof(cam->APP_data) &&
1088 parms->APPn >= 0 && parms->APPn <= 15) {
1089 cam->APPn = parms->APPn;
1090 cam->APP_len = parms->APP_len;
1091 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1093 LOG("Bad APPn Params n=%d len=%d\n",
1094 parms->APPn, parms->APP_len);
1101 if(parms->COM_len != 0) {
1102 if(parms->COM_len > 0 &&
1103 parms->COM_len <= sizeof(cam->COM_data)) {
1104 cam->COM_len = parms->COM_len;
1105 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1107 LOG("Bad COM_len=%d\n", parms->COM_len);
1115 /******************************************************************************
1119 * V4L2 Initiate memory mapping.
1120 * NOTE: The user's request is ignored. For now the buffers are fixed.
1122 *****************************************************************************/
1124 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1126 struct v4l2_requestbuffers *req = arg;
1128 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1129 req->memory != V4L2_MEMORY_MMAP)
1132 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1133 req->count = cam->num_frames;
1134 memset(&req->reserved, 0, sizeof(req->reserved));
1139 /******************************************************************************
1143 * V4L2 Query memory buffer status.
1145 *****************************************************************************/
1147 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1149 struct v4l2_buffer *buf = arg;
1151 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1152 buf->index > cam->num_frames)
1155 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1156 buf->length = cam->frame_size;
1158 buf->memory = V4L2_MEMORY_MMAP;
1161 buf->flags = V4L2_BUF_FLAG_MAPPED;
1165 switch (cam->buffers[buf->index].status) {
1170 buf->flags = V4L2_BUF_FLAG_QUEUED;
1173 buf->bytesused = cam->buffers[buf->index].length;
1174 buf->timestamp = cam->buffers[buf->index].timestamp;
1175 buf->sequence = cam->buffers[buf->index].seq;
1176 buf->flags = V4L2_BUF_FLAG_DONE;
1180 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1181 buf->index, buf->m.offset, buf->flags, buf->sequence,
1187 /******************************************************************************
1191 * V4L2 User is freeing buffer
1193 *****************************************************************************/
1195 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1197 struct v4l2_buffer *buf = arg;
1199 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1200 buf->memory != V4L2_MEMORY_MMAP ||
1201 buf->index > cam->num_frames)
1204 DBG("QBUF #%d\n", buf->index);
1206 if(cam->buffers[buf->index].status == FRAME_READY)
1207 cam->buffers[buf->index].status = FRAME_EMPTY;
1212 /******************************************************************************
1214 * find_earliest_filled_buffer
1216 * Helper for ioctl_dqbuf. Find the next ready buffer.
1218 *****************************************************************************/
1220 static int find_earliest_filled_buffer(struct camera_data *cam)
1224 for (i=0; i<cam->num_frames; i++) {
1225 if(cam->buffers[i].status == FRAME_READY) {
1229 /* find which buffer is earlier */
1230 struct timeval *tv1, *tv2;
1231 tv1 = &cam->buffers[i].timestamp;
1232 tv2 = &cam->buffers[found].timestamp;
1233 if(tv1->tv_sec < tv2->tv_sec ||
1234 (tv1->tv_sec == tv2->tv_sec &&
1235 tv1->tv_usec < tv2->tv_usec))
1243 /******************************************************************************
1247 * V4L2 User is asking for a filled buffer.
1249 *****************************************************************************/
1251 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1253 struct v4l2_buffer *buf = arg;
1256 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1257 buf->memory != V4L2_MEMORY_MMAP)
1260 frame = find_earliest_filled_buffer(cam);
1262 if(frame < 0 && file->f_flags&O_NONBLOCK)
1266 /* Wait for a frame to become available */
1267 struct framebuf *cb=cam->curbuff;
1268 mutex_unlock(&cam->v4l2_lock);
1269 wait_event_interruptible(cam->wq_stream,
1271 (cb=cam->curbuff)->status == FRAME_READY);
1272 mutex_lock(&cam->v4l2_lock);
1273 if (signal_pending(current))
1274 return -ERESTARTSYS;
1282 buf->bytesused = cam->buffers[buf->index].length;
1283 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1284 buf->field = V4L2_FIELD_NONE;
1285 buf->timestamp = cam->buffers[buf->index].timestamp;
1286 buf->sequence = cam->buffers[buf->index].seq;
1287 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1288 buf->length = cam->frame_size;
1291 memset(&buf->timecode, 0, sizeof(buf->timecode));
1293 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1294 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1299 /******************************************************************************
1303 *****************************************************************************/
1304 static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1306 struct camera_data *cam = video_drvdata(file);
1315 /* Priority check */
1319 struct cpia2_fh *fh = file->private_data;
1320 retval = v4l2_prio_check(&cam->prio, fh->prio);
1330 /* CPIA2 extension to Video4Linux API */
1331 case CPIA2_IOC_SET_GPIO:
1332 retval = ioctl_set_gpio(arg, cam);
1334 case VIDIOC_QUERYCAP:
1335 retval = ioctl_querycap(arg,cam);
1338 case VIDIOC_ENUMINPUT:
1339 case VIDIOC_G_INPUT:
1340 case VIDIOC_S_INPUT:
1341 retval = ioctl_input(cmd, arg, cam);
1344 case VIDIOC_ENUM_FMT:
1345 retval = ioctl_enum_fmt(arg,cam);
1347 case VIDIOC_TRY_FMT:
1348 retval = ioctl_try_fmt(arg,cam);
1351 retval = ioctl_get_fmt(arg,cam);
1354 retval = ioctl_set_fmt(arg,cam,file->private_data);
1357 case VIDIOC_CROPCAP:
1358 retval = ioctl_cropcap(arg,cam);
1362 // TODO: I think cropping can be implemented - SJB
1366 case VIDIOC_QUERYCTRL:
1367 retval = ioctl_queryctrl(arg,cam);
1369 case VIDIOC_QUERYMENU:
1370 retval = ioctl_querymenu(arg,cam);
1373 retval = ioctl_g_ctrl(arg,cam);
1376 retval = ioctl_s_ctrl(arg,cam);
1379 case VIDIOC_G_JPEGCOMP:
1380 retval = ioctl_g_jpegcomp(arg,cam);
1382 case VIDIOC_S_JPEGCOMP:
1383 retval = ioctl_s_jpegcomp(arg,cam);
1386 case VIDIOC_G_PRIORITY:
1388 struct cpia2_fh *fh = file->private_data;
1389 *(enum v4l2_priority*)arg = fh->prio;
1392 case VIDIOC_S_PRIORITY:
1394 struct cpia2_fh *fh = file->private_data;
1395 enum v4l2_priority prio;
1396 prio = *(enum v4l2_priority*)arg;
1397 if(cam->streaming &&
1399 fh->prio == V4L2_PRIORITY_RECORD) {
1400 /* Can't drop record priority while streaming */
1402 } else if(prio == V4L2_PRIORITY_RECORD &&
1404 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1405 /* Only one program can record at a time */
1408 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1413 case VIDIOC_REQBUFS:
1414 retval = ioctl_reqbufs(arg,cam);
1416 case VIDIOC_QUERYBUF:
1417 retval = ioctl_querybuf(arg,cam);
1420 retval = ioctl_qbuf(arg,cam);
1423 retval = ioctl_dqbuf(arg,cam,file);
1425 case VIDIOC_STREAMON:
1428 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1430 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1433 if(!cam->streaming) {
1434 retval = cpia2_usb_stream_start(cam,
1435 cam->params.camera_state.stream_mode);
1442 case VIDIOC_STREAMOFF:
1445 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1447 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1450 if(cam->streaming) {
1451 retval = cpia2_usb_stream_stop(cam);
1459 case VIDIOC_ENUMOUTPUT:
1460 case VIDIOC_G_OUTPUT:
1461 case VIDIOC_S_OUTPUT:
1462 case VIDIOC_G_MODULATOR:
1463 case VIDIOC_S_MODULATOR:
1465 case VIDIOC_ENUMAUDIO:
1466 case VIDIOC_G_AUDIO:
1467 case VIDIOC_S_AUDIO:
1469 case VIDIOC_ENUMAUDOUT:
1470 case VIDIOC_G_AUDOUT:
1471 case VIDIOC_S_AUDOUT:
1473 case VIDIOC_ENUMSTD:
1474 case VIDIOC_QUERYSTD:
1478 case VIDIOC_G_TUNER:
1479 case VIDIOC_S_TUNER:
1480 case VIDIOC_G_FREQUENCY:
1481 case VIDIOC_S_FREQUENCY:
1483 case VIDIOC_OVERLAY:
1492 retval = -ENOIOCTLCMD;
1499 static long cpia2_ioctl(struct file *file,
1500 unsigned int cmd, unsigned long arg)
1502 return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
1505 /******************************************************************************
1509 *****************************************************************************/
1510 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1512 struct camera_data *cam = video_drvdata(file);
1515 /* Priority check */
1516 struct cpia2_fh *fh = file->private_data;
1517 if(fh->prio != V4L2_PRIORITY_RECORD) {
1521 retval = cpia2_remap_buffer(cam, area);
1528 /******************************************************************************
1530 * reset_camera_struct_v4l
1532 * Sets all values to the defaults
1533 *****************************************************************************/
1534 static void reset_camera_struct_v4l(struct camera_data *cam)
1536 cam->width = cam->params.roi.width;
1537 cam->height = cam->params.roi.height;
1539 cam->frame_size = buffer_size;
1540 cam->num_frames = num_buffers;
1543 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1544 cam->params.flicker_control.mains_frequency = flicker_freq;
1547 cam->params.camera_state.stream_mode = alternate;
1549 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1550 v4l2_prio_init(&cam->prio);
1554 * The v4l video device structure initialized for this device
1556 static const struct v4l2_file_operations cpia2_fops = {
1557 .owner = THIS_MODULE,
1559 .release = cpia2_close,
1560 .read = cpia2_v4l_read,
1561 .poll = cpia2_v4l_poll,
1562 .unlocked_ioctl = cpia2_ioctl,
1566 static struct video_device cpia2_template = {
1567 /* I could not find any place for the old .initialize initializer?? */
1568 .name = "CPiA2 Camera",
1569 .fops = &cpia2_fops,
1570 .release = video_device_release,
1573 /******************************************************************************
1575 * cpia2_register_camera
1577 *****************************************************************************/
1578 int cpia2_register_camera(struct camera_data *cam)
1580 cam->vdev = video_device_alloc();
1584 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1585 video_set_drvdata(cam->vdev, cam);
1586 cam->vdev->lock = &cam->v4l2_lock;
1588 reset_camera_struct_v4l(cam);
1590 /* register v4l device */
1591 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1592 ERR("video_register_device failed\n");
1593 video_device_release(cam->vdev);
1600 /******************************************************************************
1602 * cpia2_unregister_camera
1604 *****************************************************************************/
1605 void cpia2_unregister_camera(struct camera_data *cam)
1607 if (!cam->open_count) {
1608 video_unregister_device(cam->vdev);
1610 LOG("%s removed while open, deferring "
1611 "video_unregister_device\n",
1612 video_device_node_name(cam->vdev));
1616 /******************************************************************************
1620 * Make sure that all user-supplied parameters are sensible
1621 *****************************************************************************/
1622 static void __init check_parameters(void)
1624 if(buffer_size < PAGE_SIZE) {
1625 buffer_size = PAGE_SIZE;
1626 LOG("buffer_size too small, setting to %d\n", buffer_size);
1627 } else if(buffer_size > 1024*1024) {
1628 /* arbitrary upper limiit */
1629 buffer_size = 1024*1024;
1630 LOG("buffer_size ridiculously large, setting to %d\n",
1633 buffer_size += PAGE_SIZE-1;
1634 buffer_size &= ~(PAGE_SIZE-1);
1637 if(num_buffers < 1) {
1639 LOG("num_buffers too small, setting to %d\n", num_buffers);
1640 } else if(num_buffers > VIDEO_MAX_FRAME) {
1641 num_buffers = VIDEO_MAX_FRAME;
1642 LOG("num_buffers too large, setting to %d\n", num_buffers);
1645 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1646 alternate = DEFAULT_ALT;
1647 LOG("alternate specified is invalid, using %d\n", alternate);
1650 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1651 flicker_mode = NEVER_FLICKER;
1652 LOG("Flicker mode specified is invalid, using %d\n",
1656 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1657 flicker_freq = FLICKER_60;
1658 LOG("Flicker mode specified is invalid, using %d\n",
1662 if(video_nr < -1 || video_nr > 64) {
1664 LOG("invalid video_nr specified, must be -1 to 64\n");
1667 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1668 num_buffers, buffer_size, alternate);
1671 /************ Module Stuff ***************/
1674 /******************************************************************************
1676 * cpia2_init/module_init
1678 *****************************************************************************/
1679 static int __init cpia2_init(void)
1681 LOG("%s v%d.%d.%d\n",
1682 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
1689 /******************************************************************************
1691 * cpia2_exit/module_exit
1693 *****************************************************************************/
1694 static void __exit cpia2_exit(void)
1696 cpia2_usb_cleanup();
1697 schedule_timeout(2 * HZ);
1700 module_init(cpia2_init);
1701 module_exit(cpia2_exit);