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 *****************************************************************************/
425 #ifdef CONFIG_VIDEO_V4L1_COMPAT
426 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
428 struct video_mbuf *vm;
432 memset(vm, 0, sizeof(*vm));
433 vm->size = cam->frame_size*cam->num_frames;
434 vm->frames = cam->num_frames;
435 for (i = 0; i < cam->num_frames; i++)
436 vm->offsets[i] = cam->frame_size * i;
442 /******************************************************************************
446 *****************************************************************************/
448 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
452 gpio_val = *(__u32*) arg;
454 if (gpio_val &~ 0xFFU)
457 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
460 /******************************************************************************
464 * V4L2 device capabilities
466 *****************************************************************************/
468 static int ioctl_querycap(void *arg, struct camera_data *cam)
470 struct v4l2_capability *vc = arg;
472 memset(vc, 0, sizeof(*vc));
473 strcpy(vc->driver, "cpia2");
475 if (cam->params.pnp_id.product == 0x151)
476 strcpy(vc->card, "QX5 Microscope");
478 strcpy(vc->card, "CPiA2 Camera");
479 switch (cam->params.pnp_id.device_type) {
481 strcat(vc->card, " (672/");
484 strcat(vc->card, " (676/");
487 strcat(vc->card, " (???/");
490 switch (cam->params.version.sensor_flags) {
491 case CPIA2_VP_SENSOR_FLAGS_404:
492 strcat(vc->card, "404)");
494 case CPIA2_VP_SENSOR_FLAGS_407:
495 strcat(vc->card, "407)");
497 case CPIA2_VP_SENSOR_FLAGS_409:
498 strcat(vc->card, "409)");
500 case CPIA2_VP_SENSOR_FLAGS_410:
501 strcat(vc->card, "410)");
503 case CPIA2_VP_SENSOR_FLAGS_500:
504 strcat(vc->card, "500)");
507 strcat(vc->card, "???)");
511 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
512 memset(vc->bus_info,0, sizeof(vc->bus_info));
514 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
517 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
524 /******************************************************************************
528 * V4L2 input get/set/enumerate
530 *****************************************************************************/
532 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
534 struct v4l2_input *i = arg;
536 if(ioclt_nr != VIDIOC_G_INPUT) {
541 memset(i, 0, sizeof(*i));
542 strcpy(i->name, "Camera");
543 i->type = V4L2_INPUT_TYPE_CAMERA;
548 /******************************************************************************
552 * V4L2 format enumerate
554 *****************************************************************************/
556 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
558 struct v4l2_fmtdesc *f = arg;
559 int index = f->index;
561 if (index < 0 || index > 1)
564 memset(f, 0, sizeof(*f));
566 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
567 f->flags = V4L2_FMT_FLAG_COMPRESSED;
570 strcpy(f->description, "MJPEG");
571 f->pixelformat = V4L2_PIX_FMT_MJPEG;
574 strcpy(f->description, "JPEG");
575 f->pixelformat = V4L2_PIX_FMT_JPEG;
584 /******************************************************************************
590 *****************************************************************************/
592 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
594 struct v4l2_format *f = arg;
596 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
599 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
600 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
603 f->fmt.pix.field = V4L2_FIELD_NONE;
604 f->fmt.pix.bytesperline = 0;
605 f->fmt.pix.sizeimage = cam->frame_size;
606 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
609 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
611 f->fmt.pix.width = 640;
612 f->fmt.pix.height = 480;
615 f->fmt.pix.width = 352;
616 f->fmt.pix.height = 288;
619 f->fmt.pix.width = 320;
620 f->fmt.pix.height = 240;
622 case VIDEOSIZE_288_216:
623 f->fmt.pix.width = 288;
624 f->fmt.pix.height = 216;
626 case VIDEOSIZE_256_192:
627 f->fmt.pix.width = 256;
628 f->fmt.pix.height = 192;
630 case VIDEOSIZE_224_168:
631 f->fmt.pix.width = 224;
632 f->fmt.pix.height = 168;
634 case VIDEOSIZE_192_144:
635 f->fmt.pix.width = 192;
636 f->fmt.pix.height = 144;
640 f->fmt.pix.width = 176;
641 f->fmt.pix.height = 144;
648 /******************************************************************************
654 *****************************************************************************/
656 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
658 struct v4l2_format *f = arg;
661 err = ioctl_try_fmt(arg, cam);
665 /* Ensure that only this process can change the format. */
666 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
671 cam->pixelformat = f->fmt.pix.pixelformat;
673 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
674 * the missing Huffman table properly. */
675 cam->params.compression.inhibit_htables = 0;
676 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
678 /* we set the video window to something smaller or equal to what
679 * is requested by the user???
681 DBG("Requested width = %d, height = %d\n",
682 f->fmt.pix.width, f->fmt.pix.height);
683 if (f->fmt.pix.width != cam->width ||
684 f->fmt.pix.height != cam->height) {
685 cam->width = f->fmt.pix.width;
686 cam->height = f->fmt.pix.height;
687 cam->params.roi.width = f->fmt.pix.width;
688 cam->params.roi.height = f->fmt.pix.height;
689 cpia2_set_format(cam);
692 for (frame = 0; frame < cam->num_frames; ++frame) {
693 if (cam->buffers[frame].status == FRAME_READING)
694 if ((err = sync(cam, frame)) < 0)
697 cam->buffers[frame].status = FRAME_EMPTY;
703 /******************************************************************************
709 *****************************************************************************/
711 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
713 struct v4l2_format *f = arg;
715 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
718 f->fmt.pix.width = cam->width;
719 f->fmt.pix.height = cam->height;
720 f->fmt.pix.pixelformat = cam->pixelformat;
721 f->fmt.pix.field = V4L2_FIELD_NONE;
722 f->fmt.pix.bytesperline = 0;
723 f->fmt.pix.sizeimage = cam->frame_size;
724 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
730 /******************************************************************************
734 * V4L2 query cropping capabilities
735 * NOTE: cropping is currently disabled
737 *****************************************************************************/
739 static int ioctl_cropcap(void *arg,struct camera_data *cam)
741 struct v4l2_cropcap *c = arg;
743 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
748 c->bounds.width = cam->width;
749 c->bounds.height = cam->height;
752 c->defrect.width = cam->width;
753 c->defrect.height = cam->height;
754 c->pixelaspect.numerator = 1;
755 c->pixelaspect.denominator = 1;
760 /******************************************************************************
764 * V4L2 query possible control variables
766 *****************************************************************************/
768 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
770 struct v4l2_queryctrl *c = arg;
773 for(i=0; i<NUM_CONTROLS; ++i) {
774 if(c->id == controls[i].id) {
775 memcpy(c, controls+i, sizeof(*c));
780 if(i == NUM_CONTROLS)
783 /* Some devices have additional limitations */
785 case V4L2_CID_BRIGHTNESS:
787 * Don't let the register be set to zero - bug in VP4
788 * flash of full brightness
790 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
795 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
796 c->flags |= V4L2_CTRL_FLAG_DISABLED;
798 case CPIA2_CID_FRAMERATE:
799 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
800 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
802 for(i=0; i<c->maximum; ++i) {
803 if(framerate_controls[i].value ==
804 CPIA2_VP_FRAMERATE_15) {
806 c->default_value = i;
811 case CPIA2_CID_FLICKER_MODE:
812 // Flicker control only valid for 672.
813 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
814 c->flags |= V4L2_CTRL_FLAG_DISABLED;
816 case CPIA2_CID_LIGHTS:
817 // Light control only valid for the QX5 Microscope.
818 if(cam->params.pnp_id.product != 0x151)
819 c->flags |= V4L2_CTRL_FLAG_DISABLED;
828 /******************************************************************************
832 * V4L2 query possible control variables
834 *****************************************************************************/
836 static int ioctl_querymenu(void *arg,struct camera_data *cam)
838 struct v4l2_querymenu *m = arg;
840 memset(m->name, 0, sizeof(m->name));
844 case CPIA2_CID_FLICKER_MODE:
845 if (m->index >= NUM_FLICKER_CONTROLS)
848 strcpy(m->name, flicker_controls[m->index].name);
850 case CPIA2_CID_FRAMERATE:
852 int maximum = NUM_FRAMERATE_CONTROLS - 1;
853 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
854 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
857 for(i=0; i<maximum; ++i) {
858 if(framerate_controls[i].value ==
859 CPIA2_VP_FRAMERATE_15)
863 if (m->index > maximum)
866 strcpy(m->name, framerate_controls[m->index].name);
869 case CPIA2_CID_LIGHTS:
870 if (m->index >= NUM_LIGHTS_CONTROLS)
873 strcpy(m->name, lights_controls[m->index].name);
882 /******************************************************************************
886 * V4L2 get the value of a control variable
888 *****************************************************************************/
890 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
892 struct v4l2_control *c = arg;
895 case V4L2_CID_BRIGHTNESS:
896 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
898 c->value = cam->params.color_params.brightness;
900 case V4L2_CID_CONTRAST:
901 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
903 c->value = cam->params.color_params.contrast;
905 case V4L2_CID_SATURATION:
906 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
908 c->value = cam->params.color_params.saturation;
911 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
913 c->value = (cam->params.vp_params.user_effects &
914 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
917 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
919 c->value = (cam->params.vp_params.user_effects &
920 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
922 case CPIA2_CID_TARGET_KB:
923 c->value = cam->params.vc_params.target_kb;
926 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
928 c->value = cam->params.vp_params.gpio_data;
930 case CPIA2_CID_FLICKER_MODE:
933 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
935 if(cam->params.flicker_control.cam_register &
936 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
937 mode = NEVER_FLICKER;
939 if(cam->params.flicker_control.cam_register &
940 CPIA2_VP_FLICKER_MODES_50HZ) {
946 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
947 if(flicker_controls[i].value == mode) {
952 if(i == NUM_FLICKER_CONTROLS)
956 case CPIA2_CID_FRAMERATE:
958 int maximum = NUM_FRAMERATE_CONTROLS - 1;
960 for(i=0; i<= maximum; i++) {
961 if(cam->params.vp_params.frame_rate ==
962 framerate_controls[i].value)
970 case CPIA2_CID_USB_ALT:
971 c->value = cam->params.camera_state.stream_mode;
973 case CPIA2_CID_LIGHTS:
976 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
978 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
979 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
980 lights_controls[i].value) {
984 if(i == NUM_LIGHTS_CONTROLS)
989 case CPIA2_CID_RESET_CAMERA:
995 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1000 /******************************************************************************
1004 * V4L2 set the value of a control variable
1006 *****************************************************************************/
1008 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1010 struct v4l2_control *c = arg;
1014 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1016 /* Check that the value is in range */
1017 for(i=0; i<NUM_CONTROLS; i++) {
1018 if(c->id == controls[i].id) {
1019 if(c->value < controls[i].minimum ||
1020 c->value > controls[i].maximum) {
1026 if(i == NUM_CONTROLS)
1030 case V4L2_CID_BRIGHTNESS:
1031 cpia2_set_brightness(cam, c->value);
1033 case V4L2_CID_CONTRAST:
1034 cpia2_set_contrast(cam, c->value);
1036 case V4L2_CID_SATURATION:
1037 cpia2_set_saturation(cam, c->value);
1039 case V4L2_CID_HFLIP:
1040 cpia2_set_property_mirror(cam, c->value);
1042 case V4L2_CID_VFLIP:
1043 cpia2_set_property_flip(cam, c->value);
1045 case CPIA2_CID_TARGET_KB:
1046 retval = cpia2_set_target_kb(cam, c->value);
1048 case CPIA2_CID_GPIO:
1049 retval = cpia2_set_gpio(cam, c->value);
1051 case CPIA2_CID_FLICKER_MODE:
1052 retval = cpia2_set_flicker_mode(cam,
1053 flicker_controls[c->value].value);
1055 case CPIA2_CID_FRAMERATE:
1056 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1058 case CPIA2_CID_USB_ALT:
1059 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1061 case CPIA2_CID_LIGHTS:
1062 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1064 case CPIA2_CID_RESET_CAMERA:
1065 cpia2_usb_stream_pause(cam);
1066 cpia2_reset_camera(cam);
1067 cpia2_usb_stream_resume(cam);
1076 /******************************************************************************
1080 * V4L2 get the JPEG compression parameters
1082 *****************************************************************************/
1084 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1086 struct v4l2_jpegcompression *parms = arg;
1088 memset(parms, 0, sizeof(*parms));
1090 parms->quality = 80; // TODO: Can this be made meaningful?
1092 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1093 if(!cam->params.compression.inhibit_htables) {
1094 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1097 parms->APPn = cam->APPn;
1098 parms->APP_len = cam->APP_len;
1099 if(cam->APP_len > 0) {
1100 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1101 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1104 parms->COM_len = cam->COM_len;
1105 if(cam->COM_len > 0) {
1106 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1107 parms->jpeg_markers |= JPEG_MARKER_COM;
1110 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1111 parms->APP_len, parms->COM_len);
1116 /******************************************************************************
1120 * V4L2 set the JPEG compression parameters
1121 * NOTE: quality and some jpeg_markers are ignored.
1123 *****************************************************************************/
1125 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1127 struct v4l2_jpegcompression *parms = arg;
1129 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1130 parms->APP_len, parms->COM_len);
1132 cam->params.compression.inhibit_htables =
1133 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1135 if(parms->APP_len != 0) {
1136 if(parms->APP_len > 0 &&
1137 parms->APP_len <= sizeof(cam->APP_data) &&
1138 parms->APPn >= 0 && parms->APPn <= 15) {
1139 cam->APPn = parms->APPn;
1140 cam->APP_len = parms->APP_len;
1141 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1143 LOG("Bad APPn Params n=%d len=%d\n",
1144 parms->APPn, parms->APP_len);
1151 if(parms->COM_len != 0) {
1152 if(parms->COM_len > 0 &&
1153 parms->COM_len <= sizeof(cam->COM_data)) {
1154 cam->COM_len = parms->COM_len;
1155 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1157 LOG("Bad COM_len=%d\n", parms->COM_len);
1165 /******************************************************************************
1169 * V4L2 Initiate memory mapping.
1170 * NOTE: The user's request is ignored. For now the buffers are fixed.
1172 *****************************************************************************/
1174 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1176 struct v4l2_requestbuffers *req = arg;
1178 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1179 req->memory != V4L2_MEMORY_MMAP)
1182 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1183 req->count = cam->num_frames;
1184 memset(&req->reserved, 0, sizeof(req->reserved));
1189 /******************************************************************************
1193 * V4L2 Query memory buffer status.
1195 *****************************************************************************/
1197 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1199 struct v4l2_buffer *buf = arg;
1201 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1202 buf->index > cam->num_frames)
1205 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1206 buf->length = cam->frame_size;
1208 buf->memory = V4L2_MEMORY_MMAP;
1211 buf->flags = V4L2_BUF_FLAG_MAPPED;
1215 switch (cam->buffers[buf->index].status) {
1220 buf->flags = V4L2_BUF_FLAG_QUEUED;
1223 buf->bytesused = cam->buffers[buf->index].length;
1224 buf->timestamp = cam->buffers[buf->index].timestamp;
1225 buf->sequence = cam->buffers[buf->index].seq;
1226 buf->flags = V4L2_BUF_FLAG_DONE;
1230 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1231 buf->index, buf->m.offset, buf->flags, buf->sequence,
1237 /******************************************************************************
1241 * V4L2 User is freeing buffer
1243 *****************************************************************************/
1245 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1247 struct v4l2_buffer *buf = arg;
1249 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1250 buf->memory != V4L2_MEMORY_MMAP ||
1251 buf->index > cam->num_frames)
1254 DBG("QBUF #%d\n", buf->index);
1256 if(cam->buffers[buf->index].status == FRAME_READY)
1257 cam->buffers[buf->index].status = FRAME_EMPTY;
1262 /******************************************************************************
1264 * find_earliest_filled_buffer
1266 * Helper for ioctl_dqbuf. Find the next ready buffer.
1268 *****************************************************************************/
1270 static int find_earliest_filled_buffer(struct camera_data *cam)
1274 for (i=0; i<cam->num_frames; i++) {
1275 if(cam->buffers[i].status == FRAME_READY) {
1279 /* find which buffer is earlier */
1280 struct timeval *tv1, *tv2;
1281 tv1 = &cam->buffers[i].timestamp;
1282 tv2 = &cam->buffers[found].timestamp;
1283 if(tv1->tv_sec < tv2->tv_sec ||
1284 (tv1->tv_sec == tv2->tv_sec &&
1285 tv1->tv_usec < tv2->tv_usec))
1293 /******************************************************************************
1297 * V4L2 User is asking for a filled buffer.
1299 *****************************************************************************/
1301 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1303 struct v4l2_buffer *buf = arg;
1306 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1307 buf->memory != V4L2_MEMORY_MMAP)
1310 frame = find_earliest_filled_buffer(cam);
1312 if(frame < 0 && file->f_flags&O_NONBLOCK)
1316 /* Wait for a frame to become available */
1317 struct framebuf *cb=cam->curbuff;
1318 mutex_unlock(&cam->busy_lock);
1319 wait_event_interruptible(cam->wq_stream,
1321 (cb=cam->curbuff)->status == FRAME_READY);
1322 mutex_lock(&cam->busy_lock);
1323 if (signal_pending(current))
1324 return -ERESTARTSYS;
1332 buf->bytesused = cam->buffers[buf->index].length;
1333 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1334 buf->field = V4L2_FIELD_NONE;
1335 buf->timestamp = cam->buffers[buf->index].timestamp;
1336 buf->sequence = cam->buffers[buf->index].seq;
1337 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1338 buf->length = cam->frame_size;
1341 memset(&buf->timecode, 0, sizeof(buf->timecode));
1343 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1344 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1349 /******************************************************************************
1353 *****************************************************************************/
1354 static long cpia2_do_ioctl(struct file *file, unsigned int cmd, void *arg)
1356 struct camera_data *cam = video_drvdata(file);
1362 /* make this _really_ smp-safe */
1363 if (mutex_lock_interruptible(&cam->busy_lock))
1364 return -ERESTARTSYS;
1366 if (!cam->present) {
1367 mutex_unlock(&cam->busy_lock);
1371 /* Priority check */
1375 struct cpia2_fh *fh = file->private_data;
1376 retval = v4l2_prio_check(&cam->prio, fh->prio);
1378 mutex_unlock(&cam->busy_lock);
1383 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1386 struct cpia2_fh *fh = file->private_data;
1387 if(fh->prio != V4L2_PRIORITY_RECORD) {
1388 mutex_unlock(&cam->busy_lock);
1399 /* CPIA2 extension to Video4Linux API */
1400 case CPIA2_IOC_SET_GPIO:
1401 retval = ioctl_set_gpio(arg, cam);
1403 #ifdef CONFIG_VIDEO_V4L1_COMPAT
1404 case VIDIOCGMBUF: /* mmap interface */
1405 retval = ioctl_get_mbuf(arg, cam);
1408 case VIDIOC_QUERYCAP:
1409 retval = ioctl_querycap(arg,cam);
1412 case VIDIOC_ENUMINPUT:
1413 case VIDIOC_G_INPUT:
1414 case VIDIOC_S_INPUT:
1415 retval = ioctl_input(cmd, arg, cam);
1418 case VIDIOC_ENUM_FMT:
1419 retval = ioctl_enum_fmt(arg,cam);
1421 case VIDIOC_TRY_FMT:
1422 retval = ioctl_try_fmt(arg,cam);
1425 retval = ioctl_get_fmt(arg,cam);
1428 retval = ioctl_set_fmt(arg,cam,file->private_data);
1431 case VIDIOC_CROPCAP:
1432 retval = ioctl_cropcap(arg,cam);
1436 // TODO: I think cropping can be implemented - SJB
1440 case VIDIOC_QUERYCTRL:
1441 retval = ioctl_queryctrl(arg,cam);
1443 case VIDIOC_QUERYMENU:
1444 retval = ioctl_querymenu(arg,cam);
1447 retval = ioctl_g_ctrl(arg,cam);
1450 retval = ioctl_s_ctrl(arg,cam);
1453 case VIDIOC_G_JPEGCOMP:
1454 retval = ioctl_g_jpegcomp(arg,cam);
1456 case VIDIOC_S_JPEGCOMP:
1457 retval = ioctl_s_jpegcomp(arg,cam);
1460 case VIDIOC_G_PRIORITY:
1462 struct cpia2_fh *fh = file->private_data;
1463 *(enum v4l2_priority*)arg = fh->prio;
1466 case VIDIOC_S_PRIORITY:
1468 struct cpia2_fh *fh = file->private_data;
1469 enum v4l2_priority prio;
1470 prio = *(enum v4l2_priority*)arg;
1471 if(cam->streaming &&
1473 fh->prio == V4L2_PRIORITY_RECORD) {
1474 /* Can't drop record priority while streaming */
1476 } else if(prio == V4L2_PRIORITY_RECORD &&
1478 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1479 /* Only one program can record at a time */
1482 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1487 case VIDIOC_REQBUFS:
1488 retval = ioctl_reqbufs(arg,cam);
1490 case VIDIOC_QUERYBUF:
1491 retval = ioctl_querybuf(arg,cam);
1494 retval = ioctl_qbuf(arg,cam);
1497 retval = ioctl_dqbuf(arg,cam,file);
1499 case VIDIOC_STREAMON:
1502 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1504 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1507 if(!cam->streaming) {
1508 retval = cpia2_usb_stream_start(cam,
1509 cam->params.camera_state.stream_mode);
1516 case VIDIOC_STREAMOFF:
1519 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1521 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1524 if(cam->streaming) {
1525 retval = cpia2_usb_stream_stop(cam);
1533 case VIDIOC_ENUMOUTPUT:
1534 case VIDIOC_G_OUTPUT:
1535 case VIDIOC_S_OUTPUT:
1536 case VIDIOC_G_MODULATOR:
1537 case VIDIOC_S_MODULATOR:
1539 case VIDIOC_ENUMAUDIO:
1540 case VIDIOC_G_AUDIO:
1541 case VIDIOC_S_AUDIO:
1543 case VIDIOC_ENUMAUDOUT:
1544 case VIDIOC_G_AUDOUT:
1545 case VIDIOC_S_AUDOUT:
1547 case VIDIOC_ENUMSTD:
1548 case VIDIOC_QUERYSTD:
1552 case VIDIOC_G_TUNER:
1553 case VIDIOC_S_TUNER:
1554 case VIDIOC_G_FREQUENCY:
1555 case VIDIOC_S_FREQUENCY:
1557 case VIDIOC_OVERLAY:
1566 retval = -ENOIOCTLCMD;
1570 mutex_unlock(&cam->busy_lock);
1574 static long cpia2_ioctl(struct file *file,
1575 unsigned int cmd, unsigned long arg)
1577 return video_usercopy(file, cmd, arg, cpia2_do_ioctl);
1580 /******************************************************************************
1584 *****************************************************************************/
1585 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1587 struct camera_data *cam = video_drvdata(file);
1590 /* Priority check */
1591 struct cpia2_fh *fh = file->private_data;
1592 if(fh->prio != V4L2_PRIORITY_RECORD) {
1596 retval = cpia2_remap_buffer(cam, area);
1603 /******************************************************************************
1605 * reset_camera_struct_v4l
1607 * Sets all values to the defaults
1608 *****************************************************************************/
1609 static void reset_camera_struct_v4l(struct camera_data *cam)
1611 cam->width = cam->params.roi.width;
1612 cam->height = cam->params.roi.height;
1614 cam->frame_size = buffer_size;
1615 cam->num_frames = num_buffers;
1618 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1619 cam->params.flicker_control.mains_frequency = flicker_freq;
1622 cam->params.camera_state.stream_mode = alternate;
1624 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1625 v4l2_prio_init(&cam->prio);
1629 * The v4l video device structure initialized for this device
1631 static const struct v4l2_file_operations cpia2_fops = {
1632 .owner = THIS_MODULE,
1634 .release = cpia2_close,
1635 .read = cpia2_v4l_read,
1636 .poll = cpia2_v4l_poll,
1637 .ioctl = cpia2_ioctl,
1641 static struct video_device cpia2_template = {
1642 /* I could not find any place for the old .initialize initializer?? */
1643 .name = "CPiA2 Camera",
1644 .fops = &cpia2_fops,
1645 .release = video_device_release,
1648 /******************************************************************************
1650 * cpia2_register_camera
1652 *****************************************************************************/
1653 int cpia2_register_camera(struct camera_data *cam)
1655 cam->vdev = video_device_alloc();
1659 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1660 video_set_drvdata(cam->vdev, cam);
1662 reset_camera_struct_v4l(cam);
1664 /* register v4l device */
1665 if (video_register_device(cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1666 ERR("video_register_device failed\n");
1667 video_device_release(cam->vdev);
1674 /******************************************************************************
1676 * cpia2_unregister_camera
1678 *****************************************************************************/
1679 void cpia2_unregister_camera(struct camera_data *cam)
1681 if (!cam->open_count) {
1682 video_unregister_device(cam->vdev);
1684 LOG("%s removed while open, deferring "
1685 "video_unregister_device\n",
1686 video_device_node_name(cam->vdev));
1690 /******************************************************************************
1694 * Make sure that all user-supplied parameters are sensible
1695 *****************************************************************************/
1696 static void __init check_parameters(void)
1698 if(buffer_size < PAGE_SIZE) {
1699 buffer_size = PAGE_SIZE;
1700 LOG("buffer_size too small, setting to %d\n", buffer_size);
1701 } else if(buffer_size > 1024*1024) {
1702 /* arbitrary upper limiit */
1703 buffer_size = 1024*1024;
1704 LOG("buffer_size ridiculously large, setting to %d\n",
1707 buffer_size += PAGE_SIZE-1;
1708 buffer_size &= ~(PAGE_SIZE-1);
1711 if(num_buffers < 1) {
1713 LOG("num_buffers too small, setting to %d\n", num_buffers);
1714 } else if(num_buffers > VIDEO_MAX_FRAME) {
1715 num_buffers = VIDEO_MAX_FRAME;
1716 LOG("num_buffers too large, setting to %d\n", num_buffers);
1719 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1720 alternate = DEFAULT_ALT;
1721 LOG("alternate specified is invalid, using %d\n", alternate);
1724 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
1725 flicker_mode = NEVER_FLICKER;
1726 LOG("Flicker mode specified is invalid, using %d\n",
1730 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
1731 flicker_freq = FLICKER_60;
1732 LOG("Flicker mode specified is invalid, using %d\n",
1736 if(video_nr < -1 || video_nr > 64) {
1738 LOG("invalid video_nr specified, must be -1 to 64\n");
1741 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1742 num_buffers, buffer_size, alternate);
1745 /************ Module Stuff ***************/
1748 /******************************************************************************
1750 * cpia2_init/module_init
1752 *****************************************************************************/
1753 static int __init cpia2_init(void)
1755 LOG("%s v%d.%d.%d\n",
1756 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
1763 /******************************************************************************
1765 * cpia2_exit/module_exit
1767 *****************************************************************************/
1768 static void __exit cpia2_exit(void)
1770 cpia2_usb_cleanup();
1771 schedule_timeout(2 * HZ);
1774 module_init(cpia2_init);
1775 module_exit(cpia2_exit);