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>
45 //#define _CPIA2_DEBUG_
47 #define MAKE_STRING_1(x) #x
48 #define MAKE_STRING(x) MAKE_STRING_1(x)
50 static int video_nr
= -1;
51 module_param(video_nr
, int, 0);
52 MODULE_PARM_DESC(video_nr
,"video device to register (0=/dev/video0, etc)");
54 static int buffer_size
= 68*1024;
55 module_param(buffer_size
, int, 0);
56 MODULE_PARM_DESC(buffer_size
, "Size for each frame buffer in bytes (default 68k)");
58 static int num_buffers
= 3;
59 module_param(num_buffers
, int, 0);
60 MODULE_PARM_DESC(num_buffers
, "Number of frame buffers (1-"
61 MAKE_STRING(VIDEO_MAX_FRAME
) ", default 3)");
63 static int alternate
= DEFAULT_ALT
;
64 module_param(alternate
, int, 0);
65 MODULE_PARM_DESC(alternate
, "USB Alternate (" MAKE_STRING(USBIF_ISO_1
) "-"
66 MAKE_STRING(USBIF_ISO_6
) ", default "
67 MAKE_STRING(DEFAULT_ALT
) ")");
69 static int flicker_freq
= 60;
70 module_param(flicker_freq
, int, 0);
71 MODULE_PARM_DESC(flicker_freq
, "Flicker frequency (" MAKE_STRING(50) "or"
72 MAKE_STRING(60) ", default "
75 static int flicker_mode
= NEVER_FLICKER
;
76 module_param(flicker_mode
, int, 0);
77 MODULE_PARM_DESC(flicker_mode
,
78 "Flicker supression (" MAKE_STRING(NEVER_FLICKER
) "or"
79 MAKE_STRING(ANTI_FLICKER_ON
) ", default "
80 MAKE_STRING(NEVER_FLICKER
) ")");
82 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
83 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
84 MODULE_SUPPORTED_DEVICE("video");
85 MODULE_LICENSE("GPL");
87 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
89 #ifndef VID_HARDWARE_CPIA2
90 #error "VID_HARDWARE_CPIA2 should have been defined in linux/videodev.h"
93 struct control_menu_info
{
98 static struct control_menu_info framerate_controls
[] =
100 { CPIA2_VP_FRAMERATE_6_25
, "6.25 fps" },
101 { CPIA2_VP_FRAMERATE_7_5
, "7.5 fps" },
102 { CPIA2_VP_FRAMERATE_12_5
, "12.5 fps" },
103 { CPIA2_VP_FRAMERATE_15
, "15 fps" },
104 { CPIA2_VP_FRAMERATE_25
, "25 fps" },
105 { CPIA2_VP_FRAMERATE_30
, "30 fps" },
107 #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
109 static struct control_menu_info flicker_controls
[] =
111 { NEVER_FLICKER
, "Off" },
112 { FLICKER_50
, "50 Hz" },
113 { FLICKER_60
, "60 Hz" },
115 #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
117 static struct control_menu_info lights_controls
[] =
124 #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
125 #define GPIO_LIGHTS_MASK 192
127 static struct v4l2_queryctrl controls
[] = {
129 .id
= V4L2_CID_BRIGHTNESS
,
130 .type
= V4L2_CTRL_TYPE_INTEGER
,
131 .name
= "Brightness",
135 .default_value
= DEFAULT_BRIGHTNESS
,
138 .id
= V4L2_CID_CONTRAST
,
139 .type
= V4L2_CTRL_TYPE_INTEGER
,
144 .default_value
= DEFAULT_CONTRAST
,
147 .id
= V4L2_CID_SATURATION
,
148 .type
= V4L2_CTRL_TYPE_INTEGER
,
149 .name
= "Saturation",
153 .default_value
= DEFAULT_SATURATION
,
156 .id
= V4L2_CID_HFLIP
,
157 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
158 .name
= "Mirror Horizontally",
165 .id
= V4L2_CID_VFLIP
,
166 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
167 .name
= "Flip Vertically",
174 .id
= CPIA2_CID_TARGET_KB
,
175 .type
= V4L2_CTRL_TYPE_INTEGER
,
180 .default_value
= DEFAULT_TARGET_KB
,
183 .id
= CPIA2_CID_GPIO
,
184 .type
= V4L2_CTRL_TYPE_INTEGER
,
192 .id
= CPIA2_CID_FLICKER_MODE
,
193 .type
= V4L2_CTRL_TYPE_MENU
,
194 .name
= "Flicker Reduction",
196 .maximum
= NUM_FLICKER_CONTROLS
-1,
201 .id
= CPIA2_CID_FRAMERATE
,
202 .type
= V4L2_CTRL_TYPE_MENU
,
205 .maximum
= NUM_FRAMERATE_CONTROLS
-1,
207 .default_value
= NUM_FRAMERATE_CONTROLS
-1,
210 .id
= CPIA2_CID_USB_ALT
,
211 .type
= V4L2_CTRL_TYPE_INTEGER
,
212 .name
= "USB Alternate",
213 .minimum
= USBIF_ISO_1
,
214 .maximum
= USBIF_ISO_6
,
216 .default_value
= DEFAULT_ALT
,
219 .id
= CPIA2_CID_LIGHTS
,
220 .type
= V4L2_CTRL_TYPE_MENU
,
223 .maximum
= NUM_LIGHTS_CONTROLS
-1,
228 .id
= CPIA2_CID_RESET_CAMERA
,
229 .type
= V4L2_CTRL_TYPE_BUTTON
,
230 .name
= "Reset Camera",
237 #define NUM_CONTROLS (ARRAY_SIZE(controls))
240 /******************************************************************************
244 *****************************************************************************/
245 static int cpia2_open(struct inode
*inode
, struct file
*file
)
247 struct video_device
*dev
= video_devdata(file
);
248 struct camera_data
*cam
= video_get_drvdata(dev
);
252 ERR("Internal error, camera_data not found!\n");
256 if(mutex_lock_interruptible(&cam
->busy_lock
))
264 if (cam
->open_count
> 0) {
268 if (cpia2_allocate_buffers(cam
)) {
273 /* reset the camera */
274 if (cpia2_reset_camera(cam
) < 0) {
284 struct cpia2_fh
*fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
289 file
->private_data
= fh
;
290 fh
->prio
= V4L2_PRIORITY_UNSET
;
291 v4l2_prio_open(&cam
->prio
, &fh
->prio
);
297 cpia2_dbg_dump_registers(cam
);
300 mutex_unlock(&cam
->busy_lock
);
304 /******************************************************************************
308 *****************************************************************************/
309 static int cpia2_close(struct inode
*inode
, struct file
*file
)
311 struct video_device
*dev
= video_devdata(file
);
312 struct camera_data
*cam
= video_get_drvdata(dev
);
313 struct cpia2_fh
*fh
= file
->private_data
;
315 mutex_lock(&cam
->busy_lock
);
318 (cam
->open_count
== 1
319 || fh
->prio
== V4L2_PRIORITY_RECORD
321 cpia2_usb_stream_stop(cam
);
323 if(cam
->open_count
== 1) {
324 /* save camera state for later open */
325 cpia2_save_camera_state(cam
);
327 cpia2_set_low_power(cam
);
328 cpia2_free_buffers(cam
);
335 v4l2_prio_close(&cam
->prio
,&fh
->prio
);
336 file
->private_data
= NULL
;
340 if (--cam
->open_count
== 0) {
341 cpia2_free_buffers(cam
);
343 video_unregister_device(dev
);
344 mutex_unlock(&cam
->busy_lock
);
350 mutex_unlock(&cam
->busy_lock
);
355 /******************************************************************************
359 *****************************************************************************/
360 static ssize_t
cpia2_v4l_read(struct file
*file
, char __user
*buf
, size_t count
,
363 struct video_device
*dev
= video_devdata(file
);
364 struct camera_data
*cam
= video_get_drvdata(dev
);
365 int noblock
= file
->f_flags
&O_NONBLOCK
;
367 struct cpia2_fh
*fh
= file
->private_data
;
373 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
377 return cpia2_read(cam
, buf
, count
, noblock
);
381 /******************************************************************************
385 *****************************************************************************/
386 static unsigned int cpia2_v4l_poll(struct file
*filp
, struct poll_table_struct
*wait
)
388 struct video_device
*dev
= video_devdata(filp
);
389 struct camera_data
*cam
= video_get_drvdata(dev
);
391 struct cpia2_fh
*fh
= filp
->private_data
;
397 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
401 return cpia2_poll(cam
, filp
, wait
);
405 /******************************************************************************
409 *****************************************************************************/
410 static int ioctl_cap_query(void *arg
, struct camera_data
*cam
)
412 struct video_capability
*vc
;
416 if (cam
->params
.pnp_id
.product
== 0x151)
417 strcpy(vc
->name
, "QX5 Microscope");
419 strcpy(vc
->name
, "CPiA2 Camera");
421 vc
->type
= VID_TYPE_CAPTURE
| VID_TYPE_MJPEG_ENCODER
;
424 vc
->minwidth
= 176; /* VIDEOSIZE_QCIF */
426 switch (cam
->params
.version
.sensor_flags
) {
427 case CPIA2_VP_SENSOR_FLAGS_500
:
428 vc
->maxwidth
= STV_IMAGE_VGA_COLS
;
429 vc
->maxheight
= STV_IMAGE_VGA_ROWS
;
431 case CPIA2_VP_SENSOR_FLAGS_410
:
432 vc
->maxwidth
= STV_IMAGE_CIF_COLS
;
433 vc
->maxheight
= STV_IMAGE_CIF_ROWS
;
442 /******************************************************************************
446 *****************************************************************************/
447 static int ioctl_get_channel(void *arg
)
450 struct video_channel
*v
;
457 strcpy(v
->name
, "Camera");
460 v
->type
= VIDEO_TYPE_CAMERA
;
466 /******************************************************************************
470 *****************************************************************************/
471 static int ioctl_set_channel(void *arg
)
473 struct video_channel
*v
;
477 if (retval
== 0 && v
->channel
!= 0)
483 /******************************************************************************
485 * ioctl_set_image_prop
487 *****************************************************************************/
488 static int ioctl_set_image_prop(void *arg
, struct camera_data
*cam
)
490 struct video_picture
*vp
;
494 /* brightness, color, contrast need no check 0-65535 */
495 memcpy(&cam
->vp
, vp
, sizeof(*vp
));
497 /* update cam->params.colorParams */
498 cam
->params
.color_params
.brightness
= vp
->brightness
/ 256;
499 cam
->params
.color_params
.saturation
= vp
->colour
/ 256;
500 cam
->params
.color_params
.contrast
= vp
->contrast
/ 256;
502 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
503 cam
->params
.color_params
.brightness
,
504 cam
->params
.color_params
.saturation
,
505 cam
->params
.color_params
.contrast
);
507 cpia2_set_color_params(cam
);
512 static int sync(struct camera_data
*cam
, int frame_nr
)
514 struct framebuf
*frame
= &cam
->buffers
[frame_nr
];
517 if (frame
->status
== FRAME_READY
)
520 if (!cam
->streaming
) {
521 frame
->status
= FRAME_READY
;
526 mutex_unlock(&cam
->busy_lock
);
527 wait_event_interruptible(cam
->wq_stream
,
529 frame
->status
== FRAME_READY
);
530 mutex_lock(&cam
->busy_lock
);
531 if (signal_pending(current
))
538 /******************************************************************************
540 * ioctl_set_window_size
542 *****************************************************************************/
543 static int ioctl_set_window_size(void *arg
, struct camera_data
*cam
,
546 /* copy_from_user, check validity, copy to internal structure */
547 struct video_window
*vw
;
551 if (vw
->clipcount
!= 0) /* clipping not supported */
554 if (vw
->clips
!= NULL
) /* clipping not supported */
557 /* Ensure that only this process can change the format. */
558 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
562 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
564 /* Be sure to supply the Huffman tables, this isn't MJPEG */
565 cam
->params
.compression
.inhibit_htables
= 0;
567 /* we set the video window to something smaller or equal to what
568 * is requested by the user???
570 DBG("Requested width = %d, height = %d\n", vw
->width
, vw
->height
);
571 if (vw
->width
!= cam
->vw
.width
|| vw
->height
!= cam
->vw
.height
) {
572 cam
->vw
.width
= vw
->width
;
573 cam
->vw
.height
= vw
->height
;
574 cam
->params
.roi
.width
= vw
->width
;
575 cam
->params
.roi
.height
= vw
->height
;
576 cpia2_set_format(cam
);
579 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
580 if (cam
->buffers
[frame
].status
== FRAME_READING
)
581 if ((err
= sync(cam
, frame
)) < 0)
584 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
590 /******************************************************************************
594 *****************************************************************************/
595 static int ioctl_get_mbuf(void *arg
, struct camera_data
*cam
)
597 struct video_mbuf
*vm
;
601 memset(vm
, 0, sizeof(*vm
));
602 vm
->size
= cam
->frame_size
*cam
->num_frames
;
603 vm
->frames
= cam
->num_frames
;
604 for (i
= 0; i
< cam
->num_frames
; i
++)
605 vm
->offsets
[i
] = cam
->frame_size
* i
;
610 /******************************************************************************
614 *****************************************************************************/
615 static int ioctl_mcapture(void *arg
, struct camera_data
*cam
,
618 struct video_mmap
*vm
;
622 if (vm
->frame
< 0 || vm
->frame
>= cam
->num_frames
)
626 video_size
= cpia2_match_video_size(vm
->width
, vm
->height
);
627 if (cam
->video_size
< 0) {
631 /* Ensure that only this process can change the format. */
632 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
636 if (video_size
!= cam
->video_size
) {
637 cam
->video_size
= video_size
;
638 cam
->params
.roi
.width
= vm
->width
;
639 cam
->params
.roi
.height
= vm
->height
;
640 cpia2_set_format(cam
);
643 if (cam
->buffers
[vm
->frame
].status
== FRAME_READING
)
644 if ((err
=sync(cam
, vm
->frame
)) < 0)
647 cam
->buffers
[vm
->frame
].status
= FRAME_EMPTY
;
649 return cpia2_usb_stream_start(cam
,cam
->params
.camera_state
.stream_mode
);
652 /******************************************************************************
656 *****************************************************************************/
657 static int ioctl_sync(void *arg
, struct camera_data
*cam
)
663 if (frame
< 0 || frame
>= cam
->num_frames
)
666 return sync(cam
, frame
);
670 /******************************************************************************
674 *****************************************************************************/
676 static int ioctl_set_gpio(void *arg
, struct camera_data
*cam
)
680 gpio_val
= *(__u32
*) arg
;
682 if (gpio_val
&~ 0xFFU
)
685 return cpia2_set_gpio(cam
, (unsigned char)gpio_val
);
688 /******************************************************************************
692 * V4L2 device capabilities
694 *****************************************************************************/
696 static int ioctl_querycap(void *arg
, struct camera_data
*cam
)
698 struct v4l2_capability
*vc
= arg
;
700 memset(vc
, 0, sizeof(*vc
));
701 strcpy(vc
->driver
, "cpia2");
703 if (cam
->params
.pnp_id
.product
== 0x151)
704 strcpy(vc
->card
, "QX5 Microscope");
706 strcpy(vc
->card
, "CPiA2 Camera");
707 switch (cam
->params
.pnp_id
.device_type
) {
709 strcat(vc
->card
, " (672/");
712 strcat(vc
->card
, " (676/");
715 strcat(vc
->card
, " (???/");
718 switch (cam
->params
.version
.sensor_flags
) {
719 case CPIA2_VP_SENSOR_FLAGS_404
:
720 strcat(vc
->card
, "404)");
722 case CPIA2_VP_SENSOR_FLAGS_407
:
723 strcat(vc
->card
, "407)");
725 case CPIA2_VP_SENSOR_FLAGS_409
:
726 strcat(vc
->card
, "409)");
728 case CPIA2_VP_SENSOR_FLAGS_410
:
729 strcat(vc
->card
, "410)");
731 case CPIA2_VP_SENSOR_FLAGS_500
:
732 strcat(vc
->card
, "500)");
735 strcat(vc
->card
, "???)");
739 if (usb_make_path(cam
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
)) <0)
740 memset(vc
->bus_info
,0, sizeof(vc
->bus_info
));
742 vc
->version
= KERNEL_VERSION(CPIA2_MAJ_VER
, CPIA2_MIN_VER
,
745 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
752 /******************************************************************************
756 * V4L2 input get/set/enumerate
758 *****************************************************************************/
760 static int ioctl_input(unsigned int ioclt_nr
,void *arg
,struct camera_data
*cam
)
762 struct v4l2_input
*i
= arg
;
764 if(ioclt_nr
!= VIDIOC_G_INPUT
) {
769 memset(i
, 0, sizeof(*i
));
770 strcpy(i
->name
, "Camera");
771 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
776 /******************************************************************************
780 * V4L2 format enumerate
782 *****************************************************************************/
784 static int ioctl_enum_fmt(void *arg
,struct camera_data
*cam
)
786 struct v4l2_fmtdesc
*f
= arg
;
787 int index
= f
->index
;
789 if (index
< 0 || index
> 1)
792 memset(f
, 0, sizeof(*f
));
794 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
795 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
798 strcpy(f
->description
, "MJPEG");
799 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
802 strcpy(f
->description
, "JPEG");
803 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
812 /******************************************************************************
818 *****************************************************************************/
820 static int ioctl_try_fmt(void *arg
,struct camera_data
*cam
)
822 struct v4l2_format
*f
= arg
;
824 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
827 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
828 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
831 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
832 f
->fmt
.pix
.bytesperline
= 0;
833 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
834 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
837 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
839 f
->fmt
.pix
.width
= 640;
840 f
->fmt
.pix
.height
= 480;
843 f
->fmt
.pix
.width
= 352;
844 f
->fmt
.pix
.height
= 288;
847 f
->fmt
.pix
.width
= 320;
848 f
->fmt
.pix
.height
= 240;
850 case VIDEOSIZE_288_216
:
851 f
->fmt
.pix
.width
= 288;
852 f
->fmt
.pix
.height
= 216;
854 case VIDEOSIZE_256_192
:
855 f
->fmt
.pix
.width
= 256;
856 f
->fmt
.pix
.height
= 192;
858 case VIDEOSIZE_224_168
:
859 f
->fmt
.pix
.width
= 224;
860 f
->fmt
.pix
.height
= 168;
862 case VIDEOSIZE_192_144
:
863 f
->fmt
.pix
.width
= 192;
864 f
->fmt
.pix
.height
= 144;
868 f
->fmt
.pix
.width
= 176;
869 f
->fmt
.pix
.height
= 144;
876 /******************************************************************************
882 *****************************************************************************/
884 static int ioctl_set_fmt(void *arg
,struct camera_data
*cam
, struct cpia2_fh
*fh
)
886 struct v4l2_format
*f
= arg
;
889 err
= ioctl_try_fmt(arg
, cam
);
893 /* Ensure that only this process can change the format. */
894 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
899 cam
->pixelformat
= f
->fmt
.pix
.pixelformat
;
901 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
902 * the missing Huffman table properly. */
903 cam
->params
.compression
.inhibit_htables
= 0;
904 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
906 /* we set the video window to something smaller or equal to what
907 * is requested by the user???
909 DBG("Requested width = %d, height = %d\n",
910 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
911 if (f
->fmt
.pix
.width
!= cam
->vw
.width
||
912 f
->fmt
.pix
.height
!= cam
->vw
.height
) {
913 cam
->vw
.width
= f
->fmt
.pix
.width
;
914 cam
->vw
.height
= f
->fmt
.pix
.height
;
915 cam
->params
.roi
.width
= f
->fmt
.pix
.width
;
916 cam
->params
.roi
.height
= f
->fmt
.pix
.height
;
917 cpia2_set_format(cam
);
920 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
921 if (cam
->buffers
[frame
].status
== FRAME_READING
)
922 if ((err
= sync(cam
, frame
)) < 0)
925 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
931 /******************************************************************************
937 *****************************************************************************/
939 static int ioctl_get_fmt(void *arg
,struct camera_data
*cam
)
941 struct v4l2_format
*f
= arg
;
943 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
946 f
->fmt
.pix
.width
= cam
->vw
.width
;
947 f
->fmt
.pix
.height
= cam
->vw
.height
;
948 f
->fmt
.pix
.pixelformat
= cam
->pixelformat
;
949 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
950 f
->fmt
.pix
.bytesperline
= 0;
951 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
952 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
958 /******************************************************************************
962 * V4L2 query cropping capabilities
963 * NOTE: cropping is currently disabled
965 *****************************************************************************/
967 static int ioctl_cropcap(void *arg
,struct camera_data
*cam
)
969 struct v4l2_cropcap
*c
= arg
;
971 if (c
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
976 c
->bounds
.width
= cam
->vw
.width
;
977 c
->bounds
.height
= cam
->vw
.height
;
980 c
->defrect
.width
= cam
->vw
.width
;
981 c
->defrect
.height
= cam
->vw
.height
;
982 c
->pixelaspect
.numerator
= 1;
983 c
->pixelaspect
.denominator
= 1;
988 /******************************************************************************
992 * V4L2 query possible control variables
994 *****************************************************************************/
996 static int ioctl_queryctrl(void *arg
,struct camera_data
*cam
)
998 struct v4l2_queryctrl
*c
= arg
;
1001 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
1002 if(c
->id
== controls
[i
].id
) {
1003 memcpy(c
, controls
+i
, sizeof(*c
));
1008 if(i
== NUM_CONTROLS
)
1011 /* Some devices have additional limitations */
1013 case V4L2_CID_BRIGHTNESS
:
1015 * Don't let the register be set to zero - bug in VP4
1016 * flash of full brightness
1018 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1021 case V4L2_CID_VFLIP
:
1023 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1024 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1026 case CPIA2_CID_FRAMERATE
:
1027 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
1028 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
1031 for(i
=0; i
<c
->maximum
; ++i
) {
1032 if(framerate_controls
[i
].value
==
1033 CPIA2_VP_FRAMERATE_15
) {
1035 c
->default_value
= i
;
1040 case CPIA2_CID_FLICKER_MODE
:
1041 // Flicker control only valid for 672.
1042 if(cam
->params
.pnp_id
.device_type
!= DEVICE_STV_672
)
1043 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1045 case CPIA2_CID_LIGHTS
:
1046 // Light control only valid for the QX5 Microscope.
1047 if(cam
->params
.pnp_id
.product
!= 0x151)
1048 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1057 /******************************************************************************
1061 * V4L2 query possible control variables
1063 *****************************************************************************/
1065 static int ioctl_querymenu(void *arg
,struct camera_data
*cam
)
1067 struct v4l2_querymenu
*m
= arg
;
1069 memset(m
->name
, 0, sizeof(m
->name
));
1073 case CPIA2_CID_FLICKER_MODE
:
1074 if(m
->index
< 0 || m
->index
>= NUM_FLICKER_CONTROLS
)
1077 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
1079 case CPIA2_CID_FRAMERATE
:
1081 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1082 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
1083 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
1086 for(i
=0; i
<maximum
; ++i
) {
1087 if(framerate_controls
[i
].value
==
1088 CPIA2_VP_FRAMERATE_15
)
1092 if(m
->index
< 0 || m
->index
> maximum
)
1095 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
1098 case CPIA2_CID_LIGHTS
:
1099 if(m
->index
< 0 || m
->index
>= NUM_LIGHTS_CONTROLS
)
1102 strcpy(m
->name
, lights_controls
[m
->index
].name
);
1111 /******************************************************************************
1115 * V4L2 get the value of a control variable
1117 *****************************************************************************/
1119 static int ioctl_g_ctrl(void *arg
,struct camera_data
*cam
)
1121 struct v4l2_control
*c
= arg
;
1124 case V4L2_CID_BRIGHTNESS
:
1125 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
1127 c
->value
= cam
->params
.color_params
.brightness
;
1129 case V4L2_CID_CONTRAST
:
1130 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
1132 c
->value
= cam
->params
.color_params
.contrast
;
1134 case V4L2_CID_SATURATION
:
1135 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
1137 c
->value
= cam
->params
.color_params
.saturation
;
1139 case V4L2_CID_HFLIP
:
1140 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1142 c
->value
= (cam
->params
.vp_params
.user_effects
&
1143 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
1145 case V4L2_CID_VFLIP
:
1146 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1148 c
->value
= (cam
->params
.vp_params
.user_effects
&
1149 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
1151 case CPIA2_CID_TARGET_KB
:
1152 c
->value
= cam
->params
.vc_params
.target_kb
;
1154 case CPIA2_CID_GPIO
:
1155 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1157 c
->value
= cam
->params
.vp_params
.gpio_data
;
1159 case CPIA2_CID_FLICKER_MODE
:
1162 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
1164 if(cam
->params
.flicker_control
.cam_register
&
1165 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
1166 mode
= NEVER_FLICKER
;
1168 if(cam
->params
.flicker_control
.cam_register
&
1169 CPIA2_VP_FLICKER_MODES_50HZ
) {
1175 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
1176 if(flicker_controls
[i
].value
== mode
) {
1181 if(i
== NUM_FLICKER_CONTROLS
)
1185 case CPIA2_CID_FRAMERATE
:
1187 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1189 for(i
=0; i
<= maximum
; i
++) {
1190 if(cam
->params
.vp_params
.frame_rate
==
1191 framerate_controls
[i
].value
)
1199 case CPIA2_CID_USB_ALT
:
1200 c
->value
= cam
->params
.camera_state
.stream_mode
;
1202 case CPIA2_CID_LIGHTS
:
1205 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1207 for(i
=0; i
<NUM_LIGHTS_CONTROLS
; i
++) {
1208 if((cam
->params
.vp_params
.gpio_data
&GPIO_LIGHTS_MASK
) ==
1209 lights_controls
[i
].value
) {
1213 if(i
== NUM_LIGHTS_CONTROLS
)
1218 case CPIA2_CID_RESET_CAMERA
:
1224 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
1229 /******************************************************************************
1233 * V4L2 set the value of a control variable
1235 *****************************************************************************/
1237 static int ioctl_s_ctrl(void *arg
,struct camera_data
*cam
)
1239 struct v4l2_control
*c
= arg
;
1243 DBG("Set control id:%d, value:%d\n", c
->id
, c
->value
);
1245 /* Check that the value is in range */
1246 for(i
=0; i
<NUM_CONTROLS
; i
++) {
1247 if(c
->id
== controls
[i
].id
) {
1248 if(c
->value
< controls
[i
].minimum
||
1249 c
->value
> controls
[i
].maximum
) {
1255 if(i
== NUM_CONTROLS
)
1259 case V4L2_CID_BRIGHTNESS
:
1260 cpia2_set_brightness(cam
, c
->value
);
1262 case V4L2_CID_CONTRAST
:
1263 cpia2_set_contrast(cam
, c
->value
);
1265 case V4L2_CID_SATURATION
:
1266 cpia2_set_saturation(cam
, c
->value
);
1268 case V4L2_CID_HFLIP
:
1269 cpia2_set_property_mirror(cam
, c
->value
);
1271 case V4L2_CID_VFLIP
:
1272 cpia2_set_property_flip(cam
, c
->value
);
1274 case CPIA2_CID_TARGET_KB
:
1275 retval
= cpia2_set_target_kb(cam
, c
->value
);
1277 case CPIA2_CID_GPIO
:
1278 retval
= cpia2_set_gpio(cam
, c
->value
);
1280 case CPIA2_CID_FLICKER_MODE
:
1281 retval
= cpia2_set_flicker_mode(cam
,
1282 flicker_controls
[c
->value
].value
);
1284 case CPIA2_CID_FRAMERATE
:
1285 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1287 case CPIA2_CID_USB_ALT
:
1288 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1290 case CPIA2_CID_LIGHTS
:
1291 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1293 case CPIA2_CID_RESET_CAMERA
:
1294 cpia2_usb_stream_pause(cam
);
1295 cpia2_reset_camera(cam
);
1296 cpia2_usb_stream_resume(cam
);
1305 /******************************************************************************
1309 * V4L2 get the JPEG compression parameters
1311 *****************************************************************************/
1313 static int ioctl_g_jpegcomp(void *arg
,struct camera_data
*cam
)
1315 struct v4l2_jpegcompression
*parms
= arg
;
1317 memset(parms
, 0, sizeof(*parms
));
1319 parms
->quality
= 80; // TODO: Can this be made meaningful?
1321 parms
->jpeg_markers
= V4L2_JPEG_MARKER_DQT
| V4L2_JPEG_MARKER_DRI
;
1322 if(!cam
->params
.compression
.inhibit_htables
) {
1323 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_DHT
;
1326 parms
->APPn
= cam
->APPn
;
1327 parms
->APP_len
= cam
->APP_len
;
1328 if(cam
->APP_len
> 0) {
1329 memcpy(parms
->APP_data
, cam
->APP_data
, cam
->APP_len
);
1330 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_APP
;
1333 parms
->COM_len
= cam
->COM_len
;
1334 if(cam
->COM_len
> 0) {
1335 memcpy(parms
->COM_data
, cam
->COM_data
, cam
->COM_len
);
1336 parms
->jpeg_markers
|= JPEG_MARKER_COM
;
1339 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1340 parms
->APP_len
, parms
->COM_len
);
1345 /******************************************************************************
1349 * V4L2 set the JPEG compression parameters
1350 * NOTE: quality and some jpeg_markers are ignored.
1352 *****************************************************************************/
1354 static int ioctl_s_jpegcomp(void *arg
,struct camera_data
*cam
)
1356 struct v4l2_jpegcompression
*parms
= arg
;
1358 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1359 parms
->APP_len
, parms
->COM_len
);
1361 cam
->params
.compression
.inhibit_htables
=
1362 !(parms
->jpeg_markers
& V4L2_JPEG_MARKER_DHT
);
1364 if(parms
->APP_len
!= 0) {
1365 if(parms
->APP_len
> 0 &&
1366 parms
->APP_len
<= sizeof(cam
->APP_data
) &&
1367 parms
->APPn
>= 0 && parms
->APPn
<= 15) {
1368 cam
->APPn
= parms
->APPn
;
1369 cam
->APP_len
= parms
->APP_len
;
1370 memcpy(cam
->APP_data
, parms
->APP_data
, parms
->APP_len
);
1372 LOG("Bad APPn Params n=%d len=%d\n",
1373 parms
->APPn
, parms
->APP_len
);
1380 if(parms
->COM_len
!= 0) {
1381 if(parms
->COM_len
> 0 &&
1382 parms
->COM_len
<= sizeof(cam
->COM_data
)) {
1383 cam
->COM_len
= parms
->COM_len
;
1384 memcpy(cam
->COM_data
, parms
->COM_data
, parms
->COM_len
);
1386 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1394 /******************************************************************************
1398 * V4L2 Initiate memory mapping.
1399 * NOTE: The user's request is ignored. For now the buffers are fixed.
1401 *****************************************************************************/
1403 static int ioctl_reqbufs(void *arg
,struct camera_data
*cam
)
1405 struct v4l2_requestbuffers
*req
= arg
;
1407 if(req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1408 req
->memory
!= V4L2_MEMORY_MMAP
)
1411 DBG("REQBUFS requested:%d returning:%d\n", req
->count
, cam
->num_frames
);
1412 req
->count
= cam
->num_frames
;
1413 memset(&req
->reserved
, 0, sizeof(req
->reserved
));
1418 /******************************************************************************
1422 * V4L2 Query memory buffer status.
1424 *****************************************************************************/
1426 static int ioctl_querybuf(void *arg
,struct camera_data
*cam
)
1428 struct v4l2_buffer
*buf
= arg
;
1430 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1431 buf
->index
> cam
->num_frames
)
1434 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1435 buf
->length
= cam
->frame_size
;
1437 buf
->memory
= V4L2_MEMORY_MMAP
;
1440 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1444 switch (cam
->buffers
[buf
->index
].status
) {
1449 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
1452 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1453 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1454 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1455 buf
->flags
= V4L2_BUF_FLAG_DONE
;
1459 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1460 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1466 /******************************************************************************
1470 * V4L2 User is freeing buffer
1472 *****************************************************************************/
1474 static int ioctl_qbuf(void *arg
,struct camera_data
*cam
)
1476 struct v4l2_buffer
*buf
= arg
;
1478 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1479 buf
->memory
!= V4L2_MEMORY_MMAP
||
1480 buf
->index
> cam
->num_frames
)
1483 DBG("QBUF #%d\n", buf
->index
);
1485 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1486 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
1491 /******************************************************************************
1493 * find_earliest_filled_buffer
1495 * Helper for ioctl_dqbuf. Find the next ready buffer.
1497 *****************************************************************************/
1499 static int find_earliest_filled_buffer(struct camera_data
*cam
)
1503 for (i
=0; i
<cam
->num_frames
; i
++) {
1504 if(cam
->buffers
[i
].status
== FRAME_READY
) {
1508 /* find which buffer is earlier */
1509 struct timeval
*tv1
, *tv2
;
1510 tv1
= &cam
->buffers
[i
].timestamp
;
1511 tv2
= &cam
->buffers
[found
].timestamp
;
1512 if(tv1
->tv_sec
< tv2
->tv_sec
||
1513 (tv1
->tv_sec
== tv2
->tv_sec
&&
1514 tv1
->tv_usec
< tv2
->tv_usec
))
1522 /******************************************************************************
1526 * V4L2 User is asking for a filled buffer.
1528 *****************************************************************************/
1530 static int ioctl_dqbuf(void *arg
,struct camera_data
*cam
, struct file
*file
)
1532 struct v4l2_buffer
*buf
= arg
;
1535 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1536 buf
->memory
!= V4L2_MEMORY_MMAP
)
1539 frame
= find_earliest_filled_buffer(cam
);
1541 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
1545 /* Wait for a frame to become available */
1546 struct framebuf
*cb
=cam
->curbuff
;
1547 mutex_unlock(&cam
->busy_lock
);
1548 wait_event_interruptible(cam
->wq_stream
,
1550 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1551 mutex_lock(&cam
->busy_lock
);
1552 if (signal_pending(current
))
1553 return -ERESTARTSYS
;
1561 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1562 buf
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_DONE
;
1563 buf
->field
= V4L2_FIELD_NONE
;
1564 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1565 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1566 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1567 buf
->length
= cam
->frame_size
;
1570 memset(&buf
->timecode
, 0, sizeof(buf
->timecode
));
1572 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf
->index
,
1573 cam
->buffers
[buf
->index
].status
, buf
->sequence
, buf
->bytesused
);
1578 /******************************************************************************
1582 *****************************************************************************/
1583 static int cpia2_do_ioctl(struct inode
*inode
, struct file
*file
,
1584 unsigned int ioctl_nr
, void *arg
)
1586 struct video_device
*dev
= video_devdata(file
);
1587 struct camera_data
*cam
= video_get_drvdata(dev
);
1593 /* make this _really_ smp-safe */
1594 if (mutex_lock_interruptible(&cam
->busy_lock
))
1595 return -ERESTARTSYS
;
1597 if (!cam
->present
) {
1598 mutex_unlock(&cam
->busy_lock
);
1602 /* Priority check */
1605 case VIDIOCMCAPTURE
:
1608 struct cpia2_fh
*fh
= file
->private_data
;
1609 retval
= v4l2_prio_check(&cam
->prio
, &fh
->prio
);
1611 mutex_unlock(&cam
->busy_lock
);
1619 struct cpia2_fh
*fh
= file
->private_data
;
1620 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1621 mutex_unlock(&cam
->busy_lock
);
1631 case VIDIOCGCAP
: /* query capabilities */
1632 retval
= ioctl_cap_query(arg
, cam
);
1635 case VIDIOCGCHAN
: /* get video source - we are a camera, nothing else */
1636 retval
= ioctl_get_channel(arg
);
1638 case VIDIOCSCHAN
: /* set video source - we are a camera, nothing else */
1639 retval
= ioctl_set_channel(arg
);
1641 case VIDIOCGPICT
: /* image properties */
1642 memcpy(arg
, &cam
->vp
, sizeof(struct video_picture
));
1645 retval
= ioctl_set_image_prop(arg
, cam
);
1647 case VIDIOCGWIN
: /* get/set capture window */
1648 memcpy(arg
, &cam
->vw
, sizeof(struct video_window
));
1651 retval
= ioctl_set_window_size(arg
, cam
, file
->private_data
);
1653 case VIDIOCGMBUF
: /* mmap interface */
1654 retval
= ioctl_get_mbuf(arg
, cam
);
1656 case VIDIOCMCAPTURE
:
1657 retval
= ioctl_mcapture(arg
, cam
, file
->private_data
);
1660 retval
= ioctl_sync(arg
, cam
);
1662 /* pointless to implement overlay with this camera */
1670 /* tuner interface - we have none */
1678 /* audio interface - we have none */
1684 /* CPIA2 extension to Video4Linux API */
1685 case CPIA2_IOC_SET_GPIO
:
1686 retval
= ioctl_set_gpio(arg
, cam
);
1688 case VIDIOC_QUERYCAP
:
1689 retval
= ioctl_querycap(arg
,cam
);
1692 case VIDIOC_ENUMINPUT
:
1693 case VIDIOC_G_INPUT
:
1694 case VIDIOC_S_INPUT
:
1695 retval
= ioctl_input(ioctl_nr
, arg
,cam
);
1698 case VIDIOC_ENUM_FMT
:
1699 retval
= ioctl_enum_fmt(arg
,cam
);
1701 case VIDIOC_TRY_FMT
:
1702 retval
= ioctl_try_fmt(arg
,cam
);
1705 retval
= ioctl_get_fmt(arg
,cam
);
1708 retval
= ioctl_set_fmt(arg
,cam
,file
->private_data
);
1711 case VIDIOC_CROPCAP
:
1712 retval
= ioctl_cropcap(arg
,cam
);
1716 // TODO: I think cropping can be implemented - SJB
1720 case VIDIOC_QUERYCTRL
:
1721 retval
= ioctl_queryctrl(arg
,cam
);
1723 case VIDIOC_QUERYMENU
:
1724 retval
= ioctl_querymenu(arg
,cam
);
1727 retval
= ioctl_g_ctrl(arg
,cam
);
1730 retval
= ioctl_s_ctrl(arg
,cam
);
1733 case VIDIOC_G_JPEGCOMP
:
1734 retval
= ioctl_g_jpegcomp(arg
,cam
);
1736 case VIDIOC_S_JPEGCOMP
:
1737 retval
= ioctl_s_jpegcomp(arg
,cam
);
1740 case VIDIOC_G_PRIORITY
:
1742 struct cpia2_fh
*fh
= file
->private_data
;
1743 *(enum v4l2_priority
*)arg
= fh
->prio
;
1746 case VIDIOC_S_PRIORITY
:
1748 struct cpia2_fh
*fh
= file
->private_data
;
1749 enum v4l2_priority prio
;
1750 prio
= *(enum v4l2_priority
*)arg
;
1751 if(cam
->streaming
&&
1753 fh
->prio
== V4L2_PRIORITY_RECORD
) {
1754 /* Can't drop record priority while streaming */
1756 } else if(prio
== V4L2_PRIORITY_RECORD
&&
1758 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
) {
1759 /* Only one program can record at a time */
1762 retval
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1767 case VIDIOC_REQBUFS
:
1768 retval
= ioctl_reqbufs(arg
,cam
);
1770 case VIDIOC_QUERYBUF
:
1771 retval
= ioctl_querybuf(arg
,cam
);
1774 retval
= ioctl_qbuf(arg
,cam
);
1777 retval
= ioctl_dqbuf(arg
,cam
,file
);
1779 case VIDIOC_STREAMON
:
1782 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1784 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1787 if(!cam
->streaming
) {
1788 retval
= cpia2_usb_stream_start(cam
,
1789 cam
->params
.camera_state
.stream_mode
);
1796 case VIDIOC_STREAMOFF
:
1799 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1801 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1804 if(cam
->streaming
) {
1805 retval
= cpia2_usb_stream_stop(cam
);
1813 case VIDIOC_ENUMOUTPUT
:
1814 case VIDIOC_G_OUTPUT
:
1815 case VIDIOC_S_OUTPUT
:
1816 case VIDIOC_G_MODULATOR
:
1817 case VIDIOC_S_MODULATOR
:
1819 case VIDIOC_ENUMAUDIO
:
1820 case VIDIOC_G_AUDIO
:
1821 case VIDIOC_S_AUDIO
:
1823 case VIDIOC_ENUMAUDOUT
:
1824 case VIDIOC_G_AUDOUT
:
1825 case VIDIOC_S_AUDOUT
:
1827 case VIDIOC_ENUMSTD
:
1828 case VIDIOC_QUERYSTD
:
1832 case VIDIOC_G_TUNER
:
1833 case VIDIOC_S_TUNER
:
1834 case VIDIOC_G_FREQUENCY
:
1835 case VIDIOC_S_FREQUENCY
:
1837 case VIDIOC_OVERLAY
:
1846 retval
= -ENOIOCTLCMD
;
1850 mutex_unlock(&cam
->busy_lock
);
1854 static int cpia2_ioctl(struct inode
*inode
, struct file
*file
,
1855 unsigned int ioctl_nr
, unsigned long iarg
)
1857 return video_usercopy(inode
, file
, ioctl_nr
, iarg
, cpia2_do_ioctl
);
1860 /******************************************************************************
1864 *****************************************************************************/
1865 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
1868 struct video_device
*dev
= video_devdata(file
);
1869 struct camera_data
*cam
= video_get_drvdata(dev
);
1871 /* Priority check */
1872 struct cpia2_fh
*fh
= file
->private_data
;
1873 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1877 retval
= cpia2_remap_buffer(cam
, area
);
1884 /******************************************************************************
1886 * reset_camera_struct_v4l
1888 * Sets all values to the defaults
1889 *****************************************************************************/
1890 static void reset_camera_struct_v4l(struct camera_data
*cam
)
1893 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1894 * Ioctl. Here, just do the window and picture stucts.
1896 cam
->vp
.palette
= (u16
) VIDEO_PALETTE_RGB24
; /* Is this right? */
1897 cam
->vp
.brightness
= (u16
) cam
->params
.color_params
.brightness
* 256;
1898 cam
->vp
.colour
= (u16
) cam
->params
.color_params
.saturation
* 256;
1899 cam
->vp
.contrast
= (u16
) cam
->params
.color_params
.contrast
* 256;
1903 cam
->vw
.width
= cam
->params
.roi
.width
;
1904 cam
->vw
.height
= cam
->params
.roi
.height
;
1906 cam
->vw
.clipcount
= 0;
1908 cam
->frame_size
= buffer_size
;
1909 cam
->num_frames
= num_buffers
;
1912 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1913 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1916 cam
->params
.camera_state
.stream_mode
= alternate
;
1918 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1919 v4l2_prio_init(&cam
->prio
);
1924 * The v4l video device structure initialized for this device
1926 static const struct file_operations fops_template
= {
1927 .owner
= THIS_MODULE
,
1929 .release
= cpia2_close
,
1930 .read
= cpia2_v4l_read
,
1931 .poll
= cpia2_v4l_poll
,
1932 .ioctl
= cpia2_ioctl
,
1933 .llseek
= no_llseek
,
1934 .compat_ioctl
= v4l_compat_ioctl32
,
1938 static struct video_device cpia2_template
= {
1939 /* I could not find any place for the old .initialize initializer?? */
1940 .owner
= THIS_MODULE
,
1941 .name
= "CPiA2 Camera",
1942 .type
= VID_TYPE_CAPTURE
,
1943 .type2
= V4L2_CAP_VIDEO_CAPTURE
|
1945 .hardware
= VID_HARDWARE_CPIA2
,
1947 .fops
= &fops_template
,
1948 .release
= video_device_release
,
1951 /******************************************************************************
1953 * cpia2_register_camera
1955 *****************************************************************************/
1956 int cpia2_register_camera(struct camera_data
*cam
)
1958 cam
->vdev
= video_device_alloc();
1962 memcpy(cam
->vdev
, &cpia2_template
, sizeof(cpia2_template
));
1963 video_set_drvdata(cam
->vdev
, cam
);
1965 reset_camera_struct_v4l(cam
);
1967 /* register v4l device */
1968 if (video_register_device
1969 (cam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) == -1) {
1970 ERR("video_register_device failed\n");
1971 video_device_release(cam
->vdev
);
1978 /******************************************************************************
1980 * cpia2_unregister_camera
1982 *****************************************************************************/
1983 void cpia2_unregister_camera(struct camera_data
*cam
)
1985 if (!cam
->open_count
) {
1986 video_unregister_device(cam
->vdev
);
1988 LOG("/dev/video%d removed while open, "
1989 "deferring video_unregister_device\n",
1994 /******************************************************************************
1998 * Make sure that all user-supplied parameters are sensible
1999 *****************************************************************************/
2000 static void __init
check_parameters(void)
2002 if(buffer_size
< PAGE_SIZE
) {
2003 buffer_size
= PAGE_SIZE
;
2004 LOG("buffer_size too small, setting to %d\n", buffer_size
);
2005 } else if(buffer_size
> 1024*1024) {
2006 /* arbitrary upper limiit */
2007 buffer_size
= 1024*1024;
2008 LOG("buffer_size ridiculously large, setting to %d\n",
2011 buffer_size
+= PAGE_SIZE
-1;
2012 buffer_size
&= ~(PAGE_SIZE
-1);
2015 if(num_buffers
< 1) {
2017 LOG("num_buffers too small, setting to %d\n", num_buffers
);
2018 } else if(num_buffers
> VIDEO_MAX_FRAME
) {
2019 num_buffers
= VIDEO_MAX_FRAME
;
2020 LOG("num_buffers too large, setting to %d\n", num_buffers
);
2023 if(alternate
< USBIF_ISO_1
|| alternate
> USBIF_ISO_6
) {
2024 alternate
= DEFAULT_ALT
;
2025 LOG("alternate specified is invalid, using %d\n", alternate
);
2028 if (flicker_mode
!= NEVER_FLICKER
&& flicker_mode
!= ANTI_FLICKER_ON
) {
2029 flicker_mode
= NEVER_FLICKER
;
2030 LOG("Flicker mode specified is invalid, using %d\n",
2034 if (flicker_freq
!= FLICKER_50
&& flicker_freq
!= FLICKER_60
) {
2035 flicker_freq
= FLICKER_60
;
2036 LOG("Flicker mode specified is invalid, using %d\n",
2040 if(video_nr
< -1 || video_nr
> 64) {
2042 LOG("invalid video_nr specified, must be -1 to 64\n");
2045 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2046 num_buffers
, buffer_size
, alternate
);
2049 /************ Module Stuff ***************/
2052 /******************************************************************************
2054 * cpia2_init/module_init
2056 *****************************************************************************/
2057 static int __init
cpia2_init(void)
2059 LOG("%s v%d.%d.%d\n",
2060 ABOUT
, CPIA2_MAJ_VER
, CPIA2_MIN_VER
, CPIA2_PATCH_VER
);
2067 /******************************************************************************
2069 * cpia2_exit/module_exit
2071 *****************************************************************************/
2072 static void __exit
cpia2_exit(void)
2074 cpia2_usb_cleanup();
2075 schedule_timeout(2 * HZ
);
2078 module_init(cpia2_init
);
2079 module_exit(cpia2_exit
);