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 * Stripped of 2.4 stuff ready for main kernel submit by
25 * Alan Cox <alan@lxorguk.ukuu.org.uk>
26 ****************************************************************************/
28 #define CPIA_VERSION "3.0.1"
30 #include <linux/module.h>
31 #include <linux/time.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/videodev2.h>
36 #include <linux/stringify.h>
37 #include <media/v4l2-ioctl.h>
38 #include <media/v4l2-event.h>
42 static int video_nr = -1;
43 module_param(video_nr, int, 0);
44 MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
46 static int buffer_size = 68 * 1024;
47 module_param(buffer_size, int, 0);
48 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
50 static int num_buffers = 3;
51 module_param(num_buffers, int, 0);
52 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
53 __stringify(VIDEO_MAX_FRAME) ", default 3)");
55 static int alternate = DEFAULT_ALT;
56 module_param(alternate, int, 0);
57 MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
58 __stringify(USBIF_ISO_6) ", default "
59 __stringify(DEFAULT_ALT) ")");
61 static int flicker_mode;
62 module_param(flicker_mode, int, 0);
63 MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or "
64 __stringify(60) ", default 0)");
66 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
67 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
68 MODULE_SUPPORTED_DEVICE("video");
69 MODULE_LICENSE("GPL");
70 MODULE_VERSION(CPIA_VERSION);
72 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
73 #define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
75 /******************************************************************************
79 *****************************************************************************/
80 static int cpia2_open(struct file *file)
82 struct camera_data *cam = video_drvdata(file);
85 if (mutex_lock_interruptible(&cam->v4l2_lock))
87 retval = v4l2_fh_open(file);
91 if (v4l2_fh_is_singular_file(file)) {
92 if (cpia2_allocate_buffers(cam)) {
93 v4l2_fh_release(file);
98 /* reset the camera */
99 if (cpia2_reset_camera(cam) < 0) {
100 v4l2_fh_release(file);
109 cpia2_dbg_dump_registers(cam);
111 mutex_unlock(&cam->v4l2_lock);
115 /******************************************************************************
119 *****************************************************************************/
120 static int cpia2_close(struct file *file)
122 struct video_device *dev = video_devdata(file);
123 struct camera_data *cam = video_get_drvdata(dev);
125 mutex_lock(&cam->v4l2_lock);
126 if (video_is_registered(&cam->vdev) && v4l2_fh_is_singular_file(file)) {
127 cpia2_usb_stream_stop(cam);
129 /* save camera state for later open */
130 cpia2_save_camera_state(cam);
132 cpia2_set_low_power(cam);
133 cpia2_free_buffers(cam);
136 if (cam->stream_fh == file->private_data) {
137 cam->stream_fh = NULL;
140 mutex_unlock(&cam->v4l2_lock);
141 return v4l2_fh_release(file);
144 /******************************************************************************
148 *****************************************************************************/
149 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
152 struct camera_data *cam = video_drvdata(file);
153 int noblock = file->f_flags&O_NONBLOCK;
159 if (mutex_lock_interruptible(&cam->v4l2_lock))
161 ret = cpia2_read(cam, buf, count, noblock);
162 mutex_unlock(&cam->v4l2_lock);
167 /******************************************************************************
171 *****************************************************************************/
172 static __poll_t cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
174 struct camera_data *cam = video_drvdata(filp);
177 mutex_lock(&cam->v4l2_lock);
178 res = cpia2_poll(cam, filp, wait);
179 mutex_unlock(&cam->v4l2_lock);
184 static int sync(struct camera_data *cam, int frame_nr)
186 struct framebuf *frame = &cam->buffers[frame_nr];
189 if (frame->status == FRAME_READY)
192 if (!cam->streaming) {
193 frame->status = FRAME_READY;
198 mutex_unlock(&cam->v4l2_lock);
199 wait_event_interruptible(cam->wq_stream,
201 frame->status == FRAME_READY);
202 mutex_lock(&cam->v4l2_lock);
203 if (signal_pending(current))
205 if (!video_is_registered(&cam->vdev))
210 /******************************************************************************
214 * V4L2 device capabilities
216 *****************************************************************************/
218 static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
220 struct camera_data *cam = video_drvdata(file);
222 strcpy(vc->driver, "cpia2");
224 if (cam->params.pnp_id.product == 0x151)
225 strcpy(vc->card, "QX5 Microscope");
227 strcpy(vc->card, "CPiA2 Camera");
228 switch (cam->params.pnp_id.device_type) {
230 strcat(vc->card, " (672/");
233 strcat(vc->card, " (676/");
236 strcat(vc->card, " (XXX/");
239 switch (cam->params.version.sensor_flags) {
240 case CPIA2_VP_SENSOR_FLAGS_404:
241 strcat(vc->card, "404)");
243 case CPIA2_VP_SENSOR_FLAGS_407:
244 strcat(vc->card, "407)");
246 case CPIA2_VP_SENSOR_FLAGS_409:
247 strcat(vc->card, "409)");
249 case CPIA2_VP_SENSOR_FLAGS_410:
250 strcat(vc->card, "410)");
252 case CPIA2_VP_SENSOR_FLAGS_500:
253 strcat(vc->card, "500)");
256 strcat(vc->card, "XXX)");
260 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
261 memset(vc->bus_info,0, sizeof(vc->bus_info));
263 vc->device_caps = V4L2_CAP_VIDEO_CAPTURE |
266 vc->capabilities = vc->device_caps |
267 V4L2_CAP_DEVICE_CAPS;
272 /******************************************************************************
276 * V4L2 input get/set/enumerate
278 *****************************************************************************/
280 static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
284 strcpy(i->name, "Camera");
285 i->type = V4L2_INPUT_TYPE_CAMERA;
289 static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
295 static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
297 return i ? -EINVAL : 0;
300 /******************************************************************************
304 * V4L2 format enumerate
306 *****************************************************************************/
308 static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
309 struct v4l2_fmtdesc *f)
311 int index = f->index;
313 if (index < 0 || index > 1)
316 memset(f, 0, sizeof(*f));
318 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
319 f->flags = V4L2_FMT_FLAG_COMPRESSED;
322 strcpy(f->description, "MJPEG");
323 f->pixelformat = V4L2_PIX_FMT_MJPEG;
326 strcpy(f->description, "JPEG");
327 f->pixelformat = V4L2_PIX_FMT_JPEG;
336 /******************************************************************************
342 *****************************************************************************/
344 static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
345 struct v4l2_format *f)
347 struct camera_data *cam = video_drvdata(file);
349 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
350 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
353 f->fmt.pix.field = V4L2_FIELD_NONE;
354 f->fmt.pix.bytesperline = 0;
355 f->fmt.pix.sizeimage = cam->frame_size;
356 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
359 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
361 f->fmt.pix.width = 640;
362 f->fmt.pix.height = 480;
365 f->fmt.pix.width = 352;
366 f->fmt.pix.height = 288;
369 f->fmt.pix.width = 320;
370 f->fmt.pix.height = 240;
372 case VIDEOSIZE_288_216:
373 f->fmt.pix.width = 288;
374 f->fmt.pix.height = 216;
376 case VIDEOSIZE_256_192:
377 f->fmt.pix.width = 256;
378 f->fmt.pix.height = 192;
380 case VIDEOSIZE_224_168:
381 f->fmt.pix.width = 224;
382 f->fmt.pix.height = 168;
384 case VIDEOSIZE_192_144:
385 f->fmt.pix.width = 192;
386 f->fmt.pix.height = 144;
390 f->fmt.pix.width = 176;
391 f->fmt.pix.height = 144;
398 /******************************************************************************
404 *****************************************************************************/
406 static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
407 struct v4l2_format *f)
409 struct camera_data *cam = video_drvdata(file);
412 err = cpia2_try_fmt_vid_cap(file, _fh, f);
416 cam->pixelformat = f->fmt.pix.pixelformat;
418 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
419 * the missing Huffman table properly. */
420 cam->params.compression.inhibit_htables = 0;
421 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
423 /* we set the video window to something smaller or equal to what
424 * is requested by the user???
426 DBG("Requested width = %d, height = %d\n",
427 f->fmt.pix.width, f->fmt.pix.height);
428 if (f->fmt.pix.width != cam->width ||
429 f->fmt.pix.height != cam->height) {
430 cam->width = f->fmt.pix.width;
431 cam->height = f->fmt.pix.height;
432 cam->params.roi.width = f->fmt.pix.width;
433 cam->params.roi.height = f->fmt.pix.height;
434 cpia2_set_format(cam);
437 for (frame = 0; frame < cam->num_frames; ++frame) {
438 if (cam->buffers[frame].status == FRAME_READING)
439 if ((err = sync(cam, frame)) < 0)
442 cam->buffers[frame].status = FRAME_EMPTY;
448 /******************************************************************************
454 *****************************************************************************/
456 static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
457 struct v4l2_format *f)
459 struct camera_data *cam = video_drvdata(file);
461 f->fmt.pix.width = cam->width;
462 f->fmt.pix.height = cam->height;
463 f->fmt.pix.pixelformat = cam->pixelformat;
464 f->fmt.pix.field = V4L2_FIELD_NONE;
465 f->fmt.pix.bytesperline = 0;
466 f->fmt.pix.sizeimage = cam->frame_size;
467 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
473 /******************************************************************************
477 * V4L2 query cropping capabilities
478 * NOTE: cropping is currently disabled
480 *****************************************************************************/
482 static int cpia2_cropcap(struct file *file, void *fh, struct v4l2_cropcap *c)
484 struct camera_data *cam = video_drvdata(file);
486 if (c->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
491 c->bounds.width = cam->width;
492 c->bounds.height = cam->height;
495 c->defrect.width = cam->width;
496 c->defrect.height = cam->height;
497 c->pixelaspect.numerator = 1;
498 c->pixelaspect.denominator = 1;
503 struct framerate_info {
505 struct v4l2_fract period;
508 static const struct framerate_info framerate_controls[] = {
509 { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } },
510 { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } },
511 { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } },
512 { CPIA2_VP_FRAMERATE_15, { 1, 15 } },
513 { CPIA2_VP_FRAMERATE_25, { 1, 25 } },
514 { CPIA2_VP_FRAMERATE_30, { 1, 30 } },
517 static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
519 struct camera_data *cam = video_drvdata(file);
520 struct v4l2_captureparm *cap = &p->parm.capture;
523 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
526 cap->capability = V4L2_CAP_TIMEPERFRAME;
527 cap->readbuffers = cam->num_frames;
528 for (i = 0; i < ARRAY_SIZE(framerate_controls); i++)
529 if (cam->params.vp_params.frame_rate == framerate_controls[i].value) {
530 cap->timeperframe = framerate_controls[i].period;
536 static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
538 struct camera_data *cam = video_drvdata(file);
539 struct v4l2_captureparm *cap = &p->parm.capture;
540 struct v4l2_fract tpf = cap->timeperframe;
541 int max = ARRAY_SIZE(framerate_controls) - 1;
545 ret = cpia2_g_parm(file, fh, p);
546 if (ret || !tpf.denominator || !tpf.numerator)
549 /* Maximum 15 fps for this model */
550 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
551 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
553 for (i = 0; i <= max; i++) {
554 struct v4l2_fract f1 = tpf;
555 struct v4l2_fract f2 = framerate_controls[i].period;
557 f1.numerator *= f2.denominator;
558 f2.numerator *= f1.denominator;
559 if (f1.numerator >= f2.numerator)
564 cap->timeperframe = framerate_controls[i].period;
565 return cpia2_set_fps(cam, framerate_controls[i].value);
568 static const struct {
571 } cpia2_framesizes[] = {
582 static int cpia2_enum_framesizes(struct file *file, void *fh,
583 struct v4l2_frmsizeenum *fsize)
586 if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG &&
587 fsize->pixel_format != V4L2_PIX_FMT_JPEG)
589 if (fsize->index >= ARRAY_SIZE(cpia2_framesizes))
591 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
592 fsize->discrete.width = cpia2_framesizes[fsize->index].width;
593 fsize->discrete.height = cpia2_framesizes[fsize->index].height;
598 static int cpia2_enum_frameintervals(struct file *file, void *fh,
599 struct v4l2_frmivalenum *fival)
601 struct camera_data *cam = video_drvdata(file);
602 int max = ARRAY_SIZE(framerate_controls) - 1;
605 if (fival->pixel_format != V4L2_PIX_FMT_MJPEG &&
606 fival->pixel_format != V4L2_PIX_FMT_JPEG)
609 /* Maximum 15 fps for this model */
610 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
611 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
613 if (fival->index > max)
615 for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++)
616 if (fival->width == cpia2_framesizes[i].width &&
617 fival->height == cpia2_framesizes[i].height)
619 if (i == ARRAY_SIZE(cpia2_framesizes))
621 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
622 fival->discrete = framerate_controls[fival->index].period;
626 /******************************************************************************
630 * V4L2 set the value of a control variable
632 *****************************************************************************/
634 static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl)
636 struct camera_data *cam =
637 container_of(ctrl->handler, struct camera_data, hdl);
638 static const int flicker_table[] = {
644 DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val);
647 case V4L2_CID_BRIGHTNESS:
648 cpia2_set_brightness(cam, ctrl->val);
650 case V4L2_CID_CONTRAST:
651 cpia2_set_contrast(cam, ctrl->val);
653 case V4L2_CID_SATURATION:
654 cpia2_set_saturation(cam, ctrl->val);
657 cpia2_set_property_mirror(cam, ctrl->val);
660 cpia2_set_property_flip(cam, ctrl->val);
662 case V4L2_CID_POWER_LINE_FREQUENCY:
663 return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]);
664 case V4L2_CID_ILLUMINATORS_1:
665 return cpia2_set_gpio(cam, (cam->top_light->val << 6) |
666 (cam->bottom_light->val << 7));
667 case V4L2_CID_JPEG_ACTIVE_MARKER:
668 cam->params.compression.inhibit_htables =
669 !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT);
671 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
672 cam->params.vc_params.quality = ctrl->val;
674 case CPIA2_CID_USB_ALT:
675 cam->params.camera_state.stream_mode = ctrl->val;
684 /******************************************************************************
688 * V4L2 get the JPEG compression parameters
690 *****************************************************************************/
692 static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
694 struct camera_data *cam = video_drvdata(file);
696 memset(parms, 0, sizeof(*parms));
698 parms->quality = 80; // TODO: Can this be made meaningful?
700 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
701 if(!cam->params.compression.inhibit_htables) {
702 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
705 parms->APPn = cam->APPn;
706 parms->APP_len = cam->APP_len;
707 if(cam->APP_len > 0) {
708 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
709 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
712 parms->COM_len = cam->COM_len;
713 if(cam->COM_len > 0) {
714 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
715 parms->jpeg_markers |= JPEG_MARKER_COM;
718 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
719 parms->APP_len, parms->COM_len);
724 /******************************************************************************
728 * V4L2 set the JPEG compression parameters
729 * NOTE: quality and some jpeg_markers are ignored.
731 *****************************************************************************/
733 static int cpia2_s_jpegcomp(struct file *file, void *fh,
734 const struct v4l2_jpegcompression *parms)
736 struct camera_data *cam = video_drvdata(file);
738 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
739 parms->APP_len, parms->COM_len);
741 cam->params.compression.inhibit_htables =
742 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
744 if(parms->APP_len != 0) {
745 if(parms->APP_len > 0 &&
746 parms->APP_len <= sizeof(cam->APP_data) &&
747 parms->APPn >= 0 && parms->APPn <= 15) {
748 cam->APPn = parms->APPn;
749 cam->APP_len = parms->APP_len;
750 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
752 LOG("Bad APPn Params n=%d len=%d\n",
753 parms->APPn, parms->APP_len);
760 if(parms->COM_len != 0) {
761 if(parms->COM_len > 0 &&
762 parms->COM_len <= sizeof(cam->COM_data)) {
763 cam->COM_len = parms->COM_len;
764 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
766 LOG("Bad COM_len=%d\n", parms->COM_len);
774 /******************************************************************************
778 * V4L2 Initiate memory mapping.
779 * NOTE: The user's request is ignored. For now the buffers are fixed.
781 *****************************************************************************/
783 static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
785 struct camera_data *cam = video_drvdata(file);
787 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
788 req->memory != V4L2_MEMORY_MMAP)
791 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
792 req->count = cam->num_frames;
793 memset(&req->reserved, 0, sizeof(req->reserved));
798 /******************************************************************************
802 * V4L2 Query memory buffer status.
804 *****************************************************************************/
806 static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
808 struct camera_data *cam = video_drvdata(file);
810 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
811 buf->index >= cam->num_frames)
814 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
815 buf->length = cam->frame_size;
817 buf->memory = V4L2_MEMORY_MMAP;
820 buf->flags = V4L2_BUF_FLAG_MAPPED;
824 buf->flags |= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
826 switch (cam->buffers[buf->index].status) {
831 buf->flags = V4L2_BUF_FLAG_QUEUED;
834 buf->bytesused = cam->buffers[buf->index].length;
835 buf->timestamp = cam->buffers[buf->index].timestamp;
836 buf->sequence = cam->buffers[buf->index].seq;
837 buf->flags = V4L2_BUF_FLAG_DONE;
841 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
842 buf->index, buf->m.offset, buf->flags, buf->sequence,
848 /******************************************************************************
852 * V4L2 User is freeing buffer
854 *****************************************************************************/
856 static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
858 struct camera_data *cam = video_drvdata(file);
860 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
861 buf->memory != V4L2_MEMORY_MMAP ||
862 buf->index >= cam->num_frames)
865 DBG("QBUF #%d\n", buf->index);
867 if(cam->buffers[buf->index].status == FRAME_READY)
868 cam->buffers[buf->index].status = FRAME_EMPTY;
873 /******************************************************************************
875 * find_earliest_filled_buffer
877 * Helper for ioctl_dqbuf. Find the next ready buffer.
879 *****************************************************************************/
881 static int find_earliest_filled_buffer(struct camera_data *cam)
885 for (i=0; i<cam->num_frames; i++) {
886 if(cam->buffers[i].status == FRAME_READY) {
890 /* find which buffer is earlier */
891 struct timeval *tv1, *tv2;
892 tv1 = &cam->buffers[i].timestamp;
893 tv2 = &cam->buffers[found].timestamp;
894 if(tv1->tv_sec < tv2->tv_sec ||
895 (tv1->tv_sec == tv2->tv_sec &&
896 tv1->tv_usec < tv2->tv_usec))
904 /******************************************************************************
908 * V4L2 User is asking for a filled buffer.
910 *****************************************************************************/
912 static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
914 struct camera_data *cam = video_drvdata(file);
917 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
918 buf->memory != V4L2_MEMORY_MMAP)
921 frame = find_earliest_filled_buffer(cam);
923 if(frame < 0 && file->f_flags&O_NONBLOCK)
927 /* Wait for a frame to become available */
928 struct framebuf *cb=cam->curbuff;
929 mutex_unlock(&cam->v4l2_lock);
930 wait_event_interruptible(cam->wq_stream,
931 !video_is_registered(&cam->vdev) ||
932 (cb=cam->curbuff)->status == FRAME_READY);
933 mutex_lock(&cam->v4l2_lock);
934 if (signal_pending(current))
936 if (!video_is_registered(&cam->vdev))
943 buf->bytesused = cam->buffers[buf->index].length;
944 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE
945 | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
946 buf->field = V4L2_FIELD_NONE;
947 buf->timestamp = cam->buffers[buf->index].timestamp;
948 buf->sequence = cam->buffers[buf->index].seq;
949 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
950 buf->length = cam->frame_size;
953 memset(&buf->timecode, 0, sizeof(buf->timecode));
955 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
956 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
961 static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
963 struct camera_data *cam = video_drvdata(file);
966 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
967 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
970 if (!cam->streaming) {
971 ret = cpia2_usb_stream_start(cam,
972 cam->params.camera_state.stream_mode);
974 v4l2_ctrl_grab(cam->usb_alt, true);
979 static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
981 struct camera_data *cam = video_drvdata(file);
984 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
985 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
988 if (cam->streaming) {
989 ret = cpia2_usb_stream_stop(cam);
991 v4l2_ctrl_grab(cam->usb_alt, false);
996 /******************************************************************************
1000 *****************************************************************************/
1001 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
1003 struct camera_data *cam = video_drvdata(file);
1006 if (mutex_lock_interruptible(&cam->v4l2_lock))
1007 return -ERESTARTSYS;
1008 retval = cpia2_remap_buffer(cam, area);
1011 cam->stream_fh = file->private_data;
1012 mutex_unlock(&cam->v4l2_lock);
1016 /******************************************************************************
1018 * reset_camera_struct_v4l
1020 * Sets all values to the defaults
1021 *****************************************************************************/
1022 static void reset_camera_struct_v4l(struct camera_data *cam)
1024 cam->width = cam->params.roi.width;
1025 cam->height = cam->params.roi.height;
1027 cam->frame_size = buffer_size;
1028 cam->num_frames = num_buffers;
1031 cam->params.flicker_control.flicker_mode_req = flicker_mode;
1034 cam->params.camera_state.stream_mode = alternate;
1036 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1039 static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1040 .vidioc_querycap = cpia2_querycap,
1041 .vidioc_enum_input = cpia2_enum_input,
1042 .vidioc_g_input = cpia2_g_input,
1043 .vidioc_s_input = cpia2_s_input,
1044 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1045 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1046 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1047 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
1048 .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1049 .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1050 .vidioc_cropcap = cpia2_cropcap,
1051 .vidioc_reqbufs = cpia2_reqbufs,
1052 .vidioc_querybuf = cpia2_querybuf,
1053 .vidioc_qbuf = cpia2_qbuf,
1054 .vidioc_dqbuf = cpia2_dqbuf,
1055 .vidioc_streamon = cpia2_streamon,
1056 .vidioc_streamoff = cpia2_streamoff,
1057 .vidioc_s_parm = cpia2_s_parm,
1058 .vidioc_g_parm = cpia2_g_parm,
1059 .vidioc_enum_framesizes = cpia2_enum_framesizes,
1060 .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1061 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1062 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1066 * The v4l video device structure initialized for this device
1068 static const struct v4l2_file_operations cpia2_fops = {
1069 .owner = THIS_MODULE,
1071 .release = cpia2_close,
1072 .read = cpia2_v4l_read,
1073 .poll = cpia2_v4l_poll,
1074 .unlocked_ioctl = video_ioctl2,
1078 static const struct video_device cpia2_template = {
1079 /* I could not find any place for the old .initialize initializer?? */
1080 .name = "CPiA2 Camera",
1081 .fops = &cpia2_fops,
1082 .ioctl_ops = &cpia2_ioctl_ops,
1083 .release = video_device_release_empty,
1086 void cpia2_camera_release(struct v4l2_device *v4l2_dev)
1088 struct camera_data *cam =
1089 container_of(v4l2_dev, struct camera_data, v4l2_dev);
1091 v4l2_ctrl_handler_free(&cam->hdl);
1092 v4l2_device_unregister(&cam->v4l2_dev);
1096 static const struct v4l2_ctrl_ops cpia2_ctrl_ops = {
1097 .s_ctrl = cpia2_s_ctrl,
1100 /******************************************************************************
1102 * cpia2_register_camera
1104 *****************************************************************************/
1105 int cpia2_register_camera(struct camera_data *cam)
1107 struct v4l2_ctrl_handler *hdl = &cam->hdl;
1108 struct v4l2_ctrl_config cpia2_usb_alt = {
1109 .ops = &cpia2_ctrl_ops,
1110 .id = CPIA2_CID_USB_ALT,
1111 .name = "USB Alternate",
1112 .type = V4L2_CTRL_TYPE_INTEGER,
1119 v4l2_ctrl_handler_init(hdl, 12);
1120 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1121 V4L2_CID_BRIGHTNESS,
1122 cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0,
1123 255, 1, DEFAULT_BRIGHTNESS);
1124 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1125 V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST);
1126 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1127 V4L2_CID_SATURATION, 0, 255, 1, DEFAULT_SATURATION);
1128 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1129 V4L2_CID_HFLIP, 0, 1, 1, 0);
1130 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1131 V4L2_CID_JPEG_ACTIVE_MARKER, 0,
1132 V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
1133 V4L2_JPEG_ACTIVE_MARKER_DHT);
1134 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1135 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1,
1137 cpia2_usb_alt.def = alternate;
1138 cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL);
1140 if (cam->params.pnp_id.device_type != DEVICE_STV_672)
1141 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1142 V4L2_CID_VFLIP, 0, 1, 1, 0);
1143 /* Flicker control only valid for 672 */
1144 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1145 v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops,
1146 V4L2_CID_POWER_LINE_FREQUENCY,
1147 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1148 /* Light control only valid for the QX5 Microscope */
1149 if (cam->params.pnp_id.product == 0x151) {
1150 cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1151 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
1152 cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1153 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
1154 v4l2_ctrl_cluster(2, &cam->top_light);
1159 v4l2_ctrl_handler_free(hdl);
1163 cam->vdev = cpia2_template;
1164 video_set_drvdata(&cam->vdev, cam);
1165 cam->vdev.lock = &cam->v4l2_lock;
1166 cam->vdev.ctrl_handler = hdl;
1167 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1169 reset_camera_struct_v4l(cam);
1171 /* register v4l device */
1172 if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
1173 ERR("video_register_device failed\n");
1180 /******************************************************************************
1182 * cpia2_unregister_camera
1184 *****************************************************************************/
1185 void cpia2_unregister_camera(struct camera_data *cam)
1187 video_unregister_device(&cam->vdev);
1190 /******************************************************************************
1194 * Make sure that all user-supplied parameters are sensible
1195 *****************************************************************************/
1196 static void __init check_parameters(void)
1198 if(buffer_size < PAGE_SIZE) {
1199 buffer_size = PAGE_SIZE;
1200 LOG("buffer_size too small, setting to %d\n", buffer_size);
1201 } else if(buffer_size > 1024*1024) {
1202 /* arbitrary upper limiit */
1203 buffer_size = 1024*1024;
1204 LOG("buffer_size ridiculously large, setting to %d\n",
1207 buffer_size += PAGE_SIZE-1;
1208 buffer_size &= ~(PAGE_SIZE-1);
1211 if(num_buffers < 1) {
1213 LOG("num_buffers too small, setting to %d\n", num_buffers);
1214 } else if(num_buffers > VIDEO_MAX_FRAME) {
1215 num_buffers = VIDEO_MAX_FRAME;
1216 LOG("num_buffers too large, setting to %d\n", num_buffers);
1219 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1220 alternate = DEFAULT_ALT;
1221 LOG("alternate specified is invalid, using %d\n", alternate);
1224 if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) {
1226 LOG("Flicker mode specified is invalid, using %d\n",
1230 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1231 num_buffers, buffer_size, alternate);
1234 /************ Module Stuff ***************/
1237 /******************************************************************************
1239 * cpia2_init/module_init
1241 *****************************************************************************/
1242 static int __init cpia2_init(void)
1245 ABOUT, CPIA_VERSION);
1252 /******************************************************************************
1254 * cpia2_exit/module_exit
1256 *****************************************************************************/
1257 static void __exit cpia2_exit(void)
1259 cpia2_usb_cleanup();
1260 schedule_timeout(2 * HZ);
1263 module_init(cpia2_init);
1264 module_exit(cpia2_exit);