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/videodev2.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");
251 if (cam
->open_count
== 0) {
252 if (cpia2_allocate_buffers(cam
))
255 /* reset the camera */
256 if (cpia2_reset_camera(cam
) < 0)
263 fh
= kmalloc(sizeof(*fh
), GFP_KERNEL
);
266 file
->private_data
= fh
;
267 fh
->prio
= V4L2_PRIORITY_UNSET
;
268 v4l2_prio_open(&cam
->prio
, &fh
->prio
);
273 cpia2_dbg_dump_registers(cam
);
277 /******************************************************************************
281 *****************************************************************************/
282 static int cpia2_close(struct file
*file
)
284 struct video_device
*dev
= video_devdata(file
);
285 struct camera_data
*cam
= video_get_drvdata(dev
);
286 struct cpia2_fh
*fh
= file
->private_data
;
289 (cam
->open_count
== 1 || fh
->prio
== V4L2_PRIORITY_RECORD
)) {
290 cpia2_usb_stream_stop(cam
);
292 if (cam
->open_count
== 1) {
293 /* save camera state for later open */
294 cpia2_save_camera_state(cam
);
296 cpia2_set_low_power(cam
);
297 cpia2_free_buffers(cam
);
303 v4l2_prio_close(&cam
->prio
, fh
->prio
);
304 file
->private_data
= NULL
;
307 if (--cam
->open_count
== 0) {
308 cpia2_free_buffers(cam
);
310 video_unregister_device(dev
);
319 /******************************************************************************
323 *****************************************************************************/
324 static ssize_t
cpia2_v4l_read(struct file
*file
, char __user
*buf
, size_t count
,
327 struct camera_data
*cam
= video_drvdata(file
);
328 int noblock
= file
->f_flags
&O_NONBLOCK
;
330 struct cpia2_fh
*fh
= file
->private_data
;
336 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
340 return cpia2_read(cam
, buf
, count
, noblock
);
344 /******************************************************************************
348 *****************************************************************************/
349 static unsigned int cpia2_v4l_poll(struct file
*filp
, struct poll_table_struct
*wait
)
351 struct camera_data
*cam
= video_drvdata(filp
);
352 struct cpia2_fh
*fh
= filp
->private_data
;
358 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
362 return cpia2_poll(cam
, filp
, wait
);
366 static int sync(struct camera_data
*cam
, int frame_nr
)
368 struct framebuf
*frame
= &cam
->buffers
[frame_nr
];
371 if (frame
->status
== FRAME_READY
)
374 if (!cam
->streaming
) {
375 frame
->status
= FRAME_READY
;
380 mutex_unlock(&cam
->v4l2_lock
);
381 wait_event_interruptible(cam
->wq_stream
,
383 frame
->status
== FRAME_READY
);
384 mutex_lock(&cam
->v4l2_lock
);
385 if (signal_pending(current
))
392 /******************************************************************************
396 *****************************************************************************/
398 static long cpia2_default(struct file
*file
, void *fh
, bool valid_prio
,
401 struct camera_data
*cam
= video_drvdata(file
);
404 if (cmd
!= CPIA2_CID_GPIO
)
407 gpio_val
= *(__u32
*) arg
;
409 if (gpio_val
&~ 0xFFU
)
412 return cpia2_set_gpio(cam
, (unsigned char)gpio_val
);
415 /******************************************************************************
419 * V4L2 device capabilities
421 *****************************************************************************/
423 static int cpia2_querycap(struct file
*file
, void *fh
, struct v4l2_capability
*vc
)
425 struct camera_data
*cam
= video_drvdata(file
);
427 strcpy(vc
->driver
, "cpia2");
429 if (cam
->params
.pnp_id
.product
== 0x151)
430 strcpy(vc
->card
, "QX5 Microscope");
432 strcpy(vc
->card
, "CPiA2 Camera");
433 switch (cam
->params
.pnp_id
.device_type
) {
435 strcat(vc
->card
, " (672/");
438 strcat(vc
->card
, " (676/");
441 strcat(vc
->card
, " (???/");
444 switch (cam
->params
.version
.sensor_flags
) {
445 case CPIA2_VP_SENSOR_FLAGS_404
:
446 strcat(vc
->card
, "404)");
448 case CPIA2_VP_SENSOR_FLAGS_407
:
449 strcat(vc
->card
, "407)");
451 case CPIA2_VP_SENSOR_FLAGS_409
:
452 strcat(vc
->card
, "409)");
454 case CPIA2_VP_SENSOR_FLAGS_410
:
455 strcat(vc
->card
, "410)");
457 case CPIA2_VP_SENSOR_FLAGS_500
:
458 strcat(vc
->card
, "500)");
461 strcat(vc
->card
, "???)");
465 if (usb_make_path(cam
->dev
, vc
->bus_info
, sizeof(vc
->bus_info
)) <0)
466 memset(vc
->bus_info
,0, sizeof(vc
->bus_info
));
468 vc
->version
= KERNEL_VERSION(CPIA2_MAJ_VER
, CPIA2_MIN_VER
,
471 vc
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
478 /******************************************************************************
482 * V4L2 input get/set/enumerate
484 *****************************************************************************/
486 static int cpia2_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*i
)
490 strcpy(i
->name
, "Camera");
491 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
495 static int cpia2_g_input(struct file
*file
, void *fh
, unsigned int *i
)
501 static int cpia2_s_input(struct file
*file
, void *fh
, unsigned int i
)
503 return i
? -EINVAL
: 0;
506 /******************************************************************************
510 * V4L2 format enumerate
512 *****************************************************************************/
514 static int cpia2_enum_fmt_vid_cap(struct file
*file
, void *fh
,
515 struct v4l2_fmtdesc
*f
)
517 int index
= f
->index
;
519 if (index
< 0 || index
> 1)
522 memset(f
, 0, sizeof(*f
));
524 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
525 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
528 strcpy(f
->description
, "MJPEG");
529 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
532 strcpy(f
->description
, "JPEG");
533 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
542 /******************************************************************************
548 *****************************************************************************/
550 static int cpia2_try_fmt_vid_cap(struct file
*file
, void *fh
,
551 struct v4l2_format
*f
)
553 struct camera_data
*cam
= video_drvdata(file
);
555 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
556 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
559 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
560 f
->fmt
.pix
.bytesperline
= 0;
561 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
562 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
565 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
567 f
->fmt
.pix
.width
= 640;
568 f
->fmt
.pix
.height
= 480;
571 f
->fmt
.pix
.width
= 352;
572 f
->fmt
.pix
.height
= 288;
575 f
->fmt
.pix
.width
= 320;
576 f
->fmt
.pix
.height
= 240;
578 case VIDEOSIZE_288_216
:
579 f
->fmt
.pix
.width
= 288;
580 f
->fmt
.pix
.height
= 216;
582 case VIDEOSIZE_256_192
:
583 f
->fmt
.pix
.width
= 256;
584 f
->fmt
.pix
.height
= 192;
586 case VIDEOSIZE_224_168
:
587 f
->fmt
.pix
.width
= 224;
588 f
->fmt
.pix
.height
= 168;
590 case VIDEOSIZE_192_144
:
591 f
->fmt
.pix
.width
= 192;
592 f
->fmt
.pix
.height
= 144;
596 f
->fmt
.pix
.width
= 176;
597 f
->fmt
.pix
.height
= 144;
604 /******************************************************************************
610 *****************************************************************************/
612 static int cpia2_s_fmt_vid_cap(struct file
*file
, void *_fh
,
613 struct v4l2_format
*f
)
615 struct camera_data
*cam
= video_drvdata(file
);
616 struct cpia2_fh
*fh
= _fh
;
619 err
= v4l2_prio_check(&cam
->prio
, fh
->prio
);
622 err
= cpia2_try_fmt_vid_cap(file
, _fh
, f
);
626 /* Ensure that only this process can change the format. */
627 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
632 cam
->pixelformat
= f
->fmt
.pix
.pixelformat
;
634 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
635 * the missing Huffman table properly. */
636 cam
->params
.compression
.inhibit_htables
= 0;
637 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
639 /* we set the video window to something smaller or equal to what
640 * is requested by the user???
642 DBG("Requested width = %d, height = %d\n",
643 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
644 if (f
->fmt
.pix
.width
!= cam
->width
||
645 f
->fmt
.pix
.height
!= cam
->height
) {
646 cam
->width
= f
->fmt
.pix
.width
;
647 cam
->height
= f
->fmt
.pix
.height
;
648 cam
->params
.roi
.width
= f
->fmt
.pix
.width
;
649 cam
->params
.roi
.height
= f
->fmt
.pix
.height
;
650 cpia2_set_format(cam
);
653 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
654 if (cam
->buffers
[frame
].status
== FRAME_READING
)
655 if ((err
= sync(cam
, frame
)) < 0)
658 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
664 /******************************************************************************
670 *****************************************************************************/
672 static int cpia2_g_fmt_vid_cap(struct file
*file
, void *fh
,
673 struct v4l2_format
*f
)
675 struct camera_data
*cam
= video_drvdata(file
);
677 f
->fmt
.pix
.width
= cam
->width
;
678 f
->fmt
.pix
.height
= cam
->height
;
679 f
->fmt
.pix
.pixelformat
= cam
->pixelformat
;
680 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
681 f
->fmt
.pix
.bytesperline
= 0;
682 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
683 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
689 /******************************************************************************
693 * V4L2 query cropping capabilities
694 * NOTE: cropping is currently disabled
696 *****************************************************************************/
698 static int cpia2_cropcap(struct file
*file
, void *fh
, struct v4l2_cropcap
*c
)
700 struct camera_data
*cam
= video_drvdata(file
);
702 if (c
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
707 c
->bounds
.width
= cam
->width
;
708 c
->bounds
.height
= cam
->height
;
711 c
->defrect
.width
= cam
->width
;
712 c
->defrect
.height
= cam
->height
;
713 c
->pixelaspect
.numerator
= 1;
714 c
->pixelaspect
.denominator
= 1;
719 /******************************************************************************
723 * V4L2 query possible control variables
725 *****************************************************************************/
727 static int cpia2_queryctrl(struct file
*file
, void *fh
, struct v4l2_queryctrl
*c
)
729 struct camera_data
*cam
= video_drvdata(file
);
732 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
733 if(c
->id
== controls
[i
].id
) {
734 memcpy(c
, controls
+i
, sizeof(*c
));
739 if(i
== NUM_CONTROLS
)
742 /* Some devices have additional limitations */
744 case V4L2_CID_BRIGHTNESS
:
746 * Don't let the register be set to zero - bug in VP4
747 * flash of full brightness
749 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
754 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
755 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
757 case CPIA2_CID_FRAMERATE
:
758 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
759 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
761 for(i
=0; i
<c
->maximum
; ++i
) {
762 if(framerate_controls
[i
].value
==
763 CPIA2_VP_FRAMERATE_15
) {
765 c
->default_value
= i
;
770 case CPIA2_CID_FLICKER_MODE
:
771 // Flicker control only valid for 672.
772 if(cam
->params
.pnp_id
.device_type
!= DEVICE_STV_672
)
773 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
775 case CPIA2_CID_LIGHTS
:
776 // Light control only valid for the QX5 Microscope.
777 if(cam
->params
.pnp_id
.product
!= 0x151)
778 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
787 /******************************************************************************
791 * V4L2 query possible control variables
793 *****************************************************************************/
795 static int cpia2_querymenu(struct file
*file
, void *fh
, struct v4l2_querymenu
*m
)
797 struct camera_data
*cam
= video_drvdata(file
);
800 case CPIA2_CID_FLICKER_MODE
:
801 if (m
->index
>= NUM_FLICKER_CONTROLS
)
804 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
806 case CPIA2_CID_FRAMERATE
:
808 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
809 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
810 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
813 for(i
=0; i
<maximum
; ++i
) {
814 if(framerate_controls
[i
].value
==
815 CPIA2_VP_FRAMERATE_15
)
819 if (m
->index
> maximum
)
822 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
825 case CPIA2_CID_LIGHTS
:
826 if (m
->index
>= NUM_LIGHTS_CONTROLS
)
829 strcpy(m
->name
, lights_controls
[m
->index
].name
);
838 /******************************************************************************
842 * V4L2 get the value of a control variable
844 *****************************************************************************/
846 static int cpia2_g_ctrl(struct file
*file
, void *fh
, struct v4l2_control
*c
)
848 struct camera_data
*cam
= video_drvdata(file
);
851 case V4L2_CID_BRIGHTNESS
:
852 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
854 c
->value
= cam
->params
.color_params
.brightness
;
856 case V4L2_CID_CONTRAST
:
857 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
859 c
->value
= cam
->params
.color_params
.contrast
;
861 case V4L2_CID_SATURATION
:
862 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
864 c
->value
= cam
->params
.color_params
.saturation
;
867 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
869 c
->value
= (cam
->params
.vp_params
.user_effects
&
870 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
873 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
875 c
->value
= (cam
->params
.vp_params
.user_effects
&
876 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
878 case CPIA2_CID_TARGET_KB
:
879 c
->value
= cam
->params
.vc_params
.target_kb
;
882 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
884 c
->value
= cam
->params
.vp_params
.gpio_data
;
886 case CPIA2_CID_FLICKER_MODE
:
889 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
891 if(cam
->params
.flicker_control
.cam_register
&
892 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
893 mode
= NEVER_FLICKER
;
895 if(cam
->params
.flicker_control
.cam_register
&
896 CPIA2_VP_FLICKER_MODES_50HZ
) {
902 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
903 if(flicker_controls
[i
].value
== mode
) {
908 if(i
== NUM_FLICKER_CONTROLS
)
912 case CPIA2_CID_FRAMERATE
:
914 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
916 for(i
=0; i
<= maximum
; i
++) {
917 if(cam
->params
.vp_params
.frame_rate
==
918 framerate_controls
[i
].value
)
926 case CPIA2_CID_USB_ALT
:
927 c
->value
= cam
->params
.camera_state
.stream_mode
;
929 case CPIA2_CID_LIGHTS
:
932 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
934 for(i
=0; i
<NUM_LIGHTS_CONTROLS
; i
++) {
935 if((cam
->params
.vp_params
.gpio_data
&GPIO_LIGHTS_MASK
) ==
936 lights_controls
[i
].value
) {
940 if(i
== NUM_LIGHTS_CONTROLS
)
945 case CPIA2_CID_RESET_CAMERA
:
951 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
956 /******************************************************************************
960 * V4L2 set the value of a control variable
962 *****************************************************************************/
964 static int cpia2_s_ctrl(struct file
*file
, void *fh
, struct v4l2_control
*c
)
966 struct camera_data
*cam
= video_drvdata(file
);
970 DBG("Set control id:%d, value:%d\n", c
->id
, c
->value
);
972 /* Check that the value is in range */
973 for(i
=0; i
<NUM_CONTROLS
; i
++) {
974 if(c
->id
== controls
[i
].id
) {
975 if(c
->value
< controls
[i
].minimum
||
976 c
->value
> controls
[i
].maximum
) {
982 if(i
== NUM_CONTROLS
)
986 case V4L2_CID_BRIGHTNESS
:
987 cpia2_set_brightness(cam
, c
->value
);
989 case V4L2_CID_CONTRAST
:
990 cpia2_set_contrast(cam
, c
->value
);
992 case V4L2_CID_SATURATION
:
993 cpia2_set_saturation(cam
, c
->value
);
996 cpia2_set_property_mirror(cam
, c
->value
);
999 cpia2_set_property_flip(cam
, c
->value
);
1001 case CPIA2_CID_TARGET_KB
:
1002 retval
= cpia2_set_target_kb(cam
, c
->value
);
1004 case CPIA2_CID_GPIO
:
1005 retval
= cpia2_set_gpio(cam
, c
->value
);
1007 case CPIA2_CID_FLICKER_MODE
:
1008 retval
= cpia2_set_flicker_mode(cam
,
1009 flicker_controls
[c
->value
].value
);
1011 case CPIA2_CID_FRAMERATE
:
1012 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1014 case CPIA2_CID_USB_ALT
:
1015 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1017 case CPIA2_CID_LIGHTS
:
1018 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1020 case CPIA2_CID_RESET_CAMERA
:
1021 cpia2_usb_stream_pause(cam
);
1022 cpia2_reset_camera(cam
);
1023 cpia2_usb_stream_resume(cam
);
1032 /******************************************************************************
1036 * V4L2 get the JPEG compression parameters
1038 *****************************************************************************/
1040 static int cpia2_g_jpegcomp(struct file
*file
, void *fh
, struct v4l2_jpegcompression
*parms
)
1042 struct camera_data
*cam
= video_drvdata(file
);
1044 memset(parms
, 0, sizeof(*parms
));
1046 parms
->quality
= 80; // TODO: Can this be made meaningful?
1048 parms
->jpeg_markers
= V4L2_JPEG_MARKER_DQT
| V4L2_JPEG_MARKER_DRI
;
1049 if(!cam
->params
.compression
.inhibit_htables
) {
1050 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_DHT
;
1053 parms
->APPn
= cam
->APPn
;
1054 parms
->APP_len
= cam
->APP_len
;
1055 if(cam
->APP_len
> 0) {
1056 memcpy(parms
->APP_data
, cam
->APP_data
, cam
->APP_len
);
1057 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_APP
;
1060 parms
->COM_len
= cam
->COM_len
;
1061 if(cam
->COM_len
> 0) {
1062 memcpy(parms
->COM_data
, cam
->COM_data
, cam
->COM_len
);
1063 parms
->jpeg_markers
|= JPEG_MARKER_COM
;
1066 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1067 parms
->APP_len
, parms
->COM_len
);
1072 /******************************************************************************
1076 * V4L2 set the JPEG compression parameters
1077 * NOTE: quality and some jpeg_markers are ignored.
1079 *****************************************************************************/
1081 static int cpia2_s_jpegcomp(struct file
*file
, void *fh
, struct v4l2_jpegcompression
*parms
)
1083 struct camera_data
*cam
= video_drvdata(file
);
1085 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1086 parms
->APP_len
, parms
->COM_len
);
1088 cam
->params
.compression
.inhibit_htables
=
1089 !(parms
->jpeg_markers
& V4L2_JPEG_MARKER_DHT
);
1091 if(parms
->APP_len
!= 0) {
1092 if(parms
->APP_len
> 0 &&
1093 parms
->APP_len
<= sizeof(cam
->APP_data
) &&
1094 parms
->APPn
>= 0 && parms
->APPn
<= 15) {
1095 cam
->APPn
= parms
->APPn
;
1096 cam
->APP_len
= parms
->APP_len
;
1097 memcpy(cam
->APP_data
, parms
->APP_data
, parms
->APP_len
);
1099 LOG("Bad APPn Params n=%d len=%d\n",
1100 parms
->APPn
, parms
->APP_len
);
1107 if(parms
->COM_len
!= 0) {
1108 if(parms
->COM_len
> 0 &&
1109 parms
->COM_len
<= sizeof(cam
->COM_data
)) {
1110 cam
->COM_len
= parms
->COM_len
;
1111 memcpy(cam
->COM_data
, parms
->COM_data
, parms
->COM_len
);
1113 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1121 /******************************************************************************
1125 * V4L2 Initiate memory mapping.
1126 * NOTE: The user's request is ignored. For now the buffers are fixed.
1128 *****************************************************************************/
1130 static int cpia2_reqbufs(struct file
*file
, void *fh
, struct v4l2_requestbuffers
*req
)
1132 struct camera_data
*cam
= video_drvdata(file
);
1134 if(req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1135 req
->memory
!= V4L2_MEMORY_MMAP
)
1138 DBG("REQBUFS requested:%d returning:%d\n", req
->count
, cam
->num_frames
);
1139 req
->count
= cam
->num_frames
;
1140 memset(&req
->reserved
, 0, sizeof(req
->reserved
));
1145 /******************************************************************************
1149 * V4L2 Query memory buffer status.
1151 *****************************************************************************/
1153 static int cpia2_querybuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1155 struct camera_data
*cam
= video_drvdata(file
);
1157 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1158 buf
->index
> cam
->num_frames
)
1161 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1162 buf
->length
= cam
->frame_size
;
1164 buf
->memory
= V4L2_MEMORY_MMAP
;
1167 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1171 switch (cam
->buffers
[buf
->index
].status
) {
1176 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
1179 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1180 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1181 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1182 buf
->flags
= V4L2_BUF_FLAG_DONE
;
1186 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1187 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1193 /******************************************************************************
1197 * V4L2 User is freeing buffer
1199 *****************************************************************************/
1201 static int cpia2_qbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1203 struct camera_data
*cam
= video_drvdata(file
);
1205 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1206 buf
->memory
!= V4L2_MEMORY_MMAP
||
1207 buf
->index
> cam
->num_frames
)
1210 DBG("QBUF #%d\n", buf
->index
);
1212 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1213 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
1218 /******************************************************************************
1220 * find_earliest_filled_buffer
1222 * Helper for ioctl_dqbuf. Find the next ready buffer.
1224 *****************************************************************************/
1226 static int find_earliest_filled_buffer(struct camera_data
*cam
)
1230 for (i
=0; i
<cam
->num_frames
; i
++) {
1231 if(cam
->buffers
[i
].status
== FRAME_READY
) {
1235 /* find which buffer is earlier */
1236 struct timeval
*tv1
, *tv2
;
1237 tv1
= &cam
->buffers
[i
].timestamp
;
1238 tv2
= &cam
->buffers
[found
].timestamp
;
1239 if(tv1
->tv_sec
< tv2
->tv_sec
||
1240 (tv1
->tv_sec
== tv2
->tv_sec
&&
1241 tv1
->tv_usec
< tv2
->tv_usec
))
1249 /******************************************************************************
1253 * V4L2 User is asking for a filled buffer.
1255 *****************************************************************************/
1257 static int cpia2_dqbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1259 struct camera_data
*cam
= video_drvdata(file
);
1262 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1263 buf
->memory
!= V4L2_MEMORY_MMAP
)
1266 frame
= find_earliest_filled_buffer(cam
);
1268 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
1272 /* Wait for a frame to become available */
1273 struct framebuf
*cb
=cam
->curbuff
;
1274 mutex_unlock(&cam
->v4l2_lock
);
1275 wait_event_interruptible(cam
->wq_stream
,
1277 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1278 mutex_lock(&cam
->v4l2_lock
);
1279 if (signal_pending(current
))
1280 return -ERESTARTSYS
;
1288 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1289 buf
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_DONE
;
1290 buf
->field
= V4L2_FIELD_NONE
;
1291 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1292 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1293 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1294 buf
->length
= cam
->frame_size
;
1297 memset(&buf
->timecode
, 0, sizeof(buf
->timecode
));
1299 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf
->index
,
1300 cam
->buffers
[buf
->index
].status
, buf
->sequence
, buf
->bytesused
);
1305 static int cpia2_g_priority(struct file
*file
, void *_fh
, enum v4l2_priority
*p
)
1307 struct cpia2_fh
*fh
= _fh
;
1313 static int cpia2_s_priority(struct file
*file
, void *_fh
, enum v4l2_priority prio
)
1315 struct camera_data
*cam
= video_drvdata(file
);
1316 struct cpia2_fh
*fh
= fh
;
1318 if (cam
->streaming
&& prio
!= fh
->prio
&&
1319 fh
->prio
== V4L2_PRIORITY_RECORD
)
1320 /* Can't drop record priority while streaming */
1323 if (prio
== V4L2_PRIORITY_RECORD
&& prio
!= fh
->prio
&&
1324 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
)
1325 /* Only one program can record at a time */
1327 return v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1330 static int cpia2_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1332 struct camera_data
*cam
= video_drvdata(file
);
1334 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1335 if (!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1338 if (!cam
->streaming
)
1339 return cpia2_usb_stream_start(cam
,
1340 cam
->params
.camera_state
.stream_mode
);
1344 static int cpia2_streamoff(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1346 struct camera_data
*cam
= video_drvdata(file
);
1348 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1349 if (!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1353 return cpia2_usb_stream_stop(cam
);
1357 /******************************************************************************
1361 *****************************************************************************/
1362 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
1364 struct camera_data
*cam
= video_drvdata(file
);
1367 /* Priority check */
1368 struct cpia2_fh
*fh
= file
->private_data
;
1369 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1373 retval
= cpia2_remap_buffer(cam
, area
);
1380 /******************************************************************************
1382 * reset_camera_struct_v4l
1384 * Sets all values to the defaults
1385 *****************************************************************************/
1386 static void reset_camera_struct_v4l(struct camera_data
*cam
)
1388 cam
->width
= cam
->params
.roi
.width
;
1389 cam
->height
= cam
->params
.roi
.height
;
1391 cam
->frame_size
= buffer_size
;
1392 cam
->num_frames
= num_buffers
;
1395 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1396 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1399 cam
->params
.camera_state
.stream_mode
= alternate
;
1401 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1402 v4l2_prio_init(&cam
->prio
);
1405 static const struct v4l2_ioctl_ops cpia2_ioctl_ops
= {
1406 .vidioc_querycap
= cpia2_querycap
,
1407 .vidioc_enum_input
= cpia2_enum_input
,
1408 .vidioc_g_input
= cpia2_g_input
,
1409 .vidioc_s_input
= cpia2_s_input
,
1410 .vidioc_enum_fmt_vid_cap
= cpia2_enum_fmt_vid_cap
,
1411 .vidioc_g_fmt_vid_cap
= cpia2_g_fmt_vid_cap
,
1412 .vidioc_s_fmt_vid_cap
= cpia2_s_fmt_vid_cap
,
1413 .vidioc_try_fmt_vid_cap
= cpia2_try_fmt_vid_cap
,
1414 .vidioc_queryctrl
= cpia2_queryctrl
,
1415 .vidioc_querymenu
= cpia2_querymenu
,
1416 .vidioc_g_ctrl
= cpia2_g_ctrl
,
1417 .vidioc_s_ctrl
= cpia2_s_ctrl
,
1418 .vidioc_g_jpegcomp
= cpia2_g_jpegcomp
,
1419 .vidioc_s_jpegcomp
= cpia2_s_jpegcomp
,
1420 .vidioc_cropcap
= cpia2_cropcap
,
1421 .vidioc_reqbufs
= cpia2_reqbufs
,
1422 .vidioc_querybuf
= cpia2_querybuf
,
1423 .vidioc_qbuf
= cpia2_qbuf
,
1424 .vidioc_dqbuf
= cpia2_dqbuf
,
1425 .vidioc_streamon
= cpia2_streamon
,
1426 .vidioc_streamoff
= cpia2_streamoff
,
1427 .vidioc_g_priority
= cpia2_g_priority
,
1428 .vidioc_s_priority
= cpia2_s_priority
,
1429 .vidioc_default
= cpia2_default
,
1433 * The v4l video device structure initialized for this device
1435 static const struct v4l2_file_operations cpia2_fops
= {
1436 .owner
= THIS_MODULE
,
1438 .release
= cpia2_close
,
1439 .read
= cpia2_v4l_read
,
1440 .poll
= cpia2_v4l_poll
,
1441 .unlocked_ioctl
= video_ioctl2
,
1445 static struct video_device cpia2_template
= {
1446 /* I could not find any place for the old .initialize initializer?? */
1447 .name
= "CPiA2 Camera",
1448 .fops
= &cpia2_fops
,
1449 .ioctl_ops
= &cpia2_ioctl_ops
,
1450 .release
= video_device_release
,
1453 /******************************************************************************
1455 * cpia2_register_camera
1457 *****************************************************************************/
1458 int cpia2_register_camera(struct camera_data
*cam
)
1460 cam
->vdev
= video_device_alloc();
1464 memcpy(cam
->vdev
, &cpia2_template
, sizeof(cpia2_template
));
1465 video_set_drvdata(cam
->vdev
, cam
);
1466 cam
->vdev
->lock
= &cam
->v4l2_lock
;
1468 reset_camera_struct_v4l(cam
);
1470 /* register v4l device */
1471 if (video_register_device(cam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
1472 ERR("video_register_device failed\n");
1473 video_device_release(cam
->vdev
);
1480 /******************************************************************************
1482 * cpia2_unregister_camera
1484 *****************************************************************************/
1485 void cpia2_unregister_camera(struct camera_data
*cam
)
1487 if (!cam
->open_count
) {
1488 video_unregister_device(cam
->vdev
);
1490 LOG("%s removed while open, deferring "
1491 "video_unregister_device\n",
1492 video_device_node_name(cam
->vdev
));
1496 /******************************************************************************
1500 * Make sure that all user-supplied parameters are sensible
1501 *****************************************************************************/
1502 static void __init
check_parameters(void)
1504 if(buffer_size
< PAGE_SIZE
) {
1505 buffer_size
= PAGE_SIZE
;
1506 LOG("buffer_size too small, setting to %d\n", buffer_size
);
1507 } else if(buffer_size
> 1024*1024) {
1508 /* arbitrary upper limiit */
1509 buffer_size
= 1024*1024;
1510 LOG("buffer_size ridiculously large, setting to %d\n",
1513 buffer_size
+= PAGE_SIZE
-1;
1514 buffer_size
&= ~(PAGE_SIZE
-1);
1517 if(num_buffers
< 1) {
1519 LOG("num_buffers too small, setting to %d\n", num_buffers
);
1520 } else if(num_buffers
> VIDEO_MAX_FRAME
) {
1521 num_buffers
= VIDEO_MAX_FRAME
;
1522 LOG("num_buffers too large, setting to %d\n", num_buffers
);
1525 if(alternate
< USBIF_ISO_1
|| alternate
> USBIF_ISO_6
) {
1526 alternate
= DEFAULT_ALT
;
1527 LOG("alternate specified is invalid, using %d\n", alternate
);
1530 if (flicker_mode
!= NEVER_FLICKER
&& flicker_mode
!= ANTI_FLICKER_ON
) {
1531 flicker_mode
= NEVER_FLICKER
;
1532 LOG("Flicker mode specified is invalid, using %d\n",
1536 if (flicker_freq
!= FLICKER_50
&& flicker_freq
!= FLICKER_60
) {
1537 flicker_freq
= FLICKER_60
;
1538 LOG("Flicker mode specified is invalid, using %d\n",
1542 if(video_nr
< -1 || video_nr
> 64) {
1544 LOG("invalid video_nr specified, must be -1 to 64\n");
1547 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1548 num_buffers
, buffer_size
, alternate
);
1551 /************ Module Stuff ***************/
1554 /******************************************************************************
1556 * cpia2_init/module_init
1558 *****************************************************************************/
1559 static int __init
cpia2_init(void)
1561 LOG("%s v%d.%d.%d\n",
1562 ABOUT
, CPIA2_MAJ_VER
, CPIA2_MIN_VER
, CPIA2_PATCH_VER
);
1569 /******************************************************************************
1571 * cpia2_exit/module_exit
1573 *****************************************************************************/
1574 static void __exit
cpia2_exit(void)
1576 cpia2_usb_cleanup();
1577 schedule_timeout(2 * HZ
);
1580 module_init(cpia2_init
);
1581 module_exit(cpia2_exit
);