V4L/DVB (6006): tuner: move last_div to tuner-simple private data
[wrt350n-kernel.git] / drivers / media / video / cpia2 / cpia2_v4l.c
blob92778cd1d7356a7a12bc81d1ab70b0dabfb4c4af
1 /****************************************************************************
3 * Filename: cpia2_v4l.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
9 * Description:
10 * This is a USB driver for CPia2 based video cameras.
11 * The infrastructure of this driver is based on the cpia usb driver by
12 * Jochen Scharrlach and Johannes Erdfeldt.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Stripped of 2.4 stuff ready for main kernel submit by
29 * Alan Cox <alan@redhat.com>
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/moduleparam.h>
42 #include "cpia2.h"
43 #include "cpia2dev.h"
46 //#define _CPIA2_DEBUG_
48 #define MAKE_STRING_1(x) #x
49 #define MAKE_STRING(x) MAKE_STRING_1(x)
51 static int video_nr = -1;
52 module_param(video_nr, int, 0);
53 MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
55 static int buffer_size = 68*1024;
56 module_param(buffer_size, int, 0);
57 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
59 static int num_buffers = 3;
60 module_param(num_buffers, int, 0);
61 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
62 MAKE_STRING(VIDEO_MAX_FRAME) ", default 3)");
64 static int alternate = DEFAULT_ALT;
65 module_param(alternate, int, 0);
66 MODULE_PARM_DESC(alternate, "USB Alternate (" MAKE_STRING(USBIF_ISO_1) "-"
67 MAKE_STRING(USBIF_ISO_6) ", default "
68 MAKE_STRING(DEFAULT_ALT) ")");
70 static int flicker_freq = 60;
71 module_param(flicker_freq, int, 0);
72 MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" MAKE_STRING(50) "or"
73 MAKE_STRING(60) ", default "
74 MAKE_STRING(60) ")");
76 static int flicker_mode = NEVER_FLICKER;
77 module_param(flicker_mode, int, 0);
78 MODULE_PARM_DESC(flicker_mode,
79 "Flicker supression (" MAKE_STRING(NEVER_FLICKER) "or"
80 MAKE_STRING(ANTI_FLICKER_ON) ", default "
81 MAKE_STRING(NEVER_FLICKER) ")");
83 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
84 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
85 MODULE_SUPPORTED_DEVICE("video");
86 MODULE_LICENSE("GPL");
88 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
90 #ifndef VID_HARDWARE_CPIA2
91 #error "VID_HARDWARE_CPIA2 should have been defined in linux/videodev.h"
92 #endif
94 struct control_menu_info {
95 int value;
96 char name[32];
99 static struct control_menu_info framerate_controls[] =
101 { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
102 { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
103 { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
104 { CPIA2_VP_FRAMERATE_15, "15 fps" },
105 { CPIA2_VP_FRAMERATE_25, "25 fps" },
106 { CPIA2_VP_FRAMERATE_30, "30 fps" },
108 #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
110 static struct control_menu_info flicker_controls[] =
112 { NEVER_FLICKER, "Off" },
113 { FLICKER_50, "50 Hz" },
114 { FLICKER_60, "60 Hz" },
116 #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
118 static struct control_menu_info lights_controls[] =
120 { 0, "Off" },
121 { 64, "Top" },
122 { 128, "Bottom" },
123 { 192, "Both" },
125 #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
126 #define GPIO_LIGHTS_MASK 192
128 static struct v4l2_queryctrl controls[] = {
130 .id = V4L2_CID_BRIGHTNESS,
131 .type = V4L2_CTRL_TYPE_INTEGER,
132 .name = "Brightness",
133 .minimum = 0,
134 .maximum = 255,
135 .step = 1,
136 .default_value = DEFAULT_BRIGHTNESS,
139 .id = V4L2_CID_CONTRAST,
140 .type = V4L2_CTRL_TYPE_INTEGER,
141 .name = "Contrast",
142 .minimum = 0,
143 .maximum = 255,
144 .step = 1,
145 .default_value = DEFAULT_CONTRAST,
148 .id = V4L2_CID_SATURATION,
149 .type = V4L2_CTRL_TYPE_INTEGER,
150 .name = "Saturation",
151 .minimum = 0,
152 .maximum = 255,
153 .step = 1,
154 .default_value = DEFAULT_SATURATION,
157 .id = V4L2_CID_HFLIP,
158 .type = V4L2_CTRL_TYPE_BOOLEAN,
159 .name = "Mirror Horizontally",
160 .minimum = 0,
161 .maximum = 1,
162 .step = 1,
163 .default_value = 0,
166 .id = V4L2_CID_VFLIP,
167 .type = V4L2_CTRL_TYPE_BOOLEAN,
168 .name = "Flip Vertically",
169 .minimum = 0,
170 .maximum = 1,
171 .step = 1,
172 .default_value = 0,
175 .id = CPIA2_CID_TARGET_KB,
176 .type = V4L2_CTRL_TYPE_INTEGER,
177 .name = "Target KB",
178 .minimum = 0,
179 .maximum = 255,
180 .step = 1,
181 .default_value = DEFAULT_TARGET_KB,
184 .id = CPIA2_CID_GPIO,
185 .type = V4L2_CTRL_TYPE_INTEGER,
186 .name = "GPIO",
187 .minimum = 0,
188 .maximum = 255,
189 .step = 1,
190 .default_value = 0,
193 .id = CPIA2_CID_FLICKER_MODE,
194 .type = V4L2_CTRL_TYPE_MENU,
195 .name = "Flicker Reduction",
196 .minimum = 0,
197 .maximum = NUM_FLICKER_CONTROLS-1,
198 .step = 1,
199 .default_value = 0,
202 .id = CPIA2_CID_FRAMERATE,
203 .type = V4L2_CTRL_TYPE_MENU,
204 .name = "Framerate",
205 .minimum = 0,
206 .maximum = NUM_FRAMERATE_CONTROLS-1,
207 .step = 1,
208 .default_value = NUM_FRAMERATE_CONTROLS-1,
211 .id = CPIA2_CID_USB_ALT,
212 .type = V4L2_CTRL_TYPE_INTEGER,
213 .name = "USB Alternate",
214 .minimum = USBIF_ISO_1,
215 .maximum = USBIF_ISO_6,
216 .step = 1,
217 .default_value = DEFAULT_ALT,
220 .id = CPIA2_CID_LIGHTS,
221 .type = V4L2_CTRL_TYPE_MENU,
222 .name = "Lights",
223 .minimum = 0,
224 .maximum = NUM_LIGHTS_CONTROLS-1,
225 .step = 1,
226 .default_value = 0,
229 .id = CPIA2_CID_RESET_CAMERA,
230 .type = V4L2_CTRL_TYPE_BUTTON,
231 .name = "Reset Camera",
232 .minimum = 0,
233 .maximum = 0,
234 .step = 0,
235 .default_value = 0,
238 #define NUM_CONTROLS (ARRAY_SIZE(controls))
241 /******************************************************************************
243 * cpia2_open
245 *****************************************************************************/
246 static int cpia2_open(struct inode *inode, struct file *file)
248 struct video_device *dev = video_devdata(file);
249 struct camera_data *cam = video_get_drvdata(dev);
250 int retval = 0;
252 if (!cam) {
253 ERR("Internal error, camera_data not found!\n");
254 return -ENODEV;
257 if(mutex_lock_interruptible(&cam->busy_lock))
258 return -ERESTARTSYS;
260 if(!cam->present) {
261 retval = -ENODEV;
262 goto err_return;
265 if (cam->open_count > 0) {
266 goto skip_init;
269 if (cpia2_allocate_buffers(cam)) {
270 retval = -ENOMEM;
271 goto err_return;
274 /* reset the camera */
275 if (cpia2_reset_camera(cam) < 0) {
276 retval = -EIO;
277 goto err_return;
280 cam->APP_len = 0;
281 cam->COM_len = 0;
283 skip_init:
285 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
286 if(!fh) {
287 retval = -ENOMEM;
288 goto err_return;
290 file->private_data = fh;
291 fh->prio = V4L2_PRIORITY_UNSET;
292 v4l2_prio_open(&cam->prio, &fh->prio);
293 fh->mmapped = 0;
296 ++cam->open_count;
298 cpia2_dbg_dump_registers(cam);
300 err_return:
301 mutex_unlock(&cam->busy_lock);
302 return retval;
305 /******************************************************************************
307 * cpia2_close
309 *****************************************************************************/
310 static int cpia2_close(struct inode *inode, struct file *file)
312 struct video_device *dev = video_devdata(file);
313 struct camera_data *cam = video_get_drvdata(dev);
314 struct cpia2_fh *fh = file->private_data;
316 mutex_lock(&cam->busy_lock);
318 if (cam->present &&
319 (cam->open_count == 1
320 || fh->prio == V4L2_PRIORITY_RECORD
321 )) {
322 cpia2_usb_stream_stop(cam);
324 if(cam->open_count == 1) {
325 /* save camera state for later open */
326 cpia2_save_camera_state(cam);
328 cpia2_set_low_power(cam);
329 cpia2_free_buffers(cam);
334 if(fh->mmapped)
335 cam->mmapped = 0;
336 v4l2_prio_close(&cam->prio,&fh->prio);
337 file->private_data = NULL;
338 kfree(fh);
341 if (--cam->open_count == 0) {
342 cpia2_free_buffers(cam);
343 if (!cam->present) {
344 video_unregister_device(dev);
345 mutex_unlock(&cam->busy_lock);
346 kfree(cam);
347 return 0;
351 mutex_unlock(&cam->busy_lock);
353 return 0;
356 /******************************************************************************
358 * cpia2_v4l_read
360 *****************************************************************************/
361 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
362 loff_t *off)
364 struct video_device *dev = video_devdata(file);
365 struct camera_data *cam = video_get_drvdata(dev);
366 int noblock = file->f_flags&O_NONBLOCK;
368 struct cpia2_fh *fh = file->private_data;
370 if(!cam)
371 return -EINVAL;
373 /* Priority check */
374 if(fh->prio != V4L2_PRIORITY_RECORD) {
375 return -EBUSY;
378 return cpia2_read(cam, buf, count, noblock);
382 /******************************************************************************
384 * cpia2_v4l_poll
386 *****************************************************************************/
387 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
389 struct video_device *dev = video_devdata(filp);
390 struct camera_data *cam = video_get_drvdata(dev);
392 struct cpia2_fh *fh = filp->private_data;
394 if(!cam)
395 return POLLERR;
397 /* Priority check */
398 if(fh->prio != V4L2_PRIORITY_RECORD) {
399 return POLLERR;
402 return cpia2_poll(cam, filp, wait);
406 /******************************************************************************
408 * ioctl_cap_query
410 *****************************************************************************/
411 static int ioctl_cap_query(void *arg, struct camera_data *cam)
413 struct video_capability *vc;
414 int retval = 0;
415 vc = arg;
417 if (cam->params.pnp_id.product == 0x151)
418 strcpy(vc->name, "QX5 Microscope");
419 else
420 strcpy(vc->name, "CPiA2 Camera");
422 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
423 vc->channels = 1;
424 vc->audios = 0;
425 vc->minwidth = 176; /* VIDEOSIZE_QCIF */
426 vc->minheight = 144;
427 switch (cam->params.version.sensor_flags) {
428 case CPIA2_VP_SENSOR_FLAGS_500:
429 vc->maxwidth = STV_IMAGE_VGA_COLS;
430 vc->maxheight = STV_IMAGE_VGA_ROWS;
431 break;
432 case CPIA2_VP_SENSOR_FLAGS_410:
433 vc->maxwidth = STV_IMAGE_CIF_COLS;
434 vc->maxheight = STV_IMAGE_CIF_ROWS;
435 break;
436 default:
437 return -EINVAL;
440 return retval;
443 /******************************************************************************
445 * ioctl_get_channel
447 *****************************************************************************/
448 static int ioctl_get_channel(void *arg)
450 int retval = 0;
451 struct video_channel *v;
452 v = arg;
454 if (v->channel != 0)
455 return -EINVAL;
457 v->channel = 0;
458 strcpy(v->name, "Camera");
459 v->tuners = 0;
460 v->flags = 0;
461 v->type = VIDEO_TYPE_CAMERA;
462 v->norm = 0;
464 return retval;
467 /******************************************************************************
469 * ioctl_set_channel
471 *****************************************************************************/
472 static int ioctl_set_channel(void *arg)
474 struct video_channel *v;
475 int retval = 0;
476 v = arg;
478 if (retval == 0 && v->channel != 0)
479 retval = -EINVAL;
481 return retval;
484 /******************************************************************************
486 * ioctl_set_image_prop
488 *****************************************************************************/
489 static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
491 struct video_picture *vp;
492 int retval = 0;
493 vp = arg;
495 /* brightness, color, contrast need no check 0-65535 */
496 memcpy(&cam->vp, vp, sizeof(*vp));
498 /* update cam->params.colorParams */
499 cam->params.color_params.brightness = vp->brightness / 256;
500 cam->params.color_params.saturation = vp->colour / 256;
501 cam->params.color_params.contrast = vp->contrast / 256;
503 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
504 cam->params.color_params.brightness,
505 cam->params.color_params.saturation,
506 cam->params.color_params.contrast);
508 cpia2_set_color_params(cam);
510 return retval;
513 static int sync(struct camera_data *cam, int frame_nr)
515 struct framebuf *frame = &cam->buffers[frame_nr];
517 while (1) {
518 if (frame->status == FRAME_READY)
519 return 0;
521 if (!cam->streaming) {
522 frame->status = FRAME_READY;
523 frame->length = 0;
524 return 0;
527 mutex_unlock(&cam->busy_lock);
528 wait_event_interruptible(cam->wq_stream,
529 !cam->streaming ||
530 frame->status == FRAME_READY);
531 mutex_lock(&cam->busy_lock);
532 if (signal_pending(current))
533 return -ERESTARTSYS;
534 if(!cam->present)
535 return -ENOTTY;
539 /******************************************************************************
541 * ioctl_set_window_size
543 *****************************************************************************/
544 static int ioctl_set_window_size(void *arg, struct camera_data *cam,
545 struct cpia2_fh *fh)
547 /* copy_from_user, check validity, copy to internal structure */
548 struct video_window *vw;
549 int frame, err;
550 vw = arg;
552 if (vw->clipcount != 0) /* clipping not supported */
553 return -EINVAL;
555 if (vw->clips != NULL) /* clipping not supported */
556 return -EINVAL;
558 /* Ensure that only this process can change the format. */
559 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
560 if(err != 0)
561 return err;
563 cam->pixelformat = V4L2_PIX_FMT_JPEG;
565 /* Be sure to supply the Huffman tables, this isn't MJPEG */
566 cam->params.compression.inhibit_htables = 0;
568 /* we set the video window to something smaller or equal to what
569 * is requested by the user???
571 DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
572 if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
573 cam->vw.width = vw->width;
574 cam->vw.height = vw->height;
575 cam->params.roi.width = vw->width;
576 cam->params.roi.height = vw->height;
577 cpia2_set_format(cam);
580 for (frame = 0; frame < cam->num_frames; ++frame) {
581 if (cam->buffers[frame].status == FRAME_READING)
582 if ((err = sync(cam, frame)) < 0)
583 return err;
585 cam->buffers[frame].status = FRAME_EMPTY;
588 return 0;
591 /******************************************************************************
593 * ioctl_get_mbuf
595 *****************************************************************************/
596 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
598 struct video_mbuf *vm;
599 int i;
600 vm = arg;
602 memset(vm, 0, sizeof(*vm));
603 vm->size = cam->frame_size*cam->num_frames;
604 vm->frames = cam->num_frames;
605 for (i = 0; i < cam->num_frames; i++)
606 vm->offsets[i] = cam->frame_size * i;
608 return 0;
611 /******************************************************************************
613 * ioctl_mcapture
615 *****************************************************************************/
616 static int ioctl_mcapture(void *arg, struct camera_data *cam,
617 struct cpia2_fh *fh)
619 struct video_mmap *vm;
620 int video_size, err;
621 vm = arg;
623 if (vm->frame < 0 || vm->frame >= cam->num_frames)
624 return -EINVAL;
626 /* set video size */
627 video_size = cpia2_match_video_size(vm->width, vm->height);
628 if (cam->video_size < 0) {
629 return -EINVAL;
632 /* Ensure that only this process can change the format. */
633 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
634 if(err != 0)
635 return err;
637 if (video_size != cam->video_size) {
638 cam->video_size = video_size;
639 cam->params.roi.width = vm->width;
640 cam->params.roi.height = vm->height;
641 cpia2_set_format(cam);
644 if (cam->buffers[vm->frame].status == FRAME_READING)
645 if ((err=sync(cam, vm->frame)) < 0)
646 return err;
648 cam->buffers[vm->frame].status = FRAME_EMPTY;
650 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
653 /******************************************************************************
655 * ioctl_sync
657 *****************************************************************************/
658 static int ioctl_sync(void *arg, struct camera_data *cam)
660 int frame;
662 frame = *(int*)arg;
664 if (frame < 0 || frame >= cam->num_frames)
665 return -EINVAL;
667 return sync(cam, frame);
671 /******************************************************************************
673 * ioctl_set_gpio
675 *****************************************************************************/
677 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
679 __u32 gpio_val;
681 gpio_val = *(__u32*) arg;
683 if (gpio_val &~ 0xFFU)
684 return -EINVAL;
686 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
689 /******************************************************************************
691 * ioctl_querycap
693 * V4L2 device capabilities
695 *****************************************************************************/
697 static int ioctl_querycap(void *arg, struct camera_data *cam)
699 struct v4l2_capability *vc = arg;
701 memset(vc, 0, sizeof(*vc));
702 strcpy(vc->driver, "cpia2");
704 if (cam->params.pnp_id.product == 0x151)
705 strcpy(vc->card, "QX5 Microscope");
706 else
707 strcpy(vc->card, "CPiA2 Camera");
708 switch (cam->params.pnp_id.device_type) {
709 case DEVICE_STV_672:
710 strcat(vc->card, " (672/");
711 break;
712 case DEVICE_STV_676:
713 strcat(vc->card, " (676/");
714 break;
715 default:
716 strcat(vc->card, " (???/");
717 break;
719 switch (cam->params.version.sensor_flags) {
720 case CPIA2_VP_SENSOR_FLAGS_404:
721 strcat(vc->card, "404)");
722 break;
723 case CPIA2_VP_SENSOR_FLAGS_407:
724 strcat(vc->card, "407)");
725 break;
726 case CPIA2_VP_SENSOR_FLAGS_409:
727 strcat(vc->card, "409)");
728 break;
729 case CPIA2_VP_SENSOR_FLAGS_410:
730 strcat(vc->card, "410)");
731 break;
732 case CPIA2_VP_SENSOR_FLAGS_500:
733 strcat(vc->card, "500)");
734 break;
735 default:
736 strcat(vc->card, "???)");
737 break;
740 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
741 memset(vc->bus_info,0, sizeof(vc->bus_info));
743 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
744 CPIA2_PATCH_VER);
746 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
747 V4L2_CAP_READWRITE |
748 V4L2_CAP_STREAMING;
750 return 0;
753 /******************************************************************************
755 * ioctl_input
757 * V4L2 input get/set/enumerate
759 *****************************************************************************/
761 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
763 struct v4l2_input *i = arg;
765 if(ioclt_nr != VIDIOC_G_INPUT) {
766 if (i->index != 0)
767 return -EINVAL;
770 memset(i, 0, sizeof(*i));
771 strcpy(i->name, "Camera");
772 i->type = V4L2_INPUT_TYPE_CAMERA;
774 return 0;
777 /******************************************************************************
779 * ioctl_enum_fmt
781 * V4L2 format enumerate
783 *****************************************************************************/
785 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
787 struct v4l2_fmtdesc *f = arg;
788 int index = f->index;
790 if (index < 0 || index > 1)
791 return -EINVAL;
793 memset(f, 0, sizeof(*f));
794 f->index = index;
795 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
796 f->flags = V4L2_FMT_FLAG_COMPRESSED;
797 switch(index) {
798 case 0:
799 strcpy(f->description, "MJPEG");
800 f->pixelformat = V4L2_PIX_FMT_MJPEG;
801 break;
802 case 1:
803 strcpy(f->description, "JPEG");
804 f->pixelformat = V4L2_PIX_FMT_JPEG;
805 break;
806 default:
807 return -EINVAL;
810 return 0;
813 /******************************************************************************
815 * ioctl_try_fmt
817 * V4L2 format try
819 *****************************************************************************/
821 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
823 struct v4l2_format *f = arg;
825 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
826 return -EINVAL;
828 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
829 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
830 return -EINVAL;
832 f->fmt.pix.field = V4L2_FIELD_NONE;
833 f->fmt.pix.bytesperline = 0;
834 f->fmt.pix.sizeimage = cam->frame_size;
835 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
836 f->fmt.pix.priv = 0;
838 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
839 case VIDEOSIZE_VGA:
840 f->fmt.pix.width = 640;
841 f->fmt.pix.height = 480;
842 break;
843 case VIDEOSIZE_CIF:
844 f->fmt.pix.width = 352;
845 f->fmt.pix.height = 288;
846 break;
847 case VIDEOSIZE_QVGA:
848 f->fmt.pix.width = 320;
849 f->fmt.pix.height = 240;
850 break;
851 case VIDEOSIZE_288_216:
852 f->fmt.pix.width = 288;
853 f->fmt.pix.height = 216;
854 break;
855 case VIDEOSIZE_256_192:
856 f->fmt.pix.width = 256;
857 f->fmt.pix.height = 192;
858 break;
859 case VIDEOSIZE_224_168:
860 f->fmt.pix.width = 224;
861 f->fmt.pix.height = 168;
862 break;
863 case VIDEOSIZE_192_144:
864 f->fmt.pix.width = 192;
865 f->fmt.pix.height = 144;
866 break;
867 case VIDEOSIZE_QCIF:
868 default:
869 f->fmt.pix.width = 176;
870 f->fmt.pix.height = 144;
871 break;
874 return 0;
877 /******************************************************************************
879 * ioctl_set_fmt
881 * V4L2 format set
883 *****************************************************************************/
885 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
887 struct v4l2_format *f = arg;
888 int err, frame;
890 err = ioctl_try_fmt(arg, cam);
891 if(err != 0)
892 return err;
894 /* Ensure that only this process can change the format. */
895 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
896 if(err != 0) {
897 return err;
900 cam->pixelformat = f->fmt.pix.pixelformat;
902 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
903 * the missing Huffman table properly. */
904 cam->params.compression.inhibit_htables = 0;
905 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
907 /* we set the video window to something smaller or equal to what
908 * is requested by the user???
910 DBG("Requested width = %d, height = %d\n",
911 f->fmt.pix.width, f->fmt.pix.height);
912 if (f->fmt.pix.width != cam->vw.width ||
913 f->fmt.pix.height != cam->vw.height) {
914 cam->vw.width = f->fmt.pix.width;
915 cam->vw.height = f->fmt.pix.height;
916 cam->params.roi.width = f->fmt.pix.width;
917 cam->params.roi.height = f->fmt.pix.height;
918 cpia2_set_format(cam);
921 for (frame = 0; frame < cam->num_frames; ++frame) {
922 if (cam->buffers[frame].status == FRAME_READING)
923 if ((err = sync(cam, frame)) < 0)
924 return err;
926 cam->buffers[frame].status = FRAME_EMPTY;
929 return 0;
932 /******************************************************************************
934 * ioctl_get_fmt
936 * V4L2 format get
938 *****************************************************************************/
940 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
942 struct v4l2_format *f = arg;
944 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
945 return -EINVAL;
947 f->fmt.pix.width = cam->vw.width;
948 f->fmt.pix.height = cam->vw.height;
949 f->fmt.pix.pixelformat = cam->pixelformat;
950 f->fmt.pix.field = V4L2_FIELD_NONE;
951 f->fmt.pix.bytesperline = 0;
952 f->fmt.pix.sizeimage = cam->frame_size;
953 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
954 f->fmt.pix.priv = 0;
956 return 0;
959 /******************************************************************************
961 * ioctl_cropcap
963 * V4L2 query cropping capabilities
964 * NOTE: cropping is currently disabled
966 *****************************************************************************/
968 static int ioctl_cropcap(void *arg,struct camera_data *cam)
970 struct v4l2_cropcap *c = arg;
972 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
973 return -EINVAL;
975 c->bounds.left = 0;
976 c->bounds.top = 0;
977 c->bounds.width = cam->vw.width;
978 c->bounds.height = cam->vw.height;
979 c->defrect.left = 0;
980 c->defrect.top = 0;
981 c->defrect.width = cam->vw.width;
982 c->defrect.height = cam->vw.height;
983 c->pixelaspect.numerator = 1;
984 c->pixelaspect.denominator = 1;
986 return 0;
989 /******************************************************************************
991 * ioctl_queryctrl
993 * V4L2 query possible control variables
995 *****************************************************************************/
997 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
999 struct v4l2_queryctrl *c = arg;
1000 int i;
1002 for(i=0; i<NUM_CONTROLS; ++i) {
1003 if(c->id == controls[i].id) {
1004 memcpy(c, controls+i, sizeof(*c));
1005 break;
1009 if(i == NUM_CONTROLS)
1010 return -EINVAL;
1012 /* Some devices have additional limitations */
1013 switch(c->id) {
1014 case V4L2_CID_BRIGHTNESS:
1015 /***
1016 * Don't let the register be set to zero - bug in VP4
1017 * flash of full brightness
1018 ***/
1019 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1020 c->minimum = 1;
1021 break;
1022 case V4L2_CID_VFLIP:
1023 // VP5 Only
1024 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
1025 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1026 break;
1027 case CPIA2_CID_FRAMERATE:
1028 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1029 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1030 // Maximum 15fps
1031 int i;
1032 for(i=0; i<c->maximum; ++i) {
1033 if(framerate_controls[i].value ==
1034 CPIA2_VP_FRAMERATE_15) {
1035 c->maximum = i;
1036 c->default_value = i;
1040 break;
1041 case CPIA2_CID_FLICKER_MODE:
1042 // Flicker control only valid for 672.
1043 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1044 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1045 break;
1046 case CPIA2_CID_LIGHTS:
1047 // Light control only valid for the QX5 Microscope.
1048 if(cam->params.pnp_id.product != 0x151)
1049 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1050 break;
1051 default:
1052 break;
1055 return 0;
1058 /******************************************************************************
1060 * ioctl_querymenu
1062 * V4L2 query possible control variables
1064 *****************************************************************************/
1066 static int ioctl_querymenu(void *arg,struct camera_data *cam)
1068 struct v4l2_querymenu *m = arg;
1070 memset(m->name, 0, sizeof(m->name));
1071 m->reserved = 0;
1073 switch(m->id) {
1074 case CPIA2_CID_FLICKER_MODE:
1075 if(m->index < 0 || m->index >= NUM_FLICKER_CONTROLS)
1076 return -EINVAL;
1078 strcpy(m->name, flicker_controls[m->index].name);
1079 break;
1080 case CPIA2_CID_FRAMERATE:
1082 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1083 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1084 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1085 // Maximum 15fps
1086 int i;
1087 for(i=0; i<maximum; ++i) {
1088 if(framerate_controls[i].value ==
1089 CPIA2_VP_FRAMERATE_15)
1090 maximum = i;
1093 if(m->index < 0 || m->index > maximum)
1094 return -EINVAL;
1096 strcpy(m->name, framerate_controls[m->index].name);
1097 break;
1099 case CPIA2_CID_LIGHTS:
1100 if(m->index < 0 || m->index >= NUM_LIGHTS_CONTROLS)
1101 return -EINVAL;
1103 strcpy(m->name, lights_controls[m->index].name);
1104 break;
1105 default:
1106 return -EINVAL;
1109 return 0;
1112 /******************************************************************************
1114 * ioctl_g_ctrl
1116 * V4L2 get the value of a control variable
1118 *****************************************************************************/
1120 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
1122 struct v4l2_control *c = arg;
1124 switch(c->id) {
1125 case V4L2_CID_BRIGHTNESS:
1126 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
1127 TRANSFER_READ, 0);
1128 c->value = cam->params.color_params.brightness;
1129 break;
1130 case V4L2_CID_CONTRAST:
1131 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
1132 TRANSFER_READ, 0);
1133 c->value = cam->params.color_params.contrast;
1134 break;
1135 case V4L2_CID_SATURATION:
1136 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
1137 TRANSFER_READ, 0);
1138 c->value = cam->params.color_params.saturation;
1139 break;
1140 case V4L2_CID_HFLIP:
1141 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1142 TRANSFER_READ, 0);
1143 c->value = (cam->params.vp_params.user_effects &
1144 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
1145 break;
1146 case V4L2_CID_VFLIP:
1147 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1148 TRANSFER_READ, 0);
1149 c->value = (cam->params.vp_params.user_effects &
1150 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
1151 break;
1152 case CPIA2_CID_TARGET_KB:
1153 c->value = cam->params.vc_params.target_kb;
1154 break;
1155 case CPIA2_CID_GPIO:
1156 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1157 TRANSFER_READ, 0);
1158 c->value = cam->params.vp_params.gpio_data;
1159 break;
1160 case CPIA2_CID_FLICKER_MODE:
1162 int i, mode;
1163 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1164 TRANSFER_READ, 0);
1165 if(cam->params.flicker_control.cam_register &
1166 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
1167 mode = NEVER_FLICKER;
1168 } else {
1169 if(cam->params.flicker_control.cam_register &
1170 CPIA2_VP_FLICKER_MODES_50HZ) {
1171 mode = FLICKER_50;
1172 } else {
1173 mode = FLICKER_60;
1176 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
1177 if(flicker_controls[i].value == mode) {
1178 c->value = i;
1179 break;
1182 if(i == NUM_FLICKER_CONTROLS)
1183 return -EINVAL;
1184 break;
1186 case CPIA2_CID_FRAMERATE:
1188 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1189 int i;
1190 for(i=0; i<= maximum; i++) {
1191 if(cam->params.vp_params.frame_rate ==
1192 framerate_controls[i].value)
1193 break;
1195 if(i > maximum)
1196 return -EINVAL;
1197 c->value = i;
1198 break;
1200 case CPIA2_CID_USB_ALT:
1201 c->value = cam->params.camera_state.stream_mode;
1202 break;
1203 case CPIA2_CID_LIGHTS:
1205 int i;
1206 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1207 TRANSFER_READ, 0);
1208 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
1209 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
1210 lights_controls[i].value) {
1211 break;
1214 if(i == NUM_LIGHTS_CONTROLS)
1215 return -EINVAL;
1216 c->value = i;
1217 break;
1219 case CPIA2_CID_RESET_CAMERA:
1220 return -EINVAL;
1221 default:
1222 return -EINVAL;
1225 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1227 return 0;
1230 /******************************************************************************
1232 * ioctl_s_ctrl
1234 * V4L2 set the value of a control variable
1236 *****************************************************************************/
1238 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1240 struct v4l2_control *c = arg;
1241 int i;
1242 int retval = 0;
1244 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1246 /* Check that the value is in range */
1247 for(i=0; i<NUM_CONTROLS; i++) {
1248 if(c->id == controls[i].id) {
1249 if(c->value < controls[i].minimum ||
1250 c->value > controls[i].maximum) {
1251 return -EINVAL;
1253 break;
1256 if(i == NUM_CONTROLS)
1257 return -EINVAL;
1259 switch(c->id) {
1260 case V4L2_CID_BRIGHTNESS:
1261 cpia2_set_brightness(cam, c->value);
1262 break;
1263 case V4L2_CID_CONTRAST:
1264 cpia2_set_contrast(cam, c->value);
1265 break;
1266 case V4L2_CID_SATURATION:
1267 cpia2_set_saturation(cam, c->value);
1268 break;
1269 case V4L2_CID_HFLIP:
1270 cpia2_set_property_mirror(cam, c->value);
1271 break;
1272 case V4L2_CID_VFLIP:
1273 cpia2_set_property_flip(cam, c->value);
1274 break;
1275 case CPIA2_CID_TARGET_KB:
1276 retval = cpia2_set_target_kb(cam, c->value);
1277 break;
1278 case CPIA2_CID_GPIO:
1279 retval = cpia2_set_gpio(cam, c->value);
1280 break;
1281 case CPIA2_CID_FLICKER_MODE:
1282 retval = cpia2_set_flicker_mode(cam,
1283 flicker_controls[c->value].value);
1284 break;
1285 case CPIA2_CID_FRAMERATE:
1286 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1287 break;
1288 case CPIA2_CID_USB_ALT:
1289 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1290 break;
1291 case CPIA2_CID_LIGHTS:
1292 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1293 break;
1294 case CPIA2_CID_RESET_CAMERA:
1295 cpia2_usb_stream_pause(cam);
1296 cpia2_reset_camera(cam);
1297 cpia2_usb_stream_resume(cam);
1298 break;
1299 default:
1300 retval = -EINVAL;
1303 return retval;
1306 /******************************************************************************
1308 * ioctl_g_jpegcomp
1310 * V4L2 get the JPEG compression parameters
1312 *****************************************************************************/
1314 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1316 struct v4l2_jpegcompression *parms = arg;
1318 memset(parms, 0, sizeof(*parms));
1320 parms->quality = 80; // TODO: Can this be made meaningful?
1322 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1323 if(!cam->params.compression.inhibit_htables) {
1324 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1327 parms->APPn = cam->APPn;
1328 parms->APP_len = cam->APP_len;
1329 if(cam->APP_len > 0) {
1330 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1331 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1334 parms->COM_len = cam->COM_len;
1335 if(cam->COM_len > 0) {
1336 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1337 parms->jpeg_markers |= JPEG_MARKER_COM;
1340 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1341 parms->APP_len, parms->COM_len);
1343 return 0;
1346 /******************************************************************************
1348 * ioctl_s_jpegcomp
1350 * V4L2 set the JPEG compression parameters
1351 * NOTE: quality and some jpeg_markers are ignored.
1353 *****************************************************************************/
1355 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1357 struct v4l2_jpegcompression *parms = arg;
1359 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1360 parms->APP_len, parms->COM_len);
1362 cam->params.compression.inhibit_htables =
1363 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1365 if(parms->APP_len != 0) {
1366 if(parms->APP_len > 0 &&
1367 parms->APP_len <= sizeof(cam->APP_data) &&
1368 parms->APPn >= 0 && parms->APPn <= 15) {
1369 cam->APPn = parms->APPn;
1370 cam->APP_len = parms->APP_len;
1371 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1372 } else {
1373 LOG("Bad APPn Params n=%d len=%d\n",
1374 parms->APPn, parms->APP_len);
1375 return -EINVAL;
1377 } else {
1378 cam->APP_len = 0;
1381 if(parms->COM_len != 0) {
1382 if(parms->COM_len > 0 &&
1383 parms->COM_len <= sizeof(cam->COM_data)) {
1384 cam->COM_len = parms->COM_len;
1385 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1386 } else {
1387 LOG("Bad COM_len=%d\n", parms->COM_len);
1388 return -EINVAL;
1392 return 0;
1395 /******************************************************************************
1397 * ioctl_reqbufs
1399 * V4L2 Initiate memory mapping.
1400 * NOTE: The user's request is ignored. For now the buffers are fixed.
1402 *****************************************************************************/
1404 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1406 struct v4l2_requestbuffers *req = arg;
1408 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1409 req->memory != V4L2_MEMORY_MMAP)
1410 return -EINVAL;
1412 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1413 req->count = cam->num_frames;
1414 memset(&req->reserved, 0, sizeof(req->reserved));
1416 return 0;
1419 /******************************************************************************
1421 * ioctl_querybuf
1423 * V4L2 Query memory buffer status.
1425 *****************************************************************************/
1427 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1429 struct v4l2_buffer *buf = arg;
1431 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1432 buf->index > cam->num_frames)
1433 return -EINVAL;
1435 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1436 buf->length = cam->frame_size;
1438 buf->memory = V4L2_MEMORY_MMAP;
1440 if(cam->mmapped)
1441 buf->flags = V4L2_BUF_FLAG_MAPPED;
1442 else
1443 buf->flags = 0;
1445 switch (cam->buffers[buf->index].status) {
1446 case FRAME_EMPTY:
1447 case FRAME_ERROR:
1448 case FRAME_READING:
1449 buf->bytesused = 0;
1450 buf->flags = V4L2_BUF_FLAG_QUEUED;
1451 break;
1452 case FRAME_READY:
1453 buf->bytesused = cam->buffers[buf->index].length;
1454 buf->timestamp = cam->buffers[buf->index].timestamp;
1455 buf->sequence = cam->buffers[buf->index].seq;
1456 buf->flags = V4L2_BUF_FLAG_DONE;
1457 break;
1460 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1461 buf->index, buf->m.offset, buf->flags, buf->sequence,
1462 buf->bytesused);
1464 return 0;
1467 /******************************************************************************
1469 * ioctl_qbuf
1471 * V4L2 User is freeing buffer
1473 *****************************************************************************/
1475 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1477 struct v4l2_buffer *buf = arg;
1479 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1480 buf->memory != V4L2_MEMORY_MMAP ||
1481 buf->index > cam->num_frames)
1482 return -EINVAL;
1484 DBG("QBUF #%d\n", buf->index);
1486 if(cam->buffers[buf->index].status == FRAME_READY)
1487 cam->buffers[buf->index].status = FRAME_EMPTY;
1489 return 0;
1492 /******************************************************************************
1494 * find_earliest_filled_buffer
1496 * Helper for ioctl_dqbuf. Find the next ready buffer.
1498 *****************************************************************************/
1500 static int find_earliest_filled_buffer(struct camera_data *cam)
1502 int i;
1503 int found = -1;
1504 for (i=0; i<cam->num_frames; i++) {
1505 if(cam->buffers[i].status == FRAME_READY) {
1506 if(found < 0) {
1507 found = i;
1508 } else {
1509 /* find which buffer is earlier */
1510 struct timeval *tv1, *tv2;
1511 tv1 = &cam->buffers[i].timestamp;
1512 tv2 = &cam->buffers[found].timestamp;
1513 if(tv1->tv_sec < tv2->tv_sec ||
1514 (tv1->tv_sec == tv2->tv_sec &&
1515 tv1->tv_usec < tv2->tv_usec))
1516 found = i;
1520 return found;
1523 /******************************************************************************
1525 * ioctl_dqbuf
1527 * V4L2 User is asking for a filled buffer.
1529 *****************************************************************************/
1531 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1533 struct v4l2_buffer *buf = arg;
1534 int frame;
1536 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1537 buf->memory != V4L2_MEMORY_MMAP)
1538 return -EINVAL;
1540 frame = find_earliest_filled_buffer(cam);
1542 if(frame < 0 && file->f_flags&O_NONBLOCK)
1543 return -EAGAIN;
1545 if(frame < 0) {
1546 /* Wait for a frame to become available */
1547 struct framebuf *cb=cam->curbuff;
1548 mutex_unlock(&cam->busy_lock);
1549 wait_event_interruptible(cam->wq_stream,
1550 !cam->present ||
1551 (cb=cam->curbuff)->status == FRAME_READY);
1552 mutex_lock(&cam->busy_lock);
1553 if (signal_pending(current))
1554 return -ERESTARTSYS;
1555 if(!cam->present)
1556 return -ENOTTY;
1557 frame = cb->num;
1561 buf->index = frame;
1562 buf->bytesused = cam->buffers[buf->index].length;
1563 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1564 buf->field = V4L2_FIELD_NONE;
1565 buf->timestamp = cam->buffers[buf->index].timestamp;
1566 buf->sequence = cam->buffers[buf->index].seq;
1567 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1568 buf->length = cam->frame_size;
1569 buf->input = 0;
1570 buf->reserved = 0;
1571 memset(&buf->timecode, 0, sizeof(buf->timecode));
1573 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1574 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1576 return 0;
1579 /******************************************************************************
1581 * cpia2_ioctl
1583 *****************************************************************************/
1584 static int cpia2_do_ioctl(struct inode *inode, struct file *file,
1585 unsigned int ioctl_nr, void *arg)
1587 struct video_device *dev = video_devdata(file);
1588 struct camera_data *cam = video_get_drvdata(dev);
1589 int retval = 0;
1591 if (!cam)
1592 return -ENOTTY;
1594 /* make this _really_ smp-safe */
1595 if (mutex_lock_interruptible(&cam->busy_lock))
1596 return -ERESTARTSYS;
1598 if (!cam->present) {
1599 mutex_unlock(&cam->busy_lock);
1600 return -ENODEV;
1603 /* Priority check */
1604 switch (ioctl_nr) {
1605 case VIDIOCSWIN:
1606 case VIDIOCMCAPTURE:
1607 case VIDIOC_S_FMT:
1609 struct cpia2_fh *fh = file->private_data;
1610 retval = v4l2_prio_check(&cam->prio, &fh->prio);
1611 if(retval) {
1612 mutex_unlock(&cam->busy_lock);
1613 return retval;
1615 break;
1617 case VIDIOCGMBUF:
1618 case VIDIOCSYNC:
1620 struct cpia2_fh *fh = file->private_data;
1621 if(fh->prio != V4L2_PRIORITY_RECORD) {
1622 mutex_unlock(&cam->busy_lock);
1623 return -EBUSY;
1625 break;
1627 default:
1628 break;
1631 switch (ioctl_nr) {
1632 case VIDIOCGCAP: /* query capabilities */
1633 retval = ioctl_cap_query(arg, cam);
1634 break;
1636 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
1637 retval = ioctl_get_channel(arg);
1638 break;
1639 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
1640 retval = ioctl_set_channel(arg);
1641 break;
1642 case VIDIOCGPICT: /* image properties */
1643 memcpy(arg, &cam->vp, sizeof(struct video_picture));
1644 break;
1645 case VIDIOCSPICT:
1646 retval = ioctl_set_image_prop(arg, cam);
1647 break;
1648 case VIDIOCGWIN: /* get/set capture window */
1649 memcpy(arg, &cam->vw, sizeof(struct video_window));
1650 break;
1651 case VIDIOCSWIN:
1652 retval = ioctl_set_window_size(arg, cam, file->private_data);
1653 break;
1654 case VIDIOCGMBUF: /* mmap interface */
1655 retval = ioctl_get_mbuf(arg, cam);
1656 break;
1657 case VIDIOCMCAPTURE:
1658 retval = ioctl_mcapture(arg, cam, file->private_data);
1659 break;
1660 case VIDIOCSYNC:
1661 retval = ioctl_sync(arg, cam);
1662 break;
1663 /* pointless to implement overlay with this camera */
1664 case VIDIOCCAPTURE:
1665 case VIDIOCGFBUF:
1666 case VIDIOCSFBUF:
1667 case VIDIOCKEY:
1668 retval = -EINVAL;
1669 break;
1671 /* tuner interface - we have none */
1672 case VIDIOCGTUNER:
1673 case VIDIOCSTUNER:
1674 case VIDIOCGFREQ:
1675 case VIDIOCSFREQ:
1676 retval = -EINVAL;
1677 break;
1679 /* audio interface - we have none */
1680 case VIDIOCGAUDIO:
1681 case VIDIOCSAUDIO:
1682 retval = -EINVAL;
1683 break;
1685 /* CPIA2 extension to Video4Linux API */
1686 case CPIA2_IOC_SET_GPIO:
1687 retval = ioctl_set_gpio(arg, cam);
1688 break;
1689 case VIDIOC_QUERYCAP:
1690 retval = ioctl_querycap(arg,cam);
1691 break;
1693 case VIDIOC_ENUMINPUT:
1694 case VIDIOC_G_INPUT:
1695 case VIDIOC_S_INPUT:
1696 retval = ioctl_input(ioctl_nr, arg,cam);
1697 break;
1699 case VIDIOC_ENUM_FMT:
1700 retval = ioctl_enum_fmt(arg,cam);
1701 break;
1702 case VIDIOC_TRY_FMT:
1703 retval = ioctl_try_fmt(arg,cam);
1704 break;
1705 case VIDIOC_G_FMT:
1706 retval = ioctl_get_fmt(arg,cam);
1707 break;
1708 case VIDIOC_S_FMT:
1709 retval = ioctl_set_fmt(arg,cam,file->private_data);
1710 break;
1712 case VIDIOC_CROPCAP:
1713 retval = ioctl_cropcap(arg,cam);
1714 break;
1715 case VIDIOC_G_CROP:
1716 case VIDIOC_S_CROP:
1717 // TODO: I think cropping can be implemented - SJB
1718 retval = -EINVAL;
1719 break;
1721 case VIDIOC_QUERYCTRL:
1722 retval = ioctl_queryctrl(arg,cam);
1723 break;
1724 case VIDIOC_QUERYMENU:
1725 retval = ioctl_querymenu(arg,cam);
1726 break;
1727 case VIDIOC_G_CTRL:
1728 retval = ioctl_g_ctrl(arg,cam);
1729 break;
1730 case VIDIOC_S_CTRL:
1731 retval = ioctl_s_ctrl(arg,cam);
1732 break;
1734 case VIDIOC_G_JPEGCOMP:
1735 retval = ioctl_g_jpegcomp(arg,cam);
1736 break;
1737 case VIDIOC_S_JPEGCOMP:
1738 retval = ioctl_s_jpegcomp(arg,cam);
1739 break;
1741 case VIDIOC_G_PRIORITY:
1743 struct cpia2_fh *fh = file->private_data;
1744 *(enum v4l2_priority*)arg = fh->prio;
1745 break;
1747 case VIDIOC_S_PRIORITY:
1749 struct cpia2_fh *fh = file->private_data;
1750 enum v4l2_priority prio;
1751 prio = *(enum v4l2_priority*)arg;
1752 if(cam->streaming &&
1753 prio != fh->prio &&
1754 fh->prio == V4L2_PRIORITY_RECORD) {
1755 /* Can't drop record priority while streaming */
1756 retval = -EBUSY;
1757 } else if(prio == V4L2_PRIORITY_RECORD &&
1758 prio != fh->prio &&
1759 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1760 /* Only one program can record at a time */
1761 retval = -EBUSY;
1762 } else {
1763 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1765 break;
1768 case VIDIOC_REQBUFS:
1769 retval = ioctl_reqbufs(arg,cam);
1770 break;
1771 case VIDIOC_QUERYBUF:
1772 retval = ioctl_querybuf(arg,cam);
1773 break;
1774 case VIDIOC_QBUF:
1775 retval = ioctl_qbuf(arg,cam);
1776 break;
1777 case VIDIOC_DQBUF:
1778 retval = ioctl_dqbuf(arg,cam,file);
1779 break;
1780 case VIDIOC_STREAMON:
1782 int type;
1783 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1784 type = *(int*)arg;
1785 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1786 retval = -EINVAL;
1788 if(!cam->streaming) {
1789 retval = cpia2_usb_stream_start(cam,
1790 cam->params.camera_state.stream_mode);
1791 } else {
1792 retval = -EINVAL;
1795 break;
1797 case VIDIOC_STREAMOFF:
1799 int type;
1800 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1801 type = *(int*)arg;
1802 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1803 retval = -EINVAL;
1805 if(cam->streaming) {
1806 retval = cpia2_usb_stream_stop(cam);
1807 } else {
1808 retval = -EINVAL;
1811 break;
1814 case VIDIOC_ENUMOUTPUT:
1815 case VIDIOC_G_OUTPUT:
1816 case VIDIOC_S_OUTPUT:
1817 case VIDIOC_G_MODULATOR:
1818 case VIDIOC_S_MODULATOR:
1820 case VIDIOC_ENUMAUDIO:
1821 case VIDIOC_G_AUDIO:
1822 case VIDIOC_S_AUDIO:
1824 case VIDIOC_ENUMAUDOUT:
1825 case VIDIOC_G_AUDOUT:
1826 case VIDIOC_S_AUDOUT:
1828 case VIDIOC_ENUMSTD:
1829 case VIDIOC_QUERYSTD:
1830 case VIDIOC_G_STD:
1831 case VIDIOC_S_STD:
1833 case VIDIOC_G_TUNER:
1834 case VIDIOC_S_TUNER:
1835 case VIDIOC_G_FREQUENCY:
1836 case VIDIOC_S_FREQUENCY:
1838 case VIDIOC_OVERLAY:
1839 case VIDIOC_G_FBUF:
1840 case VIDIOC_S_FBUF:
1842 case VIDIOC_G_PARM:
1843 case VIDIOC_S_PARM:
1844 retval = -EINVAL;
1845 break;
1846 default:
1847 retval = -ENOIOCTLCMD;
1848 break;
1851 mutex_unlock(&cam->busy_lock);
1852 return retval;
1855 static int cpia2_ioctl(struct inode *inode, struct file *file,
1856 unsigned int ioctl_nr, unsigned long iarg)
1858 return video_usercopy(inode, file, ioctl_nr, iarg, cpia2_do_ioctl);
1861 /******************************************************************************
1863 * cpia2_mmap
1865 *****************************************************************************/
1866 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1868 int retval;
1869 struct video_device *dev = video_devdata(file);
1870 struct camera_data *cam = video_get_drvdata(dev);
1872 /* Priority check */
1873 struct cpia2_fh *fh = file->private_data;
1874 if(fh->prio != V4L2_PRIORITY_RECORD) {
1875 return -EBUSY;
1878 retval = cpia2_remap_buffer(cam, area);
1880 if(!retval)
1881 fh->mmapped = 1;
1882 return retval;
1885 /******************************************************************************
1887 * reset_camera_struct_v4l
1889 * Sets all values to the defaults
1890 *****************************************************************************/
1891 static void reset_camera_struct_v4l(struct camera_data *cam)
1893 /***
1894 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1895 * Ioctl. Here, just do the window and picture stucts.
1896 ***/
1897 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
1898 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
1899 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
1900 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
1902 cam->vw.x = 0;
1903 cam->vw.y = 0;
1904 cam->vw.width = cam->params.roi.width;
1905 cam->vw.height = cam->params.roi.height;
1906 cam->vw.flags = 0;
1907 cam->vw.clipcount = 0;
1909 cam->frame_size = buffer_size;
1910 cam->num_frames = num_buffers;
1912 /* FlickerModes */
1913 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1914 cam->params.flicker_control.mains_frequency = flicker_freq;
1916 /* streamMode */
1917 cam->params.camera_state.stream_mode = alternate;
1919 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1920 v4l2_prio_init(&cam->prio);
1921 return;
1924 /***
1925 * The v4l video device structure initialized for this device
1926 ***/
1927 static const struct file_operations fops_template = {
1928 .owner = THIS_MODULE,
1929 .open = cpia2_open,
1930 .release = cpia2_close,
1931 .read = cpia2_v4l_read,
1932 .poll = cpia2_v4l_poll,
1933 .ioctl = cpia2_ioctl,
1934 .llseek = no_llseek,
1935 .compat_ioctl = v4l_compat_ioctl32,
1936 .mmap = cpia2_mmap,
1939 static struct video_device cpia2_template = {
1940 /* I could not find any place for the old .initialize initializer?? */
1941 .owner= THIS_MODULE,
1942 .name= "CPiA2 Camera",
1943 .type= VID_TYPE_CAPTURE,
1944 .type2 = V4L2_CAP_VIDEO_CAPTURE |
1945 V4L2_CAP_STREAMING,
1946 .hardware= VID_HARDWARE_CPIA2,
1947 .minor= -1,
1948 .fops= &fops_template,
1949 .release= video_device_release,
1952 /******************************************************************************
1954 * cpia2_register_camera
1956 *****************************************************************************/
1957 int cpia2_register_camera(struct camera_data *cam)
1959 cam->vdev = video_device_alloc();
1960 if(!cam->vdev)
1961 return -ENOMEM;
1963 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1964 video_set_drvdata(cam->vdev, cam);
1966 reset_camera_struct_v4l(cam);
1968 /* register v4l device */
1969 if (video_register_device
1970 (cam->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1971 ERR("video_register_device failed\n");
1972 video_device_release(cam->vdev);
1973 return -ENODEV;
1976 return 0;
1979 /******************************************************************************
1981 * cpia2_unregister_camera
1983 *****************************************************************************/
1984 void cpia2_unregister_camera(struct camera_data *cam)
1986 if (!cam->open_count) {
1987 video_unregister_device(cam->vdev);
1988 } else {
1989 LOG("/dev/video%d removed while open, "
1990 "deferring video_unregister_device\n",
1991 cam->vdev->minor);
1995 /******************************************************************************
1997 * check_parameters
1999 * Make sure that all user-supplied parameters are sensible
2000 *****************************************************************************/
2001 static void __init check_parameters(void)
2003 if(buffer_size < PAGE_SIZE) {
2004 buffer_size = PAGE_SIZE;
2005 LOG("buffer_size too small, setting to %d\n", buffer_size);
2006 } else if(buffer_size > 1024*1024) {
2007 /* arbitrary upper limiit */
2008 buffer_size = 1024*1024;
2009 LOG("buffer_size ridiculously large, setting to %d\n",
2010 buffer_size);
2011 } else {
2012 buffer_size += PAGE_SIZE-1;
2013 buffer_size &= ~(PAGE_SIZE-1);
2016 if(num_buffers < 1) {
2017 num_buffers = 1;
2018 LOG("num_buffers too small, setting to %d\n", num_buffers);
2019 } else if(num_buffers > VIDEO_MAX_FRAME) {
2020 num_buffers = VIDEO_MAX_FRAME;
2021 LOG("num_buffers too large, setting to %d\n", num_buffers);
2024 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
2025 alternate = DEFAULT_ALT;
2026 LOG("alternate specified is invalid, using %d\n", alternate);
2029 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
2030 flicker_mode = NEVER_FLICKER;
2031 LOG("Flicker mode specified is invalid, using %d\n",
2032 flicker_mode);
2035 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
2036 flicker_freq = FLICKER_60;
2037 LOG("Flicker mode specified is invalid, using %d\n",
2038 flicker_freq);
2041 if(video_nr < -1 || video_nr > 64) {
2042 video_nr = -1;
2043 LOG("invalid video_nr specified, must be -1 to 64\n");
2046 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2047 num_buffers, buffer_size, alternate);
2050 /************ Module Stuff ***************/
2053 /******************************************************************************
2055 * cpia2_init/module_init
2057 *****************************************************************************/
2058 static int __init cpia2_init(void)
2060 LOG("%s v%d.%d.%d\n",
2061 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
2062 check_parameters();
2063 cpia2_usb_init();
2064 return 0;
2068 /******************************************************************************
2070 * cpia2_exit/module_exit
2072 *****************************************************************************/
2073 static void __exit cpia2_exit(void)
2075 cpia2_usb_cleanup();
2076 schedule_timeout(2 * HZ);
2079 module_init(cpia2_init);
2080 module_exit(cpia2_exit);