V4L/DVB (4154): Fix use-after-free bug in cpia2 driver
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / media / video / cpia2 / cpia2_v4l.c
blobd129db57fcd4bab57861528b200092326bf86bc4
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>
34 #include <linux/config.h>
36 #include <linux/module.h>
37 #include <linux/time.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/init.h>
41 #include <linux/moduleparam.h>
43 #include "cpia2.h"
44 #include "cpia2dev.h"
47 //#define _CPIA2_DEBUG_
49 #define MAKE_STRING_1(x) #x
50 #define MAKE_STRING(x) MAKE_STRING_1(x)
52 static int video_nr = -1;
53 module_param(video_nr, int, 0);
54 MODULE_PARM_DESC(video_nr,"video device to register (0=/dev/video0, etc)");
56 static int buffer_size = 68*1024;
57 module_param(buffer_size, int, 0);
58 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
60 static int num_buffers = 3;
61 module_param(num_buffers, int, 0);
62 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
63 MAKE_STRING(VIDEO_MAX_FRAME) ", default 3)");
65 static int alternate = DEFAULT_ALT;
66 module_param(alternate, int, 0);
67 MODULE_PARM_DESC(alternate, "USB Alternate (" MAKE_STRING(USBIF_ISO_1) "-"
68 MAKE_STRING(USBIF_ISO_6) ", default "
69 MAKE_STRING(DEFAULT_ALT) ")");
71 static int flicker_freq = 60;
72 module_param(flicker_freq, int, 0);
73 MODULE_PARM_DESC(flicker_freq, "Flicker frequency (" MAKE_STRING(50) "or"
74 MAKE_STRING(60) ", default "
75 MAKE_STRING(60) ")");
77 static int flicker_mode = NEVER_FLICKER;
78 module_param(flicker_mode, int, 0);
79 MODULE_PARM_DESC(flicker_mode,
80 "Flicker supression (" MAKE_STRING(NEVER_FLICKER) "or"
81 MAKE_STRING(ANTI_FLICKER_ON) ", default "
82 MAKE_STRING(NEVER_FLICKER) ")");
84 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
85 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
86 MODULE_SUPPORTED_DEVICE("video");
87 MODULE_LICENSE("GPL");
89 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
91 #ifndef VID_HARDWARE_CPIA2
92 #error "VID_HARDWARE_CPIA2 should have been defined in linux/videodev.h"
93 #endif
95 struct control_menu_info {
96 int value;
97 char name[32];
100 static struct control_menu_info framerate_controls[] =
102 { CPIA2_VP_FRAMERATE_6_25, "6.25 fps" },
103 { CPIA2_VP_FRAMERATE_7_5, "7.5 fps" },
104 { CPIA2_VP_FRAMERATE_12_5, "12.5 fps" },
105 { CPIA2_VP_FRAMERATE_15, "15 fps" },
106 { CPIA2_VP_FRAMERATE_25, "25 fps" },
107 { CPIA2_VP_FRAMERATE_30, "30 fps" },
109 #define NUM_FRAMERATE_CONTROLS (sizeof(framerate_controls)/sizeof(framerate_controls[0]))
111 static struct control_menu_info flicker_controls[] =
113 { NEVER_FLICKER, "Off" },
114 { FLICKER_50, "50 Hz" },
115 { FLICKER_60, "60 Hz" },
117 #define NUM_FLICKER_CONTROLS (sizeof(flicker_controls)/sizeof(flicker_controls[0]))
119 static struct control_menu_info lights_controls[] =
121 { 0, "Off" },
122 { 64, "Top" },
123 { 128, "Bottom" },
124 { 192, "Both" },
126 #define NUM_LIGHTS_CONTROLS (sizeof(lights_controls)/sizeof(lights_controls[0]))
127 #define GPIO_LIGHTS_MASK 192
129 static struct v4l2_queryctrl controls[] = {
131 .id = V4L2_CID_BRIGHTNESS,
132 .type = V4L2_CTRL_TYPE_INTEGER,
133 .name = "Brightness",
134 .minimum = 0,
135 .maximum = 255,
136 .step = 1,
137 .default_value = DEFAULT_BRIGHTNESS,
140 .id = V4L2_CID_CONTRAST,
141 .type = V4L2_CTRL_TYPE_INTEGER,
142 .name = "Contrast",
143 .minimum = 0,
144 .maximum = 255,
145 .step = 1,
146 .default_value = DEFAULT_CONTRAST,
149 .id = V4L2_CID_SATURATION,
150 .type = V4L2_CTRL_TYPE_INTEGER,
151 .name = "Saturation",
152 .minimum = 0,
153 .maximum = 255,
154 .step = 1,
155 .default_value = DEFAULT_SATURATION,
158 .id = V4L2_CID_HFLIP,
159 .type = V4L2_CTRL_TYPE_BOOLEAN,
160 .name = "Mirror Horizontally",
161 .minimum = 0,
162 .maximum = 1,
163 .step = 1,
164 .default_value = 0,
167 .id = V4L2_CID_VFLIP,
168 .type = V4L2_CTRL_TYPE_BOOLEAN,
169 .name = "Flip Vertically",
170 .minimum = 0,
171 .maximum = 1,
172 .step = 1,
173 .default_value = 0,
176 .id = CPIA2_CID_TARGET_KB,
177 .type = V4L2_CTRL_TYPE_INTEGER,
178 .name = "Target KB",
179 .minimum = 0,
180 .maximum = 255,
181 .step = 1,
182 .default_value = DEFAULT_TARGET_KB,
185 .id = CPIA2_CID_GPIO,
186 .type = V4L2_CTRL_TYPE_INTEGER,
187 .name = "GPIO",
188 .minimum = 0,
189 .maximum = 255,
190 .step = 1,
191 .default_value = 0,
194 .id = CPIA2_CID_FLICKER_MODE,
195 .type = V4L2_CTRL_TYPE_MENU,
196 .name = "Flicker Reduction",
197 .minimum = 0,
198 .maximum = NUM_FLICKER_CONTROLS-1,
199 .step = 1,
200 .default_value = 0,
203 .id = CPIA2_CID_FRAMERATE,
204 .type = V4L2_CTRL_TYPE_MENU,
205 .name = "Framerate",
206 .minimum = 0,
207 .maximum = NUM_FRAMERATE_CONTROLS-1,
208 .step = 1,
209 .default_value = NUM_FRAMERATE_CONTROLS-1,
212 .id = CPIA2_CID_USB_ALT,
213 .type = V4L2_CTRL_TYPE_INTEGER,
214 .name = "USB Alternate",
215 .minimum = USBIF_ISO_1,
216 .maximum = USBIF_ISO_6,
217 .step = 1,
218 .default_value = DEFAULT_ALT,
221 .id = CPIA2_CID_LIGHTS,
222 .type = V4L2_CTRL_TYPE_MENU,
223 .name = "Lights",
224 .minimum = 0,
225 .maximum = NUM_LIGHTS_CONTROLS-1,
226 .step = 1,
227 .default_value = 0,
230 .id = CPIA2_CID_RESET_CAMERA,
231 .type = V4L2_CTRL_TYPE_BUTTON,
232 .name = "Reset Camera",
233 .minimum = 0,
234 .maximum = 0,
235 .step = 0,
236 .default_value = 0,
239 #define NUM_CONTROLS (sizeof(controls)/sizeof(controls[0]))
242 /******************************************************************************
244 * cpia2_open
246 *****************************************************************************/
247 static int cpia2_open(struct inode *inode, struct file *file)
249 struct video_device *dev = video_devdata(file);
250 struct camera_data *cam = video_get_drvdata(dev);
251 int retval = 0;
253 if (!cam) {
254 ERR("Internal error, camera_data not found!\n");
255 return -ENODEV;
258 if(mutex_lock_interruptible(&cam->busy_lock))
259 return -ERESTARTSYS;
261 if(!cam->present) {
262 retval = -ENODEV;
263 goto err_return;
266 if (cam->open_count > 0) {
267 goto skip_init;
270 if (cpia2_allocate_buffers(cam)) {
271 retval = -ENOMEM;
272 goto err_return;
275 /* reset the camera */
276 if (cpia2_reset_camera(cam) < 0) {
277 retval = -EIO;
278 goto err_return;
281 cam->APP_len = 0;
282 cam->COM_len = 0;
284 skip_init:
286 struct cpia2_fh *fh = kmalloc(sizeof(*fh),GFP_KERNEL);
287 if(!fh) {
288 retval = -ENOMEM;
289 goto err_return;
291 file->private_data = fh;
292 fh->prio = V4L2_PRIORITY_UNSET;
293 v4l2_prio_open(&cam->prio, &fh->prio);
294 fh->mmapped = 0;
297 ++cam->open_count;
299 cpia2_dbg_dump_registers(cam);
301 err_return:
302 mutex_unlock(&cam->busy_lock);
303 return retval;
306 /******************************************************************************
308 * cpia2_close
310 *****************************************************************************/
311 static int cpia2_close(struct inode *inode, struct file *file)
313 struct video_device *dev = video_devdata(file);
314 struct camera_data *cam = video_get_drvdata(dev);
315 struct cpia2_fh *fh = file->private_data;
317 mutex_lock(&cam->busy_lock);
319 if (cam->present &&
320 (cam->open_count == 1
321 || fh->prio == V4L2_PRIORITY_RECORD
322 )) {
323 cpia2_usb_stream_stop(cam);
325 if(cam->open_count == 1) {
326 /* save camera state for later open */
327 cpia2_save_camera_state(cam);
329 cpia2_set_low_power(cam);
330 cpia2_free_buffers(cam);
335 if(fh->mmapped)
336 cam->mmapped = 0;
337 v4l2_prio_close(&cam->prio,&fh->prio);
338 file->private_data = NULL;
339 kfree(fh);
342 if (--cam->open_count == 0) {
343 cpia2_free_buffers(cam);
344 if (!cam->present) {
345 video_unregister_device(dev);
346 mutex_unlock(&cam->busy_lock);
347 kfree(cam);
348 return 0;
352 mutex_unlock(&cam->busy_lock);
354 return 0;
357 /******************************************************************************
359 * cpia2_v4l_read
361 *****************************************************************************/
362 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
363 loff_t *off)
365 struct video_device *dev = video_devdata(file);
366 struct camera_data *cam = video_get_drvdata(dev);
367 int noblock = file->f_flags&O_NONBLOCK;
369 struct cpia2_fh *fh = file->private_data;
371 if(!cam)
372 return -EINVAL;
374 /* Priority check */
375 if(fh->prio != V4L2_PRIORITY_RECORD) {
376 return -EBUSY;
379 return cpia2_read(cam, buf, count, noblock);
383 /******************************************************************************
385 * cpia2_v4l_poll
387 *****************************************************************************/
388 static unsigned int cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
390 struct video_device *dev = video_devdata(filp);
391 struct camera_data *cam = video_get_drvdata(dev);
393 struct cpia2_fh *fh = filp->private_data;
395 if(!cam)
396 return POLLERR;
398 /* Priority check */
399 if(fh->prio != V4L2_PRIORITY_RECORD) {
400 return POLLERR;
403 return cpia2_poll(cam, filp, wait);
407 /******************************************************************************
409 * ioctl_cap_query
411 *****************************************************************************/
412 static int ioctl_cap_query(void *arg, struct camera_data *cam)
414 struct video_capability *vc;
415 int retval = 0;
416 vc = arg;
418 if (cam->params.pnp_id.product == 0x151)
419 strcpy(vc->name, "QX5 Microscope");
420 else
421 strcpy(vc->name, "CPiA2 Camera");
423 vc->type = VID_TYPE_CAPTURE | VID_TYPE_MJPEG_ENCODER;
424 vc->channels = 1;
425 vc->audios = 0;
426 vc->minwidth = 176; /* VIDEOSIZE_QCIF */
427 vc->minheight = 144;
428 switch (cam->params.version.sensor_flags) {
429 case CPIA2_VP_SENSOR_FLAGS_500:
430 vc->maxwidth = STV_IMAGE_VGA_COLS;
431 vc->maxheight = STV_IMAGE_VGA_ROWS;
432 break;
433 case CPIA2_VP_SENSOR_FLAGS_410:
434 vc->maxwidth = STV_IMAGE_CIF_COLS;
435 vc->maxheight = STV_IMAGE_CIF_ROWS;
436 break;
437 default:
438 return -EINVAL;
441 return retval;
444 /******************************************************************************
446 * ioctl_get_channel
448 *****************************************************************************/
449 static int ioctl_get_channel(void *arg)
451 int retval = 0;
452 struct video_channel *v;
453 v = arg;
455 if (v->channel != 0)
456 return -EINVAL;
458 v->channel = 0;
459 strcpy(v->name, "Camera");
460 v->tuners = 0;
461 v->flags = 0;
462 v->type = VIDEO_TYPE_CAMERA;
463 v->norm = 0;
465 return retval;
468 /******************************************************************************
470 * ioctl_set_channel
472 *****************************************************************************/
473 static int ioctl_set_channel(void *arg)
475 struct video_channel *v;
476 int retval = 0;
477 v = arg;
479 if (retval == 0 && v->channel != 0)
480 retval = -EINVAL;
482 return retval;
485 /******************************************************************************
487 * ioctl_set_image_prop
489 *****************************************************************************/
490 static int ioctl_set_image_prop(void *arg, struct camera_data *cam)
492 struct video_picture *vp;
493 int retval = 0;
494 vp = arg;
496 /* brightness, color, contrast need no check 0-65535 */
497 memcpy(&cam->vp, vp, sizeof(*vp));
499 /* update cam->params.colorParams */
500 cam->params.color_params.brightness = vp->brightness / 256;
501 cam->params.color_params.saturation = vp->colour / 256;
502 cam->params.color_params.contrast = vp->contrast / 256;
504 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
505 cam->params.color_params.brightness,
506 cam->params.color_params.saturation,
507 cam->params.color_params.contrast);
509 cpia2_set_color_params(cam);
511 return retval;
514 static int sync(struct camera_data *cam, int frame_nr)
516 struct framebuf *frame = &cam->buffers[frame_nr];
518 while (1) {
519 if (frame->status == FRAME_READY)
520 return 0;
522 if (!cam->streaming) {
523 frame->status = FRAME_READY;
524 frame->length = 0;
525 return 0;
528 mutex_unlock(&cam->busy_lock);
529 wait_event_interruptible(cam->wq_stream,
530 !cam->streaming ||
531 frame->status == FRAME_READY);
532 mutex_lock(&cam->busy_lock);
533 if (signal_pending(current))
534 return -ERESTARTSYS;
535 if(!cam->present)
536 return -ENOTTY;
540 /******************************************************************************
542 * ioctl_set_window_size
544 *****************************************************************************/
545 static int ioctl_set_window_size(void *arg, struct camera_data *cam,
546 struct cpia2_fh *fh)
548 /* copy_from_user, check validity, copy to internal structure */
549 struct video_window *vw;
550 int frame, err;
551 vw = arg;
553 if (vw->clipcount != 0) /* clipping not supported */
554 return -EINVAL;
556 if (vw->clips != NULL) /* clipping not supported */
557 return -EINVAL;
559 /* Ensure that only this process can change the format. */
560 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
561 if(err != 0)
562 return err;
564 cam->pixelformat = V4L2_PIX_FMT_JPEG;
566 /* Be sure to supply the Huffman tables, this isn't MJPEG */
567 cam->params.compression.inhibit_htables = 0;
569 /* we set the video window to something smaller or equal to what
570 * is requested by the user???
572 DBG("Requested width = %d, height = %d\n", vw->width, vw->height);
573 if (vw->width != cam->vw.width || vw->height != cam->vw.height) {
574 cam->vw.width = vw->width;
575 cam->vw.height = vw->height;
576 cam->params.roi.width = vw->width;
577 cam->params.roi.height = vw->height;
578 cpia2_set_format(cam);
581 for (frame = 0; frame < cam->num_frames; ++frame) {
582 if (cam->buffers[frame].status == FRAME_READING)
583 if ((err = sync(cam, frame)) < 0)
584 return err;
586 cam->buffers[frame].status = FRAME_EMPTY;
589 return 0;
592 /******************************************************************************
594 * ioctl_get_mbuf
596 *****************************************************************************/
597 static int ioctl_get_mbuf(void *arg, struct camera_data *cam)
599 struct video_mbuf *vm;
600 int i;
601 vm = arg;
603 memset(vm, 0, sizeof(*vm));
604 vm->size = cam->frame_size*cam->num_frames;
605 vm->frames = cam->num_frames;
606 for (i = 0; i < cam->num_frames; i++)
607 vm->offsets[i] = cam->frame_size * i;
609 return 0;
612 /******************************************************************************
614 * ioctl_mcapture
616 *****************************************************************************/
617 static int ioctl_mcapture(void *arg, struct camera_data *cam,
618 struct cpia2_fh *fh)
620 struct video_mmap *vm;
621 int video_size, err;
622 vm = arg;
624 if (vm->frame < 0 || vm->frame >= cam->num_frames)
625 return -EINVAL;
627 /* set video size */
628 video_size = cpia2_match_video_size(vm->width, vm->height);
629 if (cam->video_size < 0) {
630 return -EINVAL;
633 /* Ensure that only this process can change the format. */
634 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
635 if(err != 0)
636 return err;
638 if (video_size != cam->video_size) {
639 cam->video_size = video_size;
640 cam->params.roi.width = vm->width;
641 cam->params.roi.height = vm->height;
642 cpia2_set_format(cam);
645 if (cam->buffers[vm->frame].status == FRAME_READING)
646 if ((err=sync(cam, vm->frame)) < 0)
647 return err;
649 cam->buffers[vm->frame].status = FRAME_EMPTY;
651 return cpia2_usb_stream_start(cam,cam->params.camera_state.stream_mode);
654 /******************************************************************************
656 * ioctl_sync
658 *****************************************************************************/
659 static int ioctl_sync(void *arg, struct camera_data *cam)
661 int frame;
663 frame = *(int*)arg;
665 if (frame < 0 || frame >= cam->num_frames)
666 return -EINVAL;
668 return sync(cam, frame);
672 /******************************************************************************
674 * ioctl_set_gpio
676 *****************************************************************************/
678 static int ioctl_set_gpio(void *arg, struct camera_data *cam)
680 __u32 gpio_val;
682 gpio_val = *(__u32*) arg;
684 if (gpio_val &~ 0xFFU)
685 return -EINVAL;
687 return cpia2_set_gpio(cam, (unsigned char)gpio_val);
690 /******************************************************************************
692 * ioctl_querycap
694 * V4L2 device capabilities
696 *****************************************************************************/
698 static int ioctl_querycap(void *arg, struct camera_data *cam)
700 struct v4l2_capability *vc = arg;
702 memset(vc, 0, sizeof(*vc));
703 strcpy(vc->driver, "cpia2");
705 if (cam->params.pnp_id.product == 0x151)
706 strcpy(vc->card, "QX5 Microscope");
707 else
708 strcpy(vc->card, "CPiA2 Camera");
709 switch (cam->params.pnp_id.device_type) {
710 case DEVICE_STV_672:
711 strcat(vc->card, " (672/");
712 break;
713 case DEVICE_STV_676:
714 strcat(vc->card, " (676/");
715 break;
716 default:
717 strcat(vc->card, " (???/");
718 break;
720 switch (cam->params.version.sensor_flags) {
721 case CPIA2_VP_SENSOR_FLAGS_404:
722 strcat(vc->card, "404)");
723 break;
724 case CPIA2_VP_SENSOR_FLAGS_407:
725 strcat(vc->card, "407)");
726 break;
727 case CPIA2_VP_SENSOR_FLAGS_409:
728 strcat(vc->card, "409)");
729 break;
730 case CPIA2_VP_SENSOR_FLAGS_410:
731 strcat(vc->card, "410)");
732 break;
733 case CPIA2_VP_SENSOR_FLAGS_500:
734 strcat(vc->card, "500)");
735 break;
736 default:
737 strcat(vc->card, "???)");
738 break;
741 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
742 memset(vc->bus_info,0, sizeof(vc->bus_info));
744 vc->version = KERNEL_VERSION(CPIA2_MAJ_VER, CPIA2_MIN_VER,
745 CPIA2_PATCH_VER);
747 vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
748 V4L2_CAP_READWRITE |
749 V4L2_CAP_STREAMING;
751 return 0;
754 /******************************************************************************
756 * ioctl_input
758 * V4L2 input get/set/enumerate
760 *****************************************************************************/
762 static int ioctl_input(unsigned int ioclt_nr,void *arg,struct camera_data *cam)
764 struct v4l2_input *i = arg;
766 if(ioclt_nr != VIDIOC_G_INPUT) {
767 if (i->index != 0)
768 return -EINVAL;
771 memset(i, 0, sizeof(*i));
772 strcpy(i->name, "Camera");
773 i->type = V4L2_INPUT_TYPE_CAMERA;
775 return 0;
778 /******************************************************************************
780 * ioctl_enum_fmt
782 * V4L2 format enumerate
784 *****************************************************************************/
786 static int ioctl_enum_fmt(void *arg,struct camera_data *cam)
788 struct v4l2_fmtdesc *f = arg;
789 int index = f->index;
791 if (index < 0 || index > 1)
792 return -EINVAL;
794 memset(f, 0, sizeof(*f));
795 f->index = index;
796 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
797 f->flags = V4L2_FMT_FLAG_COMPRESSED;
798 switch(index) {
799 case 0:
800 strcpy(f->description, "MJPEG");
801 f->pixelformat = V4L2_PIX_FMT_MJPEG;
802 break;
803 case 1:
804 strcpy(f->description, "JPEG");
805 f->pixelformat = V4L2_PIX_FMT_JPEG;
806 break;
807 default:
808 return -EINVAL;
811 return 0;
814 /******************************************************************************
816 * ioctl_try_fmt
818 * V4L2 format try
820 *****************************************************************************/
822 static int ioctl_try_fmt(void *arg,struct camera_data *cam)
824 struct v4l2_format *f = arg;
826 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
827 return -EINVAL;
829 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
830 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
831 return -EINVAL;
833 f->fmt.pix.field = V4L2_FIELD_NONE;
834 f->fmt.pix.bytesperline = 0;
835 f->fmt.pix.sizeimage = cam->frame_size;
836 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
837 f->fmt.pix.priv = 0;
839 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
840 case VIDEOSIZE_VGA:
841 f->fmt.pix.width = 640;
842 f->fmt.pix.height = 480;
843 break;
844 case VIDEOSIZE_CIF:
845 f->fmt.pix.width = 352;
846 f->fmt.pix.height = 288;
847 break;
848 case VIDEOSIZE_QVGA:
849 f->fmt.pix.width = 320;
850 f->fmt.pix.height = 240;
851 break;
852 case VIDEOSIZE_288_216:
853 f->fmt.pix.width = 288;
854 f->fmt.pix.height = 216;
855 break;
856 case VIDEOSIZE_256_192:
857 f->fmt.pix.width = 256;
858 f->fmt.pix.height = 192;
859 break;
860 case VIDEOSIZE_224_168:
861 f->fmt.pix.width = 224;
862 f->fmt.pix.height = 168;
863 break;
864 case VIDEOSIZE_192_144:
865 f->fmt.pix.width = 192;
866 f->fmt.pix.height = 144;
867 break;
868 case VIDEOSIZE_QCIF:
869 default:
870 f->fmt.pix.width = 176;
871 f->fmt.pix.height = 144;
872 break;
875 return 0;
878 /******************************************************************************
880 * ioctl_set_fmt
882 * V4L2 format set
884 *****************************************************************************/
886 static int ioctl_set_fmt(void *arg,struct camera_data *cam, struct cpia2_fh *fh)
888 struct v4l2_format *f = arg;
889 int err, frame;
891 err = ioctl_try_fmt(arg, cam);
892 if(err != 0)
893 return err;
895 /* Ensure that only this process can change the format. */
896 err = v4l2_prio_change(&cam->prio, &fh->prio, V4L2_PRIORITY_RECORD);
897 if(err != 0) {
898 return err;
901 cam->pixelformat = f->fmt.pix.pixelformat;
903 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
904 * the missing Huffman table properly. */
905 cam->params.compression.inhibit_htables = 0;
906 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
908 /* we set the video window to something smaller or equal to what
909 * is requested by the user???
911 DBG("Requested width = %d, height = %d\n",
912 f->fmt.pix.width, f->fmt.pix.height);
913 if (f->fmt.pix.width != cam->vw.width ||
914 f->fmt.pix.height != cam->vw.height) {
915 cam->vw.width = f->fmt.pix.width;
916 cam->vw.height = f->fmt.pix.height;
917 cam->params.roi.width = f->fmt.pix.width;
918 cam->params.roi.height = f->fmt.pix.height;
919 cpia2_set_format(cam);
922 for (frame = 0; frame < cam->num_frames; ++frame) {
923 if (cam->buffers[frame].status == FRAME_READING)
924 if ((err = sync(cam, frame)) < 0)
925 return err;
927 cam->buffers[frame].status = FRAME_EMPTY;
930 return 0;
933 /******************************************************************************
935 * ioctl_get_fmt
937 * V4L2 format get
939 *****************************************************************************/
941 static int ioctl_get_fmt(void *arg,struct camera_data *cam)
943 struct v4l2_format *f = arg;
945 if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
946 return -EINVAL;
948 f->fmt.pix.width = cam->vw.width;
949 f->fmt.pix.height = cam->vw.height;
950 f->fmt.pix.pixelformat = cam->pixelformat;
951 f->fmt.pix.field = V4L2_FIELD_NONE;
952 f->fmt.pix.bytesperline = 0;
953 f->fmt.pix.sizeimage = cam->frame_size;
954 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
955 f->fmt.pix.priv = 0;
957 return 0;
960 /******************************************************************************
962 * ioctl_cropcap
964 * V4L2 query cropping capabilities
965 * NOTE: cropping is currently disabled
967 *****************************************************************************/
969 static int ioctl_cropcap(void *arg,struct camera_data *cam)
971 struct v4l2_cropcap *c = arg;
973 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
974 return -EINVAL;
976 c->bounds.left = 0;
977 c->bounds.top = 0;
978 c->bounds.width = cam->vw.width;
979 c->bounds.height = cam->vw.height;
980 c->defrect.left = 0;
981 c->defrect.top = 0;
982 c->defrect.width = cam->vw.width;
983 c->defrect.height = cam->vw.height;
984 c->pixelaspect.numerator = 1;
985 c->pixelaspect.denominator = 1;
987 return 0;
990 /******************************************************************************
992 * ioctl_queryctrl
994 * V4L2 query possible control variables
996 *****************************************************************************/
998 static int ioctl_queryctrl(void *arg,struct camera_data *cam)
1000 struct v4l2_queryctrl *c = arg;
1001 int i;
1003 for(i=0; i<NUM_CONTROLS; ++i) {
1004 if(c->id == controls[i].id) {
1005 memcpy(c, controls+i, sizeof(*c));
1006 break;
1010 if(i == NUM_CONTROLS)
1011 return -EINVAL;
1013 /* Some devices have additional limitations */
1014 switch(c->id) {
1015 case V4L2_CID_BRIGHTNESS:
1016 /***
1017 * Don't let the register be set to zero - bug in VP4
1018 * flash of full brightness
1019 ***/
1020 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1021 c->minimum = 1;
1022 break;
1023 case V4L2_CID_VFLIP:
1024 // VP5 Only
1025 if(cam->params.pnp_id.device_type == DEVICE_STV_672)
1026 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1027 break;
1028 case CPIA2_CID_FRAMERATE:
1029 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1030 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1031 // Maximum 15fps
1032 int i;
1033 for(i=0; i<c->maximum; ++i) {
1034 if(framerate_controls[i].value ==
1035 CPIA2_VP_FRAMERATE_15) {
1036 c->maximum = i;
1037 c->default_value = i;
1041 break;
1042 case CPIA2_CID_FLICKER_MODE:
1043 // Flicker control only valid for 672.
1044 if(cam->params.pnp_id.device_type != DEVICE_STV_672)
1045 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1046 break;
1047 case CPIA2_CID_LIGHTS:
1048 // Light control only valid for the QX5 Microscope.
1049 if(cam->params.pnp_id.product != 0x151)
1050 c->flags |= V4L2_CTRL_FLAG_DISABLED;
1051 break;
1052 default:
1053 break;
1056 return 0;
1059 /******************************************************************************
1061 * ioctl_querymenu
1063 * V4L2 query possible control variables
1065 *****************************************************************************/
1067 static int ioctl_querymenu(void *arg,struct camera_data *cam)
1069 struct v4l2_querymenu *m = arg;
1071 memset(m->name, 0, sizeof(m->name));
1072 m->reserved = 0;
1074 switch(m->id) {
1075 case CPIA2_CID_FLICKER_MODE:
1076 if(m->index < 0 || m->index >= NUM_FLICKER_CONTROLS)
1077 return -EINVAL;
1079 strcpy(m->name, flicker_controls[m->index].name);
1080 break;
1081 case CPIA2_CID_FRAMERATE:
1083 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1084 if(cam->params.pnp_id.device_type == DEVICE_STV_672 &&
1085 cam->params.version.sensor_flags==CPIA2_VP_SENSOR_FLAGS_500){
1086 // Maximum 15fps
1087 int i;
1088 for(i=0; i<maximum; ++i) {
1089 if(framerate_controls[i].value ==
1090 CPIA2_VP_FRAMERATE_15)
1091 maximum = i;
1094 if(m->index < 0 || m->index > maximum)
1095 return -EINVAL;
1097 strcpy(m->name, framerate_controls[m->index].name);
1098 break;
1100 case CPIA2_CID_LIGHTS:
1101 if(m->index < 0 || m->index >= NUM_LIGHTS_CONTROLS)
1102 return -EINVAL;
1104 strcpy(m->name, lights_controls[m->index].name);
1105 break;
1106 default:
1107 return -EINVAL;
1110 return 0;
1113 /******************************************************************************
1115 * ioctl_g_ctrl
1117 * V4L2 get the value of a control variable
1119 *****************************************************************************/
1121 static int ioctl_g_ctrl(void *arg,struct camera_data *cam)
1123 struct v4l2_control *c = arg;
1125 switch(c->id) {
1126 case V4L2_CID_BRIGHTNESS:
1127 cpia2_do_command(cam, CPIA2_CMD_GET_VP_BRIGHTNESS,
1128 TRANSFER_READ, 0);
1129 c->value = cam->params.color_params.brightness;
1130 break;
1131 case V4L2_CID_CONTRAST:
1132 cpia2_do_command(cam, CPIA2_CMD_GET_CONTRAST,
1133 TRANSFER_READ, 0);
1134 c->value = cam->params.color_params.contrast;
1135 break;
1136 case V4L2_CID_SATURATION:
1137 cpia2_do_command(cam, CPIA2_CMD_GET_VP_SATURATION,
1138 TRANSFER_READ, 0);
1139 c->value = cam->params.color_params.saturation;
1140 break;
1141 case V4L2_CID_HFLIP:
1142 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1143 TRANSFER_READ, 0);
1144 c->value = (cam->params.vp_params.user_effects &
1145 CPIA2_VP_USER_EFFECTS_MIRROR) != 0;
1146 break;
1147 case V4L2_CID_VFLIP:
1148 cpia2_do_command(cam, CPIA2_CMD_GET_USER_EFFECTS,
1149 TRANSFER_READ, 0);
1150 c->value = (cam->params.vp_params.user_effects &
1151 CPIA2_VP_USER_EFFECTS_FLIP) != 0;
1152 break;
1153 case CPIA2_CID_TARGET_KB:
1154 c->value = cam->params.vc_params.target_kb;
1155 break;
1156 case CPIA2_CID_GPIO:
1157 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1158 TRANSFER_READ, 0);
1159 c->value = cam->params.vp_params.gpio_data;
1160 break;
1161 case CPIA2_CID_FLICKER_MODE:
1163 int i, mode;
1164 cpia2_do_command(cam, CPIA2_CMD_GET_FLICKER_MODES,
1165 TRANSFER_READ, 0);
1166 if(cam->params.flicker_control.cam_register &
1167 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER) {
1168 mode = NEVER_FLICKER;
1169 } else {
1170 if(cam->params.flicker_control.cam_register &
1171 CPIA2_VP_FLICKER_MODES_50HZ) {
1172 mode = FLICKER_50;
1173 } else {
1174 mode = FLICKER_60;
1177 for(i=0; i<NUM_FLICKER_CONTROLS; i++) {
1178 if(flicker_controls[i].value == mode) {
1179 c->value = i;
1180 break;
1183 if(i == NUM_FLICKER_CONTROLS)
1184 return -EINVAL;
1185 break;
1187 case CPIA2_CID_FRAMERATE:
1189 int maximum = NUM_FRAMERATE_CONTROLS - 1;
1190 int i;
1191 for(i=0; i<= maximum; i++) {
1192 if(cam->params.vp_params.frame_rate ==
1193 framerate_controls[i].value)
1194 break;
1196 if(i > maximum)
1197 return -EINVAL;
1198 c->value = i;
1199 break;
1201 case CPIA2_CID_USB_ALT:
1202 c->value = cam->params.camera_state.stream_mode;
1203 break;
1204 case CPIA2_CID_LIGHTS:
1206 int i;
1207 cpia2_do_command(cam, CPIA2_CMD_GET_VP_GPIO_DATA,
1208 TRANSFER_READ, 0);
1209 for(i=0; i<NUM_LIGHTS_CONTROLS; i++) {
1210 if((cam->params.vp_params.gpio_data&GPIO_LIGHTS_MASK) ==
1211 lights_controls[i].value) {
1212 break;
1215 if(i == NUM_LIGHTS_CONTROLS)
1216 return -EINVAL;
1217 c->value = i;
1218 break;
1220 case CPIA2_CID_RESET_CAMERA:
1221 return -EINVAL;
1222 default:
1223 return -EINVAL;
1226 DBG("Get control id:%d, value:%d\n", c->id, c->value);
1228 return 0;
1231 /******************************************************************************
1233 * ioctl_s_ctrl
1235 * V4L2 set the value of a control variable
1237 *****************************************************************************/
1239 static int ioctl_s_ctrl(void *arg,struct camera_data *cam)
1241 struct v4l2_control *c = arg;
1242 int i;
1243 int retval = 0;
1245 DBG("Set control id:%d, value:%d\n", c->id, c->value);
1247 /* Check that the value is in range */
1248 for(i=0; i<NUM_CONTROLS; i++) {
1249 if(c->id == controls[i].id) {
1250 if(c->value < controls[i].minimum ||
1251 c->value > controls[i].maximum) {
1252 return -EINVAL;
1254 break;
1257 if(i == NUM_CONTROLS)
1258 return -EINVAL;
1260 switch(c->id) {
1261 case V4L2_CID_BRIGHTNESS:
1262 cpia2_set_brightness(cam, c->value);
1263 break;
1264 case V4L2_CID_CONTRAST:
1265 cpia2_set_contrast(cam, c->value);
1266 break;
1267 case V4L2_CID_SATURATION:
1268 cpia2_set_saturation(cam, c->value);
1269 break;
1270 case V4L2_CID_HFLIP:
1271 cpia2_set_property_mirror(cam, c->value);
1272 break;
1273 case V4L2_CID_VFLIP:
1274 cpia2_set_property_flip(cam, c->value);
1275 break;
1276 case CPIA2_CID_TARGET_KB:
1277 retval = cpia2_set_target_kb(cam, c->value);
1278 break;
1279 case CPIA2_CID_GPIO:
1280 retval = cpia2_set_gpio(cam, c->value);
1281 break;
1282 case CPIA2_CID_FLICKER_MODE:
1283 retval = cpia2_set_flicker_mode(cam,
1284 flicker_controls[c->value].value);
1285 break;
1286 case CPIA2_CID_FRAMERATE:
1287 retval = cpia2_set_fps(cam, framerate_controls[c->value].value);
1288 break;
1289 case CPIA2_CID_USB_ALT:
1290 retval = cpia2_usb_change_streaming_alternate(cam, c->value);
1291 break;
1292 case CPIA2_CID_LIGHTS:
1293 retval = cpia2_set_gpio(cam, lights_controls[c->value].value);
1294 break;
1295 case CPIA2_CID_RESET_CAMERA:
1296 cpia2_usb_stream_pause(cam);
1297 cpia2_reset_camera(cam);
1298 cpia2_usb_stream_resume(cam);
1299 break;
1300 default:
1301 retval = -EINVAL;
1304 return retval;
1307 /******************************************************************************
1309 * ioctl_g_jpegcomp
1311 * V4L2 get the JPEG compression parameters
1313 *****************************************************************************/
1315 static int ioctl_g_jpegcomp(void *arg,struct camera_data *cam)
1317 struct v4l2_jpegcompression *parms = arg;
1319 memset(parms, 0, sizeof(*parms));
1321 parms->quality = 80; // TODO: Can this be made meaningful?
1323 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
1324 if(!cam->params.compression.inhibit_htables) {
1325 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
1328 parms->APPn = cam->APPn;
1329 parms->APP_len = cam->APP_len;
1330 if(cam->APP_len > 0) {
1331 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
1332 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
1335 parms->COM_len = cam->COM_len;
1336 if(cam->COM_len > 0) {
1337 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
1338 parms->jpeg_markers |= JPEG_MARKER_COM;
1341 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1342 parms->APP_len, parms->COM_len);
1344 return 0;
1347 /******************************************************************************
1349 * ioctl_s_jpegcomp
1351 * V4L2 set the JPEG compression parameters
1352 * NOTE: quality and some jpeg_markers are ignored.
1354 *****************************************************************************/
1356 static int ioctl_s_jpegcomp(void *arg,struct camera_data *cam)
1358 struct v4l2_jpegcompression *parms = arg;
1360 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1361 parms->APP_len, parms->COM_len);
1363 cam->params.compression.inhibit_htables =
1364 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
1366 if(parms->APP_len != 0) {
1367 if(parms->APP_len > 0 &&
1368 parms->APP_len <= sizeof(cam->APP_data) &&
1369 parms->APPn >= 0 && parms->APPn <= 15) {
1370 cam->APPn = parms->APPn;
1371 cam->APP_len = parms->APP_len;
1372 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
1373 } else {
1374 LOG("Bad APPn Params n=%d len=%d\n",
1375 parms->APPn, parms->APP_len);
1376 return -EINVAL;
1378 } else {
1379 cam->APP_len = 0;
1382 if(parms->COM_len != 0) {
1383 if(parms->COM_len > 0 &&
1384 parms->COM_len <= sizeof(cam->COM_data)) {
1385 cam->COM_len = parms->COM_len;
1386 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
1387 } else {
1388 LOG("Bad COM_len=%d\n", parms->COM_len);
1389 return -EINVAL;
1393 return 0;
1396 /******************************************************************************
1398 * ioctl_reqbufs
1400 * V4L2 Initiate memory mapping.
1401 * NOTE: The user's request is ignored. For now the buffers are fixed.
1403 *****************************************************************************/
1405 static int ioctl_reqbufs(void *arg,struct camera_data *cam)
1407 struct v4l2_requestbuffers *req = arg;
1409 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1410 req->memory != V4L2_MEMORY_MMAP)
1411 return -EINVAL;
1413 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
1414 req->count = cam->num_frames;
1415 memset(&req->reserved, 0, sizeof(req->reserved));
1417 return 0;
1420 /******************************************************************************
1422 * ioctl_querybuf
1424 * V4L2 Query memory buffer status.
1426 *****************************************************************************/
1428 static int ioctl_querybuf(void *arg,struct camera_data *cam)
1430 struct v4l2_buffer *buf = arg;
1432 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1433 buf->index > cam->num_frames)
1434 return -EINVAL;
1436 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1437 buf->length = cam->frame_size;
1439 buf->memory = V4L2_MEMORY_MMAP;
1441 if(cam->mmapped)
1442 buf->flags = V4L2_BUF_FLAG_MAPPED;
1443 else
1444 buf->flags = 0;
1446 switch (cam->buffers[buf->index].status) {
1447 case FRAME_EMPTY:
1448 case FRAME_ERROR:
1449 case FRAME_READING:
1450 buf->bytesused = 0;
1451 buf->flags = V4L2_BUF_FLAG_QUEUED;
1452 break;
1453 case FRAME_READY:
1454 buf->bytesused = cam->buffers[buf->index].length;
1455 buf->timestamp = cam->buffers[buf->index].timestamp;
1456 buf->sequence = cam->buffers[buf->index].seq;
1457 buf->flags = V4L2_BUF_FLAG_DONE;
1458 break;
1461 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1462 buf->index, buf->m.offset, buf->flags, buf->sequence,
1463 buf->bytesused);
1465 return 0;
1468 /******************************************************************************
1470 * ioctl_qbuf
1472 * V4L2 User is freeing buffer
1474 *****************************************************************************/
1476 static int ioctl_qbuf(void *arg,struct camera_data *cam)
1478 struct v4l2_buffer *buf = arg;
1480 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1481 buf->memory != V4L2_MEMORY_MMAP ||
1482 buf->index > cam->num_frames)
1483 return -EINVAL;
1485 DBG("QBUF #%d\n", buf->index);
1487 if(cam->buffers[buf->index].status == FRAME_READY)
1488 cam->buffers[buf->index].status = FRAME_EMPTY;
1490 return 0;
1493 /******************************************************************************
1495 * find_earliest_filled_buffer
1497 * Helper for ioctl_dqbuf. Find the next ready buffer.
1499 *****************************************************************************/
1501 static int find_earliest_filled_buffer(struct camera_data *cam)
1503 int i;
1504 int found = -1;
1505 for (i=0; i<cam->num_frames; i++) {
1506 if(cam->buffers[i].status == FRAME_READY) {
1507 if(found < 0) {
1508 found = i;
1509 } else {
1510 /* find which buffer is earlier */
1511 struct timeval *tv1, *tv2;
1512 tv1 = &cam->buffers[i].timestamp;
1513 tv2 = &cam->buffers[found].timestamp;
1514 if(tv1->tv_sec < tv2->tv_sec ||
1515 (tv1->tv_sec == tv2->tv_sec &&
1516 tv1->tv_usec < tv2->tv_usec))
1517 found = i;
1521 return found;
1524 /******************************************************************************
1526 * ioctl_dqbuf
1528 * V4L2 User is asking for a filled buffer.
1530 *****************************************************************************/
1532 static int ioctl_dqbuf(void *arg,struct camera_data *cam, struct file *file)
1534 struct v4l2_buffer *buf = arg;
1535 int frame;
1537 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
1538 buf->memory != V4L2_MEMORY_MMAP)
1539 return -EINVAL;
1541 frame = find_earliest_filled_buffer(cam);
1543 if(frame < 0 && file->f_flags&O_NONBLOCK)
1544 return -EAGAIN;
1546 if(frame < 0) {
1547 /* Wait for a frame to become available */
1548 struct framebuf *cb=cam->curbuff;
1549 mutex_unlock(&cam->busy_lock);
1550 wait_event_interruptible(cam->wq_stream,
1551 !cam->present ||
1552 (cb=cam->curbuff)->status == FRAME_READY);
1553 mutex_lock(&cam->busy_lock);
1554 if (signal_pending(current))
1555 return -ERESTARTSYS;
1556 if(!cam->present)
1557 return -ENOTTY;
1558 frame = cb->num;
1562 buf->index = frame;
1563 buf->bytesused = cam->buffers[buf->index].length;
1564 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE;
1565 buf->field = V4L2_FIELD_NONE;
1566 buf->timestamp = cam->buffers[buf->index].timestamp;
1567 buf->sequence = cam->buffers[buf->index].seq;
1568 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
1569 buf->length = cam->frame_size;
1570 buf->input = 0;
1571 buf->reserved = 0;
1572 memset(&buf->timecode, 0, sizeof(buf->timecode));
1574 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
1575 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
1577 return 0;
1580 /******************************************************************************
1582 * cpia2_ioctl
1584 *****************************************************************************/
1585 static int cpia2_do_ioctl(struct inode *inode, struct file *file,
1586 unsigned int ioctl_nr, void *arg)
1588 struct video_device *dev = video_devdata(file);
1589 struct camera_data *cam = video_get_drvdata(dev);
1590 int retval = 0;
1592 if (!cam)
1593 return -ENOTTY;
1595 /* make this _really_ smp-safe */
1596 if (mutex_lock_interruptible(&cam->busy_lock))
1597 return -ERESTARTSYS;
1599 if (!cam->present) {
1600 mutex_unlock(&cam->busy_lock);
1601 return -ENODEV;
1604 /* Priority check */
1605 switch (ioctl_nr) {
1606 case VIDIOCSWIN:
1607 case VIDIOCMCAPTURE:
1608 case VIDIOC_S_FMT:
1610 struct cpia2_fh *fh = file->private_data;
1611 retval = v4l2_prio_check(&cam->prio, &fh->prio);
1612 if(retval) {
1613 mutex_unlock(&cam->busy_lock);
1614 return retval;
1616 break;
1618 case VIDIOCGMBUF:
1619 case VIDIOCSYNC:
1621 struct cpia2_fh *fh = file->private_data;
1622 if(fh->prio != V4L2_PRIORITY_RECORD) {
1623 mutex_unlock(&cam->busy_lock);
1624 return -EBUSY;
1626 break;
1628 default:
1629 break;
1632 switch (ioctl_nr) {
1633 case VIDIOCGCAP: /* query capabilities */
1634 retval = ioctl_cap_query(arg, cam);
1635 break;
1637 case VIDIOCGCHAN: /* get video source - we are a camera, nothing else */
1638 retval = ioctl_get_channel(arg);
1639 break;
1640 case VIDIOCSCHAN: /* set video source - we are a camera, nothing else */
1641 retval = ioctl_set_channel(arg);
1642 break;
1643 case VIDIOCGPICT: /* image properties */
1644 memcpy(arg, &cam->vp, sizeof(struct video_picture));
1645 break;
1646 case VIDIOCSPICT:
1647 retval = ioctl_set_image_prop(arg, cam);
1648 break;
1649 case VIDIOCGWIN: /* get/set capture window */
1650 memcpy(arg, &cam->vw, sizeof(struct video_window));
1651 break;
1652 case VIDIOCSWIN:
1653 retval = ioctl_set_window_size(arg, cam, file->private_data);
1654 break;
1655 case VIDIOCGMBUF: /* mmap interface */
1656 retval = ioctl_get_mbuf(arg, cam);
1657 break;
1658 case VIDIOCMCAPTURE:
1659 retval = ioctl_mcapture(arg, cam, file->private_data);
1660 break;
1661 case VIDIOCSYNC:
1662 retval = ioctl_sync(arg, cam);
1663 break;
1664 /* pointless to implement overlay with this camera */
1665 case VIDIOCCAPTURE:
1666 case VIDIOCGFBUF:
1667 case VIDIOCSFBUF:
1668 case VIDIOCKEY:
1669 retval = -EINVAL;
1670 break;
1672 /* tuner interface - we have none */
1673 case VIDIOCGTUNER:
1674 case VIDIOCSTUNER:
1675 case VIDIOCGFREQ:
1676 case VIDIOCSFREQ:
1677 retval = -EINVAL;
1678 break;
1680 /* audio interface - we have none */
1681 case VIDIOCGAUDIO:
1682 case VIDIOCSAUDIO:
1683 retval = -EINVAL;
1684 break;
1686 /* CPIA2 extension to Video4Linux API */
1687 case CPIA2_IOC_SET_GPIO:
1688 retval = ioctl_set_gpio(arg, cam);
1689 break;
1690 case VIDIOC_QUERYCAP:
1691 retval = ioctl_querycap(arg,cam);
1692 break;
1694 case VIDIOC_ENUMINPUT:
1695 case VIDIOC_G_INPUT:
1696 case VIDIOC_S_INPUT:
1697 retval = ioctl_input(ioctl_nr, arg,cam);
1698 break;
1700 case VIDIOC_ENUM_FMT:
1701 retval = ioctl_enum_fmt(arg,cam);
1702 break;
1703 case VIDIOC_TRY_FMT:
1704 retval = ioctl_try_fmt(arg,cam);
1705 break;
1706 case VIDIOC_G_FMT:
1707 retval = ioctl_get_fmt(arg,cam);
1708 break;
1709 case VIDIOC_S_FMT:
1710 retval = ioctl_set_fmt(arg,cam,file->private_data);
1711 break;
1713 case VIDIOC_CROPCAP:
1714 retval = ioctl_cropcap(arg,cam);
1715 break;
1716 case VIDIOC_G_CROP:
1717 case VIDIOC_S_CROP:
1718 // TODO: I think cropping can be implemented - SJB
1719 retval = -EINVAL;
1720 break;
1722 case VIDIOC_QUERYCTRL:
1723 retval = ioctl_queryctrl(arg,cam);
1724 break;
1725 case VIDIOC_QUERYMENU:
1726 retval = ioctl_querymenu(arg,cam);
1727 break;
1728 case VIDIOC_G_CTRL:
1729 retval = ioctl_g_ctrl(arg,cam);
1730 break;
1731 case VIDIOC_S_CTRL:
1732 retval = ioctl_s_ctrl(arg,cam);
1733 break;
1735 case VIDIOC_G_JPEGCOMP:
1736 retval = ioctl_g_jpegcomp(arg,cam);
1737 break;
1738 case VIDIOC_S_JPEGCOMP:
1739 retval = ioctl_s_jpegcomp(arg,cam);
1740 break;
1742 case VIDIOC_G_PRIORITY:
1744 struct cpia2_fh *fh = file->private_data;
1745 *(enum v4l2_priority*)arg = fh->prio;
1746 break;
1748 case VIDIOC_S_PRIORITY:
1750 struct cpia2_fh *fh = file->private_data;
1751 enum v4l2_priority prio;
1752 prio = *(enum v4l2_priority*)arg;
1753 if(cam->streaming &&
1754 prio != fh->prio &&
1755 fh->prio == V4L2_PRIORITY_RECORD) {
1756 /* Can't drop record priority while streaming */
1757 retval = -EBUSY;
1758 } else if(prio == V4L2_PRIORITY_RECORD &&
1759 prio != fh->prio &&
1760 v4l2_prio_max(&cam->prio) == V4L2_PRIORITY_RECORD) {
1761 /* Only one program can record at a time */
1762 retval = -EBUSY;
1763 } else {
1764 retval = v4l2_prio_change(&cam->prio, &fh->prio, prio);
1766 break;
1769 case VIDIOC_REQBUFS:
1770 retval = ioctl_reqbufs(arg,cam);
1771 break;
1772 case VIDIOC_QUERYBUF:
1773 retval = ioctl_querybuf(arg,cam);
1774 break;
1775 case VIDIOC_QBUF:
1776 retval = ioctl_qbuf(arg,cam);
1777 break;
1778 case VIDIOC_DQBUF:
1779 retval = ioctl_dqbuf(arg,cam,file);
1780 break;
1781 case VIDIOC_STREAMON:
1783 int type;
1784 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
1785 type = *(int*)arg;
1786 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1787 retval = -EINVAL;
1789 if(!cam->streaming) {
1790 retval = cpia2_usb_stream_start(cam,
1791 cam->params.camera_state.stream_mode);
1792 } else {
1793 retval = -EINVAL;
1796 break;
1798 case VIDIOC_STREAMOFF:
1800 int type;
1801 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
1802 type = *(int*)arg;
1803 if(!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1804 retval = -EINVAL;
1806 if(cam->streaming) {
1807 retval = cpia2_usb_stream_stop(cam);
1808 } else {
1809 retval = -EINVAL;
1812 break;
1815 case VIDIOC_ENUMOUTPUT:
1816 case VIDIOC_G_OUTPUT:
1817 case VIDIOC_S_OUTPUT:
1818 case VIDIOC_G_MODULATOR:
1819 case VIDIOC_S_MODULATOR:
1821 case VIDIOC_ENUMAUDIO:
1822 case VIDIOC_G_AUDIO:
1823 case VIDIOC_S_AUDIO:
1825 case VIDIOC_ENUMAUDOUT:
1826 case VIDIOC_G_AUDOUT:
1827 case VIDIOC_S_AUDOUT:
1829 case VIDIOC_ENUMSTD:
1830 case VIDIOC_QUERYSTD:
1831 case VIDIOC_G_STD:
1832 case VIDIOC_S_STD:
1834 case VIDIOC_G_TUNER:
1835 case VIDIOC_S_TUNER:
1836 case VIDIOC_G_FREQUENCY:
1837 case VIDIOC_S_FREQUENCY:
1839 case VIDIOC_OVERLAY:
1840 case VIDIOC_G_FBUF:
1841 case VIDIOC_S_FBUF:
1843 case VIDIOC_G_PARM:
1844 case VIDIOC_S_PARM:
1845 retval = -EINVAL;
1846 break;
1847 default:
1848 retval = -ENOIOCTLCMD;
1849 break;
1852 mutex_unlock(&cam->busy_lock);
1853 return retval;
1856 static int cpia2_ioctl(struct inode *inode, struct file *file,
1857 unsigned int ioctl_nr, unsigned long iarg)
1859 return video_usercopy(inode, file, ioctl_nr, iarg, cpia2_do_ioctl);
1862 /******************************************************************************
1864 * cpia2_mmap
1866 *****************************************************************************/
1867 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1869 int retval;
1870 struct video_device *dev = video_devdata(file);
1871 struct camera_data *cam = video_get_drvdata(dev);
1873 /* Priority check */
1874 struct cpia2_fh *fh = file->private_data;
1875 if(fh->prio != V4L2_PRIORITY_RECORD) {
1876 return -EBUSY;
1879 retval = cpia2_remap_buffer(cam, area);
1881 if(!retval)
1882 fh->mmapped = 1;
1883 return retval;
1886 /******************************************************************************
1888 * reset_camera_struct_v4l
1890 * Sets all values to the defaults
1891 *****************************************************************************/
1892 static void reset_camera_struct_v4l(struct camera_data *cam)
1894 /***
1895 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1896 * Ioctl. Here, just do the window and picture stucts.
1897 ***/
1898 cam->vp.palette = (u16) VIDEO_PALETTE_RGB24; /* Is this right? */
1899 cam->vp.brightness = (u16) cam->params.color_params.brightness * 256;
1900 cam->vp.colour = (u16) cam->params.color_params.saturation * 256;
1901 cam->vp.contrast = (u16) cam->params.color_params.contrast * 256;
1903 cam->vw.x = 0;
1904 cam->vw.y = 0;
1905 cam->vw.width = cam->params.roi.width;
1906 cam->vw.height = cam->params.roi.height;
1907 cam->vw.flags = 0;
1908 cam->vw.clipcount = 0;
1910 cam->frame_size = buffer_size;
1911 cam->num_frames = num_buffers;
1913 /* FlickerModes */
1914 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1915 cam->params.flicker_control.mains_frequency = flicker_freq;
1917 /* streamMode */
1918 cam->params.camera_state.stream_mode = alternate;
1920 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1921 v4l2_prio_init(&cam->prio);
1922 return;
1925 /***
1926 * The v4l video device structure initialized for this device
1927 ***/
1928 static struct file_operations fops_template = {
1929 .owner = THIS_MODULE,
1930 .open = cpia2_open,
1931 .release = cpia2_close,
1932 .read = cpia2_v4l_read,
1933 .poll = cpia2_v4l_poll,
1934 .ioctl = cpia2_ioctl,
1935 .llseek = no_llseek,
1936 .compat_ioctl = v4l_compat_ioctl32,
1937 .mmap = cpia2_mmap,
1940 static struct video_device cpia2_template = {
1941 /* I could not find any place for the old .initialize initializer?? */
1942 .owner= THIS_MODULE,
1943 .name= "CPiA2 Camera",
1944 .type= VID_TYPE_CAPTURE,
1945 .type2 = V4L2_CAP_VIDEO_CAPTURE |
1946 V4L2_CAP_STREAMING,
1947 .hardware= VID_HARDWARE_CPIA2,
1948 .minor= -1,
1949 .fops= &fops_template,
1950 .release= video_device_release,
1953 /******************************************************************************
1955 * cpia2_register_camera
1957 *****************************************************************************/
1958 int cpia2_register_camera(struct camera_data *cam)
1960 cam->vdev = video_device_alloc();
1961 if(!cam->vdev)
1962 return -ENOMEM;
1964 memcpy(cam->vdev, &cpia2_template, sizeof(cpia2_template));
1965 video_set_drvdata(cam->vdev, cam);
1967 reset_camera_struct_v4l(cam);
1969 /* register v4l device */
1970 if (video_register_device
1971 (cam->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
1972 ERR("video_register_device failed\n");
1973 video_device_release(cam->vdev);
1974 return -ENODEV;
1977 return 0;
1980 /******************************************************************************
1982 * cpia2_unregister_camera
1984 *****************************************************************************/
1985 void cpia2_unregister_camera(struct camera_data *cam)
1987 if (!cam->open_count) {
1988 video_unregister_device(cam->vdev);
1989 } else {
1990 LOG("/dev/video%d removed while open, "
1991 "deferring video_unregister_device\n",
1992 cam->vdev->minor);
1996 /******************************************************************************
1998 * check_parameters
2000 * Make sure that all user-supplied parameters are sensible
2001 *****************************************************************************/
2002 static void __init check_parameters(void)
2004 if(buffer_size < PAGE_SIZE) {
2005 buffer_size = PAGE_SIZE;
2006 LOG("buffer_size too small, setting to %d\n", buffer_size);
2007 } else if(buffer_size > 1024*1024) {
2008 /* arbitrary upper limiit */
2009 buffer_size = 1024*1024;
2010 LOG("buffer_size ridiculously large, setting to %d\n",
2011 buffer_size);
2012 } else {
2013 buffer_size += PAGE_SIZE-1;
2014 buffer_size &= ~(PAGE_SIZE-1);
2017 if(num_buffers < 1) {
2018 num_buffers = 1;
2019 LOG("num_buffers too small, setting to %d\n", num_buffers);
2020 } else if(num_buffers > VIDEO_MAX_FRAME) {
2021 num_buffers = VIDEO_MAX_FRAME;
2022 LOG("num_buffers too large, setting to %d\n", num_buffers);
2025 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
2026 alternate = DEFAULT_ALT;
2027 LOG("alternate specified is invalid, using %d\n", alternate);
2030 if (flicker_mode != NEVER_FLICKER && flicker_mode != ANTI_FLICKER_ON) {
2031 flicker_mode = NEVER_FLICKER;
2032 LOG("Flicker mode specified is invalid, using %d\n",
2033 flicker_mode);
2036 if (flicker_freq != FLICKER_50 && flicker_freq != FLICKER_60) {
2037 flicker_freq = FLICKER_60;
2038 LOG("Flicker mode specified is invalid, using %d\n",
2039 flicker_freq);
2042 if(video_nr < -1 || video_nr > 64) {
2043 video_nr = -1;
2044 LOG("invalid video_nr specified, must be -1 to 64\n");
2047 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2048 num_buffers, buffer_size, alternate);
2051 /************ Module Stuff ***************/
2054 /******************************************************************************
2056 * cpia2_init/module_init
2058 *****************************************************************************/
2059 static int __init cpia2_init(void)
2061 LOG("%s v%d.%d.%d\n",
2062 ABOUT, CPIA2_MAJ_VER, CPIA2_MIN_VER, CPIA2_PATCH_VER);
2063 check_parameters();
2064 cpia2_usb_init();
2065 return 0;
2069 /******************************************************************************
2071 * cpia2_exit/module_exit
2073 *****************************************************************************/
2074 static void __exit cpia2_exit(void)
2076 cpia2_usb_cleanup();
2077 schedule_timeout(2 * HZ);
2080 module_init(cpia2_init);
2081 module_exit(cpia2_exit);