1 /****************************************************************************
3 * Filename: cpia2_v4l.c
5 * Copyright 2001, STMicrolectronics, Inc.
6 * Contact: steve.miller@st.com
7 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
10 * This is a USB driver for CPia2 based video cameras.
11 * The infrastructure of this driver is based on the cpia usb driver by
12 * Jochen Scharrlach and Johannes Erdfeldt.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Stripped of 2.4 stuff ready for main kernel submit by
29 * Alan Cox <alan@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>
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 "
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"
94 struct control_menu_info
{
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 (sizeof(framerate_controls)/sizeof(framerate_controls[0]))
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 (sizeof(flicker_controls)/sizeof(flicker_controls[0]))
118 static struct control_menu_info lights_controls
[] =
125 #define NUM_LIGHTS_CONTROLS (sizeof(lights_controls)/sizeof(lights_controls[0]))
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",
136 .default_value
= DEFAULT_BRIGHTNESS
,
139 .id
= V4L2_CID_CONTRAST
,
140 .type
= V4L2_CTRL_TYPE_INTEGER
,
145 .default_value
= DEFAULT_CONTRAST
,
148 .id
= V4L2_CID_SATURATION
,
149 .type
= V4L2_CTRL_TYPE_INTEGER
,
150 .name
= "Saturation",
154 .default_value
= DEFAULT_SATURATION
,
157 .id
= V4L2_CID_HFLIP
,
158 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
159 .name
= "Mirror Horizontally",
166 .id
= V4L2_CID_VFLIP
,
167 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
168 .name
= "Flip Vertically",
175 .id
= CPIA2_CID_TARGET_KB
,
176 .type
= V4L2_CTRL_TYPE_INTEGER
,
181 .default_value
= DEFAULT_TARGET_KB
,
184 .id
= CPIA2_CID_GPIO
,
185 .type
= V4L2_CTRL_TYPE_INTEGER
,
193 .id
= CPIA2_CID_FLICKER_MODE
,
194 .type
= V4L2_CTRL_TYPE_MENU
,
195 .name
= "Flicker Reduction",
197 .maximum
= NUM_FLICKER_CONTROLS
-1,
202 .id
= CPIA2_CID_FRAMERATE
,
203 .type
= V4L2_CTRL_TYPE_MENU
,
206 .maximum
= NUM_FRAMERATE_CONTROLS
-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
,
217 .default_value
= DEFAULT_ALT
,
220 .id
= CPIA2_CID_LIGHTS
,
221 .type
= V4L2_CTRL_TYPE_MENU
,
224 .maximum
= NUM_LIGHTS_CONTROLS
-1,
229 .id
= CPIA2_CID_RESET_CAMERA
,
230 .type
= V4L2_CTRL_TYPE_BUTTON
,
231 .name
= "Reset Camera",
238 #define NUM_CONTROLS (sizeof(controls)/sizeof(controls[0]))
241 /******************************************************************************
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
);
253 ERR("Internal error, camera_data not found!\n");
257 if(mutex_lock_interruptible(&cam
->busy_lock
))
265 if (cam
->open_count
> 0) {
269 if (cpia2_allocate_buffers(cam
)) {
274 /* reset the camera */
275 if (cpia2_reset_camera(cam
) < 0) {
285 struct cpia2_fh
*fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
290 file
->private_data
= fh
;
291 fh
->prio
= V4L2_PRIORITY_UNSET
;
292 v4l2_prio_open(&cam
->prio
, &fh
->prio
);
298 cpia2_dbg_dump_registers(cam
);
301 mutex_unlock(&cam
->busy_lock
);
305 /******************************************************************************
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
);
319 (cam
->open_count
== 1
320 || fh
->prio
== V4L2_PRIORITY_RECORD
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
);
336 v4l2_prio_close(&cam
->prio
,&fh
->prio
);
337 file
->private_data
= NULL
;
341 if (--cam
->open_count
== 0) {
342 cpia2_free_buffers(cam
);
344 video_unregister_device(dev
);
345 mutex_unlock(&cam
->busy_lock
);
351 mutex_unlock(&cam
->busy_lock
);
356 /******************************************************************************
360 *****************************************************************************/
361 static ssize_t
cpia2_v4l_read(struct file
*file
, char __user
*buf
, size_t count
,
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
;
374 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
378 return cpia2_read(cam
, buf
, count
, noblock
);
382 /******************************************************************************
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
;
398 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
402 return cpia2_poll(cam
, filp
, wait
);
406 /******************************************************************************
410 *****************************************************************************/
411 static int ioctl_cap_query(void *arg
, struct camera_data
*cam
)
413 struct video_capability
*vc
;
417 if (cam
->params
.pnp_id
.product
== 0x151)
418 strcpy(vc
->name
, "QX5 Microscope");
420 strcpy(vc
->name
, "CPiA2 Camera");
422 vc
->type
= VID_TYPE_CAPTURE
| VID_TYPE_MJPEG_ENCODER
;
425 vc
->minwidth
= 176; /* VIDEOSIZE_QCIF */
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
;
432 case CPIA2_VP_SENSOR_FLAGS_410
:
433 vc
->maxwidth
= STV_IMAGE_CIF_COLS
;
434 vc
->maxheight
= STV_IMAGE_CIF_ROWS
;
443 /******************************************************************************
447 *****************************************************************************/
448 static int ioctl_get_channel(void *arg
)
451 struct video_channel
*v
;
458 strcpy(v
->name
, "Camera");
461 v
->type
= VIDEO_TYPE_CAMERA
;
467 /******************************************************************************
471 *****************************************************************************/
472 static int ioctl_set_channel(void *arg
)
474 struct video_channel
*v
;
478 if (retval
== 0 && v
->channel
!= 0)
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
;
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
);
513 static int sync(struct camera_data
*cam
, int frame_nr
)
515 struct framebuf
*frame
= &cam
->buffers
[frame_nr
];
518 if (frame
->status
== FRAME_READY
)
521 if (!cam
->streaming
) {
522 frame
->status
= FRAME_READY
;
527 mutex_unlock(&cam
->busy_lock
);
528 wait_event_interruptible(cam
->wq_stream
,
530 frame
->status
== FRAME_READY
);
531 mutex_lock(&cam
->busy_lock
);
532 if (signal_pending(current
))
539 /******************************************************************************
541 * ioctl_set_window_size
543 *****************************************************************************/
544 static int ioctl_set_window_size(void *arg
, struct camera_data
*cam
,
547 /* copy_from_user, check validity, copy to internal structure */
548 struct video_window
*vw
;
552 if (vw
->clipcount
!= 0) /* clipping not supported */
555 if (vw
->clips
!= NULL
) /* clipping not supported */
558 /* Ensure that only this process can change the format. */
559 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
585 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
591 /******************************************************************************
595 *****************************************************************************/
596 static int ioctl_get_mbuf(void *arg
, struct camera_data
*cam
)
598 struct video_mbuf
*vm
;
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
;
611 /******************************************************************************
615 *****************************************************************************/
616 static int ioctl_mcapture(void *arg
, struct camera_data
*cam
,
619 struct video_mmap
*vm
;
623 if (vm
->frame
< 0 || vm
->frame
>= cam
->num_frames
)
627 video_size
= cpia2_match_video_size(vm
->width
, vm
->height
);
628 if (cam
->video_size
< 0) {
632 /* Ensure that only this process can change the format. */
633 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
648 cam
->buffers
[vm
->frame
].status
= FRAME_EMPTY
;
650 return cpia2_usb_stream_start(cam
,cam
->params
.camera_state
.stream_mode
);
653 /******************************************************************************
657 *****************************************************************************/
658 static int ioctl_sync(void *arg
, struct camera_data
*cam
)
664 if (frame
< 0 || frame
>= cam
->num_frames
)
667 return sync(cam
, frame
);
671 /******************************************************************************
675 *****************************************************************************/
677 static int ioctl_set_gpio(void *arg
, struct camera_data
*cam
)
681 gpio_val
= *(__u32
*) arg
;
683 if (gpio_val
&~ 0xFFU
)
686 return cpia2_set_gpio(cam
, (unsigned char)gpio_val
);
689 /******************************************************************************
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");
707 strcpy(vc
->card
, "CPiA2 Camera");
708 switch (cam
->params
.pnp_id
.device_type
) {
710 strcat(vc
->card
, " (672/");
713 strcat(vc
->card
, " (676/");
716 strcat(vc
->card
, " (???/");
719 switch (cam
->params
.version
.sensor_flags
) {
720 case CPIA2_VP_SENSOR_FLAGS_404
:
721 strcat(vc
->card
, "404)");
723 case CPIA2_VP_SENSOR_FLAGS_407
:
724 strcat(vc
->card
, "407)");
726 case CPIA2_VP_SENSOR_FLAGS_409
:
727 strcat(vc
->card
, "409)");
729 case CPIA2_VP_SENSOR_FLAGS_410
:
730 strcat(vc
->card
, "410)");
732 case CPIA2_VP_SENSOR_FLAGS_500
:
733 strcat(vc
->card
, "500)");
736 strcat(vc
->card
, "???)");
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
,
746 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
753 /******************************************************************************
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
) {
770 memset(i
, 0, sizeof(*i
));
771 strcpy(i
->name
, "Camera");
772 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
777 /******************************************************************************
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)
793 memset(f
, 0, sizeof(*f
));
795 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
796 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
799 strcpy(f
->description
, "MJPEG");
800 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
803 strcpy(f
->description
, "JPEG");
804 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
813 /******************************************************************************
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
)
828 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
829 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
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
;
838 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
840 f
->fmt
.pix
.width
= 640;
841 f
->fmt
.pix
.height
= 480;
844 f
->fmt
.pix
.width
= 352;
845 f
->fmt
.pix
.height
= 288;
848 f
->fmt
.pix
.width
= 320;
849 f
->fmt
.pix
.height
= 240;
851 case VIDEOSIZE_288_216
:
852 f
->fmt
.pix
.width
= 288;
853 f
->fmt
.pix
.height
= 216;
855 case VIDEOSIZE_256_192
:
856 f
->fmt
.pix
.width
= 256;
857 f
->fmt
.pix
.height
= 192;
859 case VIDEOSIZE_224_168
:
860 f
->fmt
.pix
.width
= 224;
861 f
->fmt
.pix
.height
= 168;
863 case VIDEOSIZE_192_144
:
864 f
->fmt
.pix
.width
= 192;
865 f
->fmt
.pix
.height
= 144;
869 f
->fmt
.pix
.width
= 176;
870 f
->fmt
.pix
.height
= 144;
877 /******************************************************************************
883 *****************************************************************************/
885 static int ioctl_set_fmt(void *arg
,struct camera_data
*cam
, struct cpia2_fh
*fh
)
887 struct v4l2_format
*f
= arg
;
890 err
= ioctl_try_fmt(arg
, cam
);
894 /* Ensure that only this process can change the format. */
895 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
926 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
932 /******************************************************************************
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
)
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
;
959 /******************************************************************************
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
)
977 c
->bounds
.width
= cam
->vw
.width
;
978 c
->bounds
.height
= cam
->vw
.height
;
981 c
->defrect
.width
= cam
->vw
.width
;
982 c
->defrect
.height
= cam
->vw
.height
;
983 c
->pixelaspect
.numerator
= 1;
984 c
->pixelaspect
.denominator
= 1;
989 /******************************************************************************
993 * V4L2 query possible control variables
995 *****************************************************************************/
997 static int ioctl_queryctrl(void *arg
,struct camera_data
*cam
)
999 struct v4l2_queryctrl
*c
= arg
;
1002 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
1003 if(c
->id
== controls
[i
].id
) {
1004 memcpy(c
, controls
+i
, sizeof(*c
));
1009 if(i
== NUM_CONTROLS
)
1012 /* Some devices have additional limitations */
1014 case V4L2_CID_BRIGHTNESS
:
1016 * Don't let the register be set to zero - bug in VP4
1017 * flash of full brightness
1019 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1022 case V4L2_CID_VFLIP
:
1024 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1025 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
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
){
1032 for(i
=0; i
<c
->maximum
; ++i
) {
1033 if(framerate_controls
[i
].value
==
1034 CPIA2_VP_FRAMERATE_15
) {
1036 c
->default_value
= i
;
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
;
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
;
1058 /******************************************************************************
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
));
1074 case CPIA2_CID_FLICKER_MODE
:
1075 if(m
->index
< 0 || m
->index
>= NUM_FLICKER_CONTROLS
)
1078 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
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
){
1087 for(i
=0; i
<maximum
; ++i
) {
1088 if(framerate_controls
[i
].value
==
1089 CPIA2_VP_FRAMERATE_15
)
1093 if(m
->index
< 0 || m
->index
> maximum
)
1096 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
1099 case CPIA2_CID_LIGHTS
:
1100 if(m
->index
< 0 || m
->index
>= NUM_LIGHTS_CONTROLS
)
1103 strcpy(m
->name
, lights_controls
[m
->index
].name
);
1112 /******************************************************************************
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
;
1125 case V4L2_CID_BRIGHTNESS
:
1126 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
1128 c
->value
= cam
->params
.color_params
.brightness
;
1130 case V4L2_CID_CONTRAST
:
1131 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
1133 c
->value
= cam
->params
.color_params
.contrast
;
1135 case V4L2_CID_SATURATION
:
1136 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
1138 c
->value
= cam
->params
.color_params
.saturation
;
1140 case V4L2_CID_HFLIP
:
1141 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1143 c
->value
= (cam
->params
.vp_params
.user_effects
&
1144 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
1146 case V4L2_CID_VFLIP
:
1147 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1149 c
->value
= (cam
->params
.vp_params
.user_effects
&
1150 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
1152 case CPIA2_CID_TARGET_KB
:
1153 c
->value
= cam
->params
.vc_params
.target_kb
;
1155 case CPIA2_CID_GPIO
:
1156 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1158 c
->value
= cam
->params
.vp_params
.gpio_data
;
1160 case CPIA2_CID_FLICKER_MODE
:
1163 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
1165 if(cam
->params
.flicker_control
.cam_register
&
1166 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
1167 mode
= NEVER_FLICKER
;
1169 if(cam
->params
.flicker_control
.cam_register
&
1170 CPIA2_VP_FLICKER_MODES_50HZ
) {
1176 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
1177 if(flicker_controls
[i
].value
== mode
) {
1182 if(i
== NUM_FLICKER_CONTROLS
)
1186 case CPIA2_CID_FRAMERATE
:
1188 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1190 for(i
=0; i
<= maximum
; i
++) {
1191 if(cam
->params
.vp_params
.frame_rate
==
1192 framerate_controls
[i
].value
)
1200 case CPIA2_CID_USB_ALT
:
1201 c
->value
= cam
->params
.camera_state
.stream_mode
;
1203 case CPIA2_CID_LIGHTS
:
1206 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
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
) {
1214 if(i
== NUM_LIGHTS_CONTROLS
)
1219 case CPIA2_CID_RESET_CAMERA
:
1225 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
1230 /******************************************************************************
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
;
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
) {
1256 if(i
== NUM_CONTROLS
)
1260 case V4L2_CID_BRIGHTNESS
:
1261 cpia2_set_brightness(cam
, c
->value
);
1263 case V4L2_CID_CONTRAST
:
1264 cpia2_set_contrast(cam
, c
->value
);
1266 case V4L2_CID_SATURATION
:
1267 cpia2_set_saturation(cam
, c
->value
);
1269 case V4L2_CID_HFLIP
:
1270 cpia2_set_property_mirror(cam
, c
->value
);
1272 case V4L2_CID_VFLIP
:
1273 cpia2_set_property_flip(cam
, c
->value
);
1275 case CPIA2_CID_TARGET_KB
:
1276 retval
= cpia2_set_target_kb(cam
, c
->value
);
1278 case CPIA2_CID_GPIO
:
1279 retval
= cpia2_set_gpio(cam
, c
->value
);
1281 case CPIA2_CID_FLICKER_MODE
:
1282 retval
= cpia2_set_flicker_mode(cam
,
1283 flicker_controls
[c
->value
].value
);
1285 case CPIA2_CID_FRAMERATE
:
1286 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1288 case CPIA2_CID_USB_ALT
:
1289 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1291 case CPIA2_CID_LIGHTS
:
1292 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1294 case CPIA2_CID_RESET_CAMERA
:
1295 cpia2_usb_stream_pause(cam
);
1296 cpia2_reset_camera(cam
);
1297 cpia2_usb_stream_resume(cam
);
1306 /******************************************************************************
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
);
1346 /******************************************************************************
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
);
1373 LOG("Bad APPn Params n=%d len=%d\n",
1374 parms
->APPn
, parms
->APP_len
);
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
);
1387 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1395 /******************************************************************************
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
)
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
));
1419 /******************************************************************************
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
)
1435 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1436 buf
->length
= cam
->frame_size
;
1438 buf
->memory
= V4L2_MEMORY_MMAP
;
1441 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1445 switch (cam
->buffers
[buf
->index
].status
) {
1450 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
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
;
1460 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1461 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1467 /******************************************************************************
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
)
1484 DBG("QBUF #%d\n", buf
->index
);
1486 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1487 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
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
)
1504 for (i
=0; i
<cam
->num_frames
; i
++) {
1505 if(cam
->buffers
[i
].status
== FRAME_READY
) {
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
))
1523 /******************************************************************************
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
;
1536 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1537 buf
->memory
!= V4L2_MEMORY_MMAP
)
1540 frame
= find_earliest_filled_buffer(cam
);
1542 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
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
,
1551 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1552 mutex_lock(&cam
->busy_lock
);
1553 if (signal_pending(current
))
1554 return -ERESTARTSYS
;
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
;
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
);
1579 /******************************************************************************
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
);
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
);
1603 /* Priority check */
1606 case VIDIOCMCAPTURE
:
1609 struct cpia2_fh
*fh
= file
->private_data
;
1610 retval
= v4l2_prio_check(&cam
->prio
, &fh
->prio
);
1612 mutex_unlock(&cam
->busy_lock
);
1620 struct cpia2_fh
*fh
= file
->private_data
;
1621 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1622 mutex_unlock(&cam
->busy_lock
);
1632 case VIDIOCGCAP
: /* query capabilities */
1633 retval
= ioctl_cap_query(arg
, cam
);
1636 case VIDIOCGCHAN
: /* get video source - we are a camera, nothing else */
1637 retval
= ioctl_get_channel(arg
);
1639 case VIDIOCSCHAN
: /* set video source - we are a camera, nothing else */
1640 retval
= ioctl_set_channel(arg
);
1642 case VIDIOCGPICT
: /* image properties */
1643 memcpy(arg
, &cam
->vp
, sizeof(struct video_picture
));
1646 retval
= ioctl_set_image_prop(arg
, cam
);
1648 case VIDIOCGWIN
: /* get/set capture window */
1649 memcpy(arg
, &cam
->vw
, sizeof(struct video_window
));
1652 retval
= ioctl_set_window_size(arg
, cam
, file
->private_data
);
1654 case VIDIOCGMBUF
: /* mmap interface */
1655 retval
= ioctl_get_mbuf(arg
, cam
);
1657 case VIDIOCMCAPTURE
:
1658 retval
= ioctl_mcapture(arg
, cam
, file
->private_data
);
1661 retval
= ioctl_sync(arg
, cam
);
1663 /* pointless to implement overlay with this camera */
1671 /* tuner interface - we have none */
1679 /* audio interface - we have none */
1685 /* CPIA2 extension to Video4Linux API */
1686 case CPIA2_IOC_SET_GPIO
:
1687 retval
= ioctl_set_gpio(arg
, cam
);
1689 case VIDIOC_QUERYCAP
:
1690 retval
= ioctl_querycap(arg
,cam
);
1693 case VIDIOC_ENUMINPUT
:
1694 case VIDIOC_G_INPUT
:
1695 case VIDIOC_S_INPUT
:
1696 retval
= ioctl_input(ioctl_nr
, arg
,cam
);
1699 case VIDIOC_ENUM_FMT
:
1700 retval
= ioctl_enum_fmt(arg
,cam
);
1702 case VIDIOC_TRY_FMT
:
1703 retval
= ioctl_try_fmt(arg
,cam
);
1706 retval
= ioctl_get_fmt(arg
,cam
);
1709 retval
= ioctl_set_fmt(arg
,cam
,file
->private_data
);
1712 case VIDIOC_CROPCAP
:
1713 retval
= ioctl_cropcap(arg
,cam
);
1717 // TODO: I think cropping can be implemented - SJB
1721 case VIDIOC_QUERYCTRL
:
1722 retval
= ioctl_queryctrl(arg
,cam
);
1724 case VIDIOC_QUERYMENU
:
1725 retval
= ioctl_querymenu(arg
,cam
);
1728 retval
= ioctl_g_ctrl(arg
,cam
);
1731 retval
= ioctl_s_ctrl(arg
,cam
);
1734 case VIDIOC_G_JPEGCOMP
:
1735 retval
= ioctl_g_jpegcomp(arg
,cam
);
1737 case VIDIOC_S_JPEGCOMP
:
1738 retval
= ioctl_s_jpegcomp(arg
,cam
);
1741 case VIDIOC_G_PRIORITY
:
1743 struct cpia2_fh
*fh
= file
->private_data
;
1744 *(enum v4l2_priority
*)arg
= fh
->prio
;
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
&&
1754 fh
->prio
== V4L2_PRIORITY_RECORD
) {
1755 /* Can't drop record priority while streaming */
1757 } else if(prio
== V4L2_PRIORITY_RECORD
&&
1759 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
) {
1760 /* Only one program can record at a time */
1763 retval
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1768 case VIDIOC_REQBUFS
:
1769 retval
= ioctl_reqbufs(arg
,cam
);
1771 case VIDIOC_QUERYBUF
:
1772 retval
= ioctl_querybuf(arg
,cam
);
1775 retval
= ioctl_qbuf(arg
,cam
);
1778 retval
= ioctl_dqbuf(arg
,cam
,file
);
1780 case VIDIOC_STREAMON
:
1783 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1785 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1788 if(!cam
->streaming
) {
1789 retval
= cpia2_usb_stream_start(cam
,
1790 cam
->params
.camera_state
.stream_mode
);
1797 case VIDIOC_STREAMOFF
:
1800 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1802 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1805 if(cam
->streaming
) {
1806 retval
= cpia2_usb_stream_stop(cam
);
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
:
1833 case VIDIOC_G_TUNER
:
1834 case VIDIOC_S_TUNER
:
1835 case VIDIOC_G_FREQUENCY
:
1836 case VIDIOC_S_FREQUENCY
:
1838 case VIDIOC_OVERLAY
:
1847 retval
= -ENOIOCTLCMD
;
1851 mutex_unlock(&cam
->busy_lock
);
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 /******************************************************************************
1865 *****************************************************************************/
1866 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
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
) {
1878 retval
= cpia2_remap_buffer(cam
, area
);
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
)
1894 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1895 * Ioctl. Here, just do the window and picture stucts.
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;
1904 cam
->vw
.width
= cam
->params
.roi
.width
;
1905 cam
->vw
.height
= cam
->params
.roi
.height
;
1907 cam
->vw
.clipcount
= 0;
1909 cam
->frame_size
= buffer_size
;
1910 cam
->num_frames
= num_buffers
;
1913 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1914 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1917 cam
->params
.camera_state
.stream_mode
= alternate
;
1919 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1920 v4l2_prio_init(&cam
->prio
);
1925 * The v4l video device structure initialized for this device
1927 static const struct file_operations fops_template
= {
1928 .owner
= THIS_MODULE
,
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
,
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
|
1946 .hardware
= VID_HARDWARE_CPIA2
,
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();
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
);
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
);
1989 LOG("/dev/video%d removed while open, "
1990 "deferring video_unregister_device\n",
1995 /******************************************************************************
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",
2012 buffer_size
+= PAGE_SIZE
-1;
2013 buffer_size
&= ~(PAGE_SIZE
-1);
2016 if(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",
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",
2041 if(video_nr
< -1 || video_nr
> 64) {
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
);
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
);