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