1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /****************************************************************************
4 * Filename: cpia2_v4l.c
6 * Copyright 2001, STMicrolectronics, Inc.
7 * Contact: steve.miller@st.com
8 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com>
11 * This is a USB driver for CPia2 based video cameras.
12 * The infrastructure of this driver is based on the cpia usb driver by
13 * Jochen Scharrlach and Johannes Erdfeldt.
15 * Stripped of 2.4 stuff ready for main kernel submit by
16 * Alan Cox <alan@lxorguk.ukuu.org.uk>
17 ****************************************************************************/
19 #define CPIA_VERSION "3.0.1"
21 #include <linux/module.h>
22 #include <linux/time.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/videodev2.h>
27 #include <linux/stringify.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-event.h>
33 static int video_nr = -1;
34 module_param(video_nr, int, 0);
35 MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)");
37 static int buffer_size = 68 * 1024;
38 module_param(buffer_size, int, 0);
39 MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)");
41 static int num_buffers = 3;
42 module_param(num_buffers, int, 0);
43 MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-"
44 __stringify(VIDEO_MAX_FRAME) ", default 3)");
46 static int alternate = DEFAULT_ALT;
47 module_param(alternate, int, 0);
48 MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-"
49 __stringify(USBIF_ISO_6) ", default "
50 __stringify(DEFAULT_ALT) ")");
52 static int flicker_mode;
53 module_param(flicker_mode, int, 0);
54 MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or "
55 __stringify(60) ", default 0)");
57 MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>");
58 MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(CPIA_VERSION);
62 #define ABOUT "V4L-Driver for Vision CPiA2 based cameras"
63 #define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000)
65 /******************************************************************************
69 *****************************************************************************/
70 static int cpia2_open(struct file *file)
72 struct camera_data *cam = video_drvdata(file);
75 if (mutex_lock_interruptible(&cam->v4l2_lock))
77 retval = v4l2_fh_open(file);
81 if (v4l2_fh_is_singular_file(file)) {
82 if (cpia2_allocate_buffers(cam)) {
83 v4l2_fh_release(file);
88 /* reset the camera */
89 if (cpia2_reset_camera(cam) < 0) {
90 v4l2_fh_release(file);
99 cpia2_dbg_dump_registers(cam);
101 mutex_unlock(&cam->v4l2_lock);
105 /******************************************************************************
109 *****************************************************************************/
110 static int cpia2_close(struct file *file)
112 struct video_device *dev = video_devdata(file);
113 struct camera_data *cam = video_get_drvdata(dev);
115 mutex_lock(&cam->v4l2_lock);
116 if (video_is_registered(&cam->vdev) && v4l2_fh_is_singular_file(file)) {
117 cpia2_usb_stream_stop(cam);
119 /* save camera state for later open */
120 cpia2_save_camera_state(cam);
122 cpia2_set_low_power(cam);
123 cpia2_free_buffers(cam);
126 if (cam->stream_fh == file->private_data) {
127 cam->stream_fh = NULL;
130 mutex_unlock(&cam->v4l2_lock);
131 return v4l2_fh_release(file);
134 /******************************************************************************
138 *****************************************************************************/
139 static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count,
142 struct camera_data *cam = video_drvdata(file);
143 int noblock = file->f_flags&O_NONBLOCK;
149 if (mutex_lock_interruptible(&cam->v4l2_lock))
151 ret = cpia2_read(cam, buf, count, noblock);
152 mutex_unlock(&cam->v4l2_lock);
157 /******************************************************************************
161 *****************************************************************************/
162 static __poll_t cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait)
164 struct camera_data *cam = video_drvdata(filp);
167 mutex_lock(&cam->v4l2_lock);
168 res = cpia2_poll(cam, filp, wait);
169 mutex_unlock(&cam->v4l2_lock);
174 static int sync(struct camera_data *cam, int frame_nr)
176 struct framebuf *frame = &cam->buffers[frame_nr];
179 if (frame->status == FRAME_READY)
182 if (!cam->streaming) {
183 frame->status = FRAME_READY;
188 mutex_unlock(&cam->v4l2_lock);
189 wait_event_interruptible(cam->wq_stream,
191 frame->status == FRAME_READY);
192 mutex_lock(&cam->v4l2_lock);
193 if (signal_pending(current))
195 if (!video_is_registered(&cam->vdev))
200 /******************************************************************************
204 * V4L2 device capabilities
206 *****************************************************************************/
208 static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc)
210 struct camera_data *cam = video_drvdata(file);
212 strscpy(vc->driver, "cpia2", sizeof(vc->driver));
214 if (cam->params.pnp_id.product == 0x151)
215 strscpy(vc->card, "QX5 Microscope", sizeof(vc->card));
217 strscpy(vc->card, "CPiA2 Camera", sizeof(vc->card));
218 switch (cam->params.pnp_id.device_type) {
220 strcat(vc->card, " (672/");
223 strcat(vc->card, " (676/");
226 strcat(vc->card, " (XXX/");
229 switch (cam->params.version.sensor_flags) {
230 case CPIA2_VP_SENSOR_FLAGS_404:
231 strcat(vc->card, "404)");
233 case CPIA2_VP_SENSOR_FLAGS_407:
234 strcat(vc->card, "407)");
236 case CPIA2_VP_SENSOR_FLAGS_409:
237 strcat(vc->card, "409)");
239 case CPIA2_VP_SENSOR_FLAGS_410:
240 strcat(vc->card, "410)");
242 case CPIA2_VP_SENSOR_FLAGS_500:
243 strcat(vc->card, "500)");
246 strcat(vc->card, "XXX)");
250 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) <0)
251 memset(vc->bus_info,0, sizeof(vc->bus_info));
255 /******************************************************************************
259 * V4L2 input get/set/enumerate
261 *****************************************************************************/
263 static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i)
267 strscpy(i->name, "Camera", sizeof(i->name));
268 i->type = V4L2_INPUT_TYPE_CAMERA;
272 static int cpia2_g_input(struct file *file, void *fh, unsigned int *i)
278 static int cpia2_s_input(struct file *file, void *fh, unsigned int i)
280 return i ? -EINVAL : 0;
283 /******************************************************************************
287 * V4L2 format enumerate
289 *****************************************************************************/
291 static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh,
292 struct v4l2_fmtdesc *f)
298 f->pixelformat = V4L2_PIX_FMT_MJPEG;
300 f->pixelformat = V4L2_PIX_FMT_JPEG;
304 /******************************************************************************
310 *****************************************************************************/
312 static int cpia2_try_fmt_vid_cap(struct file *file, void *fh,
313 struct v4l2_format *f)
315 struct camera_data *cam = video_drvdata(file);
317 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG &&
318 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG)
321 f->fmt.pix.field = V4L2_FIELD_NONE;
322 f->fmt.pix.bytesperline = 0;
323 f->fmt.pix.sizeimage = cam->frame_size;
324 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
326 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) {
328 f->fmt.pix.width = 640;
329 f->fmt.pix.height = 480;
332 f->fmt.pix.width = 352;
333 f->fmt.pix.height = 288;
336 f->fmt.pix.width = 320;
337 f->fmt.pix.height = 240;
339 case VIDEOSIZE_288_216:
340 f->fmt.pix.width = 288;
341 f->fmt.pix.height = 216;
343 case VIDEOSIZE_256_192:
344 f->fmt.pix.width = 256;
345 f->fmt.pix.height = 192;
347 case VIDEOSIZE_224_168:
348 f->fmt.pix.width = 224;
349 f->fmt.pix.height = 168;
351 case VIDEOSIZE_192_144:
352 f->fmt.pix.width = 192;
353 f->fmt.pix.height = 144;
357 f->fmt.pix.width = 176;
358 f->fmt.pix.height = 144;
365 /******************************************************************************
371 *****************************************************************************/
373 static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh,
374 struct v4l2_format *f)
376 struct camera_data *cam = video_drvdata(file);
379 err = cpia2_try_fmt_vid_cap(file, _fh, f);
383 cam->pixelformat = f->fmt.pix.pixelformat;
385 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle
386 * the missing Huffman table properly. */
387 cam->params.compression.inhibit_htables = 0;
388 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/
390 /* we set the video window to something smaller or equal to what
391 * is requested by the user???
393 DBG("Requested width = %d, height = %d\n",
394 f->fmt.pix.width, f->fmt.pix.height);
395 if (f->fmt.pix.width != cam->width ||
396 f->fmt.pix.height != cam->height) {
397 cam->width = f->fmt.pix.width;
398 cam->height = f->fmt.pix.height;
399 cam->params.roi.width = f->fmt.pix.width;
400 cam->params.roi.height = f->fmt.pix.height;
401 cpia2_set_format(cam);
404 for (frame = 0; frame < cam->num_frames; ++frame) {
405 if (cam->buffers[frame].status == FRAME_READING)
406 if ((err = sync(cam, frame)) < 0)
409 cam->buffers[frame].status = FRAME_EMPTY;
415 /******************************************************************************
421 *****************************************************************************/
423 static int cpia2_g_fmt_vid_cap(struct file *file, void *fh,
424 struct v4l2_format *f)
426 struct camera_data *cam = video_drvdata(file);
428 f->fmt.pix.width = cam->width;
429 f->fmt.pix.height = cam->height;
430 f->fmt.pix.pixelformat = cam->pixelformat;
431 f->fmt.pix.field = V4L2_FIELD_NONE;
432 f->fmt.pix.bytesperline = 0;
433 f->fmt.pix.sizeimage = cam->frame_size;
434 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
439 /******************************************************************************
443 * V4L2 query cropping capabilities
444 * NOTE: cropping is currently disabled
446 *****************************************************************************/
448 static int cpia2_g_selection(struct file *file, void *fh,
449 struct v4l2_selection *s)
451 struct camera_data *cam = video_drvdata(file);
453 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
457 case V4L2_SEL_TGT_CROP_BOUNDS:
458 case V4L2_SEL_TGT_CROP_DEFAULT:
461 s->r.width = cam->width;
462 s->r.height = cam->height;
470 struct framerate_info {
472 struct v4l2_fract period;
475 static const struct framerate_info framerate_controls[] = {
476 { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } },
477 { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } },
478 { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } },
479 { CPIA2_VP_FRAMERATE_15, { 1, 15 } },
480 { CPIA2_VP_FRAMERATE_25, { 1, 25 } },
481 { CPIA2_VP_FRAMERATE_30, { 1, 30 } },
484 static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
486 struct camera_data *cam = video_drvdata(file);
487 struct v4l2_captureparm *cap = &p->parm.capture;
490 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
493 cap->capability = V4L2_CAP_TIMEPERFRAME;
494 cap->readbuffers = cam->num_frames;
495 for (i = 0; i < ARRAY_SIZE(framerate_controls); i++)
496 if (cam->params.vp_params.frame_rate == framerate_controls[i].value) {
497 cap->timeperframe = framerate_controls[i].period;
503 static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p)
505 struct camera_data *cam = video_drvdata(file);
506 struct v4l2_captureparm *cap = &p->parm.capture;
507 struct v4l2_fract tpf = cap->timeperframe;
508 int max = ARRAY_SIZE(framerate_controls) - 1;
512 ret = cpia2_g_parm(file, fh, p);
513 if (ret || !tpf.denominator || !tpf.numerator)
516 /* Maximum 15 fps for this model */
517 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
518 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
520 for (i = 0; i <= max; i++) {
521 struct v4l2_fract f1 = tpf;
522 struct v4l2_fract f2 = framerate_controls[i].period;
524 f1.numerator *= f2.denominator;
525 f2.numerator *= f1.denominator;
526 if (f1.numerator >= f2.numerator)
531 cap->timeperframe = framerate_controls[i].period;
532 return cpia2_set_fps(cam, framerate_controls[i].value);
535 static const struct {
538 } cpia2_framesizes[] = {
549 static int cpia2_enum_framesizes(struct file *file, void *fh,
550 struct v4l2_frmsizeenum *fsize)
553 if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG &&
554 fsize->pixel_format != V4L2_PIX_FMT_JPEG)
556 if (fsize->index >= ARRAY_SIZE(cpia2_framesizes))
558 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
559 fsize->discrete.width = cpia2_framesizes[fsize->index].width;
560 fsize->discrete.height = cpia2_framesizes[fsize->index].height;
565 static int cpia2_enum_frameintervals(struct file *file, void *fh,
566 struct v4l2_frmivalenum *fival)
568 struct camera_data *cam = video_drvdata(file);
569 int max = ARRAY_SIZE(framerate_controls) - 1;
572 if (fival->pixel_format != V4L2_PIX_FMT_MJPEG &&
573 fival->pixel_format != V4L2_PIX_FMT_JPEG)
576 /* Maximum 15 fps for this model */
577 if (cam->params.pnp_id.device_type == DEVICE_STV_672 &&
578 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500)
580 if (fival->index > max)
582 for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++)
583 if (fival->width == cpia2_framesizes[i].width &&
584 fival->height == cpia2_framesizes[i].height)
586 if (i == ARRAY_SIZE(cpia2_framesizes))
588 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
589 fival->discrete = framerate_controls[fival->index].period;
593 /******************************************************************************
597 * V4L2 set the value of a control variable
599 *****************************************************************************/
601 static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl)
603 struct camera_data *cam =
604 container_of(ctrl->handler, struct camera_data, hdl);
605 static const int flicker_table[] = {
611 DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val);
614 case V4L2_CID_BRIGHTNESS:
615 cpia2_set_brightness(cam, ctrl->val);
617 case V4L2_CID_CONTRAST:
618 cpia2_set_contrast(cam, ctrl->val);
620 case V4L2_CID_SATURATION:
621 cpia2_set_saturation(cam, ctrl->val);
624 cpia2_set_property_mirror(cam, ctrl->val);
627 cpia2_set_property_flip(cam, ctrl->val);
629 case V4L2_CID_POWER_LINE_FREQUENCY:
630 return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]);
631 case V4L2_CID_ILLUMINATORS_1:
632 return cpia2_set_gpio(cam, (cam->top_light->val << 6) |
633 (cam->bottom_light->val << 7));
634 case V4L2_CID_JPEG_ACTIVE_MARKER:
635 cam->params.compression.inhibit_htables =
636 !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT);
638 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
639 cam->params.vc_params.quality = ctrl->val;
641 case CPIA2_CID_USB_ALT:
642 cam->params.camera_state.stream_mode = ctrl->val;
651 /******************************************************************************
655 * V4L2 get the JPEG compression parameters
657 *****************************************************************************/
659 static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms)
661 struct camera_data *cam = video_drvdata(file);
663 memset(parms, 0, sizeof(*parms));
665 parms->quality = 80; // TODO: Can this be made meaningful?
667 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI;
668 if(!cam->params.compression.inhibit_htables) {
669 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT;
672 parms->APPn = cam->APPn;
673 parms->APP_len = cam->APP_len;
674 if(cam->APP_len > 0) {
675 memcpy(parms->APP_data, cam->APP_data, cam->APP_len);
676 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP;
679 parms->COM_len = cam->COM_len;
680 if(cam->COM_len > 0) {
681 memcpy(parms->COM_data, cam->COM_data, cam->COM_len);
682 parms->jpeg_markers |= JPEG_MARKER_COM;
685 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n",
686 parms->APP_len, parms->COM_len);
691 /******************************************************************************
695 * V4L2 set the JPEG compression parameters
696 * NOTE: quality and some jpeg_markers are ignored.
698 *****************************************************************************/
700 static int cpia2_s_jpegcomp(struct file *file, void *fh,
701 const struct v4l2_jpegcompression *parms)
703 struct camera_data *cam = video_drvdata(file);
705 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n",
706 parms->APP_len, parms->COM_len);
708 cam->params.compression.inhibit_htables =
709 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT);
711 if(parms->APP_len != 0) {
712 if(parms->APP_len > 0 &&
713 parms->APP_len <= sizeof(cam->APP_data) &&
714 parms->APPn >= 0 && parms->APPn <= 15) {
715 cam->APPn = parms->APPn;
716 cam->APP_len = parms->APP_len;
717 memcpy(cam->APP_data, parms->APP_data, parms->APP_len);
719 LOG("Bad APPn Params n=%d len=%d\n",
720 parms->APPn, parms->APP_len);
727 if(parms->COM_len != 0) {
728 if(parms->COM_len > 0 &&
729 parms->COM_len <= sizeof(cam->COM_data)) {
730 cam->COM_len = parms->COM_len;
731 memcpy(cam->COM_data, parms->COM_data, parms->COM_len);
733 LOG("Bad COM_len=%d\n", parms->COM_len);
741 /******************************************************************************
745 * V4L2 Initiate memory mapping.
746 * NOTE: The user's request is ignored. For now the buffers are fixed.
748 *****************************************************************************/
750 static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req)
752 struct camera_data *cam = video_drvdata(file);
754 if(req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
755 req->memory != V4L2_MEMORY_MMAP)
758 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames);
759 req->count = cam->num_frames;
760 memset(&req->reserved, 0, sizeof(req->reserved));
765 /******************************************************************************
769 * V4L2 Query memory buffer status.
771 *****************************************************************************/
773 static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf)
775 struct camera_data *cam = video_drvdata(file);
777 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
778 buf->index >= cam->num_frames)
781 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
782 buf->length = cam->frame_size;
784 buf->memory = V4L2_MEMORY_MMAP;
787 buf->flags = V4L2_BUF_FLAG_MAPPED;
791 buf->flags |= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
793 switch (cam->buffers[buf->index].status) {
798 buf->flags = V4L2_BUF_FLAG_QUEUED;
801 buf->bytesused = cam->buffers[buf->index].length;
802 v4l2_buffer_set_timestamp(buf, cam->buffers[buf->index].ts);
803 buf->sequence = cam->buffers[buf->index].seq;
804 buf->flags = V4L2_BUF_FLAG_DONE;
808 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n",
809 buf->index, buf->m.offset, buf->flags, buf->sequence,
815 /******************************************************************************
819 * V4L2 User is freeing buffer
821 *****************************************************************************/
823 static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
825 struct camera_data *cam = video_drvdata(file);
827 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
828 buf->memory != V4L2_MEMORY_MMAP ||
829 buf->index >= cam->num_frames)
832 DBG("QBUF #%d\n", buf->index);
834 if(cam->buffers[buf->index].status == FRAME_READY)
835 cam->buffers[buf->index].status = FRAME_EMPTY;
840 /******************************************************************************
842 * find_earliest_filled_buffer
844 * Helper for ioctl_dqbuf. Find the next ready buffer.
846 *****************************************************************************/
848 static int find_earliest_filled_buffer(struct camera_data *cam)
852 for (i=0; i<cam->num_frames; i++) {
853 if(cam->buffers[i].status == FRAME_READY) {
857 /* find which buffer is earlier */
858 if (cam->buffers[i].ts < cam->buffers[found].ts)
866 /******************************************************************************
870 * V4L2 User is asking for a filled buffer.
872 *****************************************************************************/
874 static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf)
876 struct camera_data *cam = video_drvdata(file);
879 if(buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
880 buf->memory != V4L2_MEMORY_MMAP)
883 frame = find_earliest_filled_buffer(cam);
885 if(frame < 0 && file->f_flags&O_NONBLOCK)
889 /* Wait for a frame to become available */
890 struct framebuf *cb=cam->curbuff;
891 mutex_unlock(&cam->v4l2_lock);
892 wait_event_interruptible(cam->wq_stream,
893 !video_is_registered(&cam->vdev) ||
894 (cb=cam->curbuff)->status == FRAME_READY);
895 mutex_lock(&cam->v4l2_lock);
896 if (signal_pending(current))
898 if (!video_is_registered(&cam->vdev))
905 buf->bytesused = cam->buffers[buf->index].length;
906 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE
907 | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
908 buf->field = V4L2_FIELD_NONE;
909 v4l2_buffer_set_timestamp(buf, cam->buffers[buf->index].ts);
910 buf->sequence = cam->buffers[buf->index].seq;
911 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer;
912 buf->length = cam->frame_size;
915 memset(&buf->timecode, 0, sizeof(buf->timecode));
917 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index,
918 cam->buffers[buf->index].status, buf->sequence, buf->bytesused);
923 static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type)
925 struct camera_data *cam = video_drvdata(file);
928 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming);
929 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
932 if (!cam->streaming) {
933 ret = cpia2_usb_stream_start(cam,
934 cam->params.camera_state.stream_mode);
936 v4l2_ctrl_grab(cam->usb_alt, true);
941 static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type)
943 struct camera_data *cam = video_drvdata(file);
946 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming);
947 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
950 if (cam->streaming) {
951 ret = cpia2_usb_stream_stop(cam);
953 v4l2_ctrl_grab(cam->usb_alt, false);
958 /******************************************************************************
962 *****************************************************************************/
963 static int cpia2_mmap(struct file *file, struct vm_area_struct *area)
965 struct camera_data *cam = video_drvdata(file);
968 if (mutex_lock_interruptible(&cam->v4l2_lock))
970 retval = cpia2_remap_buffer(cam, area);
973 cam->stream_fh = file->private_data;
974 mutex_unlock(&cam->v4l2_lock);
978 /******************************************************************************
980 * reset_camera_struct_v4l
982 * Sets all values to the defaults
983 *****************************************************************************/
984 static void reset_camera_struct_v4l(struct camera_data *cam)
986 cam->width = cam->params.roi.width;
987 cam->height = cam->params.roi.height;
989 cam->frame_size = buffer_size;
990 cam->num_frames = num_buffers;
993 cam->params.flicker_control.flicker_mode_req = flicker_mode;
996 cam->params.camera_state.stream_mode = alternate;
998 cam->pixelformat = V4L2_PIX_FMT_JPEG;
1001 static const struct v4l2_ioctl_ops cpia2_ioctl_ops = {
1002 .vidioc_querycap = cpia2_querycap,
1003 .vidioc_enum_input = cpia2_enum_input,
1004 .vidioc_g_input = cpia2_g_input,
1005 .vidioc_s_input = cpia2_s_input,
1006 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap,
1007 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap,
1008 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap,
1009 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap,
1010 .vidioc_g_jpegcomp = cpia2_g_jpegcomp,
1011 .vidioc_s_jpegcomp = cpia2_s_jpegcomp,
1012 .vidioc_g_selection = cpia2_g_selection,
1013 .vidioc_reqbufs = cpia2_reqbufs,
1014 .vidioc_querybuf = cpia2_querybuf,
1015 .vidioc_qbuf = cpia2_qbuf,
1016 .vidioc_dqbuf = cpia2_dqbuf,
1017 .vidioc_streamon = cpia2_streamon,
1018 .vidioc_streamoff = cpia2_streamoff,
1019 .vidioc_s_parm = cpia2_s_parm,
1020 .vidioc_g_parm = cpia2_g_parm,
1021 .vidioc_enum_framesizes = cpia2_enum_framesizes,
1022 .vidioc_enum_frameintervals = cpia2_enum_frameintervals,
1023 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1024 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1028 * The v4l video device structure initialized for this device
1030 static const struct v4l2_file_operations cpia2_fops = {
1031 .owner = THIS_MODULE,
1033 .release = cpia2_close,
1034 .read = cpia2_v4l_read,
1035 .poll = cpia2_v4l_poll,
1036 .unlocked_ioctl = video_ioctl2,
1040 static const struct video_device cpia2_template = {
1041 /* I could not find any place for the old .initialize initializer?? */
1042 .name = "CPiA2 Camera",
1043 .fops = &cpia2_fops,
1044 .ioctl_ops = &cpia2_ioctl_ops,
1045 .release = video_device_release_empty,
1048 void cpia2_camera_release(struct v4l2_device *v4l2_dev)
1050 struct camera_data *cam =
1051 container_of(v4l2_dev, struct camera_data, v4l2_dev);
1053 v4l2_ctrl_handler_free(&cam->hdl);
1054 v4l2_device_unregister(&cam->v4l2_dev);
1058 static const struct v4l2_ctrl_ops cpia2_ctrl_ops = {
1059 .s_ctrl = cpia2_s_ctrl,
1062 /******************************************************************************
1064 * cpia2_register_camera
1066 *****************************************************************************/
1067 int cpia2_register_camera(struct camera_data *cam)
1069 struct v4l2_ctrl_handler *hdl = &cam->hdl;
1070 struct v4l2_ctrl_config cpia2_usb_alt = {
1071 .ops = &cpia2_ctrl_ops,
1072 .id = CPIA2_CID_USB_ALT,
1073 .name = "USB Alternate",
1074 .type = V4L2_CTRL_TYPE_INTEGER,
1081 v4l2_ctrl_handler_init(hdl, 12);
1082 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1083 V4L2_CID_BRIGHTNESS,
1084 cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0,
1085 255, 1, DEFAULT_BRIGHTNESS);
1086 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1087 V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST);
1088 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1089 V4L2_CID_SATURATION, 0, 255, 1, DEFAULT_SATURATION);
1090 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1091 V4L2_CID_HFLIP, 0, 1, 1, 0);
1092 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1093 V4L2_CID_JPEG_ACTIVE_MARKER, 0,
1094 V4L2_JPEG_ACTIVE_MARKER_DHT, 0,
1095 V4L2_JPEG_ACTIVE_MARKER_DHT);
1096 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1097 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1,
1099 cpia2_usb_alt.def = alternate;
1100 cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL);
1102 if (cam->params.pnp_id.device_type != DEVICE_STV_672)
1103 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1104 V4L2_CID_VFLIP, 0, 1, 1, 0);
1105 /* Flicker control only valid for 672 */
1106 if (cam->params.pnp_id.device_type == DEVICE_STV_672)
1107 v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops,
1108 V4L2_CID_POWER_LINE_FREQUENCY,
1109 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 0, 0);
1110 /* Light control only valid for the QX5 Microscope */
1111 if (cam->params.pnp_id.product == 0x151) {
1112 cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1113 V4L2_CID_ILLUMINATORS_1, 0, 1, 1, 0);
1114 cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops,
1115 V4L2_CID_ILLUMINATORS_2, 0, 1, 1, 0);
1116 v4l2_ctrl_cluster(2, &cam->top_light);
1121 v4l2_ctrl_handler_free(hdl);
1125 cam->vdev = cpia2_template;
1126 video_set_drvdata(&cam->vdev, cam);
1127 cam->vdev.lock = &cam->v4l2_lock;
1128 cam->vdev.ctrl_handler = hdl;
1129 cam->vdev.v4l2_dev = &cam->v4l2_dev;
1130 cam->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
1133 reset_camera_struct_v4l(cam);
1135 /* register v4l device */
1136 if (video_register_device(&cam->vdev, VFL_TYPE_VIDEO, video_nr) < 0) {
1137 ERR("video_register_device failed\n");
1144 /******************************************************************************
1146 * cpia2_unregister_camera
1148 *****************************************************************************/
1149 void cpia2_unregister_camera(struct camera_data *cam)
1151 video_unregister_device(&cam->vdev);
1154 /******************************************************************************
1158 * Make sure that all user-supplied parameters are sensible
1159 *****************************************************************************/
1160 static void __init check_parameters(void)
1162 if(buffer_size < PAGE_SIZE) {
1163 buffer_size = PAGE_SIZE;
1164 LOG("buffer_size too small, setting to %d\n", buffer_size);
1165 } else if(buffer_size > 1024*1024) {
1166 /* arbitrary upper limiit */
1167 buffer_size = 1024*1024;
1168 LOG("buffer_size ridiculously large, setting to %d\n",
1171 buffer_size += PAGE_SIZE-1;
1172 buffer_size &= ~(PAGE_SIZE-1);
1175 if(num_buffers < 1) {
1177 LOG("num_buffers too small, setting to %d\n", num_buffers);
1178 } else if(num_buffers > VIDEO_MAX_FRAME) {
1179 num_buffers = VIDEO_MAX_FRAME;
1180 LOG("num_buffers too large, setting to %d\n", num_buffers);
1183 if(alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) {
1184 alternate = DEFAULT_ALT;
1185 LOG("alternate specified is invalid, using %d\n", alternate);
1188 if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) {
1190 LOG("Flicker mode specified is invalid, using %d\n",
1194 DBG("Using %d buffers, each %d bytes, alternate=%d\n",
1195 num_buffers, buffer_size, alternate);
1198 /************ Module Stuff ***************/
1201 /******************************************************************************
1203 * cpia2_init/module_init
1205 *****************************************************************************/
1206 static int __init cpia2_init(void)
1209 ABOUT, CPIA_VERSION);
1211 return cpia2_usb_init();
1215 /******************************************************************************
1217 * cpia2_exit/module_exit
1219 *****************************************************************************/
1220 static void __exit cpia2_exit(void)
1222 cpia2_usb_cleanup();
1223 schedule_timeout(2 * HZ);
1226 module_init(cpia2_init);
1227 module_exit(cpia2_exit);