1 /* 2 * STK1160 driver 3 * 4 * Copyright (C) 2012 Ezequiel Garcia 5 * <elezegarcia--a.t--gmail.com> 6 * 7 * Based on Easycap driver by R.M. Thomas 8 * Copyright (C) 2010 R.M. Thomas 9 * <rmthomas--a.t--sciolus.org> 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License as published by 13 * the Free Software Foundation; either version 2 of the License, or 14 * (at your option) any later version. 15 * 16 * This program is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU General Public License for more details. 20 * 21 */ 22 23 #include <linux/module.h> 24 #include <linux/usb.h> 25 #include <linux/mm.h> 26 #include <linux/slab.h> 27 28 #include <linux/videodev2.h> 29 #include <media/v4l2-device.h> 30 #include <media/v4l2-common.h> 31 #include <media/v4l2-ioctl.h> 32 #include <media/v4l2-fh.h> 33 #include <media/v4l2-event.h> 34 #include <media/videobuf2-vmalloc.h> 35 36 #include <media/saa7115.h> 37 38 #include "stk1160.h" 39 #include "stk1160-reg.h" 40 41 static bool keep_buffers; 42 module_param(keep_buffers, bool, 0644); 43 MODULE_PARM_DESC(keep_buffers, "don't release buffers upon stop streaming"); 44 45 /* supported video standards */ 46 static struct stk1160_fmt format[] = { 47 { 48 .name = "16 bpp YUY2, 4:2:2, packed", 49 .fourcc = V4L2_PIX_FMT_UYVY, 50 .depth = 16, 51 } 52 }; 53 54 static void stk1160_set_std(struct stk1160 *dev) 55 { 56 int i; 57 58 static struct regval std525[] = { 59 60 /* 720x480 */ 61 62 /* Frame start */ 63 {STK116_CFSPO_STX_L, 0x0000}, 64 {STK116_CFSPO_STX_H, 0x0000}, 65 {STK116_CFSPO_STY_L, 0x0003}, 66 {STK116_CFSPO_STY_H, 0x0000}, 67 68 /* Frame end */ 69 {STK116_CFEPO_ENX_L, 0x05a0}, 70 {STK116_CFEPO_ENX_H, 0x0005}, 71 {STK116_CFEPO_ENY_L, 0x00f3}, 72 {STK116_CFEPO_ENY_H, 0x0000}, 73 74 {0xffff, 0xffff} 75 }; 76 77 static struct regval std625[] = { 78 79 /* 720x576 */ 80 81 /* TODO: Each line of frame has some junk at the end */ 82 /* Frame start */ 83 {STK116_CFSPO, 0x0000}, 84 {STK116_CFSPO+1, 0x0000}, 85 {STK116_CFSPO+2, 0x0001}, 86 {STK116_CFSPO+3, 0x0000}, 87 88 /* Frame end */ 89 {STK116_CFEPO, 0x05a0}, 90 {STK116_CFEPO+1, 0x0005}, 91 {STK116_CFEPO+2, 0x0121}, 92 {STK116_CFEPO+3, 0x0001}, 93 94 {0xffff, 0xffff} 95 }; 96 97 if (dev->norm & V4L2_STD_525_60) { 98 stk1160_dbg("registers to NTSC like standard\n"); 99 for (i = 0; std525[i].reg != 0xffff; i++) 100 stk1160_write_reg(dev, std525[i].reg, std525[i].val); 101 } else { 102 stk1160_dbg("registers to PAL like standard\n"); 103 for (i = 0; std625[i].reg != 0xffff; i++) 104 stk1160_write_reg(dev, std625[i].reg, std625[i].val); 105 } 106 107 } 108 109 /* 110 * Set a new alternate setting. 111 * Returns true is dev->max_pkt_size has changed, false otherwise. 112 */ 113 static bool stk1160_set_alternate(struct stk1160 *dev) 114 { 115 int i, prev_alt = dev->alt; 116 unsigned int min_pkt_size; 117 bool new_pkt_size; 118 119 /* 120 * If we don't set right alternate, 121 * then we will get a green screen with junk. 122 */ 123 min_pkt_size = STK1160_MIN_PKT_SIZE; 124 125 for (i = 0; i < dev->num_alt; i++) { 126 /* stop when the selected alt setting offers enough bandwidth */ 127 if (dev->alt_max_pkt_size[i] >= min_pkt_size) { 128 dev->alt = i; 129 break; 130 /* 131 * otherwise make sure that we end up with the maximum bandwidth 132 * because the min_pkt_size equation might be wrong... 133 */ 134 } else if (dev->alt_max_pkt_size[i] > 135 dev->alt_max_pkt_size[dev->alt]) 136 dev->alt = i; 137 } 138 139 stk1160_info("setting alternate %d\n", dev->alt); 140 141 if (dev->alt != prev_alt) { 142 stk1160_dbg("minimum isoc packet size: %u (alt=%d)\n", 143 min_pkt_size, dev->alt); 144 stk1160_dbg("setting alt %d with wMaxPacketSize=%u\n", 145 dev->alt, dev->alt_max_pkt_size[dev->alt]); 146 usb_set_interface(dev->udev, 0, dev->alt); 147 } 148 149 new_pkt_size = dev->max_pkt_size != dev->alt_max_pkt_size[dev->alt]; 150 dev->max_pkt_size = dev->alt_max_pkt_size[dev->alt]; 151 152 return new_pkt_size; 153 } 154 155 static int stk1160_start_streaming(struct stk1160 *dev) 156 { 157 bool new_pkt_size; 158 int rc = 0; 159 int i; 160 161 /* Check device presence */ 162 if (!dev->udev) 163 return -ENODEV; 164 165 if (mutex_lock_interruptible(&dev->v4l_lock)) 166 return -ERESTARTSYS; 167 /* 168 * For some reason it is mandatory to set alternate *first* 169 * and only *then* initialize isoc urbs. 170 * Someone please explain me why ;) 171 */ 172 new_pkt_size = stk1160_set_alternate(dev); 173 174 /* 175 * We (re)allocate isoc urbs if: 176 * there is no allocated isoc urbs, OR 177 * a new dev->max_pkt_size is detected 178 */ 179 if (!dev->isoc_ctl.num_bufs || new_pkt_size) { 180 rc = stk1160_alloc_isoc(dev); 181 if (rc < 0) 182 goto out_stop_hw; 183 } 184 185 /* submit urbs and enables IRQ */ 186 for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { 187 rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL); 188 if (rc) { 189 stk1160_err("cannot submit urb[%d] (%d)\n", i, rc); 190 goto out_uninit; 191 } 192 } 193 194 /* Start saa711x */ 195 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); 196 197 /* Start stk1160 */ 198 stk1160_write_reg(dev, STK1160_DCTRL, 0xb3); 199 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); 200 201 stk1160_dbg("streaming started\n"); 202 203 mutex_unlock(&dev->v4l_lock); 204 205 return 0; 206 207 out_uninit: 208 stk1160_uninit_isoc(dev); 209 out_stop_hw: 210 usb_set_interface(dev->udev, 0, 0); 211 stk1160_clear_queue(dev); 212 213 mutex_unlock(&dev->v4l_lock); 214 215 return rc; 216 } 217 218 /* Must be called with v4l_lock hold */ 219 static void stk1160_stop_hw(struct stk1160 *dev) 220 { 221 /* If the device is not physically present, there is nothing to do */ 222 if (!dev->udev) 223 return; 224 225 /* set alternate 0 */ 226 dev->alt = 0; 227 stk1160_info("setting alternate %d\n", dev->alt); 228 usb_set_interface(dev->udev, 0, 0); 229 230 /* Stop stk1160 */ 231 stk1160_write_reg(dev, STK1160_DCTRL, 0x00); 232 stk1160_write_reg(dev, STK1160_DCTRL+3, 0x00); 233 234 /* Stop saa711x */ 235 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); 236 } 237 238 static int stk1160_stop_streaming(struct stk1160 *dev) 239 { 240 if (mutex_lock_interruptible(&dev->v4l_lock)) 241 return -ERESTARTSYS; 242 243 stk1160_cancel_isoc(dev); 244 245 /* 246 * It is possible to keep buffers around using a module parameter. 247 * This is intended to avoid memory fragmentation. 248 */ 249 if (!keep_buffers) 250 stk1160_free_isoc(dev); 251 252 stk1160_stop_hw(dev); 253 254 stk1160_clear_queue(dev); 255 256 stk1160_dbg("streaming stopped\n"); 257 258 mutex_unlock(&dev->v4l_lock); 259 260 return 0; 261 } 262 263 static struct v4l2_file_operations stk1160_fops = { 264 .owner = THIS_MODULE, 265 .open = v4l2_fh_open, 266 .release = vb2_fop_release, 267 .read = vb2_fop_read, 268 .poll = vb2_fop_poll, 269 .mmap = vb2_fop_mmap, 270 .unlocked_ioctl = video_ioctl2, 271 }; 272 273 /* 274 * vidioc ioctls 275 */ 276 static int vidioc_querycap(struct file *file, 277 void *priv, struct v4l2_capability *cap) 278 { 279 struct stk1160 *dev = video_drvdata(file); 280 281 strcpy(cap->driver, "stk1160"); 282 strcpy(cap->card, "stk1160"); 283 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 284 cap->device_caps = 285 V4L2_CAP_VIDEO_CAPTURE | 286 V4L2_CAP_STREAMING | 287 V4L2_CAP_READWRITE; 288 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 289 return 0; 290 } 291 292 static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, 293 struct v4l2_fmtdesc *f) 294 { 295 if (f->index != 0) 296 return -EINVAL; 297 298 strlcpy(f->description, format[f->index].name, sizeof(f->description)); 299 f->pixelformat = format[f->index].fourcc; 300 return 0; 301 } 302 303 static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, 304 struct v4l2_format *f) 305 { 306 struct stk1160 *dev = video_drvdata(file); 307 308 f->fmt.pix.width = dev->width; 309 f->fmt.pix.height = dev->height; 310 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 311 f->fmt.pix.pixelformat = dev->fmt->fourcc; 312 f->fmt.pix.bytesperline = dev->width * 2; 313 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; 314 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 315 316 return 0; 317 } 318 319 static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, 320 struct v4l2_format *f) 321 { 322 struct stk1160 *dev = video_drvdata(file); 323 324 /* 325 * User can't choose size at his own will, 326 * so we just return him the current size chosen 327 * at standard selection. 328 * TODO: Implement frame scaling? 329 */ 330 331 f->fmt.pix.pixelformat = dev->fmt->fourcc; 332 f->fmt.pix.width = dev->width; 333 f->fmt.pix.height = dev->height; 334 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 335 f->fmt.pix.bytesperline = dev->width * 2; 336 f->fmt.pix.sizeimage = dev->height * f->fmt.pix.bytesperline; 337 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 338 339 return 0; 340 } 341 342 static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, 343 struct v4l2_format *f) 344 { 345 struct stk1160 *dev = video_drvdata(file); 346 struct vb2_queue *q = &dev->vb_vidq; 347 348 if (vb2_is_busy(q)) 349 return -EBUSY; 350 351 vidioc_try_fmt_vid_cap(file, priv, f); 352 353 /* We don't support any format changes */ 354 355 return 0; 356 } 357 358 static int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *norm) 359 { 360 struct stk1160 *dev = video_drvdata(file); 361 v4l2_device_call_all(&dev->v4l2_dev, 0, video, querystd, norm); 362 return 0; 363 } 364 365 static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) 366 { 367 struct stk1160 *dev = video_drvdata(file); 368 369 *norm = dev->norm; 370 return 0; 371 } 372 373 static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) 374 { 375 struct stk1160 *dev = video_drvdata(file); 376 struct vb2_queue *q = &dev->vb_vidq; 377 378 if (dev->norm == norm) 379 return 0; 380 381 if (vb2_is_busy(q)) 382 return -EBUSY; 383 384 /* Check device presence */ 385 if (!dev->udev) 386 return -ENODEV; 387 388 /* We need to set this now, before we call stk1160_set_std */ 389 dev->norm = norm; 390 391 /* This is taken from saa7115 video decoder */ 392 if (dev->norm & V4L2_STD_525_60) { 393 dev->width = 720; 394 dev->height = 480; 395 } else if (dev->norm & V4L2_STD_625_50) { 396 dev->width = 720; 397 dev->height = 576; 398 } else { 399 stk1160_err("invalid standard\n"); 400 return -EINVAL; 401 } 402 403 stk1160_set_std(dev); 404 405 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, 406 dev->norm); 407 408 return 0; 409 } 410 411 412 static int vidioc_enum_input(struct file *file, void *priv, 413 struct v4l2_input *i) 414 { 415 struct stk1160 *dev = video_drvdata(file); 416 417 if (i->index > STK1160_MAX_INPUT) 418 return -EINVAL; 419 420 /* S-Video special handling */ 421 if (i->index == STK1160_SVIDEO_INPUT) 422 sprintf(i->name, "S-Video"); 423 else 424 sprintf(i->name, "Composite%d", i->index); 425 426 i->type = V4L2_INPUT_TYPE_CAMERA; 427 i->std = dev->vdev.tvnorms; 428 return 0; 429 } 430 431 static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) 432 { 433 struct stk1160 *dev = video_drvdata(file); 434 *i = dev->ctl_input; 435 return 0; 436 } 437 438 static int vidioc_s_input(struct file *file, void *priv, unsigned int i) 439 { 440 struct stk1160 *dev = video_drvdata(file); 441 442 if (i > STK1160_MAX_INPUT) 443 return -EINVAL; 444 445 dev->ctl_input = i; 446 447 stk1160_select_input(dev); 448 449 return 0; 450 } 451 452 #ifdef CONFIG_VIDEO_ADV_DEBUG 453 static int vidioc_g_register(struct file *file, void *priv, 454 struct v4l2_dbg_register *reg) 455 { 456 struct stk1160 *dev = video_drvdata(file); 457 int rc; 458 u8 val; 459 460 /* Match host */ 461 rc = stk1160_read_reg(dev, reg->reg, &val); 462 reg->val = val; 463 reg->size = 1; 464 465 return rc; 466 } 467 468 static int vidioc_s_register(struct file *file, void *priv, 469 const struct v4l2_dbg_register *reg) 470 { 471 struct stk1160 *dev = video_drvdata(file); 472 473 /* Match host */ 474 return stk1160_write_reg(dev, reg->reg, reg->val); 475 } 476 #endif 477 478 static const struct v4l2_ioctl_ops stk1160_ioctl_ops = { 479 .vidioc_querycap = vidioc_querycap, 480 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 481 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 482 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 483 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 484 .vidioc_querystd = vidioc_querystd, 485 .vidioc_g_std = vidioc_g_std, 486 .vidioc_s_std = vidioc_s_std, 487 .vidioc_enum_input = vidioc_enum_input, 488 .vidioc_g_input = vidioc_g_input, 489 .vidioc_s_input = vidioc_s_input, 490 491 /* vb2 takes care of these */ 492 .vidioc_reqbufs = vb2_ioctl_reqbufs, 493 .vidioc_querybuf = vb2_ioctl_querybuf, 494 .vidioc_qbuf = vb2_ioctl_qbuf, 495 .vidioc_dqbuf = vb2_ioctl_dqbuf, 496 .vidioc_streamon = vb2_ioctl_streamon, 497 .vidioc_streamoff = vb2_ioctl_streamoff, 498 499 .vidioc_log_status = v4l2_ctrl_log_status, 500 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 501 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 502 503 #ifdef CONFIG_VIDEO_ADV_DEBUG 504 .vidioc_g_register = vidioc_g_register, 505 .vidioc_s_register = vidioc_s_register, 506 #endif 507 }; 508 509 /********************************************************************/ 510 511 /* 512 * Videobuf2 operations 513 */ 514 static int queue_setup(struct vb2_queue *vq, const struct v4l2_format *v4l_fmt, 515 unsigned int *nbuffers, unsigned int *nplanes, 516 unsigned int sizes[], void *alloc_ctxs[]) 517 { 518 struct stk1160 *dev = vb2_get_drv_priv(vq); 519 unsigned long size; 520 521 size = dev->width * dev->height * 2; 522 523 /* 524 * Here we can change the number of buffers being requested. 525 * So, we set a minimum and a maximum like this: 526 */ 527 *nbuffers = clamp_t(unsigned int, *nbuffers, 528 STK1160_MIN_VIDEO_BUFFERS, STK1160_MAX_VIDEO_BUFFERS); 529 530 /* This means a packed colorformat */ 531 *nplanes = 1; 532 533 sizes[0] = size; 534 535 stk1160_info("%s: buffer count %d, each %ld bytes\n", 536 __func__, *nbuffers, size); 537 538 return 0; 539 } 540 541 static void buffer_queue(struct vb2_buffer *vb) 542 { 543 unsigned long flags; 544 struct stk1160 *dev = vb2_get_drv_priv(vb->vb2_queue); 545 struct stk1160_buffer *buf = 546 container_of(vb, struct stk1160_buffer, vb); 547 548 spin_lock_irqsave(&dev->buf_lock, flags); 549 if (!dev->udev) { 550 /* 551 * If the device is disconnected return the buffer to userspace 552 * directly. The next QBUF call will fail with -ENODEV. 553 */ 554 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 555 } else { 556 557 buf->mem = vb2_plane_vaddr(vb, 0); 558 buf->length = vb2_plane_size(vb, 0); 559 buf->bytesused = 0; 560 buf->pos = 0; 561 562 /* 563 * If buffer length is less from expected then we return 564 * the buffer to userspace directly. 565 */ 566 if (buf->length < dev->width * dev->height * 2) 567 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 568 else 569 list_add_tail(&buf->list, &dev->avail_bufs); 570 571 } 572 spin_unlock_irqrestore(&dev->buf_lock, flags); 573 } 574 575 static int start_streaming(struct vb2_queue *vq, unsigned int count) 576 { 577 struct stk1160 *dev = vb2_get_drv_priv(vq); 578 return stk1160_start_streaming(dev); 579 } 580 581 /* abort streaming and wait for last buffer */ 582 static void stop_streaming(struct vb2_queue *vq) 583 { 584 struct stk1160 *dev = vb2_get_drv_priv(vq); 585 stk1160_stop_streaming(dev); 586 } 587 588 static struct vb2_ops stk1160_video_qops = { 589 .queue_setup = queue_setup, 590 .buf_queue = buffer_queue, 591 .start_streaming = start_streaming, 592 .stop_streaming = stop_streaming, 593 .wait_prepare = vb2_ops_wait_prepare, 594 .wait_finish = vb2_ops_wait_finish, 595 }; 596 597 static struct video_device v4l_template = { 598 .name = "stk1160", 599 .tvnorms = V4L2_STD_525_60 | V4L2_STD_625_50, 600 .fops = &stk1160_fops, 601 .ioctl_ops = &stk1160_ioctl_ops, 602 .release = video_device_release_empty, 603 }; 604 605 /********************************************************************/ 606 607 /* Must be called with both v4l_lock and vb_queue_lock hold */ 608 void stk1160_clear_queue(struct stk1160 *dev) 609 { 610 struct stk1160_buffer *buf; 611 unsigned long flags; 612 613 /* Release all active buffers */ 614 spin_lock_irqsave(&dev->buf_lock, flags); 615 while (!list_empty(&dev->avail_bufs)) { 616 buf = list_first_entry(&dev->avail_bufs, 617 struct stk1160_buffer, list); 618 list_del(&buf->list); 619 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR); 620 stk1160_info("buffer [%p/%d] aborted\n", 621 buf, buf->vb.v4l2_buf.index); 622 } 623 /* It's important to clear current buffer */ 624 dev->isoc_ctl.buf = NULL; 625 spin_unlock_irqrestore(&dev->buf_lock, flags); 626 } 627 628 int stk1160_vb2_setup(struct stk1160 *dev) 629 { 630 int rc; 631 struct vb2_queue *q; 632 633 q = &dev->vb_vidq; 634 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 635 q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR; 636 q->drv_priv = dev; 637 q->buf_struct_size = sizeof(struct stk1160_buffer); 638 q->ops = &stk1160_video_qops; 639 q->mem_ops = &vb2_vmalloc_memops; 640 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 641 642 rc = vb2_queue_init(q); 643 if (rc < 0) 644 return rc; 645 646 /* initialize video dma queue */ 647 INIT_LIST_HEAD(&dev->avail_bufs); 648 649 return 0; 650 } 651 652 int stk1160_video_register(struct stk1160 *dev) 653 { 654 int rc; 655 656 /* Initialize video_device with a template structure */ 657 dev->vdev = v4l_template; 658 dev->vdev.queue = &dev->vb_vidq; 659 660 /* 661 * Provide mutexes for v4l2 core and for videobuf2 queue. 662 * It will be used to protect *only* v4l2 ioctls. 663 */ 664 dev->vdev.lock = &dev->v4l_lock; 665 dev->vdev.queue->lock = &dev->vb_queue_lock; 666 667 /* This will be used to set video_device parent */ 668 dev->vdev.v4l2_dev = &dev->v4l2_dev; 669 670 /* NTSC is default */ 671 dev->norm = V4L2_STD_NTSC_M; 672 dev->width = 720; 673 dev->height = 480; 674 675 /* set default format */ 676 dev->fmt = &format[0]; 677 stk1160_set_std(dev); 678 679 v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, 680 dev->norm); 681 682 video_set_drvdata(&dev->vdev, dev); 683 rc = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); 684 if (rc < 0) { 685 stk1160_err("video_register_device failed (%d)\n", rc); 686 return rc; 687 } 688 689 v4l2_info(&dev->v4l2_dev, "V4L2 device registered as %s\n", 690 video_device_node_name(&dev->vdev)); 691 692 return 0; 693 } 694