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@lxorguk.ukuu.org.uk>
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/videodev.h>
41 #include <linux/stringify.h>
42 #include <media/v4l2-ioctl.h>
47 static int video_nr
= -1;
48 module_param(video_nr
, int, 0);
49 MODULE_PARM_DESC(video_nr
,"video device to register (0=/dev/video0, etc)");
51 static int buffer_size
= 68*1024;
52 module_param(buffer_size
, int, 0);
53 MODULE_PARM_DESC(buffer_size
, "Size for each frame buffer in bytes (default 68k)");
55 static int num_buffers
= 3;
56 module_param(num_buffers
, int, 0);
57 MODULE_PARM_DESC(num_buffers
, "Number of frame buffers (1-"
58 __stringify(VIDEO_MAX_FRAME
) ", default 3)");
60 static int alternate
= DEFAULT_ALT
;
61 module_param(alternate
, int, 0);
62 MODULE_PARM_DESC(alternate
, "USB Alternate (" __stringify(USBIF_ISO_1
) "-"
63 __stringify(USBIF_ISO_6
) ", default "
64 __stringify(DEFAULT_ALT
) ")");
66 static int flicker_freq
= 60;
67 module_param(flicker_freq
, int, 0);
68 MODULE_PARM_DESC(flicker_freq
, "Flicker frequency (" __stringify(50) "or"
69 __stringify(60) ", default "
72 static int flicker_mode
= NEVER_FLICKER
;
73 module_param(flicker_mode
, int, 0);
74 MODULE_PARM_DESC(flicker_mode
,
75 "Flicker supression (" __stringify(NEVER_FLICKER
) "or"
76 __stringify(ANTI_FLICKER_ON
) ", default "
77 __stringify(NEVER_FLICKER
) ")");
79 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
80 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
81 MODULE_SUPPORTED_DEVICE("video");
82 MODULE_LICENSE("GPL");
84 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
86 struct control_menu_info
{
91 static struct control_menu_info framerate_controls
[] =
93 { CPIA2_VP_FRAMERATE_6_25
, "6.25 fps" },
94 { CPIA2_VP_FRAMERATE_7_5
, "7.5 fps" },
95 { CPIA2_VP_FRAMERATE_12_5
, "12.5 fps" },
96 { CPIA2_VP_FRAMERATE_15
, "15 fps" },
97 { CPIA2_VP_FRAMERATE_25
, "25 fps" },
98 { CPIA2_VP_FRAMERATE_30
, "30 fps" },
100 #define NUM_FRAMERATE_CONTROLS (ARRAY_SIZE(framerate_controls))
102 static struct control_menu_info flicker_controls
[] =
104 { NEVER_FLICKER
, "Off" },
105 { FLICKER_50
, "50 Hz" },
106 { FLICKER_60
, "60 Hz" },
108 #define NUM_FLICKER_CONTROLS (ARRAY_SIZE(flicker_controls))
110 static struct control_menu_info lights_controls
[] =
117 #define NUM_LIGHTS_CONTROLS (ARRAY_SIZE(lights_controls))
118 #define GPIO_LIGHTS_MASK 192
120 static struct v4l2_queryctrl controls
[] = {
122 .id
= V4L2_CID_BRIGHTNESS
,
123 .type
= V4L2_CTRL_TYPE_INTEGER
,
124 .name
= "Brightness",
128 .default_value
= DEFAULT_BRIGHTNESS
,
131 .id
= V4L2_CID_CONTRAST
,
132 .type
= V4L2_CTRL_TYPE_INTEGER
,
137 .default_value
= DEFAULT_CONTRAST
,
140 .id
= V4L2_CID_SATURATION
,
141 .type
= V4L2_CTRL_TYPE_INTEGER
,
142 .name
= "Saturation",
146 .default_value
= DEFAULT_SATURATION
,
149 .id
= V4L2_CID_HFLIP
,
150 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
151 .name
= "Mirror Horizontally",
158 .id
= V4L2_CID_VFLIP
,
159 .type
= V4L2_CTRL_TYPE_BOOLEAN
,
160 .name
= "Flip Vertically",
167 .id
= CPIA2_CID_TARGET_KB
,
168 .type
= V4L2_CTRL_TYPE_INTEGER
,
173 .default_value
= DEFAULT_TARGET_KB
,
176 .id
= CPIA2_CID_GPIO
,
177 .type
= V4L2_CTRL_TYPE_INTEGER
,
185 .id
= CPIA2_CID_FLICKER_MODE
,
186 .type
= V4L2_CTRL_TYPE_MENU
,
187 .name
= "Flicker Reduction",
189 .maximum
= NUM_FLICKER_CONTROLS
-1,
194 .id
= CPIA2_CID_FRAMERATE
,
195 .type
= V4L2_CTRL_TYPE_MENU
,
198 .maximum
= NUM_FRAMERATE_CONTROLS
-1,
200 .default_value
= NUM_FRAMERATE_CONTROLS
-1,
203 .id
= CPIA2_CID_USB_ALT
,
204 .type
= V4L2_CTRL_TYPE_INTEGER
,
205 .name
= "USB Alternate",
206 .minimum
= USBIF_ISO_1
,
207 .maximum
= USBIF_ISO_6
,
209 .default_value
= DEFAULT_ALT
,
212 .id
= CPIA2_CID_LIGHTS
,
213 .type
= V4L2_CTRL_TYPE_MENU
,
216 .maximum
= NUM_LIGHTS_CONTROLS
-1,
221 .id
= CPIA2_CID_RESET_CAMERA
,
222 .type
= V4L2_CTRL_TYPE_BUTTON
,
223 .name
= "Reset Camera",
230 #define NUM_CONTROLS (ARRAY_SIZE(controls))
233 /******************************************************************************
237 *****************************************************************************/
238 static int cpia2_open(struct file
*file
)
240 struct camera_data
*cam
= video_drvdata(file
);
244 ERR("Internal error, camera_data not found!\n");
248 if(mutex_lock_interruptible(&cam
->busy_lock
))
256 if (cam
->open_count
> 0) {
260 if (cpia2_allocate_buffers(cam
)) {
265 /* reset the camera */
266 if (cpia2_reset_camera(cam
) < 0) {
276 struct cpia2_fh
*fh
= kmalloc(sizeof(*fh
),GFP_KERNEL
);
281 file
->private_data
= fh
;
282 fh
->prio
= V4L2_PRIORITY_UNSET
;
283 v4l2_prio_open(&cam
->prio
, &fh
->prio
);
289 cpia2_dbg_dump_registers(cam
);
292 mutex_unlock(&cam
->busy_lock
);
296 /******************************************************************************
300 *****************************************************************************/
301 static int cpia2_close(struct file
*file
)
303 struct video_device
*dev
= video_devdata(file
);
304 struct camera_data
*cam
= video_get_drvdata(dev
);
305 struct cpia2_fh
*fh
= file
->private_data
;
307 mutex_lock(&cam
->busy_lock
);
310 (cam
->open_count
== 1
311 || fh
->prio
== V4L2_PRIORITY_RECORD
313 cpia2_usb_stream_stop(cam
);
315 if(cam
->open_count
== 1) {
316 /* save camera state for later open */
317 cpia2_save_camera_state(cam
);
319 cpia2_set_low_power(cam
);
320 cpia2_free_buffers(cam
);
327 v4l2_prio_close(&cam
->prio
,&fh
->prio
);
328 file
->private_data
= NULL
;
332 if (--cam
->open_count
== 0) {
333 cpia2_free_buffers(cam
);
335 video_unregister_device(dev
);
336 mutex_unlock(&cam
->busy_lock
);
342 mutex_unlock(&cam
->busy_lock
);
347 /******************************************************************************
351 *****************************************************************************/
352 static ssize_t
cpia2_v4l_read(struct file
*file
, char __user
*buf
, size_t count
,
355 struct camera_data
*cam
= video_drvdata(file
);
356 int noblock
= file
->f_flags
&O_NONBLOCK
;
358 struct cpia2_fh
*fh
= file
->private_data
;
364 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
368 return cpia2_read(cam
, buf
, count
, noblock
);
372 /******************************************************************************
376 *****************************************************************************/
377 static unsigned int cpia2_v4l_poll(struct file
*filp
, struct poll_table_struct
*wait
)
379 struct camera_data
*cam
= video_drvdata(filp
);
380 struct cpia2_fh
*fh
= filp
->private_data
;
386 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
390 return cpia2_poll(cam
, filp
, wait
);
394 /******************************************************************************
398 *****************************************************************************/
399 static int ioctl_cap_query(void *arg
, struct camera_data
*cam
)
401 struct video_capability
*vc
;
405 if (cam
->params
.pnp_id
.product
== 0x151)
406 strcpy(vc
->name
, "QX5 Microscope");
408 strcpy(vc
->name
, "CPiA2 Camera");
410 vc
->type
= VID_TYPE_CAPTURE
| VID_TYPE_MJPEG_ENCODER
;
413 vc
->minwidth
= 176; /* VIDEOSIZE_QCIF */
415 switch (cam
->params
.version
.sensor_flags
) {
416 case CPIA2_VP_SENSOR_FLAGS_500
:
417 vc
->maxwidth
= STV_IMAGE_VGA_COLS
;
418 vc
->maxheight
= STV_IMAGE_VGA_ROWS
;
420 case CPIA2_VP_SENSOR_FLAGS_410
:
421 vc
->maxwidth
= STV_IMAGE_CIF_COLS
;
422 vc
->maxheight
= STV_IMAGE_CIF_ROWS
;
431 /******************************************************************************
435 *****************************************************************************/
436 static int ioctl_get_channel(void *arg
)
439 struct video_channel
*v
;
446 strcpy(v
->name
, "Camera");
449 v
->type
= VIDEO_TYPE_CAMERA
;
455 /******************************************************************************
459 *****************************************************************************/
460 static int ioctl_set_channel(void *arg
)
462 struct video_channel
*v
;
466 if (retval
== 0 && v
->channel
!= 0)
472 /******************************************************************************
474 * ioctl_set_image_prop
476 *****************************************************************************/
477 static int ioctl_set_image_prop(void *arg
, struct camera_data
*cam
)
479 struct video_picture
*vp
;
483 /* brightness, color, contrast need no check 0-65535 */
484 memcpy(&cam
->vp
, vp
, sizeof(*vp
));
486 /* update cam->params.colorParams */
487 cam
->params
.color_params
.brightness
= vp
->brightness
/ 256;
488 cam
->params
.color_params
.saturation
= vp
->colour
/ 256;
489 cam
->params
.color_params
.contrast
= vp
->contrast
/ 256;
491 DBG("Requested params: bright 0x%X, sat 0x%X, contrast 0x%X\n",
492 cam
->params
.color_params
.brightness
,
493 cam
->params
.color_params
.saturation
,
494 cam
->params
.color_params
.contrast
);
496 cpia2_set_color_params(cam
);
501 static int sync(struct camera_data
*cam
, int frame_nr
)
503 struct framebuf
*frame
= &cam
->buffers
[frame_nr
];
506 if (frame
->status
== FRAME_READY
)
509 if (!cam
->streaming
) {
510 frame
->status
= FRAME_READY
;
515 mutex_unlock(&cam
->busy_lock
);
516 wait_event_interruptible(cam
->wq_stream
,
518 frame
->status
== FRAME_READY
);
519 mutex_lock(&cam
->busy_lock
);
520 if (signal_pending(current
))
527 /******************************************************************************
529 * ioctl_set_window_size
531 *****************************************************************************/
532 static int ioctl_set_window_size(void *arg
, struct camera_data
*cam
,
535 /* copy_from_user, check validity, copy to internal structure */
536 struct video_window
*vw
;
540 if (vw
->clipcount
!= 0) /* clipping not supported */
543 if (vw
->clips
!= NULL
) /* clipping not supported */
546 /* Ensure that only this process can change the format. */
547 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
551 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
553 /* Be sure to supply the Huffman tables, this isn't MJPEG */
554 cam
->params
.compression
.inhibit_htables
= 0;
556 /* we set the video window to something smaller or equal to what
557 * is requested by the user???
559 DBG("Requested width = %d, height = %d\n", vw
->width
, vw
->height
);
560 if (vw
->width
!= cam
->vw
.width
|| vw
->height
!= cam
->vw
.height
) {
561 cam
->vw
.width
= vw
->width
;
562 cam
->vw
.height
= vw
->height
;
563 cam
->params
.roi
.width
= vw
->width
;
564 cam
->params
.roi
.height
= vw
->height
;
565 cpia2_set_format(cam
);
568 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
569 if (cam
->buffers
[frame
].status
== FRAME_READING
)
570 if ((err
= sync(cam
, frame
)) < 0)
573 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
579 /******************************************************************************
583 *****************************************************************************/
584 static int ioctl_get_mbuf(void *arg
, struct camera_data
*cam
)
586 struct video_mbuf
*vm
;
590 memset(vm
, 0, sizeof(*vm
));
591 vm
->size
= cam
->frame_size
*cam
->num_frames
;
592 vm
->frames
= cam
->num_frames
;
593 for (i
= 0; i
< cam
->num_frames
; i
++)
594 vm
->offsets
[i
] = cam
->frame_size
* i
;
599 /******************************************************************************
603 *****************************************************************************/
604 static int ioctl_mcapture(void *arg
, struct camera_data
*cam
,
607 struct video_mmap
*vm
;
611 if (vm
->frame
< 0 || vm
->frame
>= cam
->num_frames
)
615 video_size
= cpia2_match_video_size(vm
->width
, vm
->height
);
616 if (cam
->video_size
< 0) {
620 /* Ensure that only this process can change the format. */
621 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
625 if (video_size
!= cam
->video_size
) {
626 cam
->video_size
= video_size
;
627 cam
->params
.roi
.width
= vm
->width
;
628 cam
->params
.roi
.height
= vm
->height
;
629 cpia2_set_format(cam
);
632 if (cam
->buffers
[vm
->frame
].status
== FRAME_READING
)
633 if ((err
=sync(cam
, vm
->frame
)) < 0)
636 cam
->buffers
[vm
->frame
].status
= FRAME_EMPTY
;
638 return cpia2_usb_stream_start(cam
,cam
->params
.camera_state
.stream_mode
);
641 /******************************************************************************
645 *****************************************************************************/
646 static int ioctl_sync(void *arg
, struct camera_data
*cam
)
652 if (frame
< 0 || frame
>= cam
->num_frames
)
655 return sync(cam
, frame
);
659 /******************************************************************************
663 *****************************************************************************/
665 static int ioctl_set_gpio(void *arg
, struct camera_data
*cam
)
669 gpio_val
= *(__u32
*) arg
;
671 if (gpio_val
&~ 0xFFU
)
674 return cpia2_set_gpio(cam
, (unsigned char)gpio_val
);
677 /******************************************************************************
681 * V4L2 device capabilities
683 *****************************************************************************/
685 static int ioctl_querycap(void *arg
, struct camera_data
*cam
)
687 struct v4l2_capability
*vc
= arg
;
689 memset(vc
, 0, sizeof(*vc
));
690 strcpy(vc
->driver
, "cpia2");
692 if (cam
->params
.pnp_id
.product
== 0x151)
693 strcpy(vc
->card
, "QX5 Microscope");
695 strcpy(vc
->card
, "CPiA2 Camera");
696 switch (cam
->params
.pnp_id
.device_type
) {
698 strcat(vc
->card
, " (672/");
701 strcat(vc
->card
, " (676/");
704 strcat(vc
->card
, " (???/");
707 switch (cam
->params
.version
.sensor_flags
) {
708 case CPIA2_VP_SENSOR_FLAGS_404
:
709 strcat(vc
->card
, "404)");
711 case CPIA2_VP_SENSOR_FLAGS_407
:
712 strcat(vc
->card
, "407)");
714 case CPIA2_VP_SENSOR_FLAGS_409
:
715 strcat(vc
->card
, "409)");
717 case CPIA2_VP_SENSOR_FLAGS_410
:
718 strcat(vc
->card
, "410)");
720 case CPIA2_VP_SENSOR_FLAGS_500
:
721 strcat(vc
->card
, "500)");
724 strcat(vc
->card
, "???)");
728 if (usb_make_path(cam
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
)) <0)
729 memset(vc
->bus_info
,0, sizeof(vc
->bus_info
));
731 vc
->version
= KERNEL_VERSION(CPIA2_MAJ_VER
, CPIA2_MIN_VER
,
734 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
741 /******************************************************************************
745 * V4L2 input get/set/enumerate
747 *****************************************************************************/
749 static int ioctl_input(unsigned int ioclt_nr
,void *arg
,struct camera_data
*cam
)
751 struct v4l2_input
*i
= arg
;
753 if(ioclt_nr
!= VIDIOC_G_INPUT
) {
758 memset(i
, 0, sizeof(*i
));
759 strcpy(i
->name
, "Camera");
760 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
765 /******************************************************************************
769 * V4L2 format enumerate
771 *****************************************************************************/
773 static int ioctl_enum_fmt(void *arg
,struct camera_data
*cam
)
775 struct v4l2_fmtdesc
*f
= arg
;
776 int index
= f
->index
;
778 if (index
< 0 || index
> 1)
781 memset(f
, 0, sizeof(*f
));
783 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
784 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
787 strcpy(f
->description
, "MJPEG");
788 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
791 strcpy(f
->description
, "JPEG");
792 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
801 /******************************************************************************
807 *****************************************************************************/
809 static int ioctl_try_fmt(void *arg
,struct camera_data
*cam
)
811 struct v4l2_format
*f
= arg
;
813 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
816 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
817 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
820 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
821 f
->fmt
.pix
.bytesperline
= 0;
822 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
823 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
826 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
828 f
->fmt
.pix
.width
= 640;
829 f
->fmt
.pix
.height
= 480;
832 f
->fmt
.pix
.width
= 352;
833 f
->fmt
.pix
.height
= 288;
836 f
->fmt
.pix
.width
= 320;
837 f
->fmt
.pix
.height
= 240;
839 case VIDEOSIZE_288_216
:
840 f
->fmt
.pix
.width
= 288;
841 f
->fmt
.pix
.height
= 216;
843 case VIDEOSIZE_256_192
:
844 f
->fmt
.pix
.width
= 256;
845 f
->fmt
.pix
.height
= 192;
847 case VIDEOSIZE_224_168
:
848 f
->fmt
.pix
.width
= 224;
849 f
->fmt
.pix
.height
= 168;
851 case VIDEOSIZE_192_144
:
852 f
->fmt
.pix
.width
= 192;
853 f
->fmt
.pix
.height
= 144;
857 f
->fmt
.pix
.width
= 176;
858 f
->fmt
.pix
.height
= 144;
865 /******************************************************************************
871 *****************************************************************************/
873 static int ioctl_set_fmt(void *arg
,struct camera_data
*cam
, struct cpia2_fh
*fh
)
875 struct v4l2_format
*f
= arg
;
878 err
= ioctl_try_fmt(arg
, cam
);
882 /* Ensure that only this process can change the format. */
883 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
888 cam
->pixelformat
= f
->fmt
.pix
.pixelformat
;
890 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
891 * the missing Huffman table properly. */
892 cam
->params
.compression
.inhibit_htables
= 0;
893 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
895 /* we set the video window to something smaller or equal to what
896 * is requested by the user???
898 DBG("Requested width = %d, height = %d\n",
899 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
900 if (f
->fmt
.pix
.width
!= cam
->vw
.width
||
901 f
->fmt
.pix
.height
!= cam
->vw
.height
) {
902 cam
->vw
.width
= f
->fmt
.pix
.width
;
903 cam
->vw
.height
= f
->fmt
.pix
.height
;
904 cam
->params
.roi
.width
= f
->fmt
.pix
.width
;
905 cam
->params
.roi
.height
= f
->fmt
.pix
.height
;
906 cpia2_set_format(cam
);
909 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
910 if (cam
->buffers
[frame
].status
== FRAME_READING
)
911 if ((err
= sync(cam
, frame
)) < 0)
914 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
920 /******************************************************************************
926 *****************************************************************************/
928 static int ioctl_get_fmt(void *arg
,struct camera_data
*cam
)
930 struct v4l2_format
*f
= arg
;
932 if (f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
935 f
->fmt
.pix
.width
= cam
->vw
.width
;
936 f
->fmt
.pix
.height
= cam
->vw
.height
;
937 f
->fmt
.pix
.pixelformat
= cam
->pixelformat
;
938 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
939 f
->fmt
.pix
.bytesperline
= 0;
940 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
941 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
947 /******************************************************************************
951 * V4L2 query cropping capabilities
952 * NOTE: cropping is currently disabled
954 *****************************************************************************/
956 static int ioctl_cropcap(void *arg
,struct camera_data
*cam
)
958 struct v4l2_cropcap
*c
= arg
;
960 if (c
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
965 c
->bounds
.width
= cam
->vw
.width
;
966 c
->bounds
.height
= cam
->vw
.height
;
969 c
->defrect
.width
= cam
->vw
.width
;
970 c
->defrect
.height
= cam
->vw
.height
;
971 c
->pixelaspect
.numerator
= 1;
972 c
->pixelaspect
.denominator
= 1;
977 /******************************************************************************
981 * V4L2 query possible control variables
983 *****************************************************************************/
985 static int ioctl_queryctrl(void *arg
,struct camera_data
*cam
)
987 struct v4l2_queryctrl
*c
= arg
;
990 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
991 if(c
->id
== controls
[i
].id
) {
992 memcpy(c
, controls
+i
, sizeof(*c
));
997 if(i
== NUM_CONTROLS
)
1000 /* Some devices have additional limitations */
1002 case V4L2_CID_BRIGHTNESS
:
1004 * Don't let the register be set to zero - bug in VP4
1005 * flash of full brightness
1007 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1010 case V4L2_CID_VFLIP
:
1012 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
1013 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1015 case CPIA2_CID_FRAMERATE
:
1016 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
1017 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
1019 for(i
=0; i
<c
->maximum
; ++i
) {
1020 if(framerate_controls
[i
].value
==
1021 CPIA2_VP_FRAMERATE_15
) {
1023 c
->default_value
= i
;
1028 case CPIA2_CID_FLICKER_MODE
:
1029 // Flicker control only valid for 672.
1030 if(cam
->params
.pnp_id
.device_type
!= DEVICE_STV_672
)
1031 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1033 case CPIA2_CID_LIGHTS
:
1034 // Light control only valid for the QX5 Microscope.
1035 if(cam
->params
.pnp_id
.product
!= 0x151)
1036 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
1045 /******************************************************************************
1049 * V4L2 query possible control variables
1051 *****************************************************************************/
1053 static int ioctl_querymenu(void *arg
,struct camera_data
*cam
)
1055 struct v4l2_querymenu
*m
= arg
;
1057 memset(m
->name
, 0, sizeof(m
->name
));
1061 case CPIA2_CID_FLICKER_MODE
:
1062 if (m
->index
>= NUM_FLICKER_CONTROLS
)
1065 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
1067 case CPIA2_CID_FRAMERATE
:
1069 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1070 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
1071 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
1074 for(i
=0; i
<maximum
; ++i
) {
1075 if(framerate_controls
[i
].value
==
1076 CPIA2_VP_FRAMERATE_15
)
1080 if (m
->index
> maximum
)
1083 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
1086 case CPIA2_CID_LIGHTS
:
1087 if (m
->index
>= NUM_LIGHTS_CONTROLS
)
1090 strcpy(m
->name
, lights_controls
[m
->index
].name
);
1099 /******************************************************************************
1103 * V4L2 get the value of a control variable
1105 *****************************************************************************/
1107 static int ioctl_g_ctrl(void *arg
,struct camera_data
*cam
)
1109 struct v4l2_control
*c
= arg
;
1112 case V4L2_CID_BRIGHTNESS
:
1113 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
1115 c
->value
= cam
->params
.color_params
.brightness
;
1117 case V4L2_CID_CONTRAST
:
1118 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
1120 c
->value
= cam
->params
.color_params
.contrast
;
1122 case V4L2_CID_SATURATION
:
1123 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
1125 c
->value
= cam
->params
.color_params
.saturation
;
1127 case V4L2_CID_HFLIP
:
1128 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1130 c
->value
= (cam
->params
.vp_params
.user_effects
&
1131 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
1133 case V4L2_CID_VFLIP
:
1134 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
1136 c
->value
= (cam
->params
.vp_params
.user_effects
&
1137 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
1139 case CPIA2_CID_TARGET_KB
:
1140 c
->value
= cam
->params
.vc_params
.target_kb
;
1142 case CPIA2_CID_GPIO
:
1143 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1145 c
->value
= cam
->params
.vp_params
.gpio_data
;
1147 case CPIA2_CID_FLICKER_MODE
:
1150 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
1152 if(cam
->params
.flicker_control
.cam_register
&
1153 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
1154 mode
= NEVER_FLICKER
;
1156 if(cam
->params
.flicker_control
.cam_register
&
1157 CPIA2_VP_FLICKER_MODES_50HZ
) {
1163 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
1164 if(flicker_controls
[i
].value
== mode
) {
1169 if(i
== NUM_FLICKER_CONTROLS
)
1173 case CPIA2_CID_FRAMERATE
:
1175 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
1177 for(i
=0; i
<= maximum
; i
++) {
1178 if(cam
->params
.vp_params
.frame_rate
==
1179 framerate_controls
[i
].value
)
1187 case CPIA2_CID_USB_ALT
:
1188 c
->value
= cam
->params
.camera_state
.stream_mode
;
1190 case CPIA2_CID_LIGHTS
:
1193 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
1195 for(i
=0; i
<NUM_LIGHTS_CONTROLS
; i
++) {
1196 if((cam
->params
.vp_params
.gpio_data
&GPIO_LIGHTS_MASK
) ==
1197 lights_controls
[i
].value
) {
1201 if(i
== NUM_LIGHTS_CONTROLS
)
1206 case CPIA2_CID_RESET_CAMERA
:
1212 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
1217 /******************************************************************************
1221 * V4L2 set the value of a control variable
1223 *****************************************************************************/
1225 static int ioctl_s_ctrl(void *arg
,struct camera_data
*cam
)
1227 struct v4l2_control
*c
= arg
;
1231 DBG("Set control id:%d, value:%d\n", c
->id
, c
->value
);
1233 /* Check that the value is in range */
1234 for(i
=0; i
<NUM_CONTROLS
; i
++) {
1235 if(c
->id
== controls
[i
].id
) {
1236 if(c
->value
< controls
[i
].minimum
||
1237 c
->value
> controls
[i
].maximum
) {
1243 if(i
== NUM_CONTROLS
)
1247 case V4L2_CID_BRIGHTNESS
:
1248 cpia2_set_brightness(cam
, c
->value
);
1250 case V4L2_CID_CONTRAST
:
1251 cpia2_set_contrast(cam
, c
->value
);
1253 case V4L2_CID_SATURATION
:
1254 cpia2_set_saturation(cam
, c
->value
);
1256 case V4L2_CID_HFLIP
:
1257 cpia2_set_property_mirror(cam
, c
->value
);
1259 case V4L2_CID_VFLIP
:
1260 cpia2_set_property_flip(cam
, c
->value
);
1262 case CPIA2_CID_TARGET_KB
:
1263 retval
= cpia2_set_target_kb(cam
, c
->value
);
1265 case CPIA2_CID_GPIO
:
1266 retval
= cpia2_set_gpio(cam
, c
->value
);
1268 case CPIA2_CID_FLICKER_MODE
:
1269 retval
= cpia2_set_flicker_mode(cam
,
1270 flicker_controls
[c
->value
].value
);
1272 case CPIA2_CID_FRAMERATE
:
1273 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1275 case CPIA2_CID_USB_ALT
:
1276 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1278 case CPIA2_CID_LIGHTS
:
1279 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1281 case CPIA2_CID_RESET_CAMERA
:
1282 cpia2_usb_stream_pause(cam
);
1283 cpia2_reset_camera(cam
);
1284 cpia2_usb_stream_resume(cam
);
1293 /******************************************************************************
1297 * V4L2 get the JPEG compression parameters
1299 *****************************************************************************/
1301 static int ioctl_g_jpegcomp(void *arg
,struct camera_data
*cam
)
1303 struct v4l2_jpegcompression
*parms
= arg
;
1305 memset(parms
, 0, sizeof(*parms
));
1307 parms
->quality
= 80; // TODO: Can this be made meaningful?
1309 parms
->jpeg_markers
= V4L2_JPEG_MARKER_DQT
| V4L2_JPEG_MARKER_DRI
;
1310 if(!cam
->params
.compression
.inhibit_htables
) {
1311 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_DHT
;
1314 parms
->APPn
= cam
->APPn
;
1315 parms
->APP_len
= cam
->APP_len
;
1316 if(cam
->APP_len
> 0) {
1317 memcpy(parms
->APP_data
, cam
->APP_data
, cam
->APP_len
);
1318 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_APP
;
1321 parms
->COM_len
= cam
->COM_len
;
1322 if(cam
->COM_len
> 0) {
1323 memcpy(parms
->COM_data
, cam
->COM_data
, cam
->COM_len
);
1324 parms
->jpeg_markers
|= JPEG_MARKER_COM
;
1327 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1328 parms
->APP_len
, parms
->COM_len
);
1333 /******************************************************************************
1337 * V4L2 set the JPEG compression parameters
1338 * NOTE: quality and some jpeg_markers are ignored.
1340 *****************************************************************************/
1342 static int ioctl_s_jpegcomp(void *arg
,struct camera_data
*cam
)
1344 struct v4l2_jpegcompression
*parms
= arg
;
1346 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1347 parms
->APP_len
, parms
->COM_len
);
1349 cam
->params
.compression
.inhibit_htables
=
1350 !(parms
->jpeg_markers
& V4L2_JPEG_MARKER_DHT
);
1352 if(parms
->APP_len
!= 0) {
1353 if(parms
->APP_len
> 0 &&
1354 parms
->APP_len
<= sizeof(cam
->APP_data
) &&
1355 parms
->APPn
>= 0 && parms
->APPn
<= 15) {
1356 cam
->APPn
= parms
->APPn
;
1357 cam
->APP_len
= parms
->APP_len
;
1358 memcpy(cam
->APP_data
, parms
->APP_data
, parms
->APP_len
);
1360 LOG("Bad APPn Params n=%d len=%d\n",
1361 parms
->APPn
, parms
->APP_len
);
1368 if(parms
->COM_len
!= 0) {
1369 if(parms
->COM_len
> 0 &&
1370 parms
->COM_len
<= sizeof(cam
->COM_data
)) {
1371 cam
->COM_len
= parms
->COM_len
;
1372 memcpy(cam
->COM_data
, parms
->COM_data
, parms
->COM_len
);
1374 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1382 /******************************************************************************
1386 * V4L2 Initiate memory mapping.
1387 * NOTE: The user's request is ignored. For now the buffers are fixed.
1389 *****************************************************************************/
1391 static int ioctl_reqbufs(void *arg
,struct camera_data
*cam
)
1393 struct v4l2_requestbuffers
*req
= arg
;
1395 if(req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1396 req
->memory
!= V4L2_MEMORY_MMAP
)
1399 DBG("REQBUFS requested:%d returning:%d\n", req
->count
, cam
->num_frames
);
1400 req
->count
= cam
->num_frames
;
1401 memset(&req
->reserved
, 0, sizeof(req
->reserved
));
1406 /******************************************************************************
1410 * V4L2 Query memory buffer status.
1412 *****************************************************************************/
1414 static int ioctl_querybuf(void *arg
,struct camera_data
*cam
)
1416 struct v4l2_buffer
*buf
= arg
;
1418 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1419 buf
->index
> cam
->num_frames
)
1422 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1423 buf
->length
= cam
->frame_size
;
1425 buf
->memory
= V4L2_MEMORY_MMAP
;
1428 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1432 switch (cam
->buffers
[buf
->index
].status
) {
1437 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
1440 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1441 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1442 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1443 buf
->flags
= V4L2_BUF_FLAG_DONE
;
1447 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1448 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1454 /******************************************************************************
1458 * V4L2 User is freeing buffer
1460 *****************************************************************************/
1462 static int ioctl_qbuf(void *arg
,struct camera_data
*cam
)
1464 struct v4l2_buffer
*buf
= arg
;
1466 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1467 buf
->memory
!= V4L2_MEMORY_MMAP
||
1468 buf
->index
> cam
->num_frames
)
1471 DBG("QBUF #%d\n", buf
->index
);
1473 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1474 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
1479 /******************************************************************************
1481 * find_earliest_filled_buffer
1483 * Helper for ioctl_dqbuf. Find the next ready buffer.
1485 *****************************************************************************/
1487 static int find_earliest_filled_buffer(struct camera_data
*cam
)
1491 for (i
=0; i
<cam
->num_frames
; i
++) {
1492 if(cam
->buffers
[i
].status
== FRAME_READY
) {
1496 /* find which buffer is earlier */
1497 struct timeval
*tv1
, *tv2
;
1498 tv1
= &cam
->buffers
[i
].timestamp
;
1499 tv2
= &cam
->buffers
[found
].timestamp
;
1500 if(tv1
->tv_sec
< tv2
->tv_sec
||
1501 (tv1
->tv_sec
== tv2
->tv_sec
&&
1502 tv1
->tv_usec
< tv2
->tv_usec
))
1510 /******************************************************************************
1514 * V4L2 User is asking for a filled buffer.
1516 *****************************************************************************/
1518 static int ioctl_dqbuf(void *arg
,struct camera_data
*cam
, struct file
*file
)
1520 struct v4l2_buffer
*buf
= arg
;
1523 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1524 buf
->memory
!= V4L2_MEMORY_MMAP
)
1527 frame
= find_earliest_filled_buffer(cam
);
1529 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
1533 /* Wait for a frame to become available */
1534 struct framebuf
*cb
=cam
->curbuff
;
1535 mutex_unlock(&cam
->busy_lock
);
1536 wait_event_interruptible(cam
->wq_stream
,
1538 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1539 mutex_lock(&cam
->busy_lock
);
1540 if (signal_pending(current
))
1541 return -ERESTARTSYS
;
1549 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1550 buf
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_DONE
;
1551 buf
->field
= V4L2_FIELD_NONE
;
1552 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1553 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1554 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1555 buf
->length
= cam
->frame_size
;
1558 memset(&buf
->timecode
, 0, sizeof(buf
->timecode
));
1560 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf
->index
,
1561 cam
->buffers
[buf
->index
].status
, buf
->sequence
, buf
->bytesused
);
1566 /******************************************************************************
1570 *****************************************************************************/
1571 static long cpia2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
1573 struct camera_data
*cam
= video_drvdata(file
);
1579 /* make this _really_ smp-safe */
1580 if (mutex_lock_interruptible(&cam
->busy_lock
))
1581 return -ERESTARTSYS
;
1583 if (!cam
->present
) {
1584 mutex_unlock(&cam
->busy_lock
);
1588 /* Priority check */
1591 case VIDIOCMCAPTURE
:
1594 struct cpia2_fh
*fh
= file
->private_data
;
1595 retval
= v4l2_prio_check(&cam
->prio
, &fh
->prio
);
1597 mutex_unlock(&cam
->busy_lock
);
1605 struct cpia2_fh
*fh
= file
->private_data
;
1606 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1607 mutex_unlock(&cam
->busy_lock
);
1617 case VIDIOCGCAP
: /* query capabilities */
1618 retval
= ioctl_cap_query(arg
, cam
);
1621 case VIDIOCGCHAN
: /* get video source - we are a camera, nothing else */
1622 retval
= ioctl_get_channel(arg
);
1624 case VIDIOCSCHAN
: /* set video source - we are a camera, nothing else */
1625 retval
= ioctl_set_channel(arg
);
1627 case VIDIOCGPICT
: /* image properties */
1628 memcpy(arg
, &cam
->vp
, sizeof(struct video_picture
));
1631 retval
= ioctl_set_image_prop(arg
, cam
);
1633 case VIDIOCGWIN
: /* get/set capture window */
1634 memcpy(arg
, &cam
->vw
, sizeof(struct video_window
));
1637 retval
= ioctl_set_window_size(arg
, cam
, file
->private_data
);
1639 case VIDIOCGMBUF
: /* mmap interface */
1640 retval
= ioctl_get_mbuf(arg
, cam
);
1642 case VIDIOCMCAPTURE
:
1643 retval
= ioctl_mcapture(arg
, cam
, file
->private_data
);
1646 retval
= ioctl_sync(arg
, cam
);
1648 /* pointless to implement overlay with this camera */
1656 /* tuner interface - we have none */
1664 /* audio interface - we have none */
1670 /* CPIA2 extension to Video4Linux API */
1671 case CPIA2_IOC_SET_GPIO
:
1672 retval
= ioctl_set_gpio(arg
, cam
);
1674 case VIDIOC_QUERYCAP
:
1675 retval
= ioctl_querycap(arg
,cam
);
1678 case VIDIOC_ENUMINPUT
:
1679 case VIDIOC_G_INPUT
:
1680 case VIDIOC_S_INPUT
:
1681 retval
= ioctl_input(cmd
, arg
, cam
);
1684 case VIDIOC_ENUM_FMT
:
1685 retval
= ioctl_enum_fmt(arg
,cam
);
1687 case VIDIOC_TRY_FMT
:
1688 retval
= ioctl_try_fmt(arg
,cam
);
1691 retval
= ioctl_get_fmt(arg
,cam
);
1694 retval
= ioctl_set_fmt(arg
,cam
,file
->private_data
);
1697 case VIDIOC_CROPCAP
:
1698 retval
= ioctl_cropcap(arg
,cam
);
1702 // TODO: I think cropping can be implemented - SJB
1706 case VIDIOC_QUERYCTRL
:
1707 retval
= ioctl_queryctrl(arg
,cam
);
1709 case VIDIOC_QUERYMENU
:
1710 retval
= ioctl_querymenu(arg
,cam
);
1713 retval
= ioctl_g_ctrl(arg
,cam
);
1716 retval
= ioctl_s_ctrl(arg
,cam
);
1719 case VIDIOC_G_JPEGCOMP
:
1720 retval
= ioctl_g_jpegcomp(arg
,cam
);
1722 case VIDIOC_S_JPEGCOMP
:
1723 retval
= ioctl_s_jpegcomp(arg
,cam
);
1726 case VIDIOC_G_PRIORITY
:
1728 struct cpia2_fh
*fh
= file
->private_data
;
1729 *(enum v4l2_priority
*)arg
= fh
->prio
;
1732 case VIDIOC_S_PRIORITY
:
1734 struct cpia2_fh
*fh
= file
->private_data
;
1735 enum v4l2_priority prio
;
1736 prio
= *(enum v4l2_priority
*)arg
;
1737 if(cam
->streaming
&&
1739 fh
->prio
== V4L2_PRIORITY_RECORD
) {
1740 /* Can't drop record priority while streaming */
1742 } else if(prio
== V4L2_PRIORITY_RECORD
&&
1744 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
) {
1745 /* Only one program can record at a time */
1748 retval
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1753 case VIDIOC_REQBUFS
:
1754 retval
= ioctl_reqbufs(arg
,cam
);
1756 case VIDIOC_QUERYBUF
:
1757 retval
= ioctl_querybuf(arg
,cam
);
1760 retval
= ioctl_qbuf(arg
,cam
);
1763 retval
= ioctl_dqbuf(arg
,cam
,file
);
1765 case VIDIOC_STREAMON
:
1768 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1770 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1773 if(!cam
->streaming
) {
1774 retval
= cpia2_usb_stream_start(cam
,
1775 cam
->params
.camera_state
.stream_mode
);
1782 case VIDIOC_STREAMOFF
:
1785 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1787 if(!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1790 if(cam
->streaming
) {
1791 retval
= cpia2_usb_stream_stop(cam
);
1799 case VIDIOC_ENUMOUTPUT
:
1800 case VIDIOC_G_OUTPUT
:
1801 case VIDIOC_S_OUTPUT
:
1802 case VIDIOC_G_MODULATOR
:
1803 case VIDIOC_S_MODULATOR
:
1805 case VIDIOC_ENUMAUDIO
:
1806 case VIDIOC_G_AUDIO
:
1807 case VIDIOC_S_AUDIO
:
1809 case VIDIOC_ENUMAUDOUT
:
1810 case VIDIOC_G_AUDOUT
:
1811 case VIDIOC_S_AUDOUT
:
1813 case VIDIOC_ENUMSTD
:
1814 case VIDIOC_QUERYSTD
:
1818 case VIDIOC_G_TUNER
:
1819 case VIDIOC_S_TUNER
:
1820 case VIDIOC_G_FREQUENCY
:
1821 case VIDIOC_S_FREQUENCY
:
1823 case VIDIOC_OVERLAY
:
1832 retval
= -ENOIOCTLCMD
;
1836 mutex_unlock(&cam
->busy_lock
);
1840 static long cpia2_ioctl(struct file
*file
,
1841 unsigned int cmd
, unsigned long arg
)
1843 return video_usercopy(file
, cmd
, arg
, cpia2_do_ioctl
);
1846 /******************************************************************************
1850 *****************************************************************************/
1851 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
1853 struct camera_data
*cam
= video_drvdata(file
);
1856 /* Priority check */
1857 struct cpia2_fh
*fh
= file
->private_data
;
1858 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1862 retval
= cpia2_remap_buffer(cam
, area
);
1869 /******************************************************************************
1871 * reset_camera_struct_v4l
1873 * Sets all values to the defaults
1874 *****************************************************************************/
1875 static void reset_camera_struct_v4l(struct camera_data
*cam
)
1878 * Fill in the v4l structures. video_cap is filled in inside the VIDIOCCAP
1879 * Ioctl. Here, just do the window and picture stucts.
1881 cam
->vp
.palette
= (u16
) VIDEO_PALETTE_RGB24
; /* Is this right? */
1882 cam
->vp
.brightness
= (u16
) cam
->params
.color_params
.brightness
* 256;
1883 cam
->vp
.colour
= (u16
) cam
->params
.color_params
.saturation
* 256;
1884 cam
->vp
.contrast
= (u16
) cam
->params
.color_params
.contrast
* 256;
1888 cam
->vw
.width
= cam
->params
.roi
.width
;
1889 cam
->vw
.height
= cam
->params
.roi
.height
;
1891 cam
->vw
.clipcount
= 0;
1893 cam
->frame_size
= buffer_size
;
1894 cam
->num_frames
= num_buffers
;
1897 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1898 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1901 cam
->params
.camera_state
.stream_mode
= alternate
;
1903 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1904 v4l2_prio_init(&cam
->prio
);
1909 * The v4l video device structure initialized for this device
1911 static const struct v4l2_file_operations fops_template
= {
1912 .owner
= THIS_MODULE
,
1914 .release
= cpia2_close
,
1915 .read
= cpia2_v4l_read
,
1916 .poll
= cpia2_v4l_poll
,
1917 .ioctl
= cpia2_ioctl
,
1921 static struct video_device cpia2_template
= {
1922 /* I could not find any place for the old .initialize initializer?? */
1923 .name
= "CPiA2 Camera",
1924 .fops
= &fops_template
,
1925 .release
= video_device_release
,
1928 /******************************************************************************
1930 * cpia2_register_camera
1932 *****************************************************************************/
1933 int cpia2_register_camera(struct camera_data
*cam
)
1935 cam
->vdev
= video_device_alloc();
1939 memcpy(cam
->vdev
, &cpia2_template
, sizeof(cpia2_template
));
1940 video_set_drvdata(cam
->vdev
, cam
);
1942 reset_camera_struct_v4l(cam
);
1944 /* register v4l device */
1945 if (video_register_device(cam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
1946 ERR("video_register_device failed\n");
1947 video_device_release(cam
->vdev
);
1954 /******************************************************************************
1956 * cpia2_unregister_camera
1958 *****************************************************************************/
1959 void cpia2_unregister_camera(struct camera_data
*cam
)
1961 if (!cam
->open_count
) {
1962 video_unregister_device(cam
->vdev
);
1964 LOG("%s removed while open, deferring "
1965 "video_unregister_device\n",
1966 video_device_node_name(cam
->vdev
));
1970 /******************************************************************************
1974 * Make sure that all user-supplied parameters are sensible
1975 *****************************************************************************/
1976 static void __init
check_parameters(void)
1978 if(buffer_size
< PAGE_SIZE
) {
1979 buffer_size
= PAGE_SIZE
;
1980 LOG("buffer_size too small, setting to %d\n", buffer_size
);
1981 } else if(buffer_size
> 1024*1024) {
1982 /* arbitrary upper limiit */
1983 buffer_size
= 1024*1024;
1984 LOG("buffer_size ridiculously large, setting to %d\n",
1987 buffer_size
+= PAGE_SIZE
-1;
1988 buffer_size
&= ~(PAGE_SIZE
-1);
1991 if(num_buffers
< 1) {
1993 LOG("num_buffers too small, setting to %d\n", num_buffers
);
1994 } else if(num_buffers
> VIDEO_MAX_FRAME
) {
1995 num_buffers
= VIDEO_MAX_FRAME
;
1996 LOG("num_buffers too large, setting to %d\n", num_buffers
);
1999 if(alternate
< USBIF_ISO_1
|| alternate
> USBIF_ISO_6
) {
2000 alternate
= DEFAULT_ALT
;
2001 LOG("alternate specified is invalid, using %d\n", alternate
);
2004 if (flicker_mode
!= NEVER_FLICKER
&& flicker_mode
!= ANTI_FLICKER_ON
) {
2005 flicker_mode
= NEVER_FLICKER
;
2006 LOG("Flicker mode specified is invalid, using %d\n",
2010 if (flicker_freq
!= FLICKER_50
&& flicker_freq
!= FLICKER_60
) {
2011 flicker_freq
= FLICKER_60
;
2012 LOG("Flicker mode specified is invalid, using %d\n",
2016 if(video_nr
< -1 || video_nr
> 64) {
2018 LOG("invalid video_nr specified, must be -1 to 64\n");
2021 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
2022 num_buffers
, buffer_size
, alternate
);
2025 /************ Module Stuff ***************/
2028 /******************************************************************************
2030 * cpia2_init/module_init
2032 *****************************************************************************/
2033 static int __init
cpia2_init(void)
2035 LOG("%s v%d.%d.%d\n",
2036 ABOUT
, CPIA2_MAJ_VER
, CPIA2_MIN_VER
, CPIA2_PATCH_VER
);
2043 /******************************************************************************
2045 * cpia2_exit/module_exit
2047 *****************************************************************************/
2048 static void __exit
cpia2_exit(void)
2050 cpia2_usb_cleanup();
2051 schedule_timeout(2 * HZ
);
2054 module_init(cpia2_init
);
2055 module_exit(cpia2_exit
);