1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2005-2006 Micronas USA Inc. 4 */ 5 6 #include <linux/module.h> 7 #include <linux/delay.h> 8 #include <linux/sched.h> 9 #include <linux/spinlock.h> 10 #include <linux/slab.h> 11 #include <linux/fs.h> 12 #include <linux/unistd.h> 13 #include <linux/time.h> 14 #include <linux/vmalloc.h> 15 #include <linux/pagemap.h> 16 #include <linux/i2c.h> 17 #include <linux/mutex.h> 18 #include <linux/uaccess.h> 19 #include <linux/videodev2.h> 20 #include <media/v4l2-common.h> 21 #include <media/v4l2-ioctl.h> 22 #include <media/v4l2-subdev.h> 23 #include <media/v4l2-event.h> 24 #include <media/videobuf2-vmalloc.h> 25 #include <media/i2c/saa7115.h> 26 27 #include "go7007-priv.h" 28 29 #define call_all(dev, o, f, args...) \ 30 v4l2_device_call_until_err(dev, 0, o, f, ##args) 31 32 static bool valid_pixelformat(u32 pixelformat) 33 { 34 switch (pixelformat) { 35 case V4L2_PIX_FMT_MJPEG: 36 case V4L2_PIX_FMT_MPEG1: 37 case V4L2_PIX_FMT_MPEG2: 38 case V4L2_PIX_FMT_MPEG4: 39 return true; 40 default: 41 return false; 42 } 43 } 44 45 static u32 get_frame_type_flag(struct go7007_buffer *vb, int format) 46 { 47 u8 *ptr = vb2_plane_vaddr(&vb->vb.vb2_buf, 0); 48 49 switch (format) { 50 case V4L2_PIX_FMT_MJPEG: 51 return V4L2_BUF_FLAG_KEYFRAME; 52 case V4L2_PIX_FMT_MPEG4: 53 switch ((ptr[vb->frame_offset + 4] >> 6) & 0x3) { 54 case 0: 55 return V4L2_BUF_FLAG_KEYFRAME; 56 case 1: 57 return V4L2_BUF_FLAG_PFRAME; 58 case 2: 59 return V4L2_BUF_FLAG_BFRAME; 60 default: 61 return 0; 62 } 63 case V4L2_PIX_FMT_MPEG1: 64 case V4L2_PIX_FMT_MPEG2: 65 switch ((ptr[vb->frame_offset + 5] >> 3) & 0x7) { 66 case 1: 67 return V4L2_BUF_FLAG_KEYFRAME; 68 case 2: 69 return V4L2_BUF_FLAG_PFRAME; 70 case 3: 71 return V4L2_BUF_FLAG_BFRAME; 72 default: 73 return 0; 74 } 75 } 76 77 return 0; 78 } 79 80 static void get_resolution(struct go7007 *go, int *width, int *height) 81 { 82 switch (go->standard) { 83 case GO7007_STD_NTSC: 84 *width = 720; 85 *height = 480; 86 break; 87 case GO7007_STD_PAL: 88 *width = 720; 89 *height = 576; 90 break; 91 case GO7007_STD_OTHER: 92 default: 93 *width = go->board_info->sensor_width; 94 *height = go->board_info->sensor_height; 95 break; 96 } 97 } 98 99 static void set_formatting(struct go7007 *go) 100 { 101 if (go->format == V4L2_PIX_FMT_MJPEG) { 102 go->pali = 0; 103 go->aspect_ratio = GO7007_RATIO_1_1; 104 go->gop_size = 0; 105 go->ipb = 0; 106 go->closed_gop = 0; 107 go->repeat_seqhead = 0; 108 go->seq_header_enable = 0; 109 go->gop_header_enable = 0; 110 go->dvd_mode = 0; 111 return; 112 } 113 114 switch (go->format) { 115 case V4L2_PIX_FMT_MPEG1: 116 go->pali = 0; 117 break; 118 default: 119 case V4L2_PIX_FMT_MPEG2: 120 go->pali = 0x48; 121 break; 122 case V4L2_PIX_FMT_MPEG4: 123 /* For future reference: this is the list of MPEG4 124 * profiles that are available, although they are 125 * untested: 126 * 127 * Profile pali 128 * -------------- ---- 129 * PROFILE_S_L0 0x08 130 * PROFILE_S_L1 0x01 131 * PROFILE_S_L2 0x02 132 * PROFILE_S_L3 0x03 133 * PROFILE_ARTS_L1 0x91 134 * PROFILE_ARTS_L2 0x92 135 * PROFILE_ARTS_L3 0x93 136 * PROFILE_ARTS_L4 0x94 137 * PROFILE_AS_L0 0xf0 138 * PROFILE_AS_L1 0xf1 139 * PROFILE_AS_L2 0xf2 140 * PROFILE_AS_L3 0xf3 141 * PROFILE_AS_L4 0xf4 142 * PROFILE_AS_L5 0xf5 143 */ 144 go->pali = 0xf5; 145 break; 146 } 147 go->gop_size = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_size); 148 go->closed_gop = v4l2_ctrl_g_ctrl(go->mpeg_video_gop_closure); 149 go->ipb = v4l2_ctrl_g_ctrl(go->mpeg_video_b_frames) != 0; 150 go->bitrate = v4l2_ctrl_g_ctrl(go->mpeg_video_bitrate); 151 go->repeat_seqhead = v4l2_ctrl_g_ctrl(go->mpeg_video_rep_seqheader); 152 go->gop_header_enable = 1; 153 go->dvd_mode = 0; 154 if (go->format == V4L2_PIX_FMT_MPEG2) 155 go->dvd_mode = 156 go->bitrate == 9800000 && 157 go->gop_size == 15 && 158 go->ipb == 0 && 159 go->repeat_seqhead == 1 && 160 go->closed_gop; 161 162 switch (v4l2_ctrl_g_ctrl(go->mpeg_video_aspect_ratio)) { 163 default: 164 case V4L2_MPEG_VIDEO_ASPECT_1x1: 165 go->aspect_ratio = GO7007_RATIO_1_1; 166 break; 167 case V4L2_MPEG_VIDEO_ASPECT_4x3: 168 go->aspect_ratio = GO7007_RATIO_4_3; 169 break; 170 case V4L2_MPEG_VIDEO_ASPECT_16x9: 171 go->aspect_ratio = GO7007_RATIO_16_9; 172 break; 173 } 174 } 175 176 static int set_capture_size(struct go7007 *go, struct v4l2_format *fmt, int try) 177 { 178 int sensor_height = 0, sensor_width = 0; 179 int width, height; 180 181 if (fmt != NULL && !valid_pixelformat(fmt->fmt.pix.pixelformat)) 182 return -EINVAL; 183 184 get_resolution(go, &sensor_width, &sensor_height); 185 186 if (fmt == NULL) { 187 width = sensor_width; 188 height = sensor_height; 189 } else if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) { 190 if (fmt->fmt.pix.width > sensor_width) 191 width = sensor_width; 192 else if (fmt->fmt.pix.width < 144) 193 width = 144; 194 else 195 width = fmt->fmt.pix.width & ~0x0f; 196 197 if (fmt->fmt.pix.height > sensor_height) 198 height = sensor_height; 199 else if (fmt->fmt.pix.height < 96) 200 height = 96; 201 else 202 height = fmt->fmt.pix.height & ~0x0f; 203 } else { 204 width = fmt->fmt.pix.width; 205 206 if (width <= sensor_width / 4) { 207 width = sensor_width / 4; 208 height = sensor_height / 4; 209 } else if (width <= sensor_width / 2) { 210 width = sensor_width / 2; 211 height = sensor_height / 2; 212 } else { 213 width = sensor_width; 214 height = sensor_height; 215 } 216 width &= ~0xf; 217 height &= ~0xf; 218 } 219 220 if (fmt != NULL) { 221 u32 pixelformat = fmt->fmt.pix.pixelformat; 222 223 memset(fmt, 0, sizeof(*fmt)); 224 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 225 fmt->fmt.pix.width = width; 226 fmt->fmt.pix.height = height; 227 fmt->fmt.pix.pixelformat = pixelformat; 228 fmt->fmt.pix.field = V4L2_FIELD_NONE; 229 fmt->fmt.pix.bytesperline = 0; 230 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE; 231 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 232 } 233 234 if (try) 235 return 0; 236 237 if (fmt) 238 go->format = fmt->fmt.pix.pixelformat; 239 go->width = width; 240 go->height = height; 241 go->encoder_h_offset = go->board_info->sensor_h_offset; 242 go->encoder_v_offset = go->board_info->sensor_v_offset; 243 244 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) { 245 struct v4l2_subdev_format format = { 246 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 247 }; 248 249 format.format.code = MEDIA_BUS_FMT_FIXED; 250 format.format.width = fmt ? fmt->fmt.pix.width : width; 251 format.format.height = height; 252 go->encoder_h_halve = 0; 253 go->encoder_v_halve = 0; 254 go->encoder_subsample = 0; 255 call_all(&go->v4l2_dev, pad, set_fmt, NULL, &format); 256 } else { 257 if (width <= sensor_width / 4) { 258 go->encoder_h_halve = 1; 259 go->encoder_v_halve = 1; 260 go->encoder_subsample = 1; 261 } else if (width <= sensor_width / 2) { 262 go->encoder_h_halve = 1; 263 go->encoder_v_halve = 1; 264 go->encoder_subsample = 0; 265 } else { 266 go->encoder_h_halve = 0; 267 go->encoder_v_halve = 0; 268 go->encoder_subsample = 0; 269 } 270 } 271 return 0; 272 } 273 274 static int vidioc_querycap(struct file *file, void *priv, 275 struct v4l2_capability *cap) 276 { 277 struct go7007 *go = video_drvdata(file); 278 279 strscpy(cap->driver, "go7007", sizeof(cap->driver)); 280 strscpy(cap->card, go->name, sizeof(cap->card)); 281 strscpy(cap->bus_info, go->bus_info, sizeof(cap->bus_info)); 282 283 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE | 284 V4L2_CAP_STREAMING; 285 286 if (go->board_info->num_aud_inputs) 287 cap->device_caps |= V4L2_CAP_AUDIO; 288 if (go->board_info->flags & GO7007_BOARD_HAS_TUNER) 289 cap->device_caps |= V4L2_CAP_TUNER; 290 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 291 return 0; 292 } 293 294 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 295 struct v4l2_fmtdesc *fmt) 296 { 297 char *desc = NULL; 298 299 switch (fmt->index) { 300 case 0: 301 fmt->pixelformat = V4L2_PIX_FMT_MJPEG; 302 desc = "Motion JPEG"; 303 break; 304 case 1: 305 fmt->pixelformat = V4L2_PIX_FMT_MPEG1; 306 desc = "MPEG-1 ES"; 307 break; 308 case 2: 309 fmt->pixelformat = V4L2_PIX_FMT_MPEG2; 310 desc = "MPEG-2 ES"; 311 break; 312 case 3: 313 fmt->pixelformat = V4L2_PIX_FMT_MPEG4; 314 desc = "MPEG-4 ES"; 315 break; 316 default: 317 return -EINVAL; 318 } 319 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 320 fmt->flags = V4L2_FMT_FLAG_COMPRESSED; 321 322 strscpy(fmt->description, desc, sizeof(fmt->description)); 323 324 return 0; 325 } 326 327 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 328 struct v4l2_format *fmt) 329 { 330 struct go7007 *go = video_drvdata(file); 331 332 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 333 fmt->fmt.pix.width = go->width; 334 fmt->fmt.pix.height = go->height; 335 fmt->fmt.pix.pixelformat = go->format; 336 fmt->fmt.pix.field = V4L2_FIELD_NONE; 337 fmt->fmt.pix.bytesperline = 0; 338 fmt->fmt.pix.sizeimage = GO7007_BUF_SIZE; 339 fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 340 341 return 0; 342 } 343 344 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 345 struct v4l2_format *fmt) 346 { 347 struct go7007 *go = video_drvdata(file); 348 349 return set_capture_size(go, fmt, 1); 350 } 351 352 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 353 struct v4l2_format *fmt) 354 { 355 struct go7007 *go = video_drvdata(file); 356 357 if (vb2_is_busy(&go->vidq)) 358 return -EBUSY; 359 360 return set_capture_size(go, fmt, 0); 361 } 362 363 static int go7007_queue_setup(struct vb2_queue *q, 364 unsigned int *num_buffers, unsigned int *num_planes, 365 unsigned int sizes[], struct device *alloc_devs[]) 366 { 367 sizes[0] = GO7007_BUF_SIZE; 368 *num_planes = 1; 369 370 if (*num_buffers < 2) 371 *num_buffers = 2; 372 373 return 0; 374 } 375 376 static void go7007_buf_queue(struct vb2_buffer *vb) 377 { 378 struct vb2_queue *vq = vb->vb2_queue; 379 struct go7007 *go = vb2_get_drv_priv(vq); 380 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 381 struct go7007_buffer *go7007_vb = 382 container_of(vbuf, struct go7007_buffer, vb); 383 unsigned long flags; 384 385 spin_lock_irqsave(&go->spinlock, flags); 386 list_add_tail(&go7007_vb->list, &go->vidq_active); 387 spin_unlock_irqrestore(&go->spinlock, flags); 388 } 389 390 static int go7007_buf_prepare(struct vb2_buffer *vb) 391 { 392 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 393 struct go7007_buffer *go7007_vb = 394 container_of(vbuf, struct go7007_buffer, vb); 395 396 go7007_vb->modet_active = 0; 397 go7007_vb->frame_offset = 0; 398 vb->planes[0].bytesused = 0; 399 return 0; 400 } 401 402 static void go7007_buf_finish(struct vb2_buffer *vb) 403 { 404 struct vb2_queue *vq = vb->vb2_queue; 405 struct go7007 *go = vb2_get_drv_priv(vq); 406 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 407 struct go7007_buffer *go7007_vb = 408 container_of(vbuf, struct go7007_buffer, vb); 409 u32 frame_type_flag = get_frame_type_flag(go7007_vb, go->format); 410 411 vbuf->flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_BFRAME | 412 V4L2_BUF_FLAG_PFRAME); 413 vbuf->flags |= frame_type_flag; 414 vbuf->field = V4L2_FIELD_NONE; 415 } 416 417 static int go7007_start_streaming(struct vb2_queue *q, unsigned int count) 418 { 419 struct go7007 *go = vb2_get_drv_priv(q); 420 int ret; 421 422 set_formatting(go); 423 mutex_lock(&go->hw_lock); 424 go->next_seq = 0; 425 go->active_buf = NULL; 426 go->modet_event_status = 0; 427 q->streaming = 1; 428 if (go7007_start_encoder(go) < 0) 429 ret = -EIO; 430 else 431 ret = 0; 432 mutex_unlock(&go->hw_lock); 433 if (ret) { 434 q->streaming = 0; 435 return ret; 436 } 437 call_all(&go->v4l2_dev, video, s_stream, 1); 438 v4l2_ctrl_grab(go->mpeg_video_gop_size, true); 439 v4l2_ctrl_grab(go->mpeg_video_gop_closure, true); 440 v4l2_ctrl_grab(go->mpeg_video_bitrate, true); 441 v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, true); 442 /* Turn on Capture LED */ 443 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) 444 go7007_write_addr(go, 0x3c82, 0x0005); 445 return ret; 446 } 447 448 static void go7007_stop_streaming(struct vb2_queue *q) 449 { 450 struct go7007 *go = vb2_get_drv_priv(q); 451 unsigned long flags; 452 453 q->streaming = 0; 454 go7007_stream_stop(go); 455 mutex_lock(&go->hw_lock); 456 go7007_reset_encoder(go); 457 mutex_unlock(&go->hw_lock); 458 call_all(&go->v4l2_dev, video, s_stream, 0); 459 460 spin_lock_irqsave(&go->spinlock, flags); 461 INIT_LIST_HEAD(&go->vidq_active); 462 spin_unlock_irqrestore(&go->spinlock, flags); 463 v4l2_ctrl_grab(go->mpeg_video_gop_size, false); 464 v4l2_ctrl_grab(go->mpeg_video_gop_closure, false); 465 v4l2_ctrl_grab(go->mpeg_video_bitrate, false); 466 v4l2_ctrl_grab(go->mpeg_video_aspect_ratio, false); 467 /* Turn on Capture LED */ 468 if (go->board_id == GO7007_BOARDID_ADS_USBAV_709) 469 go7007_write_addr(go, 0x3c82, 0x000d); 470 } 471 472 static const struct vb2_ops go7007_video_qops = { 473 .queue_setup = go7007_queue_setup, 474 .buf_queue = go7007_buf_queue, 475 .buf_prepare = go7007_buf_prepare, 476 .buf_finish = go7007_buf_finish, 477 .start_streaming = go7007_start_streaming, 478 .stop_streaming = go7007_stop_streaming, 479 .wait_prepare = vb2_ops_wait_prepare, 480 .wait_finish = vb2_ops_wait_finish, 481 }; 482 483 static int vidioc_g_parm(struct file *filp, void *priv, 484 struct v4l2_streamparm *parm) 485 { 486 struct go7007 *go = video_drvdata(filp); 487 struct v4l2_fract timeperframe = { 488 .numerator = 1001 * go->fps_scale, 489 .denominator = go->sensor_framerate, 490 }; 491 492 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 493 return -EINVAL; 494 495 parm->parm.capture.readbuffers = 2; 496 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME; 497 parm->parm.capture.timeperframe = timeperframe; 498 499 return 0; 500 } 501 502 static int vidioc_s_parm(struct file *filp, void *priv, 503 struct v4l2_streamparm *parm) 504 { 505 struct go7007 *go = video_drvdata(filp); 506 unsigned int n, d; 507 508 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 509 return -EINVAL; 510 511 n = go->sensor_framerate * 512 parm->parm.capture.timeperframe.numerator; 513 d = 1001 * parm->parm.capture.timeperframe.denominator; 514 if (n != 0 && d != 0 && n > d) 515 go->fps_scale = (n + d/2) / d; 516 else 517 go->fps_scale = 1; 518 519 return vidioc_g_parm(filp, priv, parm); 520 } 521 522 /* VIDIOC_ENUMSTD on go7007 were used for enumerating the supported fps and 523 its resolution, when the device is not connected to TV. 524 This is were an API abuse, probably used by the lack of specific IOCTL's to 525 enumerate it, by the time the driver was written. 526 527 However, since kernel 2.6.19, two new ioctls (VIDIOC_ENUM_FRAMEINTERVALS 528 and VIDIOC_ENUM_FRAMESIZES) were added for this purpose. 529 530 The two functions below implement the newer ioctls 531 */ 532 static int vidioc_enum_framesizes(struct file *filp, void *priv, 533 struct v4l2_frmsizeenum *fsize) 534 { 535 struct go7007 *go = video_drvdata(filp); 536 int width, height; 537 538 if (fsize->index > 2) 539 return -EINVAL; 540 541 if (!valid_pixelformat(fsize->pixel_format)) 542 return -EINVAL; 543 544 get_resolution(go, &width, &height); 545 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE; 546 fsize->discrete.width = (width >> fsize->index) & ~0xf; 547 fsize->discrete.height = (height >> fsize->index) & ~0xf; 548 return 0; 549 } 550 551 static int vidioc_enum_frameintervals(struct file *filp, void *priv, 552 struct v4l2_frmivalenum *fival) 553 { 554 struct go7007 *go = video_drvdata(filp); 555 int width, height; 556 int i; 557 558 if (fival->index > 4) 559 return -EINVAL; 560 561 if (!valid_pixelformat(fival->pixel_format)) 562 return -EINVAL; 563 564 if (!(go->board_info->sensor_flags & GO7007_SENSOR_SCALING)) { 565 get_resolution(go, &width, &height); 566 for (i = 0; i <= 2; i++) 567 if (fival->width == ((width >> i) & ~0xf) && 568 fival->height == ((height >> i) & ~0xf)) 569 break; 570 if (i > 2) 571 return -EINVAL; 572 } 573 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE; 574 fival->discrete.numerator = 1001 * (fival->index + 1); 575 fival->discrete.denominator = go->sensor_framerate; 576 return 0; 577 } 578 579 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *std) 580 { 581 struct go7007 *go = video_drvdata(file); 582 583 *std = go->std; 584 return 0; 585 } 586 587 static int go7007_s_std(struct go7007 *go) 588 { 589 if (go->std & V4L2_STD_625_50) { 590 go->standard = GO7007_STD_PAL; 591 go->sensor_framerate = 25025; 592 } else { 593 go->standard = GO7007_STD_NTSC; 594 go->sensor_framerate = 30000; 595 } 596 597 call_all(&go->v4l2_dev, video, s_std, go->std); 598 set_capture_size(go, NULL, 0); 599 return 0; 600 } 601 602 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id std) 603 { 604 struct go7007 *go = video_drvdata(file); 605 606 if (vb2_is_busy(&go->vidq)) 607 return -EBUSY; 608 609 go->std = std; 610 611 return go7007_s_std(go); 612 } 613 614 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *std) 615 { 616 struct go7007 *go = video_drvdata(file); 617 618 return call_all(&go->v4l2_dev, video, querystd, std); 619 } 620 621 static int vidioc_enum_input(struct file *file, void *priv, 622 struct v4l2_input *inp) 623 { 624 struct go7007 *go = video_drvdata(file); 625 626 if (inp->index >= go->board_info->num_inputs) 627 return -EINVAL; 628 629 strscpy(inp->name, go->board_info->inputs[inp->index].name, 630 sizeof(inp->name)); 631 632 /* If this board has a tuner, it will be the first input */ 633 if ((go->board_info->flags & GO7007_BOARD_HAS_TUNER) && 634 inp->index == 0) 635 inp->type = V4L2_INPUT_TYPE_TUNER; 636 else 637 inp->type = V4L2_INPUT_TYPE_CAMERA; 638 639 if (go->board_info->num_aud_inputs) 640 inp->audioset = (1 << go->board_info->num_aud_inputs) - 1; 641 else 642 inp->audioset = 0; 643 inp->tuner = 0; 644 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) 645 inp->std = video_devdata(file)->tvnorms; 646 else 647 inp->std = 0; 648 649 return 0; 650 } 651 652 653 static int vidioc_g_input(struct file *file, void *priv, unsigned int *input) 654 { 655 struct go7007 *go = video_drvdata(file); 656 657 *input = go->input; 658 659 return 0; 660 } 661 662 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a) 663 { 664 struct go7007 *go = video_drvdata(file); 665 666 if (a->index >= go->board_info->num_aud_inputs) 667 return -EINVAL; 668 strscpy(a->name, go->board_info->aud_inputs[a->index].name, 669 sizeof(a->name)); 670 a->capability = V4L2_AUDCAP_STEREO; 671 return 0; 672 } 673 674 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a) 675 { 676 struct go7007 *go = video_drvdata(file); 677 678 a->index = go->aud_input; 679 strscpy(a->name, go->board_info->aud_inputs[go->aud_input].name, 680 sizeof(a->name)); 681 a->capability = V4L2_AUDCAP_STEREO; 682 return 0; 683 } 684 685 static int vidioc_s_audio(struct file *file, void *fh, 686 const struct v4l2_audio *a) 687 { 688 struct go7007 *go = video_drvdata(file); 689 690 if (a->index >= go->board_info->num_aud_inputs) 691 return -EINVAL; 692 go->aud_input = a->index; 693 v4l2_subdev_call(go->sd_audio, audio, s_routing, 694 go->board_info->aud_inputs[go->aud_input].audio_input, 0, 0); 695 return 0; 696 } 697 698 static void go7007_s_input(struct go7007 *go) 699 { 700 unsigned int input = go->input; 701 702 v4l2_subdev_call(go->sd_video, video, s_routing, 703 go->board_info->inputs[input].video_input, 0, 704 go->board_info->video_config); 705 if (go->board_info->num_aud_inputs) { 706 int aud_input = go->board_info->inputs[input].audio_index; 707 708 v4l2_subdev_call(go->sd_audio, audio, s_routing, 709 go->board_info->aud_inputs[aud_input].audio_input, 0, 0); 710 go->aud_input = aud_input; 711 } 712 } 713 714 static int vidioc_s_input(struct file *file, void *priv, unsigned int input) 715 { 716 struct go7007 *go = video_drvdata(file); 717 718 if (input >= go->board_info->num_inputs) 719 return -EINVAL; 720 if (vb2_is_busy(&go->vidq)) 721 return -EBUSY; 722 723 go->input = input; 724 go7007_s_input(go); 725 726 return 0; 727 } 728 729 static int vidioc_g_tuner(struct file *file, void *priv, 730 struct v4l2_tuner *t) 731 { 732 struct go7007 *go = video_drvdata(file); 733 734 if (t->index != 0) 735 return -EINVAL; 736 737 strscpy(t->name, "Tuner", sizeof(t->name)); 738 return call_all(&go->v4l2_dev, tuner, g_tuner, t); 739 } 740 741 static int vidioc_s_tuner(struct file *file, void *priv, 742 const struct v4l2_tuner *t) 743 { 744 struct go7007 *go = video_drvdata(file); 745 746 if (t->index != 0) 747 return -EINVAL; 748 749 return call_all(&go->v4l2_dev, tuner, s_tuner, t); 750 } 751 752 static int vidioc_g_frequency(struct file *file, void *priv, 753 struct v4l2_frequency *f) 754 { 755 struct go7007 *go = video_drvdata(file); 756 757 if (f->tuner) 758 return -EINVAL; 759 760 return call_all(&go->v4l2_dev, tuner, g_frequency, f); 761 } 762 763 static int vidioc_s_frequency(struct file *file, void *priv, 764 const struct v4l2_frequency *f) 765 { 766 struct go7007 *go = video_drvdata(file); 767 768 if (f->tuner) 769 return -EINVAL; 770 771 return call_all(&go->v4l2_dev, tuner, s_frequency, f); 772 } 773 774 static int vidioc_log_status(struct file *file, void *priv) 775 { 776 struct go7007 *go = video_drvdata(file); 777 778 v4l2_ctrl_log_status(file, priv); 779 return call_all(&go->v4l2_dev, core, log_status); 780 } 781 782 static int vidioc_subscribe_event(struct v4l2_fh *fh, 783 const struct v4l2_event_subscription *sub) 784 { 785 786 switch (sub->type) { 787 case V4L2_EVENT_MOTION_DET: 788 /* Allow for up to 30 events (1 second for NTSC) to be 789 * stored. */ 790 return v4l2_event_subscribe(fh, sub, 30, NULL); 791 default: 792 return v4l2_ctrl_subscribe_event(fh, sub); 793 } 794 } 795 796 797 static int go7007_s_ctrl(struct v4l2_ctrl *ctrl) 798 { 799 struct go7007 *go = 800 container_of(ctrl->handler, struct go7007, hdl); 801 unsigned y; 802 u8 *mt; 803 804 switch (ctrl->id) { 805 case V4L2_CID_PIXEL_THRESHOLD0: 806 go->modet[0].pixel_threshold = ctrl->val; 807 break; 808 case V4L2_CID_MOTION_THRESHOLD0: 809 go->modet[0].motion_threshold = ctrl->val; 810 break; 811 case V4L2_CID_MB_THRESHOLD0: 812 go->modet[0].mb_threshold = ctrl->val; 813 break; 814 case V4L2_CID_PIXEL_THRESHOLD1: 815 go->modet[1].pixel_threshold = ctrl->val; 816 break; 817 case V4L2_CID_MOTION_THRESHOLD1: 818 go->modet[1].motion_threshold = ctrl->val; 819 break; 820 case V4L2_CID_MB_THRESHOLD1: 821 go->modet[1].mb_threshold = ctrl->val; 822 break; 823 case V4L2_CID_PIXEL_THRESHOLD2: 824 go->modet[2].pixel_threshold = ctrl->val; 825 break; 826 case V4L2_CID_MOTION_THRESHOLD2: 827 go->modet[2].motion_threshold = ctrl->val; 828 break; 829 case V4L2_CID_MB_THRESHOLD2: 830 go->modet[2].mb_threshold = ctrl->val; 831 break; 832 case V4L2_CID_PIXEL_THRESHOLD3: 833 go->modet[3].pixel_threshold = ctrl->val; 834 break; 835 case V4L2_CID_MOTION_THRESHOLD3: 836 go->modet[3].motion_threshold = ctrl->val; 837 break; 838 case V4L2_CID_MB_THRESHOLD3: 839 go->modet[3].mb_threshold = ctrl->val; 840 break; 841 case V4L2_CID_DETECT_MD_REGION_GRID: 842 mt = go->modet_map; 843 for (y = 0; y < go->height / 16; y++, mt += go->width / 16) 844 memcpy(mt, ctrl->p_new.p_u8 + y * (720 / 16), go->width / 16); 845 break; 846 default: 847 return -EINVAL; 848 } 849 return 0; 850 } 851 852 static const struct v4l2_file_operations go7007_fops = { 853 .owner = THIS_MODULE, 854 .open = v4l2_fh_open, 855 .release = vb2_fop_release, 856 .unlocked_ioctl = video_ioctl2, 857 .read = vb2_fop_read, 858 .mmap = vb2_fop_mmap, 859 .poll = vb2_fop_poll, 860 }; 861 862 static const struct v4l2_ioctl_ops video_ioctl_ops = { 863 .vidioc_querycap = vidioc_querycap, 864 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 865 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 866 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 867 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 868 .vidioc_reqbufs = vb2_ioctl_reqbufs, 869 .vidioc_querybuf = vb2_ioctl_querybuf, 870 .vidioc_qbuf = vb2_ioctl_qbuf, 871 .vidioc_dqbuf = vb2_ioctl_dqbuf, 872 .vidioc_g_std = vidioc_g_std, 873 .vidioc_s_std = vidioc_s_std, 874 .vidioc_querystd = vidioc_querystd, 875 .vidioc_enum_input = vidioc_enum_input, 876 .vidioc_g_input = vidioc_g_input, 877 .vidioc_s_input = vidioc_s_input, 878 .vidioc_enumaudio = vidioc_enumaudio, 879 .vidioc_g_audio = vidioc_g_audio, 880 .vidioc_s_audio = vidioc_s_audio, 881 .vidioc_streamon = vb2_ioctl_streamon, 882 .vidioc_streamoff = vb2_ioctl_streamoff, 883 .vidioc_g_tuner = vidioc_g_tuner, 884 .vidioc_s_tuner = vidioc_s_tuner, 885 .vidioc_g_frequency = vidioc_g_frequency, 886 .vidioc_s_frequency = vidioc_s_frequency, 887 .vidioc_g_parm = vidioc_g_parm, 888 .vidioc_s_parm = vidioc_s_parm, 889 .vidioc_enum_framesizes = vidioc_enum_framesizes, 890 .vidioc_enum_frameintervals = vidioc_enum_frameintervals, 891 .vidioc_log_status = vidioc_log_status, 892 .vidioc_subscribe_event = vidioc_subscribe_event, 893 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 894 }; 895 896 static const struct video_device go7007_template = { 897 .name = "go7007", 898 .fops = &go7007_fops, 899 .release = video_device_release_empty, 900 .ioctl_ops = &video_ioctl_ops, 901 .tvnorms = V4L2_STD_ALL, 902 }; 903 904 static const struct v4l2_ctrl_ops go7007_ctrl_ops = { 905 .s_ctrl = go7007_s_ctrl, 906 }; 907 908 static const struct v4l2_ctrl_config go7007_pixel_threshold0_ctrl = { 909 .ops = &go7007_ctrl_ops, 910 .id = V4L2_CID_PIXEL_THRESHOLD0, 911 .name = "Pixel Threshold Region 0", 912 .type = V4L2_CTRL_TYPE_INTEGER, 913 .def = 20, 914 .max = 32767, 915 .step = 1, 916 }; 917 918 static const struct v4l2_ctrl_config go7007_motion_threshold0_ctrl = { 919 .ops = &go7007_ctrl_ops, 920 .id = V4L2_CID_MOTION_THRESHOLD0, 921 .name = "Motion Threshold Region 0", 922 .type = V4L2_CTRL_TYPE_INTEGER, 923 .def = 80, 924 .max = 32767, 925 .step = 1, 926 }; 927 928 static const struct v4l2_ctrl_config go7007_mb_threshold0_ctrl = { 929 .ops = &go7007_ctrl_ops, 930 .id = V4L2_CID_MB_THRESHOLD0, 931 .name = "MB Threshold Region 0", 932 .type = V4L2_CTRL_TYPE_INTEGER, 933 .def = 200, 934 .max = 32767, 935 .step = 1, 936 }; 937 938 static const struct v4l2_ctrl_config go7007_pixel_threshold1_ctrl = { 939 .ops = &go7007_ctrl_ops, 940 .id = V4L2_CID_PIXEL_THRESHOLD1, 941 .name = "Pixel Threshold Region 1", 942 .type = V4L2_CTRL_TYPE_INTEGER, 943 .def = 20, 944 .max = 32767, 945 .step = 1, 946 }; 947 948 static const struct v4l2_ctrl_config go7007_motion_threshold1_ctrl = { 949 .ops = &go7007_ctrl_ops, 950 .id = V4L2_CID_MOTION_THRESHOLD1, 951 .name = "Motion Threshold Region 1", 952 .type = V4L2_CTRL_TYPE_INTEGER, 953 .def = 80, 954 .max = 32767, 955 .step = 1, 956 }; 957 958 static const struct v4l2_ctrl_config go7007_mb_threshold1_ctrl = { 959 .ops = &go7007_ctrl_ops, 960 .id = V4L2_CID_MB_THRESHOLD1, 961 .name = "MB Threshold Region 1", 962 .type = V4L2_CTRL_TYPE_INTEGER, 963 .def = 200, 964 .max = 32767, 965 .step = 1, 966 }; 967 968 static const struct v4l2_ctrl_config go7007_pixel_threshold2_ctrl = { 969 .ops = &go7007_ctrl_ops, 970 .id = V4L2_CID_PIXEL_THRESHOLD2, 971 .name = "Pixel Threshold Region 2", 972 .type = V4L2_CTRL_TYPE_INTEGER, 973 .def = 20, 974 .max = 32767, 975 .step = 1, 976 }; 977 978 static const struct v4l2_ctrl_config go7007_motion_threshold2_ctrl = { 979 .ops = &go7007_ctrl_ops, 980 .id = V4L2_CID_MOTION_THRESHOLD2, 981 .name = "Motion Threshold Region 2", 982 .type = V4L2_CTRL_TYPE_INTEGER, 983 .def = 80, 984 .max = 32767, 985 .step = 1, 986 }; 987 988 static const struct v4l2_ctrl_config go7007_mb_threshold2_ctrl = { 989 .ops = &go7007_ctrl_ops, 990 .id = V4L2_CID_MB_THRESHOLD2, 991 .name = "MB Threshold Region 2", 992 .type = V4L2_CTRL_TYPE_INTEGER, 993 .def = 200, 994 .max = 32767, 995 .step = 1, 996 }; 997 998 static const struct v4l2_ctrl_config go7007_pixel_threshold3_ctrl = { 999 .ops = &go7007_ctrl_ops, 1000 .id = V4L2_CID_PIXEL_THRESHOLD3, 1001 .name = "Pixel Threshold Region 3", 1002 .type = V4L2_CTRL_TYPE_INTEGER, 1003 .def = 20, 1004 .max = 32767, 1005 .step = 1, 1006 }; 1007 1008 static const struct v4l2_ctrl_config go7007_motion_threshold3_ctrl = { 1009 .ops = &go7007_ctrl_ops, 1010 .id = V4L2_CID_MOTION_THRESHOLD3, 1011 .name = "Motion Threshold Region 3", 1012 .type = V4L2_CTRL_TYPE_INTEGER, 1013 .def = 80, 1014 .max = 32767, 1015 .step = 1, 1016 }; 1017 1018 static const struct v4l2_ctrl_config go7007_mb_threshold3_ctrl = { 1019 .ops = &go7007_ctrl_ops, 1020 .id = V4L2_CID_MB_THRESHOLD3, 1021 .name = "MB Threshold Region 3", 1022 .type = V4L2_CTRL_TYPE_INTEGER, 1023 .def = 200, 1024 .max = 32767, 1025 .step = 1, 1026 }; 1027 1028 static const struct v4l2_ctrl_config go7007_mb_regions_ctrl = { 1029 .ops = &go7007_ctrl_ops, 1030 .id = V4L2_CID_DETECT_MD_REGION_GRID, 1031 .dims = { 576 / 16, 720 / 16 }, 1032 .max = 3, 1033 .step = 1, 1034 }; 1035 1036 int go7007_v4l2_ctrl_init(struct go7007 *go) 1037 { 1038 struct v4l2_ctrl_handler *hdl = &go->hdl; 1039 struct v4l2_ctrl *ctrl; 1040 1041 v4l2_ctrl_handler_init(hdl, 22); 1042 go->mpeg_video_gop_size = v4l2_ctrl_new_std(hdl, NULL, 1043 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 0, 34, 1, 15); 1044 go->mpeg_video_gop_closure = v4l2_ctrl_new_std(hdl, NULL, 1045 V4L2_CID_MPEG_VIDEO_GOP_CLOSURE, 0, 1, 1, 1); 1046 go->mpeg_video_bitrate = v4l2_ctrl_new_std(hdl, NULL, 1047 V4L2_CID_MPEG_VIDEO_BITRATE, 1048 64000, 10000000, 1, 9800000); 1049 go->mpeg_video_b_frames = v4l2_ctrl_new_std(hdl, NULL, 1050 V4L2_CID_MPEG_VIDEO_B_FRAMES, 0, 2, 2, 0); 1051 go->mpeg_video_rep_seqheader = v4l2_ctrl_new_std(hdl, NULL, 1052 V4L2_CID_MPEG_VIDEO_REPEAT_SEQ_HEADER, 0, 1, 1, 1); 1053 1054 go->mpeg_video_aspect_ratio = v4l2_ctrl_new_std_menu(hdl, NULL, 1055 V4L2_CID_MPEG_VIDEO_ASPECT, 1056 V4L2_MPEG_VIDEO_ASPECT_16x9, 0, 1057 V4L2_MPEG_VIDEO_ASPECT_1x1); 1058 ctrl = v4l2_ctrl_new_std(hdl, NULL, 1059 V4L2_CID_JPEG_ACTIVE_MARKER, 0, 1060 V4L2_JPEG_ACTIVE_MARKER_DQT | 1061 V4L2_JPEG_ACTIVE_MARKER_DHT, 0, 1062 V4L2_JPEG_ACTIVE_MARKER_DQT | 1063 V4L2_JPEG_ACTIVE_MARKER_DHT); 1064 if (ctrl) 1065 ctrl->flags |= V4L2_CTRL_FLAG_READ_ONLY; 1066 v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold0_ctrl, NULL); 1067 v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold0_ctrl, NULL); 1068 v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold0_ctrl, NULL); 1069 v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold1_ctrl, NULL); 1070 v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold1_ctrl, NULL); 1071 v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold1_ctrl, NULL); 1072 v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold2_ctrl, NULL); 1073 v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold2_ctrl, NULL); 1074 v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold2_ctrl, NULL); 1075 v4l2_ctrl_new_custom(hdl, &go7007_pixel_threshold3_ctrl, NULL); 1076 v4l2_ctrl_new_custom(hdl, &go7007_motion_threshold3_ctrl, NULL); 1077 v4l2_ctrl_new_custom(hdl, &go7007_mb_threshold3_ctrl, NULL); 1078 v4l2_ctrl_new_custom(hdl, &go7007_mb_regions_ctrl, NULL); 1079 go->modet_mode = v4l2_ctrl_new_std_menu(hdl, NULL, 1080 V4L2_CID_DETECT_MD_MODE, 1081 V4L2_DETECT_MD_MODE_REGION_GRID, 1082 1 << V4L2_DETECT_MD_MODE_THRESHOLD_GRID, 1083 V4L2_DETECT_MD_MODE_DISABLED); 1084 if (hdl->error) { 1085 int rv = hdl->error; 1086 1087 v4l2_err(&go->v4l2_dev, "Could not register controls\n"); 1088 return rv; 1089 } 1090 go->v4l2_dev.ctrl_handler = hdl; 1091 return 0; 1092 } 1093 1094 int go7007_v4l2_init(struct go7007 *go) 1095 { 1096 struct video_device *vdev = &go->vdev; 1097 int rv; 1098 1099 mutex_init(&go->serialize_lock); 1100 mutex_init(&go->queue_lock); 1101 1102 INIT_LIST_HEAD(&go->vidq_active); 1103 go->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 1104 go->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ; 1105 go->vidq.ops = &go7007_video_qops; 1106 go->vidq.mem_ops = &vb2_vmalloc_memops; 1107 go->vidq.drv_priv = go; 1108 go->vidq.buf_struct_size = sizeof(struct go7007_buffer); 1109 go->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 1110 go->vidq.lock = &go->queue_lock; 1111 rv = vb2_queue_init(&go->vidq); 1112 if (rv) 1113 return rv; 1114 *vdev = go7007_template; 1115 vdev->lock = &go->serialize_lock; 1116 vdev->queue = &go->vidq; 1117 video_set_drvdata(vdev, go); 1118 vdev->v4l2_dev = &go->v4l2_dev; 1119 if (!v4l2_device_has_op(&go->v4l2_dev, 0, video, querystd)) 1120 v4l2_disable_ioctl(vdev, VIDIOC_QUERYSTD); 1121 if (!(go->board_info->flags & GO7007_BOARD_HAS_TUNER)) { 1122 v4l2_disable_ioctl(vdev, VIDIOC_S_FREQUENCY); 1123 v4l2_disable_ioctl(vdev, VIDIOC_G_FREQUENCY); 1124 v4l2_disable_ioctl(vdev, VIDIOC_S_TUNER); 1125 v4l2_disable_ioctl(vdev, VIDIOC_G_TUNER); 1126 } else { 1127 struct v4l2_frequency f = { 1128 .type = V4L2_TUNER_ANALOG_TV, 1129 .frequency = 980, 1130 }; 1131 1132 call_all(&go->v4l2_dev, tuner, s_frequency, &f); 1133 } 1134 if (!(go->board_info->sensor_flags & GO7007_SENSOR_TV)) { 1135 v4l2_disable_ioctl(vdev, VIDIOC_G_STD); 1136 v4l2_disable_ioctl(vdev, VIDIOC_S_STD); 1137 vdev->tvnorms = 0; 1138 } 1139 if (go->board_info->sensor_flags & GO7007_SENSOR_SCALING) 1140 v4l2_disable_ioctl(vdev, VIDIOC_ENUM_FRAMESIZES); 1141 if (go->board_info->num_aud_inputs == 0) { 1142 v4l2_disable_ioctl(vdev, VIDIOC_G_AUDIO); 1143 v4l2_disable_ioctl(vdev, VIDIOC_S_AUDIO); 1144 v4l2_disable_ioctl(vdev, VIDIOC_ENUMAUDIO); 1145 } 1146 /* Setup correct crystal frequency on this board */ 1147 if (go->board_info->sensor_flags & GO7007_SENSOR_SAA7115) 1148 v4l2_subdev_call(go->sd_video, video, s_crystal_freq, 1149 SAA7115_FREQ_24_576_MHZ, 1150 SAA7115_FREQ_FL_APLL | SAA7115_FREQ_FL_UCGC | 1151 SAA7115_FREQ_FL_DOUBLE_ASCLK); 1152 go7007_s_input(go); 1153 if (go->board_info->sensor_flags & GO7007_SENSOR_TV) 1154 go7007_s_std(go); 1155 rv = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 1156 if (rv < 0) 1157 return rv; 1158 dev_info(go->dev, "registered device %s [v4l2]\n", 1159 video_device_node_name(vdev)); 1160 1161 return 0; 1162 } 1163 1164 void go7007_v4l2_remove(struct go7007 *go) 1165 { 1166 v4l2_ctrl_handler_free(&go->hdl); 1167 } 1168