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>
39 #include <linux/videodev2.h>
46 static int video_nr = -1;
50 static int buffer_size = 68 * 1024;
52 MODULE_PARM_DESC(buffer_size,
"Size for each frame buffer in bytes (default 68k)");
54 static int num_buffers = 3;
65 static int flicker_mode;
76 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
77 #define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
84 static int cpia2_open(
struct file *
file)
95 if (v4l2_fh_is_singular_file(file)) {
124 static int cpia2_close(
struct file *
file)
130 if (video_is_registered(&cam->
vdev) && v4l2_fh_is_singular_file(file)) {
153 static ssize_t cpia2_v4l_read(
struct file *file,
char __user *
buf,
size_t count,
209 if (!video_is_registered(&cam->
vdev))
222 static int cpia2_querycap(
struct file *file,
void *fh,
struct v4l2_capability *
vc)
228 if (cam->
params.pnp_id.product == 0x151)
232 switch (cam->
params.pnp_id.device_type) {
243 switch (cam->
params.version.sensor_flags) {
284 static int cpia2_enum_input(
struct file *file,
void *fh,
struct v4l2_input *
i)
293 static int cpia2_g_input(
struct file *file,
void *fh,
unsigned int *i)
299 static int cpia2_s_input(
struct file *file,
void *fh,
unsigned int i)
312 static int cpia2_enum_fmt_vid_cap(
struct file *file,
void *fh,
317 if (index < 0 || index > 1)
348 static int cpia2_try_fmt_vid_cap(
struct file *file,
void *fh,
358 f->
fmt.
pix.bytesperline = 0;
410 static int cpia2_s_fmt_vid_cap(
struct file *file,
void *_fh,
416 err = cpia2_try_fmt_vid_cap(file, _fh, f);
424 cam->
params.compression.inhibit_htables = 0;
430 DBG(
"Requested width = %d, height = %d\n",
443 if ((err =
sync(cam, frame)) < 0)
460 static int cpia2_g_fmt_vid_cap(
struct file *file,
void *fh,
469 f->
fmt.
pix.bytesperline = 0;
486 static int cpia2_cropcap(
struct file *file,
void *fh,
struct v4l2_cropcap *
c)
521 static int cpia2_g_parm(
struct file *file,
void *fh,
struct v4l2_streamparm *
p)
532 for (i = 0; i <
ARRAY_SIZE(framerate_controls); i++)
533 if (cam->
params.vp_params.frame_rate == framerate_controls[i].
value) {
540 static int cpia2_s_parm(
struct file *file,
void *fh,
struct v4l2_streamparm *p)
549 ret = cpia2_g_parm(file, fh, p);
557 for (i = 0; i <=
max; i++) {
572 static const struct {
575 } cpia2_framesizes[] = {
586 static int cpia2_enum_framesizes(
struct file *file,
void *fh,
596 fsize->
discrete.width = cpia2_framesizes[fsize->
index].width;
597 fsize->
discrete.height = cpia2_framesizes[fsize->
index].height;
602 static int cpia2_enum_frameintervals(
struct file *file,
void *fh,
617 if (fival->
index > max)
619 for (i = 0; i <
ARRAY_SIZE(cpia2_framesizes); i++)
620 if (fival->
width == cpia2_framesizes[i].width &&
621 fival->
height == cpia2_framesizes[i].height)
642 static const int flicker_table[] = {
648 DBG(
"Set control id:%d, value:%d\n", ctrl->
id, ctrl->
val);
672 cam->
params.compression.inhibit_htables =
676 cam->
params.vc_params.quality = ctrl->
val;
679 cam->
params.camera_state.stream_mode = ctrl->
val;
700 memset(parms, 0,
sizeof(*parms));
705 if(!cam->
params.compression.inhibit_htables) {
722 DBG(
"G_JPEGCOMP APP_len:%d COM_len:%d\n",
737 static int cpia2_s_jpegcomp(
struct file *file,
void *fh,
742 DBG(
"S_JPEGCOMP APP_len:%d COM_len:%d\n",
745 cam->
params.compression.inhibit_htables =
751 parms->
APPn >= 0 && parms->
APPn <= 15) {
756 LOG(
"Bad APPn Params n=%d len=%d\n",
810 static int cpia2_querybuf(
struct file *file,
void *fh,
struct v4l2_buffer *buf)
843 DBG(
"QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
858 static int cpia2_qbuf(
struct file *file,
void *fh,
struct v4l2_buffer *buf)
883 static int find_earliest_filled_buffer(
struct camera_data *cam)
895 tv2 = &cam->
buffers[found].timestamp;
914 static int cpia2_dqbuf(
struct file *file,
void *fh,
struct v4l2_buffer *buf)
923 frame = find_earliest_filled_buffer(cam);
933 !video_is_registered(&cam->
vdev) ||
938 if (!video_is_registered(&cam->
vdev))
956 DBG(
"DQBUF #%d status:%d seq:%d length:%d\n", buf->
index,
973 cam->
params.camera_state.stream_mode);
980 static int cpia2_streamoff(
struct file *file,
void *fh,
enum v4l2_buf_type type)
985 DBG(
"VIDIOC_STREAMOFF, streaming=%d\n", cam->
streaming);
1002 static int cpia2_mmap(
struct file *file,
struct vm_area_struct *area)
1023 static void reset_camera_struct_v4l(
struct camera_data *cam)
1032 cam->
params.flicker_control.flicker_mode_req = flicker_mode;
1035 cam->
params.camera_state.stream_mode = alternate;
1041 .vidioc_querycap = cpia2_querycap,
1042 .vidioc_enum_input = cpia2_enum_input,
1043 .vidioc_g_input = cpia2_g_input,
1044 .vidioc_s_input = cpia2_s_input,
1045 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1046 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1047 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1048 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
1049 .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1050 .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1051 .vidioc_cropcap = cpia2_cropcap,
1052 .vidioc_reqbufs = cpia2_reqbufs,
1053 .vidioc_querybuf = cpia2_querybuf,
1054 .vidioc_qbuf = cpia2_qbuf,
1055 .vidioc_dqbuf = cpia2_dqbuf,
1056 .vidioc_streamon = cpia2_streamon,
1057 .vidioc_streamoff = cpia2_streamoff,
1058 .vidioc_s_parm = cpia2_s_parm,
1059 .vidioc_g_parm = cpia2_g_parm,
1060 .vidioc_enum_framesizes = cpia2_enum_framesizes,
1061 .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1072 .release = cpia2_close,
1073 .read = cpia2_v4l_read,
1074 .poll = cpia2_v4l_poll,
1081 .name =
"CPiA2 Camera",
1082 .fops = &cpia2_fops,
1083 .ioctl_ops = &cpia2_ioctl_ops,
1098 .s_ctrl = cpia2_s_ctrl,
1110 .
ops = &cpia2_ctrl_ops,
1112 .name =
"USB Alternate",
1138 cpia2_usb_alt.
def = alternate;
1150 if (cam->
params.pnp_id.product == 0x151) {
1164 cam->
vdev = cpia2_template;
1165 video_set_drvdata(&cam->
vdev, cam);
1167 cam->
vdev.ctrl_handler = hdl;
1171 reset_camera_struct_v4l(cam);
1175 ERR(
"video_register_device failed\n");
1198 static void __init check_parameters(
void)
1202 LOG(
"buffer_size too small, setting to %d\n", buffer_size);
1203 }
else if(buffer_size > 1024*1024) {
1205 buffer_size = 1024*1024;
1206 LOG(
"buffer_size ridiculously large, setting to %d\n",
1213 if(num_buffers < 1) {
1215 LOG(
"num_buffers too small, setting to %d\n", num_buffers);
1218 LOG(
"num_buffers too large, setting to %d\n", num_buffers);
1223 LOG(
"alternate specified is invalid, using %d\n", alternate);
1228 LOG(
"Flicker mode specified is invalid, using %d\n",
1232 DBG(
"Using %d buffers, each %d bytes, alternate=%d\n",
1233 num_buffers, buffer_size, alternate);
1244 static int __init cpia2_init(
void)
1259 static void __exit cpia2_exit(
void)