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 #define CPIA_VERSION "3.0.1"
34 #include <linux/module.h>
35 #include <linux/time.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/videodev2.h>
40 #include <linux/stringify.h>
41 #include <media/v4l2-ioctl.h>
46 static int video_nr
= -1;
47 module_param(video_nr
, int, 0);
48 MODULE_PARM_DESC(video_nr
,"video device to register (0=/dev/video0, etc)");
50 static int buffer_size
= 68*1024;
51 module_param(buffer_size
, int, 0);
52 MODULE_PARM_DESC(buffer_size
, "Size for each frame buffer in bytes (default 68k)");
54 static int num_buffers
= 3;
55 module_param(num_buffers
, int, 0);
56 MODULE_PARM_DESC(num_buffers
, "Number of frame buffers (1-"
57 __stringify(VIDEO_MAX_FRAME
) ", default 3)");
59 static int alternate
= DEFAULT_ALT
;
60 module_param(alternate
, int, 0);
61 MODULE_PARM_DESC(alternate
, "USB Alternate (" __stringify(USBIF_ISO_1
) "-"
62 __stringify(USBIF_ISO_6
) ", default "
63 __stringify(DEFAULT_ALT
) ")");
65 static int flicker_freq
= 60;
66 module_param(flicker_freq
, int, 0);
67 MODULE_PARM_DESC(flicker_freq
, "Flicker frequency (" __stringify(50) "or"
68 __stringify(60) ", default "
71 static int flicker_mode
= NEVER_FLICKER
;
72 module_param(flicker_mode
, int, 0);
73 MODULE_PARM_DESC(flicker_mode
,
74 "Flicker supression (" __stringify(NEVER_FLICKER
) "or"
75 __stringify(ANTI_FLICKER_ON
) ", default "
76 __stringify(NEVER_FLICKER
) ")");
78 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
79 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
80 MODULE_SUPPORTED_DEVICE("video");
81 MODULE_LICENSE("GPL");
82 MODULE_VERSION(CPIA_VERSION
);
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
, " (XXX/");
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
, "XXX)");
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
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
475 /******************************************************************************
479 * V4L2 input get/set/enumerate
481 *****************************************************************************/
483 static int cpia2_enum_input(struct file
*file
, void *fh
, struct v4l2_input
*i
)
487 strcpy(i
->name
, "Camera");
488 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
492 static int cpia2_g_input(struct file
*file
, void *fh
, unsigned int *i
)
498 static int cpia2_s_input(struct file
*file
, void *fh
, unsigned int i
)
500 return i
? -EINVAL
: 0;
503 /******************************************************************************
507 * V4L2 format enumerate
509 *****************************************************************************/
511 static int cpia2_enum_fmt_vid_cap(struct file
*file
, void *fh
,
512 struct v4l2_fmtdesc
*f
)
514 int index
= f
->index
;
516 if (index
< 0 || index
> 1)
519 memset(f
, 0, sizeof(*f
));
521 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
522 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
525 strcpy(f
->description
, "MJPEG");
526 f
->pixelformat
= V4L2_PIX_FMT_MJPEG
;
529 strcpy(f
->description
, "JPEG");
530 f
->pixelformat
= V4L2_PIX_FMT_JPEG
;
539 /******************************************************************************
545 *****************************************************************************/
547 static int cpia2_try_fmt_vid_cap(struct file
*file
, void *fh
,
548 struct v4l2_format
*f
)
550 struct camera_data
*cam
= video_drvdata(file
);
552 if (f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_MJPEG
&&
553 f
->fmt
.pix
.pixelformat
!= V4L2_PIX_FMT_JPEG
)
556 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
557 f
->fmt
.pix
.bytesperline
= 0;
558 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
559 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
562 switch (cpia2_match_video_size(f
->fmt
.pix
.width
, f
->fmt
.pix
.height
)) {
564 f
->fmt
.pix
.width
= 640;
565 f
->fmt
.pix
.height
= 480;
568 f
->fmt
.pix
.width
= 352;
569 f
->fmt
.pix
.height
= 288;
572 f
->fmt
.pix
.width
= 320;
573 f
->fmt
.pix
.height
= 240;
575 case VIDEOSIZE_288_216
:
576 f
->fmt
.pix
.width
= 288;
577 f
->fmt
.pix
.height
= 216;
579 case VIDEOSIZE_256_192
:
580 f
->fmt
.pix
.width
= 256;
581 f
->fmt
.pix
.height
= 192;
583 case VIDEOSIZE_224_168
:
584 f
->fmt
.pix
.width
= 224;
585 f
->fmt
.pix
.height
= 168;
587 case VIDEOSIZE_192_144
:
588 f
->fmt
.pix
.width
= 192;
589 f
->fmt
.pix
.height
= 144;
593 f
->fmt
.pix
.width
= 176;
594 f
->fmt
.pix
.height
= 144;
601 /******************************************************************************
607 *****************************************************************************/
609 static int cpia2_s_fmt_vid_cap(struct file
*file
, void *_fh
,
610 struct v4l2_format
*f
)
612 struct camera_data
*cam
= video_drvdata(file
);
613 struct cpia2_fh
*fh
= _fh
;
616 err
= v4l2_prio_check(&cam
->prio
, fh
->prio
);
619 err
= cpia2_try_fmt_vid_cap(file
, _fh
, f
);
623 /* Ensure that only this process can change the format. */
624 err
= v4l2_prio_change(&cam
->prio
, &fh
->prio
, V4L2_PRIORITY_RECORD
);
629 cam
->pixelformat
= f
->fmt
.pix
.pixelformat
;
631 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
632 * the missing Huffman table properly. */
633 cam
->params
.compression
.inhibit_htables
= 0;
634 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
636 /* we set the video window to something smaller or equal to what
637 * is requested by the user???
639 DBG("Requested width = %d, height = %d\n",
640 f
->fmt
.pix
.width
, f
->fmt
.pix
.height
);
641 if (f
->fmt
.pix
.width
!= cam
->width
||
642 f
->fmt
.pix
.height
!= cam
->height
) {
643 cam
->width
= f
->fmt
.pix
.width
;
644 cam
->height
= f
->fmt
.pix
.height
;
645 cam
->params
.roi
.width
= f
->fmt
.pix
.width
;
646 cam
->params
.roi
.height
= f
->fmt
.pix
.height
;
647 cpia2_set_format(cam
);
650 for (frame
= 0; frame
< cam
->num_frames
; ++frame
) {
651 if (cam
->buffers
[frame
].status
== FRAME_READING
)
652 if ((err
= sync(cam
, frame
)) < 0)
655 cam
->buffers
[frame
].status
= FRAME_EMPTY
;
661 /******************************************************************************
667 *****************************************************************************/
669 static int cpia2_g_fmt_vid_cap(struct file
*file
, void *fh
,
670 struct v4l2_format
*f
)
672 struct camera_data
*cam
= video_drvdata(file
);
674 f
->fmt
.pix
.width
= cam
->width
;
675 f
->fmt
.pix
.height
= cam
->height
;
676 f
->fmt
.pix
.pixelformat
= cam
->pixelformat
;
677 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
678 f
->fmt
.pix
.bytesperline
= 0;
679 f
->fmt
.pix
.sizeimage
= cam
->frame_size
;
680 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_JPEG
;
686 /******************************************************************************
690 * V4L2 query cropping capabilities
691 * NOTE: cropping is currently disabled
693 *****************************************************************************/
695 static int cpia2_cropcap(struct file
*file
, void *fh
, struct v4l2_cropcap
*c
)
697 struct camera_data
*cam
= video_drvdata(file
);
699 if (c
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
704 c
->bounds
.width
= cam
->width
;
705 c
->bounds
.height
= cam
->height
;
708 c
->defrect
.width
= cam
->width
;
709 c
->defrect
.height
= cam
->height
;
710 c
->pixelaspect
.numerator
= 1;
711 c
->pixelaspect
.denominator
= 1;
716 /******************************************************************************
720 * V4L2 query possible control variables
722 *****************************************************************************/
724 static int cpia2_queryctrl(struct file
*file
, void *fh
, struct v4l2_queryctrl
*c
)
726 struct camera_data
*cam
= video_drvdata(file
);
729 for(i
=0; i
<NUM_CONTROLS
; ++i
) {
730 if(c
->id
== controls
[i
].id
) {
731 memcpy(c
, controls
+i
, sizeof(*c
));
736 if(i
== NUM_CONTROLS
)
739 /* Some devices have additional limitations */
741 case V4L2_CID_BRIGHTNESS
:
743 * Don't let the register be set to zero - bug in VP4
744 * flash of full brightness
746 if (cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
751 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
)
752 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
754 case CPIA2_CID_FRAMERATE
:
755 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
756 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
758 for(i
=0; i
<c
->maximum
; ++i
) {
759 if(framerate_controls
[i
].value
==
760 CPIA2_VP_FRAMERATE_15
) {
762 c
->default_value
= i
;
767 case CPIA2_CID_FLICKER_MODE
:
768 // Flicker control only valid for 672.
769 if(cam
->params
.pnp_id
.device_type
!= DEVICE_STV_672
)
770 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
772 case CPIA2_CID_LIGHTS
:
773 // Light control only valid for the QX5 Microscope.
774 if(cam
->params
.pnp_id
.product
!= 0x151)
775 c
->flags
|= V4L2_CTRL_FLAG_DISABLED
;
784 /******************************************************************************
788 * V4L2 query possible control variables
790 *****************************************************************************/
792 static int cpia2_querymenu(struct file
*file
, void *fh
, struct v4l2_querymenu
*m
)
794 struct camera_data
*cam
= video_drvdata(file
);
797 case CPIA2_CID_FLICKER_MODE
:
798 if (m
->index
>= NUM_FLICKER_CONTROLS
)
801 strcpy(m
->name
, flicker_controls
[m
->index
].name
);
803 case CPIA2_CID_FRAMERATE
:
805 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
806 if(cam
->params
.pnp_id
.device_type
== DEVICE_STV_672
&&
807 cam
->params
.version
.sensor_flags
==CPIA2_VP_SENSOR_FLAGS_500
){
810 for(i
=0; i
<maximum
; ++i
) {
811 if(framerate_controls
[i
].value
==
812 CPIA2_VP_FRAMERATE_15
)
816 if (m
->index
> maximum
)
819 strcpy(m
->name
, framerate_controls
[m
->index
].name
);
822 case CPIA2_CID_LIGHTS
:
823 if (m
->index
>= NUM_LIGHTS_CONTROLS
)
826 strcpy(m
->name
, lights_controls
[m
->index
].name
);
835 /******************************************************************************
839 * V4L2 get the value of a control variable
841 *****************************************************************************/
843 static int cpia2_g_ctrl(struct file
*file
, void *fh
, struct v4l2_control
*c
)
845 struct camera_data
*cam
= video_drvdata(file
);
848 case V4L2_CID_BRIGHTNESS
:
849 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_BRIGHTNESS
,
851 c
->value
= cam
->params
.color_params
.brightness
;
853 case V4L2_CID_CONTRAST
:
854 cpia2_do_command(cam
, CPIA2_CMD_GET_CONTRAST
,
856 c
->value
= cam
->params
.color_params
.contrast
;
858 case V4L2_CID_SATURATION
:
859 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_SATURATION
,
861 c
->value
= cam
->params
.color_params
.saturation
;
864 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
866 c
->value
= (cam
->params
.vp_params
.user_effects
&
867 CPIA2_VP_USER_EFFECTS_MIRROR
) != 0;
870 cpia2_do_command(cam
, CPIA2_CMD_GET_USER_EFFECTS
,
872 c
->value
= (cam
->params
.vp_params
.user_effects
&
873 CPIA2_VP_USER_EFFECTS_FLIP
) != 0;
875 case CPIA2_CID_TARGET_KB
:
876 c
->value
= cam
->params
.vc_params
.target_kb
;
879 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
881 c
->value
= cam
->params
.vp_params
.gpio_data
;
883 case CPIA2_CID_FLICKER_MODE
:
886 cpia2_do_command(cam
, CPIA2_CMD_GET_FLICKER_MODES
,
888 if(cam
->params
.flicker_control
.cam_register
&
889 CPIA2_VP_FLICKER_MODES_NEVER_FLICKER
) {
890 mode
= NEVER_FLICKER
;
892 if(cam
->params
.flicker_control
.cam_register
&
893 CPIA2_VP_FLICKER_MODES_50HZ
) {
899 for(i
=0; i
<NUM_FLICKER_CONTROLS
; i
++) {
900 if(flicker_controls
[i
].value
== mode
) {
905 if(i
== NUM_FLICKER_CONTROLS
)
909 case CPIA2_CID_FRAMERATE
:
911 int maximum
= NUM_FRAMERATE_CONTROLS
- 1;
913 for(i
=0; i
<= maximum
; i
++) {
914 if(cam
->params
.vp_params
.frame_rate
==
915 framerate_controls
[i
].value
)
923 case CPIA2_CID_USB_ALT
:
924 c
->value
= cam
->params
.camera_state
.stream_mode
;
926 case CPIA2_CID_LIGHTS
:
929 cpia2_do_command(cam
, CPIA2_CMD_GET_VP_GPIO_DATA
,
931 for(i
=0; i
<NUM_LIGHTS_CONTROLS
; i
++) {
932 if((cam
->params
.vp_params
.gpio_data
&GPIO_LIGHTS_MASK
) ==
933 lights_controls
[i
].value
) {
937 if(i
== NUM_LIGHTS_CONTROLS
)
942 case CPIA2_CID_RESET_CAMERA
:
948 DBG("Get control id:%d, value:%d\n", c
->id
, c
->value
);
953 /******************************************************************************
957 * V4L2 set the value of a control variable
959 *****************************************************************************/
961 static int cpia2_s_ctrl(struct file
*file
, void *fh
, struct v4l2_control
*c
)
963 struct camera_data
*cam
= video_drvdata(file
);
967 DBG("Set control id:%d, value:%d\n", c
->id
, c
->value
);
969 /* Check that the value is in range */
970 for(i
=0; i
<NUM_CONTROLS
; i
++) {
971 if(c
->id
== controls
[i
].id
) {
972 if(c
->value
< controls
[i
].minimum
||
973 c
->value
> controls
[i
].maximum
) {
979 if(i
== NUM_CONTROLS
)
983 case V4L2_CID_BRIGHTNESS
:
984 cpia2_set_brightness(cam
, c
->value
);
986 case V4L2_CID_CONTRAST
:
987 cpia2_set_contrast(cam
, c
->value
);
989 case V4L2_CID_SATURATION
:
990 cpia2_set_saturation(cam
, c
->value
);
993 cpia2_set_property_mirror(cam
, c
->value
);
996 cpia2_set_property_flip(cam
, c
->value
);
998 case CPIA2_CID_TARGET_KB
:
999 retval
= cpia2_set_target_kb(cam
, c
->value
);
1001 case CPIA2_CID_GPIO
:
1002 retval
= cpia2_set_gpio(cam
, c
->value
);
1004 case CPIA2_CID_FLICKER_MODE
:
1005 retval
= cpia2_set_flicker_mode(cam
,
1006 flicker_controls
[c
->value
].value
);
1008 case CPIA2_CID_FRAMERATE
:
1009 retval
= cpia2_set_fps(cam
, framerate_controls
[c
->value
].value
);
1011 case CPIA2_CID_USB_ALT
:
1012 retval
= cpia2_usb_change_streaming_alternate(cam
, c
->value
);
1014 case CPIA2_CID_LIGHTS
:
1015 retval
= cpia2_set_gpio(cam
, lights_controls
[c
->value
].value
);
1017 case CPIA2_CID_RESET_CAMERA
:
1018 cpia2_usb_stream_pause(cam
);
1019 cpia2_reset_camera(cam
);
1020 cpia2_usb_stream_resume(cam
);
1029 /******************************************************************************
1033 * V4L2 get the JPEG compression parameters
1035 *****************************************************************************/
1037 static int cpia2_g_jpegcomp(struct file
*file
, void *fh
, struct v4l2_jpegcompression
*parms
)
1039 struct camera_data
*cam
= video_drvdata(file
);
1041 memset(parms
, 0, sizeof(*parms
));
1043 parms
->quality
= 80; // TODO: Can this be made meaningful?
1045 parms
->jpeg_markers
= V4L2_JPEG_MARKER_DQT
| V4L2_JPEG_MARKER_DRI
;
1046 if(!cam
->params
.compression
.inhibit_htables
) {
1047 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_DHT
;
1050 parms
->APPn
= cam
->APPn
;
1051 parms
->APP_len
= cam
->APP_len
;
1052 if(cam
->APP_len
> 0) {
1053 memcpy(parms
->APP_data
, cam
->APP_data
, cam
->APP_len
);
1054 parms
->jpeg_markers
|= V4L2_JPEG_MARKER_APP
;
1057 parms
->COM_len
= cam
->COM_len
;
1058 if(cam
->COM_len
> 0) {
1059 memcpy(parms
->COM_data
, cam
->COM_data
, cam
->COM_len
);
1060 parms
->jpeg_markers
|= JPEG_MARKER_COM
;
1063 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
1064 parms
->APP_len
, parms
->COM_len
);
1069 /******************************************************************************
1073 * V4L2 set the JPEG compression parameters
1074 * NOTE: quality and some jpeg_markers are ignored.
1076 *****************************************************************************/
1078 static int cpia2_s_jpegcomp(struct file
*file
, void *fh
, struct v4l2_jpegcompression
*parms
)
1080 struct camera_data
*cam
= video_drvdata(file
);
1082 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
1083 parms
->APP_len
, parms
->COM_len
);
1085 cam
->params
.compression
.inhibit_htables
=
1086 !(parms
->jpeg_markers
& V4L2_JPEG_MARKER_DHT
);
1088 if(parms
->APP_len
!= 0) {
1089 if(parms
->APP_len
> 0 &&
1090 parms
->APP_len
<= sizeof(cam
->APP_data
) &&
1091 parms
->APPn
>= 0 && parms
->APPn
<= 15) {
1092 cam
->APPn
= parms
->APPn
;
1093 cam
->APP_len
= parms
->APP_len
;
1094 memcpy(cam
->APP_data
, parms
->APP_data
, parms
->APP_len
);
1096 LOG("Bad APPn Params n=%d len=%d\n",
1097 parms
->APPn
, parms
->APP_len
);
1104 if(parms
->COM_len
!= 0) {
1105 if(parms
->COM_len
> 0 &&
1106 parms
->COM_len
<= sizeof(cam
->COM_data
)) {
1107 cam
->COM_len
= parms
->COM_len
;
1108 memcpy(cam
->COM_data
, parms
->COM_data
, parms
->COM_len
);
1110 LOG("Bad COM_len=%d\n", parms
->COM_len
);
1118 /******************************************************************************
1122 * V4L2 Initiate memory mapping.
1123 * NOTE: The user's request is ignored. For now the buffers are fixed.
1125 *****************************************************************************/
1127 static int cpia2_reqbufs(struct file
*file
, void *fh
, struct v4l2_requestbuffers
*req
)
1129 struct camera_data
*cam
= video_drvdata(file
);
1131 if(req
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1132 req
->memory
!= V4L2_MEMORY_MMAP
)
1135 DBG("REQBUFS requested:%d returning:%d\n", req
->count
, cam
->num_frames
);
1136 req
->count
= cam
->num_frames
;
1137 memset(&req
->reserved
, 0, sizeof(req
->reserved
));
1142 /******************************************************************************
1146 * V4L2 Query memory buffer status.
1148 *****************************************************************************/
1150 static int cpia2_querybuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1152 struct camera_data
*cam
= video_drvdata(file
);
1154 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1155 buf
->index
> cam
->num_frames
)
1158 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1159 buf
->length
= cam
->frame_size
;
1161 buf
->memory
= V4L2_MEMORY_MMAP
;
1164 buf
->flags
= V4L2_BUF_FLAG_MAPPED
;
1168 switch (cam
->buffers
[buf
->index
].status
) {
1173 buf
->flags
= V4L2_BUF_FLAG_QUEUED
;
1176 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1177 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1178 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1179 buf
->flags
= V4L2_BUF_FLAG_DONE
;
1183 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
1184 buf
->index
, buf
->m
.offset
, buf
->flags
, buf
->sequence
,
1190 /******************************************************************************
1194 * V4L2 User is freeing buffer
1196 *****************************************************************************/
1198 static int cpia2_qbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1200 struct camera_data
*cam
= video_drvdata(file
);
1202 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1203 buf
->memory
!= V4L2_MEMORY_MMAP
||
1204 buf
->index
> cam
->num_frames
)
1207 DBG("QBUF #%d\n", buf
->index
);
1209 if(cam
->buffers
[buf
->index
].status
== FRAME_READY
)
1210 cam
->buffers
[buf
->index
].status
= FRAME_EMPTY
;
1215 /******************************************************************************
1217 * find_earliest_filled_buffer
1219 * Helper for ioctl_dqbuf. Find the next ready buffer.
1221 *****************************************************************************/
1223 static int find_earliest_filled_buffer(struct camera_data
*cam
)
1227 for (i
=0; i
<cam
->num_frames
; i
++) {
1228 if(cam
->buffers
[i
].status
== FRAME_READY
) {
1232 /* find which buffer is earlier */
1233 struct timeval
*tv1
, *tv2
;
1234 tv1
= &cam
->buffers
[i
].timestamp
;
1235 tv2
= &cam
->buffers
[found
].timestamp
;
1236 if(tv1
->tv_sec
< tv2
->tv_sec
||
1237 (tv1
->tv_sec
== tv2
->tv_sec
&&
1238 tv1
->tv_usec
< tv2
->tv_usec
))
1246 /******************************************************************************
1250 * V4L2 User is asking for a filled buffer.
1252 *****************************************************************************/
1254 static int cpia2_dqbuf(struct file
*file
, void *fh
, struct v4l2_buffer
*buf
)
1256 struct camera_data
*cam
= video_drvdata(file
);
1259 if(buf
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
||
1260 buf
->memory
!= V4L2_MEMORY_MMAP
)
1263 frame
= find_earliest_filled_buffer(cam
);
1265 if(frame
< 0 && file
->f_flags
&O_NONBLOCK
)
1269 /* Wait for a frame to become available */
1270 struct framebuf
*cb
=cam
->curbuff
;
1271 mutex_unlock(&cam
->v4l2_lock
);
1272 wait_event_interruptible(cam
->wq_stream
,
1274 (cb
=cam
->curbuff
)->status
== FRAME_READY
);
1275 mutex_lock(&cam
->v4l2_lock
);
1276 if (signal_pending(current
))
1277 return -ERESTARTSYS
;
1285 buf
->bytesused
= cam
->buffers
[buf
->index
].length
;
1286 buf
->flags
= V4L2_BUF_FLAG_MAPPED
| V4L2_BUF_FLAG_DONE
;
1287 buf
->field
= V4L2_FIELD_NONE
;
1288 buf
->timestamp
= cam
->buffers
[buf
->index
].timestamp
;
1289 buf
->sequence
= cam
->buffers
[buf
->index
].seq
;
1290 buf
->m
.offset
= cam
->buffers
[buf
->index
].data
- cam
->frame_buffer
;
1291 buf
->length
= cam
->frame_size
;
1294 memset(&buf
->timecode
, 0, sizeof(buf
->timecode
));
1296 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf
->index
,
1297 cam
->buffers
[buf
->index
].status
, buf
->sequence
, buf
->bytesused
);
1302 static int cpia2_g_priority(struct file
*file
, void *_fh
, enum v4l2_priority
*p
)
1304 struct cpia2_fh
*fh
= _fh
;
1310 static int cpia2_s_priority(struct file
*file
, void *_fh
, enum v4l2_priority prio
)
1312 struct camera_data
*cam
= video_drvdata(file
);
1313 struct cpia2_fh
*fh
= _fh
;
1315 if (cam
->streaming
&& prio
!= fh
->prio
&&
1316 fh
->prio
== V4L2_PRIORITY_RECORD
)
1317 /* Can't drop record priority while streaming */
1320 if (prio
== V4L2_PRIORITY_RECORD
&& prio
!= fh
->prio
&&
1321 v4l2_prio_max(&cam
->prio
) == V4L2_PRIORITY_RECORD
)
1322 /* Only one program can record at a time */
1324 return v4l2_prio_change(&cam
->prio
, &fh
->prio
, prio
);
1327 static int cpia2_streamon(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1329 struct camera_data
*cam
= video_drvdata(file
);
1331 DBG("VIDIOC_STREAMON, streaming=%d\n", cam
->streaming
);
1332 if (!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1335 if (!cam
->streaming
)
1336 return cpia2_usb_stream_start(cam
,
1337 cam
->params
.camera_state
.stream_mode
);
1341 static int cpia2_streamoff(struct file
*file
, void *fh
, enum v4l2_buf_type type
)
1343 struct camera_data
*cam
= video_drvdata(file
);
1345 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam
->streaming
);
1346 if (!cam
->mmapped
|| type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1350 return cpia2_usb_stream_stop(cam
);
1354 /******************************************************************************
1358 *****************************************************************************/
1359 static int cpia2_mmap(struct file
*file
, struct vm_area_struct
*area
)
1361 struct camera_data
*cam
= video_drvdata(file
);
1364 /* Priority check */
1365 struct cpia2_fh
*fh
= file
->private_data
;
1366 if(fh
->prio
!= V4L2_PRIORITY_RECORD
) {
1370 retval
= cpia2_remap_buffer(cam
, area
);
1377 /******************************************************************************
1379 * reset_camera_struct_v4l
1381 * Sets all values to the defaults
1382 *****************************************************************************/
1383 static void reset_camera_struct_v4l(struct camera_data
*cam
)
1385 cam
->width
= cam
->params
.roi
.width
;
1386 cam
->height
= cam
->params
.roi
.height
;
1388 cam
->frame_size
= buffer_size
;
1389 cam
->num_frames
= num_buffers
;
1392 cam
->params
.flicker_control
.flicker_mode_req
= flicker_mode
;
1393 cam
->params
.flicker_control
.mains_frequency
= flicker_freq
;
1396 cam
->params
.camera_state
.stream_mode
= alternate
;
1398 cam
->pixelformat
= V4L2_PIX_FMT_JPEG
;
1399 v4l2_prio_init(&cam
->prio
);
1402 static const struct v4l2_ioctl_ops cpia2_ioctl_ops
= {
1403 .vidioc_querycap
= cpia2_querycap
,
1404 .vidioc_enum_input
= cpia2_enum_input
,
1405 .vidioc_g_input
= cpia2_g_input
,
1406 .vidioc_s_input
= cpia2_s_input
,
1407 .vidioc_enum_fmt_vid_cap
= cpia2_enum_fmt_vid_cap
,
1408 .vidioc_g_fmt_vid_cap
= cpia2_g_fmt_vid_cap
,
1409 .vidioc_s_fmt_vid_cap
= cpia2_s_fmt_vid_cap
,
1410 .vidioc_try_fmt_vid_cap
= cpia2_try_fmt_vid_cap
,
1411 .vidioc_queryctrl
= cpia2_queryctrl
,
1412 .vidioc_querymenu
= cpia2_querymenu
,
1413 .vidioc_g_ctrl
= cpia2_g_ctrl
,
1414 .vidioc_s_ctrl
= cpia2_s_ctrl
,
1415 .vidioc_g_jpegcomp
= cpia2_g_jpegcomp
,
1416 .vidioc_s_jpegcomp
= cpia2_s_jpegcomp
,
1417 .vidioc_cropcap
= cpia2_cropcap
,
1418 .vidioc_reqbufs
= cpia2_reqbufs
,
1419 .vidioc_querybuf
= cpia2_querybuf
,
1420 .vidioc_qbuf
= cpia2_qbuf
,
1421 .vidioc_dqbuf
= cpia2_dqbuf
,
1422 .vidioc_streamon
= cpia2_streamon
,
1423 .vidioc_streamoff
= cpia2_streamoff
,
1424 .vidioc_g_priority
= cpia2_g_priority
,
1425 .vidioc_s_priority
= cpia2_s_priority
,
1426 .vidioc_default
= cpia2_default
,
1430 * The v4l video device structure initialized for this device
1432 static const struct v4l2_file_operations cpia2_fops
= {
1433 .owner
= THIS_MODULE
,
1435 .release
= cpia2_close
,
1436 .read
= cpia2_v4l_read
,
1437 .poll
= cpia2_v4l_poll
,
1438 .unlocked_ioctl
= video_ioctl2
,
1442 static struct video_device cpia2_template
= {
1443 /* I could not find any place for the old .initialize initializer?? */
1444 .name
= "CPiA2 Camera",
1445 .fops
= &cpia2_fops
,
1446 .ioctl_ops
= &cpia2_ioctl_ops
,
1447 .release
= video_device_release
,
1450 /******************************************************************************
1452 * cpia2_register_camera
1454 *****************************************************************************/
1455 int cpia2_register_camera(struct camera_data
*cam
)
1457 cam
->vdev
= video_device_alloc();
1461 memcpy(cam
->vdev
, &cpia2_template
, sizeof(cpia2_template
));
1462 video_set_drvdata(cam
->vdev
, cam
);
1463 cam
->vdev
->lock
= &cam
->v4l2_lock
;
1465 reset_camera_struct_v4l(cam
);
1467 /* register v4l device */
1468 if (video_register_device(cam
->vdev
, VFL_TYPE_GRABBER
, video_nr
) < 0) {
1469 ERR("video_register_device failed\n");
1470 video_device_release(cam
->vdev
);
1477 /******************************************************************************
1479 * cpia2_unregister_camera
1481 *****************************************************************************/
1482 void cpia2_unregister_camera(struct camera_data
*cam
)
1484 if (!cam
->open_count
) {
1485 video_unregister_device(cam
->vdev
);
1487 LOG("%s removed while open, deferring "
1488 "video_unregister_device\n",
1489 video_device_node_name(cam
->vdev
));
1493 /******************************************************************************
1497 * Make sure that all user-supplied parameters are sensible
1498 *****************************************************************************/
1499 static void __init
check_parameters(void)
1501 if(buffer_size
< PAGE_SIZE
) {
1502 buffer_size
= PAGE_SIZE
;
1503 LOG("buffer_size too small, setting to %d\n", buffer_size
);
1504 } else if(buffer_size
> 1024*1024) {
1505 /* arbitrary upper limiit */
1506 buffer_size
= 1024*1024;
1507 LOG("buffer_size ridiculously large, setting to %d\n",
1510 buffer_size
+= PAGE_SIZE
-1;
1511 buffer_size
&= ~(PAGE_SIZE
-1);
1514 if(num_buffers
< 1) {
1516 LOG("num_buffers too small, setting to %d\n", num_buffers
);
1517 } else if(num_buffers
> VIDEO_MAX_FRAME
) {
1518 num_buffers
= VIDEO_MAX_FRAME
;
1519 LOG("num_buffers too large, setting to %d\n", num_buffers
);
1522 if(alternate
< USBIF_ISO_1
|| alternate
> USBIF_ISO_6
) {
1523 alternate
= DEFAULT_ALT
;
1524 LOG("alternate specified is invalid, using %d\n", alternate
);
1527 if (flicker_mode
!= NEVER_FLICKER
&& flicker_mode
!= ANTI_FLICKER_ON
) {
1528 flicker_mode
= NEVER_FLICKER
;
1529 LOG("Flicker mode specified is invalid, using %d\n",
1533 if (flicker_freq
!= FLICKER_50
&& flicker_freq
!= FLICKER_60
) {
1534 flicker_freq
= FLICKER_60
;
1535 LOG("Flicker mode specified is invalid, using %d\n",
1539 if(video_nr
< -1 || video_nr
> 64) {
1541 LOG("invalid video_nr specified, must be -1 to 64\n");
1544 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1545 num_buffers
, buffer_size
, alternate
);
1548 /************ Module Stuff ***************/
1551 /******************************************************************************
1553 * cpia2_init/module_init
1555 *****************************************************************************/
1556 static int __init
cpia2_init(void)
1559 ABOUT
, CPIA_VERSION
);
1566 /******************************************************************************
1568 * cpia2_exit/module_exit
1570 *****************************************************************************/
1571 static void __exit
cpia2_exit(void)
1573 cpia2_usb_cleanup();
1574 schedule_timeout(2 * HZ
);
1577 module_init(cpia2_init
);
1578 module_exit(cpia2_exit
);