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