cpia2_v4l.c (33265B)
1// SPDX-License-Identifier: GPL-2.0-or-later 2/**************************************************************************** 3 * 4 * Filename: cpia2_v4l.c 5 * 6 * Copyright 2001, STMicrolectronics, Inc. 7 * Contact: steve.miller@st.com 8 * Copyright 2001,2005, Scott J. Bertin <scottbertin@yahoo.com> 9 * 10 * Description: 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. 14 * 15 * Stripped of 2.4 stuff ready for main kernel submit by 16 * Alan Cox <alan@lxorguk.ukuu.org.uk> 17 ****************************************************************************/ 18 19#define CPIA_VERSION "3.0.1" 20 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> 30 31#include "cpia2.h" 32 33static int video_nr = -1; 34module_param(video_nr, int, 0); 35MODULE_PARM_DESC(video_nr, "video device to register (0=/dev/video0, etc)"); 36 37static int buffer_size = 68 * 1024; 38module_param(buffer_size, int, 0); 39MODULE_PARM_DESC(buffer_size, "Size for each frame buffer in bytes (default 68k)"); 40 41static int num_buffers = 3; 42module_param(num_buffers, int, 0); 43MODULE_PARM_DESC(num_buffers, "Number of frame buffers (1-" 44 __stringify(VIDEO_MAX_FRAME) ", default 3)"); 45 46static int alternate = DEFAULT_ALT; 47module_param(alternate, int, 0); 48MODULE_PARM_DESC(alternate, "USB Alternate (" __stringify(USBIF_ISO_1) "-" 49 __stringify(USBIF_ISO_6) ", default " 50 __stringify(DEFAULT_ALT) ")"); 51 52static int flicker_mode; 53module_param(flicker_mode, int, 0); 54MODULE_PARM_DESC(flicker_mode, "Flicker frequency (0 (disabled), " __stringify(50) " or " 55 __stringify(60) ", default 0)"); 56 57MODULE_AUTHOR("Steve Miller (STMicroelectronics) <steve.miller@st.com>"); 58MODULE_DESCRIPTION("V4L-driver for STMicroelectronics CPiA2 based cameras"); 59MODULE_LICENSE("GPL"); 60MODULE_VERSION(CPIA_VERSION); 61 62#define ABOUT "V4L-Driver for Vision CPiA2 based cameras" 63#define CPIA2_CID_USB_ALT (V4L2_CID_USER_BASE | 0xf000) 64 65/****************************************************************************** 66 * 67 * cpia2_open 68 * 69 *****************************************************************************/ 70static int cpia2_open(struct file *file) 71{ 72 struct camera_data *cam = video_drvdata(file); 73 int retval; 74 75 if (mutex_lock_interruptible(&cam->v4l2_lock)) 76 return -ERESTARTSYS; 77 retval = v4l2_fh_open(file); 78 if (retval) 79 goto open_unlock; 80 81 if (v4l2_fh_is_singular_file(file)) { 82 if (cpia2_allocate_buffers(cam)) { 83 v4l2_fh_release(file); 84 retval = -ENOMEM; 85 goto open_unlock; 86 } 87 88 /* reset the camera */ 89 if (cpia2_reset_camera(cam) < 0) { 90 v4l2_fh_release(file); 91 retval = -EIO; 92 goto open_unlock; 93 } 94 95 cam->APP_len = 0; 96 cam->COM_len = 0; 97 } 98 99 cpia2_dbg_dump_registers(cam); 100open_unlock: 101 mutex_unlock(&cam->v4l2_lock); 102 return retval; 103} 104 105/****************************************************************************** 106 * 107 * cpia2_close 108 * 109 *****************************************************************************/ 110static int cpia2_close(struct file *file) 111{ 112 struct video_device *dev = video_devdata(file); 113 struct camera_data *cam = video_get_drvdata(dev); 114 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); 118 119 /* save camera state for later open */ 120 cpia2_save_camera_state(cam); 121 122 cpia2_set_low_power(cam); 123 cpia2_free_buffers(cam); 124 } 125 126 if (cam->stream_fh == file->private_data) { 127 cam->stream_fh = NULL; 128 cam->mmapped = 0; 129 } 130 mutex_unlock(&cam->v4l2_lock); 131 return v4l2_fh_release(file); 132} 133 134/****************************************************************************** 135 * 136 * cpia2_v4l_read 137 * 138 *****************************************************************************/ 139static ssize_t cpia2_v4l_read(struct file *file, char __user *buf, size_t count, 140 loff_t *off) 141{ 142 struct camera_data *cam = video_drvdata(file); 143 int noblock = file->f_flags & O_NONBLOCK; 144 ssize_t ret; 145 146 if (!cam) 147 return -EINVAL; 148 149 if (mutex_lock_interruptible(&cam->v4l2_lock)) 150 return -ERESTARTSYS; 151 ret = cpia2_read(cam, buf, count, noblock); 152 mutex_unlock(&cam->v4l2_lock); 153 return ret; 154} 155 156/****************************************************************************** 157 * 158 * cpia2_v4l_poll 159 * 160 *****************************************************************************/ 161static __poll_t cpia2_v4l_poll(struct file *filp, struct poll_table_struct *wait) 162{ 163 struct camera_data *cam = video_drvdata(filp); 164 __poll_t res; 165 166 mutex_lock(&cam->v4l2_lock); 167 res = cpia2_poll(cam, filp, wait); 168 mutex_unlock(&cam->v4l2_lock); 169 return res; 170} 171 172static int sync(struct camera_data *cam, int frame_nr) 173{ 174 struct framebuf *frame = &cam->buffers[frame_nr]; 175 176 while (1) { 177 if (frame->status == FRAME_READY) 178 return 0; 179 180 if (!cam->streaming) { 181 frame->status = FRAME_READY; 182 frame->length = 0; 183 return 0; 184 } 185 186 mutex_unlock(&cam->v4l2_lock); 187 wait_event_interruptible(cam->wq_stream, 188 !cam->streaming || 189 frame->status == FRAME_READY); 190 mutex_lock(&cam->v4l2_lock); 191 if (signal_pending(current)) 192 return -ERESTARTSYS; 193 if (!video_is_registered(&cam->vdev)) 194 return -ENOTTY; 195 } 196} 197 198/****************************************************************************** 199 * 200 * ioctl_querycap 201 * 202 * V4L2 device capabilities 203 * 204 *****************************************************************************/ 205 206static int cpia2_querycap(struct file *file, void *fh, struct v4l2_capability *vc) 207{ 208 struct camera_data *cam = video_drvdata(file); 209 210 strscpy(vc->driver, "cpia2", sizeof(vc->driver)); 211 212 if (cam->params.pnp_id.product == 0x151) 213 strscpy(vc->card, "QX5 Microscope", sizeof(vc->card)); 214 else 215 strscpy(vc->card, "CPiA2 Camera", sizeof(vc->card)); 216 switch (cam->params.pnp_id.device_type) { 217 case DEVICE_STV_672: 218 strcat(vc->card, " (672/"); 219 break; 220 case DEVICE_STV_676: 221 strcat(vc->card, " (676/"); 222 break; 223 default: 224 strcat(vc->card, " (XXX/"); 225 break; 226 } 227 switch (cam->params.version.sensor_flags) { 228 case CPIA2_VP_SENSOR_FLAGS_404: 229 strcat(vc->card, "404)"); 230 break; 231 case CPIA2_VP_SENSOR_FLAGS_407: 232 strcat(vc->card, "407)"); 233 break; 234 case CPIA2_VP_SENSOR_FLAGS_409: 235 strcat(vc->card, "409)"); 236 break; 237 case CPIA2_VP_SENSOR_FLAGS_410: 238 strcat(vc->card, "410)"); 239 break; 240 case CPIA2_VP_SENSOR_FLAGS_500: 241 strcat(vc->card, "500)"); 242 break; 243 default: 244 strcat(vc->card, "XXX)"); 245 break; 246 } 247 248 if (usb_make_path(cam->dev, vc->bus_info, sizeof(vc->bus_info)) < 0) 249 memset(vc->bus_info, 0, sizeof(vc->bus_info)); 250 return 0; 251} 252 253/****************************************************************************** 254 * 255 * ioctl_input 256 * 257 * V4L2 input get/set/enumerate 258 * 259 *****************************************************************************/ 260 261static int cpia2_enum_input(struct file *file, void *fh, struct v4l2_input *i) 262{ 263 if (i->index) 264 return -EINVAL; 265 strscpy(i->name, "Camera", sizeof(i->name)); 266 i->type = V4L2_INPUT_TYPE_CAMERA; 267 return 0; 268} 269 270static int cpia2_g_input(struct file *file, void *fh, unsigned int *i) 271{ 272 *i = 0; 273 return 0; 274} 275 276static int cpia2_s_input(struct file *file, void *fh, unsigned int i) 277{ 278 return i ? -EINVAL : 0; 279} 280 281/****************************************************************************** 282 * 283 * ioctl_enum_fmt 284 * 285 * V4L2 format enumerate 286 * 287 *****************************************************************************/ 288 289static int cpia2_enum_fmt_vid_cap(struct file *file, void *fh, 290 struct v4l2_fmtdesc *f) 291{ 292 if (f->index > 1) 293 return -EINVAL; 294 295 if (f->index == 0) 296 f->pixelformat = V4L2_PIX_FMT_MJPEG; 297 else 298 f->pixelformat = V4L2_PIX_FMT_JPEG; 299 return 0; 300} 301 302/****************************************************************************** 303 * 304 * ioctl_try_fmt 305 * 306 * V4L2 format try 307 * 308 *****************************************************************************/ 309 310static int cpia2_try_fmt_vid_cap(struct file *file, void *fh, 311 struct v4l2_format *f) 312{ 313 struct camera_data *cam = video_drvdata(file); 314 315 if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_MJPEG && 316 f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) 317 return -EINVAL; 318 319 f->fmt.pix.field = V4L2_FIELD_NONE; 320 f->fmt.pix.bytesperline = 0; 321 f->fmt.pix.sizeimage = cam->frame_size; 322 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; 323 324 switch (cpia2_match_video_size(f->fmt.pix.width, f->fmt.pix.height)) { 325 case VIDEOSIZE_VGA: 326 f->fmt.pix.width = 640; 327 f->fmt.pix.height = 480; 328 break; 329 case VIDEOSIZE_CIF: 330 f->fmt.pix.width = 352; 331 f->fmt.pix.height = 288; 332 break; 333 case VIDEOSIZE_QVGA: 334 f->fmt.pix.width = 320; 335 f->fmt.pix.height = 240; 336 break; 337 case VIDEOSIZE_288_216: 338 f->fmt.pix.width = 288; 339 f->fmt.pix.height = 216; 340 break; 341 case VIDEOSIZE_256_192: 342 f->fmt.pix.width = 256; 343 f->fmt.pix.height = 192; 344 break; 345 case VIDEOSIZE_224_168: 346 f->fmt.pix.width = 224; 347 f->fmt.pix.height = 168; 348 break; 349 case VIDEOSIZE_192_144: 350 f->fmt.pix.width = 192; 351 f->fmt.pix.height = 144; 352 break; 353 case VIDEOSIZE_QCIF: 354 default: 355 f->fmt.pix.width = 176; 356 f->fmt.pix.height = 144; 357 break; 358 } 359 360 return 0; 361} 362 363/****************************************************************************** 364 * 365 * ioctl_set_fmt 366 * 367 * V4L2 format set 368 * 369 *****************************************************************************/ 370 371static int cpia2_s_fmt_vid_cap(struct file *file, void *_fh, 372 struct v4l2_format *f) 373{ 374 struct camera_data *cam = video_drvdata(file); 375 int err, frame; 376 377 err = cpia2_try_fmt_vid_cap(file, _fh, f); 378 if (err != 0) 379 return err; 380 381 cam->pixelformat = f->fmt.pix.pixelformat; 382 383 /* NOTE: This should be set to 1 for MJPEG, but some apps don't handle 384 * the missing Huffman table properly. 385 */ 386 cam->params.compression.inhibit_htables = 0; 387 /*f->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG;*/ 388 389 /* we set the video window to something smaller or equal to what 390 * is requested by the user??? 391 */ 392 DBG("Requested width = %d, height = %d\n", 393 f->fmt.pix.width, f->fmt.pix.height); 394 if (f->fmt.pix.width != cam->width || 395 f->fmt.pix.height != cam->height) { 396 cam->width = f->fmt.pix.width; 397 cam->height = f->fmt.pix.height; 398 cam->params.roi.width = f->fmt.pix.width; 399 cam->params.roi.height = f->fmt.pix.height; 400 cpia2_set_format(cam); 401 } 402 403 for (frame = 0; frame < cam->num_frames; ++frame) { 404 if (cam->buffers[frame].status == FRAME_READING) 405 if ((err = sync(cam, frame)) < 0) 406 return err; 407 408 cam->buffers[frame].status = FRAME_EMPTY; 409 } 410 411 return 0; 412} 413 414/****************************************************************************** 415 * 416 * ioctl_get_fmt 417 * 418 * V4L2 format get 419 * 420 *****************************************************************************/ 421 422static int cpia2_g_fmt_vid_cap(struct file *file, void *fh, 423 struct v4l2_format *f) 424{ 425 struct camera_data *cam = video_drvdata(file); 426 427 f->fmt.pix.width = cam->width; 428 f->fmt.pix.height = cam->height; 429 f->fmt.pix.pixelformat = cam->pixelformat; 430 f->fmt.pix.field = V4L2_FIELD_NONE; 431 f->fmt.pix.bytesperline = 0; 432 f->fmt.pix.sizeimage = cam->frame_size; 433 f->fmt.pix.colorspace = V4L2_COLORSPACE_JPEG; 434 435 return 0; 436} 437 438/****************************************************************************** 439 * 440 * ioctl_cropcap 441 * 442 * V4L2 query cropping capabilities 443 * NOTE: cropping is currently disabled 444 * 445 *****************************************************************************/ 446 447static int cpia2_g_selection(struct file *file, void *fh, 448 struct v4l2_selection *s) 449{ 450 struct camera_data *cam = video_drvdata(file); 451 452 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 453 return -EINVAL; 454 455 switch (s->target) { 456 case V4L2_SEL_TGT_CROP_BOUNDS: 457 case V4L2_SEL_TGT_CROP_DEFAULT: 458 s->r.left = 0; 459 s->r.top = 0; 460 s->r.width = cam->width; 461 s->r.height = cam->height; 462 break; 463 default: 464 return -EINVAL; 465 } 466 return 0; 467} 468 469struct framerate_info { 470 int value; 471 struct v4l2_fract period; 472}; 473 474static const struct framerate_info framerate_controls[] = { 475 { CPIA2_VP_FRAMERATE_6_25, { 4, 25 } }, 476 { CPIA2_VP_FRAMERATE_7_5, { 2, 15 } }, 477 { CPIA2_VP_FRAMERATE_12_5, { 2, 25 } }, 478 { CPIA2_VP_FRAMERATE_15, { 1, 15 } }, 479 { CPIA2_VP_FRAMERATE_25, { 1, 25 } }, 480 { CPIA2_VP_FRAMERATE_30, { 1, 30 } }, 481}; 482 483static int cpia2_g_parm(struct file *file, void *fh, struct v4l2_streamparm *p) 484{ 485 struct camera_data *cam = video_drvdata(file); 486 struct v4l2_captureparm *cap = &p->parm.capture; 487 int i; 488 489 if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 490 return -EINVAL; 491 492 cap->capability = V4L2_CAP_TIMEPERFRAME; 493 cap->readbuffers = cam->num_frames; 494 for (i = 0; i < ARRAY_SIZE(framerate_controls); i++) 495 if (cam->params.vp_params.frame_rate == framerate_controls[i].value) { 496 cap->timeperframe = framerate_controls[i].period; 497 break; 498 } 499 return 0; 500} 501 502static int cpia2_s_parm(struct file *file, void *fh, struct v4l2_streamparm *p) 503{ 504 struct camera_data *cam = video_drvdata(file); 505 struct v4l2_captureparm *cap = &p->parm.capture; 506 struct v4l2_fract tpf = cap->timeperframe; 507 int max = ARRAY_SIZE(framerate_controls) - 1; 508 int ret; 509 int i; 510 511 ret = cpia2_g_parm(file, fh, p); 512 if (ret || !tpf.denominator || !tpf.numerator) 513 return ret; 514 515 /* Maximum 15 fps for this model */ 516 if (cam->params.pnp_id.device_type == DEVICE_STV_672 && 517 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500) 518 max -= 2; 519 for (i = 0; i <= max; i++) { 520 struct v4l2_fract f1 = tpf; 521 struct v4l2_fract f2 = framerate_controls[i].period; 522 523 f1.numerator *= f2.denominator; 524 f2.numerator *= f1.denominator; 525 if (f1.numerator >= f2.numerator) 526 break; 527 } 528 if (i > max) 529 i = max; 530 cap->timeperframe = framerate_controls[i].period; 531 return cpia2_set_fps(cam, framerate_controls[i].value); 532} 533 534static const struct { 535 u32 width; 536 u32 height; 537} cpia2_framesizes[] = { 538 { 640, 480 }, 539 { 352, 288 }, 540 { 320, 240 }, 541 { 288, 216 }, 542 { 256, 192 }, 543 { 224, 168 }, 544 { 192, 144 }, 545 { 176, 144 }, 546}; 547 548static int cpia2_enum_framesizes(struct file *file, void *fh, 549 struct v4l2_frmsizeenum *fsize) 550{ 551 if (fsize->pixel_format != V4L2_PIX_FMT_MJPEG && 552 fsize->pixel_format != V4L2_PIX_FMT_JPEG) 553 return -EINVAL; 554 if (fsize->index >= ARRAY_SIZE(cpia2_framesizes)) 555 return -EINVAL; 556 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 557 fsize->discrete.width = cpia2_framesizes[fsize->index].width; 558 fsize->discrete.height = cpia2_framesizes[fsize->index].height; 559 560 return 0; 561} 562 563static int cpia2_enum_frameintervals(struct file *file, void *fh, 564 struct v4l2_frmivalenum *fival) 565{ 566 struct camera_data *cam = video_drvdata(file); 567 int max = ARRAY_SIZE(framerate_controls) - 1; 568 int i; 569 570 if (fival->pixel_format != V4L2_PIX_FMT_MJPEG && 571 fival->pixel_format != V4L2_PIX_FMT_JPEG) 572 return -EINVAL; 573 574 /* Maximum 15 fps for this model */ 575 if (cam->params.pnp_id.device_type == DEVICE_STV_672 && 576 cam->params.version.sensor_flags == CPIA2_VP_SENSOR_FLAGS_500) 577 max -= 2; 578 if (fival->index > max) 579 return -EINVAL; 580 for (i = 0; i < ARRAY_SIZE(cpia2_framesizes); i++) 581 if (fival->width == cpia2_framesizes[i].width && 582 fival->height == cpia2_framesizes[i].height) 583 break; 584 if (i == ARRAY_SIZE(cpia2_framesizes)) 585 return -EINVAL; 586 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 587 fival->discrete = framerate_controls[fival->index].period; 588 return 0; 589} 590 591/****************************************************************************** 592 * 593 * ioctl_s_ctrl 594 * 595 * V4L2 set the value of a control variable 596 * 597 *****************************************************************************/ 598 599static int cpia2_s_ctrl(struct v4l2_ctrl *ctrl) 600{ 601 struct camera_data *cam = 602 container_of(ctrl->handler, struct camera_data, hdl); 603 static const int flicker_table[] = { 604 NEVER_FLICKER, 605 FLICKER_50, 606 FLICKER_60, 607 }; 608 609 DBG("Set control id:%d, value:%d\n", ctrl->id, ctrl->val); 610 611 switch (ctrl->id) { 612 case V4L2_CID_BRIGHTNESS: 613 cpia2_set_brightness(cam, ctrl->val); 614 break; 615 case V4L2_CID_CONTRAST: 616 cpia2_set_contrast(cam, ctrl->val); 617 break; 618 case V4L2_CID_SATURATION: 619 cpia2_set_saturation(cam, ctrl->val); 620 break; 621 case V4L2_CID_HFLIP: 622 cpia2_set_property_mirror(cam, ctrl->val); 623 break; 624 case V4L2_CID_VFLIP: 625 cpia2_set_property_flip(cam, ctrl->val); 626 break; 627 case V4L2_CID_POWER_LINE_FREQUENCY: 628 return cpia2_set_flicker_mode(cam, flicker_table[ctrl->val]); 629 case V4L2_CID_ILLUMINATORS_1: 630 return cpia2_set_gpio(cam, (cam->top_light->val << 6) | 631 (cam->bottom_light->val << 7)); 632 case V4L2_CID_JPEG_ACTIVE_MARKER: 633 cam->params.compression.inhibit_htables = 634 !(ctrl->val & V4L2_JPEG_ACTIVE_MARKER_DHT); 635 break; 636 case V4L2_CID_JPEG_COMPRESSION_QUALITY: 637 cam->params.vc_params.quality = ctrl->val; 638 break; 639 case CPIA2_CID_USB_ALT: 640 cam->params.camera_state.stream_mode = ctrl->val; 641 break; 642 default: 643 return -EINVAL; 644 } 645 646 return 0; 647} 648 649/****************************************************************************** 650 * 651 * ioctl_g_jpegcomp 652 * 653 * V4L2 get the JPEG compression parameters 654 * 655 *****************************************************************************/ 656 657static int cpia2_g_jpegcomp(struct file *file, void *fh, struct v4l2_jpegcompression *parms) 658{ 659 struct camera_data *cam = video_drvdata(file); 660 661 memset(parms, 0, sizeof(*parms)); 662 663 parms->quality = 80; // TODO: Can this be made meaningful? 664 665 parms->jpeg_markers = V4L2_JPEG_MARKER_DQT | V4L2_JPEG_MARKER_DRI; 666 if (!cam->params.compression.inhibit_htables) 667 parms->jpeg_markers |= V4L2_JPEG_MARKER_DHT; 668 669 parms->APPn = cam->APPn; 670 parms->APP_len = cam->APP_len; 671 if (cam->APP_len > 0) { 672 memcpy(parms->APP_data, cam->APP_data, cam->APP_len); 673 parms->jpeg_markers |= V4L2_JPEG_MARKER_APP; 674 } 675 676 parms->COM_len = cam->COM_len; 677 if (cam->COM_len > 0) { 678 memcpy(parms->COM_data, cam->COM_data, cam->COM_len); 679 parms->jpeg_markers |= JPEG_MARKER_COM; 680 } 681 682 DBG("G_JPEGCOMP APP_len:%d COM_len:%d\n", 683 parms->APP_len, parms->COM_len); 684 685 return 0; 686} 687 688/****************************************************************************** 689 * 690 * ioctl_s_jpegcomp 691 * 692 * V4L2 set the JPEG compression parameters 693 * NOTE: quality and some jpeg_markers are ignored. 694 * 695 *****************************************************************************/ 696 697static int cpia2_s_jpegcomp(struct file *file, void *fh, 698 const struct v4l2_jpegcompression *parms) 699{ 700 struct camera_data *cam = video_drvdata(file); 701 702 DBG("S_JPEGCOMP APP_len:%d COM_len:%d\n", 703 parms->APP_len, parms->COM_len); 704 705 cam->params.compression.inhibit_htables = 706 !(parms->jpeg_markers & V4L2_JPEG_MARKER_DHT); 707 708 if (parms->APP_len != 0) { 709 if (parms->APP_len > 0 && 710 parms->APP_len <= sizeof(cam->APP_data) && 711 parms->APPn >= 0 && parms->APPn <= 15) { 712 cam->APPn = parms->APPn; 713 cam->APP_len = parms->APP_len; 714 memcpy(cam->APP_data, parms->APP_data, parms->APP_len); 715 } else { 716 LOG("Bad APPn Params n=%d len=%d\n", 717 parms->APPn, parms->APP_len); 718 return -EINVAL; 719 } 720 } else { 721 cam->APP_len = 0; 722 } 723 724 if (parms->COM_len != 0) { 725 if (parms->COM_len > 0 && 726 parms->COM_len <= sizeof(cam->COM_data)) { 727 cam->COM_len = parms->COM_len; 728 memcpy(cam->COM_data, parms->COM_data, parms->COM_len); 729 } else { 730 LOG("Bad COM_len=%d\n", parms->COM_len); 731 return -EINVAL; 732 } 733 } 734 735 return 0; 736} 737 738/****************************************************************************** 739 * 740 * ioctl_reqbufs 741 * 742 * V4L2 Initiate memory mapping. 743 * NOTE: The user's request is ignored. For now the buffers are fixed. 744 * 745 *****************************************************************************/ 746 747static int cpia2_reqbufs(struct file *file, void *fh, struct v4l2_requestbuffers *req) 748{ 749 struct camera_data *cam = video_drvdata(file); 750 751 if (req->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 752 req->memory != V4L2_MEMORY_MMAP) 753 return -EINVAL; 754 755 DBG("REQBUFS requested:%d returning:%d\n", req->count, cam->num_frames); 756 req->count = cam->num_frames; 757 memset(&req->reserved, 0, sizeof(req->reserved)); 758 759 return 0; 760} 761 762/****************************************************************************** 763 * 764 * ioctl_querybuf 765 * 766 * V4L2 Query memory buffer status. 767 * 768 *****************************************************************************/ 769 770static int cpia2_querybuf(struct file *file, void *fh, struct v4l2_buffer *buf) 771{ 772 struct camera_data *cam = video_drvdata(file); 773 774 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 775 buf->index >= cam->num_frames) 776 return -EINVAL; 777 778 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer; 779 buf->length = cam->frame_size; 780 781 buf->memory = V4L2_MEMORY_MMAP; 782 783 if (cam->mmapped) 784 buf->flags = V4L2_BUF_FLAG_MAPPED; 785 else 786 buf->flags = 0; 787 788 buf->flags |= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 789 790 switch (cam->buffers[buf->index].status) { 791 case FRAME_EMPTY: 792 case FRAME_ERROR: 793 case FRAME_READING: 794 buf->bytesused = 0; 795 buf->flags = V4L2_BUF_FLAG_QUEUED; 796 break; 797 case FRAME_READY: 798 buf->bytesused = cam->buffers[buf->index].length; 799 v4l2_buffer_set_timestamp(buf, cam->buffers[buf->index].ts); 800 buf->sequence = cam->buffers[buf->index].seq; 801 buf->flags = V4L2_BUF_FLAG_DONE; 802 break; 803 } 804 805 DBG("QUERYBUF index:%d offset:%d flags:%d seq:%d bytesused:%d\n", 806 buf->index, buf->m.offset, buf->flags, buf->sequence, 807 buf->bytesused); 808 809 return 0; 810} 811 812/****************************************************************************** 813 * 814 * ioctl_qbuf 815 * 816 * V4L2 User is freeing buffer 817 * 818 *****************************************************************************/ 819 820static int cpia2_qbuf(struct file *file, void *fh, struct v4l2_buffer *buf) 821{ 822 struct camera_data *cam = video_drvdata(file); 823 824 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 825 buf->memory != V4L2_MEMORY_MMAP || 826 buf->index >= cam->num_frames) 827 return -EINVAL; 828 829 DBG("QBUF #%d\n", buf->index); 830 831 if (cam->buffers[buf->index].status == FRAME_READY) 832 cam->buffers[buf->index].status = FRAME_EMPTY; 833 834 return 0; 835} 836 837/****************************************************************************** 838 * 839 * find_earliest_filled_buffer 840 * 841 * Helper for ioctl_dqbuf. Find the next ready buffer. 842 * 843 *****************************************************************************/ 844 845static int find_earliest_filled_buffer(struct camera_data *cam) 846{ 847 int i; 848 int found = -1; 849 850 for (i = 0; i < cam->num_frames; i++) { 851 if (cam->buffers[i].status == FRAME_READY) { 852 if (found < 0) { 853 found = i; 854 } else { 855 /* find which buffer is earlier */ 856 if (cam->buffers[i].ts < cam->buffers[found].ts) 857 found = i; 858 } 859 } 860 } 861 return found; 862} 863 864/****************************************************************************** 865 * 866 * ioctl_dqbuf 867 * 868 * V4L2 User is asking for a filled buffer. 869 * 870 *****************************************************************************/ 871 872static int cpia2_dqbuf(struct file *file, void *fh, struct v4l2_buffer *buf) 873{ 874 struct camera_data *cam = video_drvdata(file); 875 int frame; 876 877 if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE || 878 buf->memory != V4L2_MEMORY_MMAP) 879 return -EINVAL; 880 881 frame = find_earliest_filled_buffer(cam); 882 883 if (frame < 0 && file->f_flags & O_NONBLOCK) 884 return -EAGAIN; 885 886 if (frame < 0) { 887 /* Wait for a frame to become available */ 888 struct framebuf *cb = cam->curbuff; 889 890 mutex_unlock(&cam->v4l2_lock); 891 wait_event_interruptible(cam->wq_stream, 892 !video_is_registered(&cam->vdev) || 893 (cb = cam->curbuff)->status == FRAME_READY); 894 mutex_lock(&cam->v4l2_lock); 895 if (signal_pending(current)) 896 return -ERESTARTSYS; 897 if (!video_is_registered(&cam->vdev)) 898 return -ENOTTY; 899 frame = cb->num; 900 } 901 902 buf->index = frame; 903 buf->bytesused = cam->buffers[buf->index].length; 904 buf->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_DONE 905 | V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 906 buf->field = V4L2_FIELD_NONE; 907 v4l2_buffer_set_timestamp(buf, cam->buffers[buf->index].ts); 908 buf->sequence = cam->buffers[buf->index].seq; 909 buf->m.offset = cam->buffers[buf->index].data - cam->frame_buffer; 910 buf->length = cam->frame_size; 911 buf->reserved2 = 0; 912 buf->request_fd = 0; 913 memset(&buf->timecode, 0, sizeof(buf->timecode)); 914 915 DBG("DQBUF #%d status:%d seq:%d length:%d\n", buf->index, 916 cam->buffers[buf->index].status, buf->sequence, buf->bytesused); 917 918 return 0; 919} 920 921static int cpia2_streamon(struct file *file, void *fh, enum v4l2_buf_type type) 922{ 923 struct camera_data *cam = video_drvdata(file); 924 int ret = -EINVAL; 925 926 DBG("VIDIOC_STREAMON, streaming=%d\n", cam->streaming); 927 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 928 return -EINVAL; 929 930 if (!cam->streaming) { 931 ret = cpia2_usb_stream_start(cam, 932 cam->params.camera_state.stream_mode); 933 if (!ret) 934 v4l2_ctrl_grab(cam->usb_alt, true); 935 } 936 return ret; 937} 938 939static int cpia2_streamoff(struct file *file, void *fh, enum v4l2_buf_type type) 940{ 941 struct camera_data *cam = video_drvdata(file); 942 int ret = -EINVAL; 943 944 DBG("VIDIOC_STREAMOFF, streaming=%d\n", cam->streaming); 945 if (!cam->mmapped || type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 946 return -EINVAL; 947 948 if (cam->streaming) { 949 ret = cpia2_usb_stream_stop(cam); 950 if (!ret) 951 v4l2_ctrl_grab(cam->usb_alt, false); 952 } 953 return ret; 954} 955 956/****************************************************************************** 957 * 958 * cpia2_mmap 959 * 960 *****************************************************************************/ 961static int cpia2_mmap(struct file *file, struct vm_area_struct *area) 962{ 963 struct camera_data *cam = video_drvdata(file); 964 int retval; 965 966 if (mutex_lock_interruptible(&cam->v4l2_lock)) 967 return -ERESTARTSYS; 968 retval = cpia2_remap_buffer(cam, area); 969 970 if (!retval) 971 cam->stream_fh = file->private_data; 972 mutex_unlock(&cam->v4l2_lock); 973 return retval; 974} 975 976/****************************************************************************** 977 * 978 * reset_camera_struct_v4l 979 * 980 * Sets all values to the defaults 981 *****************************************************************************/ 982static void reset_camera_struct_v4l(struct camera_data *cam) 983{ 984 cam->width = cam->params.roi.width; 985 cam->height = cam->params.roi.height; 986 987 cam->frame_size = buffer_size; 988 cam->num_frames = num_buffers; 989 990 /* Flicker modes */ 991 cam->params.flicker_control.flicker_mode_req = flicker_mode; 992 993 /* stream modes */ 994 cam->params.camera_state.stream_mode = alternate; 995 996 cam->pixelformat = V4L2_PIX_FMT_JPEG; 997} 998 999static const struct v4l2_ioctl_ops cpia2_ioctl_ops = { 1000 .vidioc_querycap = cpia2_querycap, 1001 .vidioc_enum_input = cpia2_enum_input, 1002 .vidioc_g_input = cpia2_g_input, 1003 .vidioc_s_input = cpia2_s_input, 1004 .vidioc_enum_fmt_vid_cap = cpia2_enum_fmt_vid_cap, 1005 .vidioc_g_fmt_vid_cap = cpia2_g_fmt_vid_cap, 1006 .vidioc_s_fmt_vid_cap = cpia2_s_fmt_vid_cap, 1007 .vidioc_try_fmt_vid_cap = cpia2_try_fmt_vid_cap, 1008 .vidioc_g_jpegcomp = cpia2_g_jpegcomp, 1009 .vidioc_s_jpegcomp = cpia2_s_jpegcomp, 1010 .vidioc_g_selection = cpia2_g_selection, 1011 .vidioc_reqbufs = cpia2_reqbufs, 1012 .vidioc_querybuf = cpia2_querybuf, 1013 .vidioc_qbuf = cpia2_qbuf, 1014 .vidioc_dqbuf = cpia2_dqbuf, 1015 .vidioc_streamon = cpia2_streamon, 1016 .vidioc_streamoff = cpia2_streamoff, 1017 .vidioc_s_parm = cpia2_s_parm, 1018 .vidioc_g_parm = cpia2_g_parm, 1019 .vidioc_enum_framesizes = cpia2_enum_framesizes, 1020 .vidioc_enum_frameintervals = cpia2_enum_frameintervals, 1021 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1022 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1023}; 1024 1025/*** 1026 * The v4l video device structure initialized for this device 1027 ***/ 1028static const struct v4l2_file_operations cpia2_fops = { 1029 .owner = THIS_MODULE, 1030 .open = cpia2_open, 1031 .release = cpia2_close, 1032 .read = cpia2_v4l_read, 1033 .poll = cpia2_v4l_poll, 1034 .unlocked_ioctl = video_ioctl2, 1035 .mmap = cpia2_mmap, 1036}; 1037 1038static const struct video_device cpia2_template = { 1039 /* I could not find any place for the old .initialize initializer?? */ 1040 .name = "CPiA2 Camera", 1041 .fops = &cpia2_fops, 1042 .ioctl_ops = &cpia2_ioctl_ops, 1043 .release = video_device_release_empty, 1044}; 1045 1046void cpia2_camera_release(struct v4l2_device *v4l2_dev) 1047{ 1048 struct camera_data *cam = 1049 container_of(v4l2_dev, struct camera_data, v4l2_dev); 1050 1051 v4l2_ctrl_handler_free(&cam->hdl); 1052 v4l2_device_unregister(&cam->v4l2_dev); 1053 kfree(cam); 1054} 1055 1056static const struct v4l2_ctrl_ops cpia2_ctrl_ops = { 1057 .s_ctrl = cpia2_s_ctrl, 1058}; 1059 1060/****************************************************************************** 1061 * 1062 * cpia2_register_camera 1063 * 1064 *****************************************************************************/ 1065int cpia2_register_camera(struct camera_data *cam) 1066{ 1067 struct v4l2_ctrl_handler *hdl = &cam->hdl; 1068 struct v4l2_ctrl_config cpia2_usb_alt = { 1069 .ops = &cpia2_ctrl_ops, 1070 .id = CPIA2_CID_USB_ALT, 1071 .name = "USB Alternate", 1072 .type = V4L2_CTRL_TYPE_INTEGER, 1073 .min = USBIF_ISO_1, 1074 .max = USBIF_ISO_6, 1075 .step = 1, 1076 }; 1077 int ret; 1078 1079 v4l2_ctrl_handler_init(hdl, 12); 1080 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1081 V4L2_CID_BRIGHTNESS, 1082 cam->params.pnp_id.device_type == DEVICE_STV_672 ? 1 : 0, 1083 255, 1, DEFAULT_BRIGHTNESS); 1084 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1085 V4L2_CID_CONTRAST, 0, 255, 1, DEFAULT_CONTRAST); 1086 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1087 V4L2_CID_SATURATION, 0, 255, 1, DEFAULT_SATURATION); 1088 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1089 V4L2_CID_HFLIP, 0, 1, 1, 0); 1090 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1091 V4L2_CID_JPEG_ACTIVE_MARKER, 0, 1092 V4L2_JPEG_ACTIVE_MARKER_DHT, 0, 1093 V4L2_JPEG_ACTIVE_MARKER_DHT); 1094 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1095 V4L2_CID_JPEG_COMPRESSION_QUALITY, 1, 1096 100, 1, 100); 1097 cpia2_usb_alt.def = alternate; 1098 cam->usb_alt = v4l2_ctrl_new_custom(hdl, &cpia2_usb_alt, NULL); 1099 /* VP5 Only */ 1100 if (cam->params.pnp_id.device_type != DEVICE_STV_672) 1101 v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1102 V4L2_CID_VFLIP, 0, 1, 1, 0); 1103 /* Flicker control only valid for 672 */ 1104 if (cam->params.pnp_id.device_type == DEVICE_STV_672) 1105 v4l2_ctrl_new_std_menu(hdl, &cpia2_ctrl_ops, 1106 V4L2_CID_POWER_LINE_FREQUENCY, 1107 V4L2_CID_POWER_LINE_FREQUENCY_60HZ, 1108 0, 0); 1109 /* Light control only valid for the QX5 Microscope */ 1110 if (cam->params.pnp_id.product == 0x151) { 1111 cam->top_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1112 V4L2_CID_ILLUMINATORS_1, 1113 0, 1, 1, 0); 1114 cam->bottom_light = v4l2_ctrl_new_std(hdl, &cpia2_ctrl_ops, 1115 V4L2_CID_ILLUMINATORS_2, 1116 0, 1, 1, 0); 1117 v4l2_ctrl_cluster(2, &cam->top_light); 1118 } 1119 1120 if (hdl->error) { 1121 ret = hdl->error; 1122 v4l2_ctrl_handler_free(hdl); 1123 return ret; 1124 } 1125 1126 cam->vdev = cpia2_template; 1127 video_set_drvdata(&cam->vdev, cam); 1128 cam->vdev.lock = &cam->v4l2_lock; 1129 cam->vdev.ctrl_handler = hdl; 1130 cam->vdev.v4l2_dev = &cam->v4l2_dev; 1131 cam->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | 1132 V4L2_CAP_STREAMING; 1133 1134 reset_camera_struct_v4l(cam); 1135 1136 /* register v4l device */ 1137 if (video_register_device(&cam->vdev, VFL_TYPE_VIDEO, video_nr) < 0) { 1138 ERR("video_register_device failed\n"); 1139 return -ENODEV; 1140 } 1141 1142 return 0; 1143} 1144 1145/****************************************************************************** 1146 * 1147 * cpia2_unregister_camera 1148 * 1149 *****************************************************************************/ 1150void cpia2_unregister_camera(struct camera_data *cam) 1151{ 1152 video_unregister_device(&cam->vdev); 1153} 1154 1155/****************************************************************************** 1156 * 1157 * check_parameters 1158 * 1159 * Make sure that all user-supplied parameters are sensible 1160 *****************************************************************************/ 1161static void __init check_parameters(void) 1162{ 1163 if (buffer_size < PAGE_SIZE) { 1164 buffer_size = PAGE_SIZE; 1165 LOG("buffer_size too small, setting to %d\n", buffer_size); 1166 } else if (buffer_size > 1024 * 1024) { 1167 /* arbitrary upper limiit */ 1168 buffer_size = 1024 * 1024; 1169 LOG("buffer_size ridiculously large, setting to %d\n", 1170 buffer_size); 1171 } else { 1172 buffer_size += PAGE_SIZE - 1; 1173 buffer_size &= ~(PAGE_SIZE - 1); 1174 } 1175 1176 if (num_buffers < 1) { 1177 num_buffers = 1; 1178 LOG("num_buffers too small, setting to %d\n", num_buffers); 1179 } else if (num_buffers > VIDEO_MAX_FRAME) { 1180 num_buffers = VIDEO_MAX_FRAME; 1181 LOG("num_buffers too large, setting to %d\n", num_buffers); 1182 } 1183 1184 if (alternate < USBIF_ISO_1 || alternate > USBIF_ISO_6) { 1185 alternate = DEFAULT_ALT; 1186 LOG("alternate specified is invalid, using %d\n", alternate); 1187 } 1188 1189 if (flicker_mode != 0 && flicker_mode != FLICKER_50 && flicker_mode != FLICKER_60) { 1190 flicker_mode = 0; 1191 LOG("Flicker mode specified is invalid, using %d\n", 1192 flicker_mode); 1193 } 1194 1195 DBG("Using %d buffers, each %d bytes, alternate=%d\n", 1196 num_buffers, buffer_size, alternate); 1197} 1198 1199/************ Module Stuff ***************/ 1200 1201/****************************************************************************** 1202 * 1203 * cpia2_init/module_init 1204 * 1205 *****************************************************************************/ 1206static int __init cpia2_init(void) 1207{ 1208 LOG("%s v%s\n", 1209 ABOUT, CPIA_VERSION); 1210 check_parameters(); 1211 return cpia2_usb_init(); 1212} 1213 1214/****************************************************************************** 1215 * 1216 * cpia2_exit/module_exit 1217 * 1218 *****************************************************************************/ 1219static void __exit cpia2_exit(void) 1220{ 1221 cpia2_usb_cleanup(); 1222 schedule_timeout(2 * HZ); 1223} 1224 1225module_init(cpia2_init); 1226module_exit(cpia2_exit);