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