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