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>
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>
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 "
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"
95 struct control_menu_info
{
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
[] =
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",
137 .default_value
= DEFAULT_BRIGHTNESS
,
140 .id
= V4L2_CID_CONTRAST
,
141 .type
= V4L2_CTRL_TYPE_INTEGER
,
146 .default_value
= DEFAULT_CONTRAST
,
149 .id
= V4L2_CID_SATURATION
,
150 .type
= V4L2_CTRL_TYPE_INTEGER
,
151 .name
= "Saturation",
155 .default_value
= DEFAULT_SATURATION
,
158 .id
= V4L2_CID_HFLIP
,
159 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
160 .name
= "Mirror Horizontally",
167 .id
= V4L2_CID_VFLIP
,
168 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
169 .name
= "Flip Vertically",
176 .id
= CPIA2_CID_TARGET_KB
,
177 .type
= V4L2_CTRL_TYPE_INTEGER
,
182 .default_value
= DEFAULT_TARGET_KB
,
185 .id
= CPIA2_CID_GPIO
,
186 .type
= V4L2_CTRL_TYPE_INTEGER
,
194 .id
= CPIA2_CID_FLICKER_MODE
,
195 .type
= V4L2_CTRL_TYPE_MENU
,
196 .name
= "Flicker Reduction",
198 .maximum
= NUM_FLICKER_CONTROLS
-1,
203 .id
= CPIA2_CID_FRAMERATE
,
204 .type
= V4L2_CTRL_TYPE_MENU
,
207 .maximum
= NUM_FRAMERATE_CONTROLS
-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
,
218 .default_value
= DEFAULT_ALT
,
221 .id
= CPIA2_CID_LIGHTS
,
222 .type
= V4L2_CTRL_TYPE_MENU
,
225 .maximum
= NUM_LIGHTS_CONTROLS
-1,
230 .id
= CPIA2_CID_RESET_CAMERA
,
231 .type
= V4L2_CTRL_TYPE_BUTTON
,
232 .name
= "Reset Camera",
239 #define NUM_CONTROLS (sizeof(controls)/sizeof(controls[0]))
242 /******************************************************************************
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
);
254 ERR("Internal error, camera_data not found!\n");
258 if(mutex_lock_interruptible(&cam
->busy_lock
))
266 if (cam
->open_count
> 0) {
270 if (cpia2_allocate_buffers(cam
)) {
275 /* reset the camera */
276 if (cpia2_reset_camera(cam
) < 0) {
286 struct cpia2_fh
*fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
291 file
->private_data
= fh
;
292 fh
->prio
= V4L2_PRIORITY_UNSET
;
293 v4l2_prio_open(&cam
->prio
, &fh
->prio
);
299 cpia2_dbg_dump_registers(cam
);
302 mutex_unlock(&cam
->busy_lock
);
306 /******************************************************************************
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
);
320 (cam
->open_count
== 1
321 || fh
->prio
== V4L2_PRIORITY_RECORD
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
);
337 v4l2_prio_close(&cam
->prio
,&fh
->prio
);
338 file
->private_data
= NULL
;
342 if (--cam
->open_count
== 0) {
343 cpia2_free_buffers(cam
);
345 video_unregister_device(dev
);
346 mutex_unlock(&cam
->busy_lock
);
352 mutex_unlock(&cam
->busy_lock
);
357 /******************************************************************************
361 *****************************************************************************/
362 static ssize_t
cpia2_v4l_read(struct file
*file
, char __user
*buf
, size_t count
,
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
;
375 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
379 return cpia2_read(cam
, buf
, count
, noblock
);
383 /******************************************************************************
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
;
399 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
403 return cpia2_poll(cam
, filp
, wait
);
407 /******************************************************************************
411 *****************************************************************************/
412 static int ioctl_cap_query(void *arg
, struct camera_data
*cam
)
414 struct video_capability
*vc
;
418 if (cam
->params
.pnp_id
.product
== 0x151)
419 strcpy(vc
->name
, "QX5 Microscope");
421 strcpy(vc
->name
, "CPiA2 Camera");
423 vc
->type
= VID_TYPE_CAPTURE
| VID_TYPE_MJPEG_ENCODER
;
426 vc
->minwidth
= 176; /* VIDEOSIZE_QCIF */
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
;
433 case CPIA2_VP_SENSOR_FLAGS_410
:
434 vc
->maxwidth
= STV_IMAGE_CIF_COLS
;
435 vc
->maxheight
= STV_IMAGE_CIF_ROWS
;
444 /******************************************************************************
448 *****************************************************************************/
449 static int ioctl_get_channel(void *arg
)
452 struct video_channel
*v
;
459 strcpy(v
->name
, "Camera");
462 v
->type
= VIDEO_TYPE_CAMERA
;
468 /******************************************************************************
472 *****************************************************************************/
473 static int ioctl_set_channel(void *arg
)
475 struct video_channel
*v
;
479 if (retval
== 0 && v
->channel
!= 0)
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
;
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
);
514 static int sync(struct camera_data
*cam
, int frame_nr
)
516 struct framebuf
*frame
= &cam
->buffers
[frame_nr
];
519 if (frame
->status
== FRAME_READY
)
522 if (!cam
->streaming
) {
523 frame
->status
= FRAME_READY
;
528 mutex_unlock(&cam
->busy_lock
);
529 wait_event_interruptible(cam
->wq_stream
,
531 frame
->status
== FRAME_READY
);
532 mutex_lock(&cam
->busy_lock
);
533 if (signal_pending(current
))
540 /******************************************************************************
542 * ioctl_set_window_size
544 *****************************************************************************/
545 static int ioctl_set_window_size(void *arg
, struct camera_data
*cam
,
548 /* copy_from_user, check validity, copy to internal structure */
549 struct video_window
*vw
;
553 if (vw
->clipcount
!= 0) /* clipping not supported */
556 if (vw
->clips
!= NULL
) /* clipping not supported */
559 /* Ensure that only this process can change the format. */
560 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
586 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
592 /******************************************************************************
596 *****************************************************************************/
597 static int ioctl_get_mbuf(void *arg
, struct camera_data
*cam
)
599 struct video_mbuf
*vm
;
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
;
612 /******************************************************************************
616 *****************************************************************************/
617 static int ioctl_mcapture(void *arg
, struct camera_data
*cam
,
620 struct video_mmap
*vm
;
624 if (vm
->frame
< 0 || vm
->frame
>= cam
->num_frames
)
628 video_size
= cpia2_match_video_size(vm
->width
, vm
->height
);
629 if (cam
->video_size
< 0) {
633 /* Ensure that only this process can change the format. */
634 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
649 cam
->buffers
[vm
->frame
].status
= FRAME_EMPTY
;
651 return cpia2_usb_stream_start(cam
,cam
->params
.camera_state
.stream_mode
);
654 /******************************************************************************
658 *****************************************************************************/
659 static int ioctl_sync(void *arg
, struct camera_data
*cam
)
665 if (frame
< 0 || frame
>= cam
->num_frames
)
668 return sync(cam
, frame
);
672 /******************************************************************************
676 *****************************************************************************/
678 static int ioctl_set_gpio(void *arg
, struct camera_data
*cam
)
682 gpio_val
= *(__u32
*) arg
;
684 if (gpio_val
&~ 0xFFU
)
687 return cpia2_set_gpio(cam
, (unsigned char)gpio_val
);
690 /******************************************************************************
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");
708 strcpy(vc
->card
, "CPiA2 Camera");
709 switch (cam
->params
.pnp_id
.device_type
) {
711 strcat(vc
->card
, " (672/");
714 strcat(vc
->card
, " (676/");
717 strcat(vc
->card
, " (???/");
720 switch (cam
->params
.version
.sensor_flags
) {
721 case CPIA2_VP_SENSOR_FLAGS_404
:
722 strcat(vc
->card
, "404)");
724 case CPIA2_VP_SENSOR_FLAGS_407
:
725 strcat(vc
->card
, "407)");
727 case CPIA2_VP_SENSOR_FLAGS_409
:
728 strcat(vc
->card
, "409)");
730 case CPIA2_VP_SENSOR_FLAGS_410
:
731 strcat(vc
->card
, "410)");
733 case CPIA2_VP_SENSOR_FLAGS_500
:
734 strcat(vc
->card
, "500)");
737 strcat(vc
->card
, "???)");
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
,
747 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
754 /******************************************************************************
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
) {
771 memset(i
, 0, sizeof(*i
));
772 strcpy(i
->name
, "Camera");
773 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
778 /******************************************************************************
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)
794 memset(f
, 0, sizeof(*f
));
796 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
797 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
800 strcpy(f
->description
, "MJPEG");
801 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
804 strcpy(f
->description
, "JPEG");
805 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
814 /******************************************************************************
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
)
829 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
830 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
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
;
839 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
841 f
->fmt
.pix
.width
= 640;
842 f
->fmt
.pix
.height
= 480;
845 f
->fmt
.pix
.width
= 352;
846 f
->fmt
.pix
.height
= 288;
849 f
->fmt
.pix
.width
= 320;
850 f
->fmt
.pix
.height
= 240;
852 case VIDEOSIZE_288_216
:
853 f
->fmt
.pix
.width
= 288;
854 f
->fmt
.pix
.height
= 216;
856 case VIDEOSIZE_256_192
:
857 f
->fmt
.pix
.width
= 256;
858 f
->fmt
.pix
.height
= 192;
860 case VIDEOSIZE_224_168
:
861 f
->fmt
.pix
.width
= 224;
862 f
->fmt
.pix
.height
= 168;
864 case VIDEOSIZE_192_144
:
865 f
->fmt
.pix
.width
= 192;
866 f
->fmt
.pix
.height
= 144;
870 f
->fmt
.pix
.width
= 176;
871 f
->fmt
.pix
.height
= 144;
878 /******************************************************************************
884 *****************************************************************************/
886 static int ioctl_set_fmt(void *arg
,struct camera_data
*cam
, struct cpia2_fh
*fh
)
888 struct v4l2_format
*f
= arg
;
891 err
= ioctl_try_fmt(arg
, cam
);
895 /* Ensure that only this process can change the format. */
896 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
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)
927 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
933 /******************************************************************************
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
)
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
;
960 /******************************************************************************
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
)
978 c
->bounds
.width
= cam
->vw
.width
;
979 c
->bounds
.height
= cam
->vw
.height
;
982 c
->defrect
.width
= cam
->vw
.width
;
983 c
->defrect
.height
= cam
->vw
.height
;
984 c
->pixelaspect
.numerator
= 1;
985 c
->pixelaspect
.denominator
= 1;
990 /******************************************************************************
994 * V4L2 query possible control variables
996 *****************************************************************************/
998 static int ioctl_queryctrl(void *arg
,struct camera_data
*cam
)
1000 struct v4l2_queryctrl
*c
= arg
;
1003 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
1004 if(c
->id
== controls
[i
].id
) {
1005 memcpy(c
, controls
+i
, sizeof(*c
));
1010 if(i
== NUM_CONTROLS
)
1013 /* Some devices have additional limitations */
1015 case V4L2_CID_BRIGHTNESS
:
1017 * Don't let the register be set to zero - bug in VP4
1018 * flash of full brightness
1020 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1023 case V4L2_CID_VFLIP
:
1025 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1026 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
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
){
1033 for(i
=0; i
<c
->maximum
; ++i
) {
1034 if(framerate_controls
[i
].value
==
1035 CPIA2_VP_FRAMERATE_15
) {
1037 c
->default_value
= i
;
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
;
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
;
1059 /******************************************************************************
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
));
1075 case CPIA2_CID_FLICKER_MODE
:
1076 if(m
->index
< 0 || m
->index
>= NUM_FLICKER_CONTROLS
)
1079 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
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
){
1088 for(i
=0; i
<maximum
; ++i
) {
1089 if(framerate_controls
[i
].value
==
1090 CPIA2_VP_FRAMERATE_15
)
1094 if(m
->index
< 0 || m
->index
> maximum
)
1097 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
1100 case CPIA2_CID_LIGHTS
:
1101 if(m
->index
< 0 || m
->index
>= NUM_LIGHTS_CONTROLS
)
1104 strcpy(m
->name
, lights_controls
[m
->index
].name
);
1113 /******************************************************************************
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
;
1126 case V4L2_CID_BRIGHTNESS
:
1127 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
1129 c
->value
= cam
->params
.color_params
.brightness
;
1131 case V4L2_CID_CONTRAST
:
1132 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
1134 c
->value
= cam
->params
.color_params
.contrast
;
1136 case V4L2_CID_SATURATION
:
1137 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
1139 c
->value
= cam
->params
.color_params
.saturation
;
1141 case V4L2_CID_HFLIP
:
1142 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1144 c
->value
= (cam
->params
.vp_params
.user_effects
&
1145 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
1147 case V4L2_CID_VFLIP
:
1148 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1150 c
->value
= (cam
->params
.vp_params
.user_effects
&
1151 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
1153 case CPIA2_CID_TARGET_KB
:
1154 c
->value
= cam
->params
.vc_params
.target_kb
;
1156 case CPIA2_CID_GPIO
:
1157 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1159 c
->value
= cam
->params
.vp_params
.gpio_data
;
1161 case CPIA2_CID_FLICKER_MODE
:
1164 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
1166 if(cam
->params
.flicker_control
.cam_register
&
1167 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
1168 mode
= NEVER_FLICKER
;
1170 if(cam
->params
.flicker_control
.cam_register
&
1171 CPIA2_VP_FLICKER_MODES_50HZ
) {
1177 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
1178 if(flicker_controls
[i
].value
== mode
) {
1183 if(i
== NUM_FLICKER_CONTROLS
)
1187 case CPIA2_CID_FRAMERATE
:
1189 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1191 for(i
=0; i
<= maximum
; i
++) {
1192 if(cam
->params
.vp_params
.frame_rate
==
1193 framerate_controls
[i
].value
)
1201 case CPIA2_CID_USB_ALT
:
1202 c
->value
= cam
->params
.camera_state
.stream_mode
;
1204 case CPIA2_CID_LIGHTS
:
1207 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
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
) {
1215 if(i
== NUM_LIGHTS_CONTROLS
)
1220 case CPIA2_CID_RESET_CAMERA
:
1226 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
1231 /******************************************************************************
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
;
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
) {
1257 if(i
== NUM_CONTROLS
)
1261 case V4L2_CID_BRIGHTNESS
:
1262 cpia2_set_brightness(cam
, c
->value
);
1264 case V4L2_CID_CONTRAST
:
1265 cpia2_set_contrast(cam
, c
->value
);
1267 case V4L2_CID_SATURATION
:
1268 cpia2_set_saturation(cam
, c
->value
);
1270 case V4L2_CID_HFLIP
:
1271 cpia2_set_property_mirror(cam
, c
->value
);
1273 case V4L2_CID_VFLIP
:
1274 cpia2_set_property_flip(cam
, c
->value
);
1276 case CPIA2_CID_TARGET_KB
:
1277 retval
= cpia2_set_target_kb(cam
, c
->value
);
1279 case CPIA2_CID_GPIO
:
1280 retval
= cpia2_set_gpio(cam
, c
->value
);
1282 case CPIA2_CID_FLICKER_MODE
:
1283 retval
= cpia2_set_flicker_mode(cam
,
1284 flicker_controls
[c
->value
].value
);
1286 case CPIA2_CID_FRAMERATE
:
1287 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1289 case CPIA2_CID_USB_ALT
:
1290 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1292 case CPIA2_CID_LIGHTS
:
1293 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1295 case CPIA2_CID_RESET_CAMERA
:
1296 cpia2_usb_stream_pause(cam
);
1297 cpia2_reset_camera(cam
);
1298 cpia2_usb_stream_resume(cam
);
1307 /******************************************************************************
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
);
1347 /******************************************************************************
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
);
1374 LOG("Bad APPn Params n=%d len=%d\n",
1375 parms
->APPn
, parms
->APP_len
);
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
);
1388 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1396 /******************************************************************************
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
)
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
));
1420 /******************************************************************************
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
)
1436 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1437 buf
->length
= cam
->frame_size
;
1439 buf
->memory
= V4L2_MEMORY_MMAP
;
1442 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1446 switch (cam
->buffers
[buf
->index
].status
) {
1451 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
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
;
1461 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1462 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1468 /******************************************************************************
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
)
1485 DBG("QBUF #%d\n", buf
->index
);
1487 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1488 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
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
)
1505 for (i
=0; i
<cam
->num_frames
; i
++) {
1506 if(cam
->buffers
[i
].status
== FRAME_READY
) {
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
))
1524 /******************************************************************************
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
;
1537 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1538 buf
->memory
!= V4L2_MEMORY_MMAP
)
1541 frame
= find_earliest_filled_buffer(cam
);
1543 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
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
,
1552 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1553 mutex_lock(&cam
->busy_lock
);
1554 if (signal_pending(current
))
1555 return -ERESTARTSYS
;
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
;
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
);
1580 /******************************************************************************
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
);
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
);
1604 /* Priority check */
1607 case VIDIOCMCAPTURE
:
1610 struct cpia2_fh
*fh
= file
->private_data
;
1611 retval
= v4l2_prio_check(&cam
->prio
, &fh
->prio
);
1613 mutex_unlock(&cam
->busy_lock
);
1621 struct cpia2_fh
*fh
= file
->private_data
;
1622 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1623 mutex_unlock(&cam
->busy_lock
);
1633 case VIDIOCGCAP
: /* query capabilities */
1634 retval
= ioctl_cap_query(arg
, cam
);
1637 case VIDIOCGCHAN
: /* get video source - we are a camera, nothing else */
1638 retval
= ioctl_get_channel(arg
);
1640 case VIDIOCSCHAN
: /* set video source - we are a camera, nothing else */
1641 retval
= ioctl_set_channel(arg
);
1643 case VIDIOCGPICT
: /* image properties */
1644 memcpy(arg
, &cam
->vp
, sizeof(struct video_picture
));
1647 retval
= ioctl_set_image_prop(arg
, cam
);
1649 case VIDIOCGWIN
: /* get/set capture window */
1650 memcpy(arg
, &cam
->vw
, sizeof(struct video_window
));
1653 retval
= ioctl_set_window_size(arg
, cam
, file
->private_data
);
1655 case VIDIOCGMBUF
: /* mmap interface */
1656 retval
= ioctl_get_mbuf(arg
, cam
);
1658 case VIDIOCMCAPTURE
:
1659 retval
= ioctl_mcapture(arg
, cam
, file
->private_data
);
1662 retval
= ioctl_sync(arg
, cam
);
1664 /* pointless to implement overlay with this camera */
1672 /* tuner interface - we have none */
1680 /* audio interface - we have none */
1686 /* CPIA2 extension to Video4Linux API */
1687 case CPIA2_IOC_SET_GPIO
:
1688 retval
= ioctl_set_gpio(arg
, cam
);
1690 case VIDIOC_QUERYCAP
:
1691 retval
= ioctl_querycap(arg
,cam
);
1694 case VIDIOC_ENUMINPUT
:
1695 case VIDIOC_G_INPUT
:
1696 case VIDIOC_S_INPUT
:
1697 retval
= ioctl_input(ioctl_nr
, arg
,cam
);
1700 case VIDIOC_ENUM_FMT
:
1701 retval
= ioctl_enum_fmt(arg
,cam
);
1703 case VIDIOC_TRY_FMT
:
1704 retval
= ioctl_try_fmt(arg
,cam
);
1707 retval
= ioctl_get_fmt(arg
,cam
);
1710 retval
= ioctl_set_fmt(arg
,cam
,file
->private_data
);
1713 case VIDIOC_CROPCAP
:
1714 retval
= ioctl_cropcap(arg
,cam
);
1718 // TODO: I think cropping can be implemented - SJB
1722 case VIDIOC_QUERYCTRL
:
1723 retval
= ioctl_queryctrl(arg
,cam
);
1725 case VIDIOC_QUERYMENU
:
1726 retval
= ioctl_querymenu(arg
,cam
);
1729 retval
= ioctl_g_ctrl(arg
,cam
);
1732 retval
= ioctl_s_ctrl(arg
,cam
);
1735 case VIDIOC_G_JPEGCOMP
:
1736 retval
= ioctl_g_jpegcomp(arg
,cam
);
1738 case VIDIOC_S_JPEGCOMP
:
1739 retval
= ioctl_s_jpegcomp(arg
,cam
);
1742 case VIDIOC_G_PRIORITY
:
1744 struct cpia2_fh
*fh
= file
->private_data
;
1745 *(enum v4l2_priority
*)arg
= fh
->prio
;
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
&&
1755 fh
->prio
== V4L2_PRIORITY_RECORD
) {
1756 /* Can't drop record priority while streaming */
1758 } else if(prio
== V4L2_PRIORITY_RECORD
&&
1760 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
) {
1761 /* Only one program can record at a time */
1764 retval
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1769 case VIDIOC_REQBUFS
:
1770 retval
= ioctl_reqbufs(arg
,cam
);
1772 case VIDIOC_QUERYBUF
:
1773 retval
= ioctl_querybuf(arg
,cam
);
1776 retval
= ioctl_qbuf(arg
,cam
);
1779 retval
= ioctl_dqbuf(arg
,cam
,file
);
1781 case VIDIOC_STREAMON
:
1784 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1786 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1789 if(!cam
->streaming
) {
1790 retval
= cpia2_usb_stream_start(cam
,
1791 cam
->params
.camera_state
.stream_mode
);
1798 case VIDIOC_STREAMOFF
:
1801 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1803 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1806 if(cam
->streaming
) {
1807 retval
= cpia2_usb_stream_stop(cam
);
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
:
1834 case VIDIOC_G_TUNER
:
1835 case VIDIOC_S_TUNER
:
1836 case VIDIOC_G_FREQUENCY
:
1837 case VIDIOC_S_FREQUENCY
:
1839 case VIDIOC_OVERLAY
:
1848 retval
= -ENOIOCTLCMD
;
1852 mutex_unlock(&cam
->busy_lock
);
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 /******************************************************************************
1866 *****************************************************************************/
1867 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
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
) {
1879 retval
= cpia2_remap_buffer(cam
, area
);
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
)
1895 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1896 * Ioctl. Here, just do the window and picture stucts.
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;
1905 cam
->vw
.width
= cam
->params
.roi
.width
;
1906 cam
->vw
.height
= cam
->params
.roi
.height
;
1908 cam
->vw
.clipcount
= 0;
1910 cam
->frame_size
= buffer_size
;
1911 cam
->num_frames
= num_buffers
;
1914 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1915 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1918 cam
->params
.camera_state
.stream_mode
= alternate
;
1920 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1921 v4l2_prio_init(&cam
->prio
);
1926 * The v4l video device structure initialized for this device
1928 static struct file_operations fops_template
= {
1929 .owner
= THIS_MODULE
,
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
,
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
|
1947 .hardware
= VID_HARDWARE_CPIA2
,
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();
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
);
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
);
1990 LOG("/dev/video%d removed while open, "
1991 "deferring video_unregister_device\n",
1996 /******************************************************************************
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",
2013 buffer_size
+= PAGE_SIZE
-1;
2014 buffer_size
&= ~(PAGE_SIZE
-1);
2017 if(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",
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",
2042 if(video_nr
< -1 || video_nr
> 64) {
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
);
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
);