1 /* 2 * Hauppauge HD PVR USB driver - video 4 linux 2 interface 3 * 4 * Copyright (C) 2008 Janne Grunau (j@jannau.net) 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation, version 2. 9 * 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/errno.h> 14 #include <linux/init.h> 15 #include <linux/slab.h> 16 #include <linux/module.h> 17 #include <linux/uaccess.h> 18 #include <linux/usb.h> 19 #include <linux/mutex.h> 20 #include <linux/workqueue.h> 21 22 #include <linux/videodev2.h> 23 #include <linux/v4l2-dv-timings.h> 24 #include <media/v4l2-dev.h> 25 #include <media/v4l2-common.h> 26 #include <media/v4l2-dv-timings.h> 27 #include <media/v4l2-ioctl.h> 28 #include <media/v4l2-event.h> 29 #include "hdpvr.h" 30 31 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */ 32 33 #define print_buffer_status() { \ 34 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \ 35 "%s:%d buffer stat: %d free, %d proc\n", \ 36 __func__, __LINE__, \ 37 list_size(&dev->free_buff_list), \ 38 list_size(&dev->rec_buff_list)); } 39 40 static const struct v4l2_dv_timings hdpvr_dv_timings[] = { 41 V4L2_DV_BT_CEA_720X480I59_94, 42 V4L2_DV_BT_CEA_720X576I50, 43 V4L2_DV_BT_CEA_720X480P59_94, 44 V4L2_DV_BT_CEA_720X576P50, 45 V4L2_DV_BT_CEA_1280X720P50, 46 V4L2_DV_BT_CEA_1280X720P60, 47 V4L2_DV_BT_CEA_1920X1080I50, 48 V4L2_DV_BT_CEA_1920X1080I60, 49 }; 50 51 /* Use 480i59 as the default timings */ 52 #define HDPVR_DEF_DV_TIMINGS_IDX (0) 53 54 struct hdpvr_fh { 55 struct v4l2_fh fh; 56 bool legacy_mode; 57 }; 58 59 static uint list_size(struct list_head *list) 60 { 61 struct list_head *tmp; 62 uint count = 0; 63 64 list_for_each(tmp, list) { 65 count++; 66 } 67 68 return count; 69 } 70 71 /*=========================================================================*/ 72 /* urb callback */ 73 static void hdpvr_read_bulk_callback(struct urb *urb) 74 { 75 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context; 76 struct hdpvr_device *dev = buf->dev; 77 78 /* marking buffer as received and wake waiting */ 79 buf->status = BUFSTAT_READY; 80 wake_up_interruptible(&dev->wait_data); 81 } 82 83 /*=========================================================================*/ 84 /* buffer bits */ 85 86 /* function expects dev->io_mutex to be hold by caller */ 87 int hdpvr_cancel_queue(struct hdpvr_device *dev) 88 { 89 struct hdpvr_buffer *buf; 90 91 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) { 92 usb_kill_urb(buf->urb); 93 buf->status = BUFSTAT_AVAILABLE; 94 } 95 96 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev); 97 98 return 0; 99 } 100 101 static int hdpvr_free_queue(struct list_head *q) 102 { 103 struct list_head *tmp; 104 struct list_head *p; 105 struct hdpvr_buffer *buf; 106 struct urb *urb; 107 108 for (p = q->next; p != q;) { 109 buf = list_entry(p, struct hdpvr_buffer, buff_list); 110 111 urb = buf->urb; 112 usb_free_coherent(urb->dev, urb->transfer_buffer_length, 113 urb->transfer_buffer, urb->transfer_dma); 114 usb_free_urb(urb); 115 tmp = p->next; 116 list_del(p); 117 kfree(buf); 118 p = tmp; 119 } 120 121 return 0; 122 } 123 124 /* function expects dev->io_mutex to be hold by caller */ 125 int hdpvr_free_buffers(struct hdpvr_device *dev) 126 { 127 hdpvr_cancel_queue(dev); 128 129 hdpvr_free_queue(&dev->free_buff_list); 130 hdpvr_free_queue(&dev->rec_buff_list); 131 132 return 0; 133 } 134 135 /* function expects dev->io_mutex to be hold by caller */ 136 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count) 137 { 138 uint i; 139 int retval = -ENOMEM; 140 u8 *mem; 141 struct hdpvr_buffer *buf; 142 struct urb *urb; 143 144 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 145 "allocating %u buffers\n", count); 146 147 for (i = 0; i < count; i++) { 148 149 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL); 150 if (!buf) { 151 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n"); 152 goto exit; 153 } 154 buf->dev = dev; 155 156 urb = usb_alloc_urb(0, GFP_KERNEL); 157 if (!urb) 158 goto exit_urb; 159 buf->urb = urb; 160 161 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL, 162 &urb->transfer_dma); 163 if (!mem) { 164 v4l2_err(&dev->v4l2_dev, 165 "cannot allocate usb transfer buffer\n"); 166 goto exit_urb_buffer; 167 } 168 169 usb_fill_bulk_urb(buf->urb, dev->udev, 170 usb_rcvbulkpipe(dev->udev, 171 dev->bulk_in_endpointAddr), 172 mem, dev->bulk_in_size, 173 hdpvr_read_bulk_callback, buf); 174 175 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 176 buf->status = BUFSTAT_AVAILABLE; 177 list_add_tail(&buf->buff_list, &dev->free_buff_list); 178 } 179 return 0; 180 exit_urb_buffer: 181 usb_free_urb(urb); 182 exit_urb: 183 kfree(buf); 184 exit: 185 hdpvr_free_buffers(dev); 186 return retval; 187 } 188 189 static int hdpvr_submit_buffers(struct hdpvr_device *dev) 190 { 191 struct hdpvr_buffer *buf; 192 struct urb *urb; 193 int ret = 0, err_count = 0; 194 195 mutex_lock(&dev->io_mutex); 196 197 while (dev->status == STATUS_STREAMING && 198 !list_empty(&dev->free_buff_list)) { 199 200 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer, 201 buff_list); 202 if (buf->status != BUFSTAT_AVAILABLE) { 203 v4l2_err(&dev->v4l2_dev, 204 "buffer not marked as available\n"); 205 ret = -EFAULT; 206 goto err; 207 } 208 209 urb = buf->urb; 210 urb->status = 0; 211 urb->actual_length = 0; 212 ret = usb_submit_urb(urb, GFP_KERNEL); 213 if (ret) { 214 v4l2_err(&dev->v4l2_dev, 215 "usb_submit_urb in %s returned %d\n", 216 __func__, ret); 217 if (++err_count > 2) 218 break; 219 continue; 220 } 221 buf->status = BUFSTAT_INPROGRESS; 222 list_move_tail(&buf->buff_list, &dev->rec_buff_list); 223 } 224 err: 225 print_buffer_status(); 226 mutex_unlock(&dev->io_mutex); 227 return ret; 228 } 229 230 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev) 231 { 232 struct hdpvr_buffer *buf; 233 234 mutex_lock(&dev->io_mutex); 235 236 if (list_empty(&dev->rec_buff_list)) { 237 mutex_unlock(&dev->io_mutex); 238 return NULL; 239 } 240 241 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer, 242 buff_list); 243 mutex_unlock(&dev->io_mutex); 244 245 return buf; 246 } 247 248 static void hdpvr_transmit_buffers(struct work_struct *work) 249 { 250 struct hdpvr_device *dev = container_of(work, struct hdpvr_device, 251 worker); 252 253 while (dev->status == STATUS_STREAMING) { 254 255 if (hdpvr_submit_buffers(dev)) { 256 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n"); 257 goto error; 258 } 259 if (wait_event_interruptible(dev->wait_buffer, 260 !list_empty(&dev->free_buff_list) || 261 dev->status != STATUS_STREAMING)) 262 goto error; 263 } 264 265 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 266 "transmit worker exited\n"); 267 return; 268 error: 269 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 270 "transmit buffers errored\n"); 271 dev->status = STATUS_ERROR; 272 } 273 274 /* function expects dev->io_mutex to be hold by caller */ 275 static int hdpvr_start_streaming(struct hdpvr_device *dev) 276 { 277 int ret; 278 struct hdpvr_video_info vidinf; 279 280 if (dev->status == STATUS_STREAMING) 281 return 0; 282 if (dev->status != STATUS_IDLE) 283 return -EAGAIN; 284 285 ret = get_video_info(dev, &vidinf); 286 if (ret < 0) 287 return ret; 288 289 if (!vidinf.valid) { 290 msleep(250); 291 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 292 "no video signal at input %d\n", dev->options.video_input); 293 return -EAGAIN; 294 } 295 296 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 297 "video signal: %dx%d@%dhz\n", vidinf.width, 298 vidinf.height, vidinf.fps); 299 300 /* start streaming 2 request */ 301 ret = usb_control_msg(dev->udev, 302 usb_sndctrlpipe(dev->udev, 0), 303 0xb8, 0x38, 0x1, 0, NULL, 0, 8000); 304 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 305 "encoder start control request returned %d\n", ret); 306 if (ret < 0) 307 return ret; 308 309 ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00); 310 if (ret) 311 return ret; 312 313 dev->status = STATUS_STREAMING; 314 315 INIT_WORK(&dev->worker, hdpvr_transmit_buffers); 316 schedule_work(&dev->worker); 317 318 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 319 "streaming started\n"); 320 321 return 0; 322 } 323 324 325 /* function expects dev->io_mutex to be hold by caller */ 326 static int hdpvr_stop_streaming(struct hdpvr_device *dev) 327 { 328 int actual_length; 329 uint c = 0; 330 u8 *buf; 331 332 if (dev->status == STATUS_IDLE) 333 return 0; 334 else if (dev->status != STATUS_STREAMING) 335 return -EAGAIN; 336 337 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL); 338 if (!buf) 339 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer for emptying the internal device buffer. Next capture start will be slow\n"); 340 341 dev->status = STATUS_SHUTTING_DOWN; 342 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00); 343 mutex_unlock(&dev->io_mutex); 344 345 wake_up_interruptible(&dev->wait_buffer); 346 msleep(50); 347 348 flush_work(&dev->worker); 349 350 mutex_lock(&dev->io_mutex); 351 /* kill the still outstanding urbs */ 352 hdpvr_cancel_queue(dev); 353 354 /* emptying the device buffer beforeshutting it down */ 355 while (buf && ++c < 500 && 356 !usb_bulk_msg(dev->udev, 357 usb_rcvbulkpipe(dev->udev, 358 dev->bulk_in_endpointAddr), 359 buf, dev->bulk_in_size, &actual_length, 360 BULK_URB_TIMEOUT)) { 361 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 362 "%2d: got %d bytes\n", c, actual_length); 363 } 364 kfree(buf); 365 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 366 "used %d urbs to empty device buffers\n", c-1); 367 msleep(10); 368 369 dev->status = STATUS_IDLE; 370 371 return 0; 372 } 373 374 375 /*=======================================================================*/ 376 /* 377 * video 4 linux 2 file operations 378 */ 379 380 static int hdpvr_open(struct file *file) 381 { 382 struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL); 383 384 if (fh == NULL) 385 return -ENOMEM; 386 fh->legacy_mode = true; 387 v4l2_fh_init(&fh->fh, video_devdata(file)); 388 v4l2_fh_add(&fh->fh); 389 file->private_data = fh; 390 return 0; 391 } 392 393 static int hdpvr_release(struct file *file) 394 { 395 struct hdpvr_device *dev = video_drvdata(file); 396 397 mutex_lock(&dev->io_mutex); 398 if (file->private_data == dev->owner) { 399 hdpvr_stop_streaming(dev); 400 dev->owner = NULL; 401 } 402 mutex_unlock(&dev->io_mutex); 403 404 return v4l2_fh_release(file); 405 } 406 407 /* 408 * hdpvr_v4l2_read() 409 * will allocate buffers when called for the first time 410 */ 411 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count, 412 loff_t *pos) 413 { 414 struct hdpvr_device *dev = video_drvdata(file); 415 struct hdpvr_buffer *buf = NULL; 416 struct urb *urb; 417 unsigned int ret = 0; 418 int rem, cnt; 419 420 if (*pos) 421 return -ESPIPE; 422 423 mutex_lock(&dev->io_mutex); 424 if (dev->status == STATUS_IDLE) { 425 if (hdpvr_start_streaming(dev)) { 426 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 427 "start_streaming failed\n"); 428 ret = -EIO; 429 msleep(200); 430 dev->status = STATUS_IDLE; 431 mutex_unlock(&dev->io_mutex); 432 goto err; 433 } 434 dev->owner = file->private_data; 435 print_buffer_status(); 436 } 437 mutex_unlock(&dev->io_mutex); 438 439 /* wait for the first buffer */ 440 if (!(file->f_flags & O_NONBLOCK)) { 441 if (wait_event_interruptible(dev->wait_data, 442 hdpvr_get_next_buffer(dev))) 443 return -ERESTARTSYS; 444 } 445 446 buf = hdpvr_get_next_buffer(dev); 447 448 while (count > 0 && buf) { 449 450 if (buf->status != BUFSTAT_READY && 451 dev->status != STATUS_DISCONNECTED) { 452 int err; 453 /* return nonblocking */ 454 if (file->f_flags & O_NONBLOCK) { 455 if (!ret) 456 ret = -EAGAIN; 457 goto err; 458 } 459 460 err = wait_event_interruptible_timeout(dev->wait_data, 461 buf->status == BUFSTAT_READY, 462 msecs_to_jiffies(1000)); 463 if (err < 0) { 464 ret = err; 465 goto err; 466 } 467 if (!err) { 468 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 469 "timeout: restart streaming\n"); 470 hdpvr_stop_streaming(dev); 471 msecs_to_jiffies(4000); 472 err = hdpvr_start_streaming(dev); 473 if (err) { 474 ret = err; 475 goto err; 476 } 477 } 478 } 479 480 if (buf->status != BUFSTAT_READY) 481 break; 482 483 /* set remaining bytes to copy */ 484 urb = buf->urb; 485 rem = urb->actual_length - buf->pos; 486 cnt = rem > count ? count : rem; 487 488 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos, 489 cnt)) { 490 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n"); 491 if (!ret) 492 ret = -EFAULT; 493 goto err; 494 } 495 496 buf->pos += cnt; 497 count -= cnt; 498 buffer += cnt; 499 ret += cnt; 500 501 /* finished, take next buffer */ 502 if (buf->pos == urb->actual_length) { 503 mutex_lock(&dev->io_mutex); 504 buf->pos = 0; 505 buf->status = BUFSTAT_AVAILABLE; 506 507 list_move_tail(&buf->buff_list, &dev->free_buff_list); 508 509 print_buffer_status(); 510 511 mutex_unlock(&dev->io_mutex); 512 513 wake_up_interruptible(&dev->wait_buffer); 514 515 buf = hdpvr_get_next_buffer(dev); 516 } 517 } 518 err: 519 if (!ret && !buf) 520 ret = -EAGAIN; 521 return ret; 522 } 523 524 static __poll_t hdpvr_poll(struct file *filp, poll_table *wait) 525 { 526 __poll_t req_events = poll_requested_events(wait); 527 struct hdpvr_buffer *buf = NULL; 528 struct hdpvr_device *dev = video_drvdata(filp); 529 __poll_t mask = v4l2_ctrl_poll(filp, wait); 530 531 if (!(req_events & (EPOLLIN | EPOLLRDNORM))) 532 return mask; 533 534 mutex_lock(&dev->io_mutex); 535 536 if (dev->status == STATUS_IDLE) { 537 if (hdpvr_start_streaming(dev)) { 538 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, 539 "start_streaming failed\n"); 540 dev->status = STATUS_IDLE; 541 } else { 542 dev->owner = filp->private_data; 543 } 544 545 print_buffer_status(); 546 } 547 mutex_unlock(&dev->io_mutex); 548 549 buf = hdpvr_get_next_buffer(dev); 550 /* only wait if no data is available */ 551 if (!buf || buf->status != BUFSTAT_READY) { 552 poll_wait(filp, &dev->wait_data, wait); 553 buf = hdpvr_get_next_buffer(dev); 554 } 555 if (buf && buf->status == BUFSTAT_READY) 556 mask |= EPOLLIN | EPOLLRDNORM; 557 558 return mask; 559 } 560 561 562 static const struct v4l2_file_operations hdpvr_fops = { 563 .owner = THIS_MODULE, 564 .open = hdpvr_open, 565 .release = hdpvr_release, 566 .read = hdpvr_read, 567 .poll = hdpvr_poll, 568 .unlocked_ioctl = video_ioctl2, 569 }; 570 571 /*=======================================================================*/ 572 /* 573 * V4L2 ioctl handling 574 */ 575 576 static int vidioc_querycap(struct file *file, void *priv, 577 struct v4l2_capability *cap) 578 { 579 struct hdpvr_device *dev = video_drvdata(file); 580 581 strscpy(cap->driver, "hdpvr", sizeof(cap->driver)); 582 strscpy(cap->card, "Hauppauge HD PVR", sizeof(cap->card)); 583 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); 584 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO | 585 V4L2_CAP_READWRITE; 586 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; 587 return 0; 588 } 589 590 static int vidioc_s_std(struct file *file, void *_fh, 591 v4l2_std_id std) 592 { 593 struct hdpvr_device *dev = video_drvdata(file); 594 struct hdpvr_fh *fh = _fh; 595 u8 std_type = 1; 596 597 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT) 598 return -ENODATA; 599 if (dev->status != STATUS_IDLE) 600 return -EBUSY; 601 if (std & V4L2_STD_525_60) 602 std_type = 0; 603 dev->cur_std = std; 604 dev->width = 720; 605 dev->height = std_type ? 576 : 480; 606 607 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type); 608 } 609 610 static int vidioc_g_std(struct file *file, void *_fh, 611 v4l2_std_id *std) 612 { 613 struct hdpvr_device *dev = video_drvdata(file); 614 struct hdpvr_fh *fh = _fh; 615 616 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT) 617 return -ENODATA; 618 *std = dev->cur_std; 619 return 0; 620 } 621 622 static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a) 623 { 624 struct hdpvr_device *dev = video_drvdata(file); 625 struct hdpvr_video_info vid_info; 626 struct hdpvr_fh *fh = _fh; 627 int ret; 628 629 *a = V4L2_STD_UNKNOWN; 630 if (dev->options.video_input == HDPVR_COMPONENT) 631 return fh->legacy_mode ? 0 : -ENODATA; 632 ret = get_video_info(dev, &vid_info); 633 if (vid_info.valid && vid_info.width == 720 && 634 (vid_info.height == 480 || vid_info.height == 576)) { 635 *a = (vid_info.height == 480) ? 636 V4L2_STD_525_60 : V4L2_STD_625_50; 637 } 638 return ret; 639 } 640 641 static int vidioc_s_dv_timings(struct file *file, void *_fh, 642 struct v4l2_dv_timings *timings) 643 { 644 struct hdpvr_device *dev = video_drvdata(file); 645 struct hdpvr_fh *fh = _fh; 646 int i; 647 648 fh->legacy_mode = false; 649 if (dev->options.video_input) 650 return -ENODATA; 651 if (dev->status != STATUS_IDLE) 652 return -EBUSY; 653 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) 654 if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false)) 655 break; 656 if (i == ARRAY_SIZE(hdpvr_dv_timings)) 657 return -EINVAL; 658 dev->cur_dv_timings = hdpvr_dv_timings[i]; 659 dev->width = hdpvr_dv_timings[i].bt.width; 660 dev->height = hdpvr_dv_timings[i].bt.height; 661 return 0; 662 } 663 664 static int vidioc_g_dv_timings(struct file *file, void *_fh, 665 struct v4l2_dv_timings *timings) 666 { 667 struct hdpvr_device *dev = video_drvdata(file); 668 struct hdpvr_fh *fh = _fh; 669 670 fh->legacy_mode = false; 671 if (dev->options.video_input) 672 return -ENODATA; 673 *timings = dev->cur_dv_timings; 674 return 0; 675 } 676 677 static int vidioc_query_dv_timings(struct file *file, void *_fh, 678 struct v4l2_dv_timings *timings) 679 { 680 struct hdpvr_device *dev = video_drvdata(file); 681 struct hdpvr_fh *fh = _fh; 682 struct hdpvr_video_info vid_info; 683 bool interlaced; 684 int ret = 0; 685 int i; 686 687 fh->legacy_mode = false; 688 if (dev->options.video_input) 689 return -ENODATA; 690 ret = get_video_info(dev, &vid_info); 691 if (ret) 692 return ret; 693 if (!vid_info.valid) 694 return -ENOLCK; 695 interlaced = vid_info.fps <= 30; 696 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) { 697 const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt; 698 unsigned hsize; 699 unsigned vsize; 700 unsigned fps; 701 702 hsize = V4L2_DV_BT_FRAME_WIDTH(bt); 703 vsize = V4L2_DV_BT_FRAME_HEIGHT(bt); 704 fps = (unsigned)bt->pixelclock / (hsize * vsize); 705 if (bt->width != vid_info.width || 706 bt->height != vid_info.height || 707 bt->interlaced != interlaced || 708 (fps != vid_info.fps && fps + 1 != vid_info.fps)) 709 continue; 710 *timings = hdpvr_dv_timings[i]; 711 break; 712 } 713 if (i == ARRAY_SIZE(hdpvr_dv_timings)) 714 ret = -ERANGE; 715 716 return ret; 717 } 718 719 static int vidioc_enum_dv_timings(struct file *file, void *_fh, 720 struct v4l2_enum_dv_timings *timings) 721 { 722 struct hdpvr_device *dev = video_drvdata(file); 723 struct hdpvr_fh *fh = _fh; 724 725 fh->legacy_mode = false; 726 memset(timings->reserved, 0, sizeof(timings->reserved)); 727 if (dev->options.video_input) 728 return -ENODATA; 729 if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings)) 730 return -EINVAL; 731 timings->timings = hdpvr_dv_timings[timings->index]; 732 return 0; 733 } 734 735 static int vidioc_dv_timings_cap(struct file *file, void *_fh, 736 struct v4l2_dv_timings_cap *cap) 737 { 738 struct hdpvr_device *dev = video_drvdata(file); 739 struct hdpvr_fh *fh = _fh; 740 741 fh->legacy_mode = false; 742 if (dev->options.video_input) 743 return -ENODATA; 744 cap->type = V4L2_DV_BT_656_1120; 745 cap->bt.min_width = 720; 746 cap->bt.max_width = 1920; 747 cap->bt.min_height = 480; 748 cap->bt.max_height = 1080; 749 cap->bt.min_pixelclock = 27000000; 750 cap->bt.max_pixelclock = 74250000; 751 cap->bt.standards = V4L2_DV_BT_STD_CEA861; 752 cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE; 753 return 0; 754 } 755 756 static const char *iname[] = { 757 [HDPVR_COMPONENT] = "Component", 758 [HDPVR_SVIDEO] = "S-Video", 759 [HDPVR_COMPOSITE] = "Composite", 760 }; 761 762 static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i) 763 { 764 unsigned int n; 765 766 n = i->index; 767 if (n >= HDPVR_VIDEO_INPUTS) 768 return -EINVAL; 769 770 i->type = V4L2_INPUT_TYPE_CAMERA; 771 772 strscpy(i->name, iname[n], sizeof(i->name)); 773 774 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF; 775 776 i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS; 777 i->std = n ? V4L2_STD_ALL : 0; 778 779 return 0; 780 } 781 782 static int vidioc_s_input(struct file *file, void *_fh, 783 unsigned int index) 784 { 785 struct hdpvr_device *dev = video_drvdata(file); 786 int retval; 787 788 if (index >= HDPVR_VIDEO_INPUTS) 789 return -EINVAL; 790 791 if (dev->status != STATUS_IDLE) 792 return -EBUSY; 793 794 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1); 795 if (!retval) { 796 dev->options.video_input = index; 797 /* 798 * Unfortunately gstreamer calls ENUMSTD and bails out if it 799 * won't find any formats, even though component input is 800 * selected. This means that we have to leave tvnorms at 801 * V4L2_STD_ALL. We cannot use the 'legacy' trick since 802 * tvnorms is set at the device node level and not at the 803 * filehandle level. 804 * 805 * Comment this out for now, but if the legacy mode can be 806 * removed in the future, then this code should be enabled 807 * again. 808 dev->video_dev.tvnorms = 809 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0; 810 */ 811 } 812 813 return retval; 814 } 815 816 static int vidioc_g_input(struct file *file, void *private_data, 817 unsigned int *index) 818 { 819 struct hdpvr_device *dev = video_drvdata(file); 820 821 *index = dev->options.video_input; 822 return 0; 823 } 824 825 826 static const char *audio_iname[] = { 827 [HDPVR_RCA_FRONT] = "RCA front", 828 [HDPVR_RCA_BACK] = "RCA back", 829 [HDPVR_SPDIF] = "SPDIF", 830 }; 831 832 static int vidioc_enumaudio(struct file *file, void *priv, 833 struct v4l2_audio *audio) 834 { 835 unsigned int n; 836 837 n = audio->index; 838 if (n >= HDPVR_AUDIO_INPUTS) 839 return -EINVAL; 840 841 audio->capability = V4L2_AUDCAP_STEREO; 842 843 strscpy(audio->name, audio_iname[n], sizeof(audio->name)); 844 845 return 0; 846 } 847 848 static int vidioc_s_audio(struct file *file, void *private_data, 849 const struct v4l2_audio *audio) 850 { 851 struct hdpvr_device *dev = video_drvdata(file); 852 int retval; 853 854 if (audio->index >= HDPVR_AUDIO_INPUTS) 855 return -EINVAL; 856 857 if (dev->status != STATUS_IDLE) 858 return -EBUSY; 859 860 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec); 861 if (!retval) 862 dev->options.audio_input = audio->index; 863 864 return retval; 865 } 866 867 static int vidioc_g_audio(struct file *file, void *private_data, 868 struct v4l2_audio *audio) 869 { 870 struct hdpvr_device *dev = video_drvdata(file); 871 872 audio->index = dev->options.audio_input; 873 audio->capability = V4L2_AUDCAP_STEREO; 874 strscpy(audio->name, audio_iname[audio->index], sizeof(audio->name)); 875 return 0; 876 } 877 878 static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl) 879 { 880 struct hdpvr_device *dev = 881 container_of(ctrl->handler, struct hdpvr_device, hdl); 882 883 switch (ctrl->id) { 884 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: 885 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR && 886 dev->video_bitrate->val >= dev->video_bitrate_peak->val) 887 dev->video_bitrate_peak->val = 888 dev->video_bitrate->val + 100000; 889 break; 890 } 891 return 0; 892 } 893 894 static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl) 895 { 896 struct hdpvr_device *dev = 897 container_of(ctrl->handler, struct hdpvr_device, hdl); 898 struct hdpvr_options *opt = &dev->options; 899 int ret = -EINVAL; 900 901 switch (ctrl->id) { 902 case V4L2_CID_BRIGHTNESS: 903 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val); 904 if (ret) 905 break; 906 dev->options.brightness = ctrl->val; 907 return 0; 908 case V4L2_CID_CONTRAST: 909 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val); 910 if (ret) 911 break; 912 dev->options.contrast = ctrl->val; 913 return 0; 914 case V4L2_CID_SATURATION: 915 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val); 916 if (ret) 917 break; 918 dev->options.saturation = ctrl->val; 919 return 0; 920 case V4L2_CID_HUE: 921 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val); 922 if (ret) 923 break; 924 dev->options.hue = ctrl->val; 925 return 0; 926 case V4L2_CID_SHARPNESS: 927 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val); 928 if (ret) 929 break; 930 dev->options.sharpness = ctrl->val; 931 return 0; 932 case V4L2_CID_MPEG_AUDIO_ENCODING: 933 if (dev->flags & HDPVR_FLAG_AC3_CAP) { 934 opt->audio_codec = ctrl->val; 935 return hdpvr_set_audio(dev, opt->audio_input + 1, 936 opt->audio_codec); 937 } 938 return 0; 939 case V4L2_CID_MPEG_VIDEO_ENCODING: 940 return 0; 941 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */ 942 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */ 943 /* opt->gop_mode |= 0x2; */ 944 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */ 945 /* opt->gop_mode); */ 946 /* } */ 947 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */ 948 /* opt->gop_mode &= ~0x2; */ 949 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */ 950 /* opt->gop_mode); */ 951 /* } */ 952 /* break; */ 953 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: { 954 uint peak_bitrate = dev->video_bitrate_peak->val / 100000; 955 uint bitrate = dev->video_bitrate->val / 100000; 956 957 if (ctrl->is_new) { 958 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) 959 opt->bitrate_mode = HDPVR_CONSTANT; 960 else 961 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE; 962 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE, 963 opt->bitrate_mode); 964 v4l2_ctrl_activate(dev->video_bitrate_peak, 965 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); 966 } 967 968 if (dev->video_bitrate_peak->is_new || 969 dev->video_bitrate->is_new) { 970 opt->bitrate = bitrate; 971 opt->peak_bitrate = peak_bitrate; 972 hdpvr_set_bitrate(dev); 973 } 974 return 0; 975 } 976 case V4L2_CID_MPEG_STREAM_TYPE: 977 return 0; 978 default: 979 break; 980 } 981 return ret; 982 } 983 984 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data, 985 struct v4l2_fmtdesc *f) 986 { 987 if (f->index != 0) 988 return -EINVAL; 989 990 f->flags = V4L2_FMT_FLAG_COMPRESSED; 991 strscpy(f->description, "MPEG2-TS with AVC/AAC streams", 992 sizeof(f->description)); 993 f->pixelformat = V4L2_PIX_FMT_MPEG; 994 995 return 0; 996 } 997 998 static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh, 999 struct v4l2_format *f) 1000 { 1001 struct hdpvr_device *dev = video_drvdata(file); 1002 struct hdpvr_fh *fh = _fh; 1003 int ret; 1004 1005 /* 1006 * The original driver would always returns the current detected 1007 * resolution as the format (and EFAULT if it couldn't be detected). 1008 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a 1009 * better way of doing this, but to stay compatible with existing 1010 * applications we assume legacy mode every time an application opens 1011 * the device. Only if one of the new DV_TIMINGS ioctls is called 1012 * will the filehandle go into 'normal' mode where g_fmt returns the 1013 * last set format. 1014 */ 1015 if (fh->legacy_mode) { 1016 struct hdpvr_video_info vid_info; 1017 1018 ret = get_video_info(dev, &vid_info); 1019 if (ret < 0) 1020 return ret; 1021 if (!vid_info.valid) 1022 return -EFAULT; 1023 f->fmt.pix.width = vid_info.width; 1024 f->fmt.pix.height = vid_info.height; 1025 } else { 1026 f->fmt.pix.width = dev->width; 1027 f->fmt.pix.height = dev->height; 1028 } 1029 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 1030 f->fmt.pix.sizeimage = dev->bulk_in_size; 1031 f->fmt.pix.bytesperline = 0; 1032 if (f->fmt.pix.width == 720) { 1033 /* SDTV formats */ 1034 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; 1035 f->fmt.pix.field = V4L2_FIELD_INTERLACED; 1036 } else { 1037 /* HDTV formats */ 1038 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709; 1039 f->fmt.pix.field = V4L2_FIELD_NONE; 1040 } 1041 return 0; 1042 } 1043 1044 static int vidioc_encoder_cmd(struct file *filp, void *priv, 1045 struct v4l2_encoder_cmd *a) 1046 { 1047 struct hdpvr_device *dev = video_drvdata(filp); 1048 int res = 0; 1049 1050 mutex_lock(&dev->io_mutex); 1051 a->flags = 0; 1052 1053 switch (a->cmd) { 1054 case V4L2_ENC_CMD_START: 1055 if (dev->owner && filp->private_data != dev->owner) { 1056 res = -EBUSY; 1057 break; 1058 } 1059 if (dev->status == STATUS_STREAMING) 1060 break; 1061 res = hdpvr_start_streaming(dev); 1062 if (!res) 1063 dev->owner = filp->private_data; 1064 else 1065 dev->status = STATUS_IDLE; 1066 break; 1067 case V4L2_ENC_CMD_STOP: 1068 if (dev->owner && filp->private_data != dev->owner) { 1069 res = -EBUSY; 1070 break; 1071 } 1072 if (dev->status == STATUS_IDLE) 1073 break; 1074 res = hdpvr_stop_streaming(dev); 1075 if (!res) 1076 dev->owner = NULL; 1077 break; 1078 default: 1079 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, 1080 "Unsupported encoder cmd %d\n", a->cmd); 1081 res = -EINVAL; 1082 break; 1083 } 1084 1085 mutex_unlock(&dev->io_mutex); 1086 return res; 1087 } 1088 1089 static int vidioc_try_encoder_cmd(struct file *filp, void *priv, 1090 struct v4l2_encoder_cmd *a) 1091 { 1092 a->flags = 0; 1093 switch (a->cmd) { 1094 case V4L2_ENC_CMD_START: 1095 case V4L2_ENC_CMD_STOP: 1096 return 0; 1097 default: 1098 return -EINVAL; 1099 } 1100 } 1101 1102 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = { 1103 .vidioc_querycap = vidioc_querycap, 1104 .vidioc_s_std = vidioc_s_std, 1105 .vidioc_g_std = vidioc_g_std, 1106 .vidioc_querystd = vidioc_querystd, 1107 .vidioc_s_dv_timings = vidioc_s_dv_timings, 1108 .vidioc_g_dv_timings = vidioc_g_dv_timings, 1109 .vidioc_query_dv_timings= vidioc_query_dv_timings, 1110 .vidioc_enum_dv_timings = vidioc_enum_dv_timings, 1111 .vidioc_dv_timings_cap = vidioc_dv_timings_cap, 1112 .vidioc_enum_input = vidioc_enum_input, 1113 .vidioc_g_input = vidioc_g_input, 1114 .vidioc_s_input = vidioc_s_input, 1115 .vidioc_enumaudio = vidioc_enumaudio, 1116 .vidioc_g_audio = vidioc_g_audio, 1117 .vidioc_s_audio = vidioc_s_audio, 1118 .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap, 1119 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1120 .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1121 .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap, 1122 .vidioc_encoder_cmd = vidioc_encoder_cmd, 1123 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd, 1124 .vidioc_log_status = v4l2_ctrl_log_status, 1125 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 1126 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 1127 }; 1128 1129 static void hdpvr_device_release(struct video_device *vdev) 1130 { 1131 struct hdpvr_device *dev = video_get_drvdata(vdev); 1132 1133 hdpvr_delete(dev); 1134 mutex_lock(&dev->io_mutex); 1135 flush_work(&dev->worker); 1136 mutex_unlock(&dev->io_mutex); 1137 1138 v4l2_device_unregister(&dev->v4l2_dev); 1139 v4l2_ctrl_handler_free(&dev->hdl); 1140 1141 /* deregister I2C adapter */ 1142 #if IS_ENABLED(CONFIG_I2C) 1143 mutex_lock(&dev->i2c_mutex); 1144 i2c_del_adapter(&dev->i2c_adapter); 1145 mutex_unlock(&dev->i2c_mutex); 1146 #endif /* CONFIG_I2C */ 1147 1148 kfree(dev->usbc_buf); 1149 kfree(dev); 1150 } 1151 1152 static const struct video_device hdpvr_video_template = { 1153 .fops = &hdpvr_fops, 1154 .release = hdpvr_device_release, 1155 .ioctl_ops = &hdpvr_ioctl_ops, 1156 .tvnorms = V4L2_STD_ALL, 1157 }; 1158 1159 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = { 1160 .try_ctrl = hdpvr_try_ctrl, 1161 .s_ctrl = hdpvr_s_ctrl, 1162 }; 1163 1164 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent, 1165 int devnum) 1166 { 1167 struct v4l2_ctrl_handler *hdl = &dev->hdl; 1168 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP; 1169 int res; 1170 1171 dev->cur_std = V4L2_STD_525_60; 1172 dev->width = 720; 1173 dev->height = 480; 1174 dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX]; 1175 v4l2_ctrl_handler_init(hdl, 11); 1176 if (dev->fw_ver > 0x15) { 1177 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1178 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80); 1179 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1180 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40); 1181 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1182 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40); 1183 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1184 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf); 1185 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1186 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80); 1187 } else { 1188 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1189 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86); 1190 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1191 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80); 1192 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1193 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80); 1194 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1195 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80); 1196 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1197 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80); 1198 } 1199 1200 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, 1201 V4L2_CID_MPEG_STREAM_TYPE, 1202 V4L2_MPEG_STREAM_TYPE_MPEG2_TS, 1203 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS); 1204 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, 1205 V4L2_CID_MPEG_AUDIO_ENCODING, 1206 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC, 1207 0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC); 1208 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, 1209 V4L2_CID_MPEG_VIDEO_ENCODING, 1210 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3, 1211 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC); 1212 1213 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, 1214 V4L2_CID_MPEG_VIDEO_BITRATE_MODE, 1215 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0, 1216 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); 1217 1218 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1219 V4L2_CID_MPEG_VIDEO_BITRATE, 1220 1000000, 13500000, 100000, 6500000); 1221 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, 1222 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, 1223 1100000, 20200000, 100000, 9000000); 1224 dev->v4l2_dev.ctrl_handler = hdl; 1225 if (hdl->error) { 1226 res = hdl->error; 1227 v4l2_err(&dev->v4l2_dev, "Could not register controls\n"); 1228 goto error; 1229 } 1230 v4l2_ctrl_cluster(3, &dev->video_mode); 1231 res = v4l2_ctrl_handler_setup(hdl); 1232 if (res < 0) { 1233 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n"); 1234 goto error; 1235 } 1236 1237 /* setup and register video device */ 1238 dev->video_dev = hdpvr_video_template; 1239 strscpy(dev->video_dev.name, "Hauppauge HD PVR", 1240 sizeof(dev->video_dev.name)); 1241 dev->video_dev.v4l2_dev = &dev->v4l2_dev; 1242 video_set_drvdata(&dev->video_dev, dev); 1243 1244 res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum); 1245 if (res < 0) { 1246 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n"); 1247 goto error; 1248 } 1249 1250 return 0; 1251 error: 1252 v4l2_ctrl_handler_free(hdl); 1253 return res; 1254 } 1255