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> |
Mauro Carvalho Chehab | 54d8090 | 2013-03-21 15:46:18 -0300 | [diff] [blame] | 13 | #include <linux/kconfig.h> |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 14 | #include <linux/errno.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/uaccess.h> |
| 19 | #include <linux/usb.h> |
| 20 | #include <linux/mutex.h> |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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> |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 27 | #include <media/v4l2-event.h> |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 28 | #include "hdpvr.h" |
| 29 | |
Alan Young | 39bcb3a | 2010-07-26 08:50:32 -0300 | [diff] [blame] | 30 | #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */ |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 31 | |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 32 | #define print_buffer_status() { \ |
| 33 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \ |
| 34 | "%s:%d buffer stat: %d free, %d proc\n", \ |
| 35 | __func__, __LINE__, \ |
| 36 | list_size(&dev->free_buff_list), \ |
| 37 | list_size(&dev->rec_buff_list)); } |
Janne Grunau | d211bfc | 2009-03-26 20:29:39 -0300 | [diff] [blame] | 38 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 39 | static uint list_size(struct list_head *list) |
| 40 | { |
| 41 | struct list_head *tmp; |
| 42 | uint count = 0; |
| 43 | |
| 44 | list_for_each(tmp, list) { |
| 45 | count++; |
| 46 | } |
| 47 | |
| 48 | return count; |
| 49 | } |
| 50 | |
| 51 | /*=========================================================================*/ |
| 52 | /* urb callback */ |
| 53 | static void hdpvr_read_bulk_callback(struct urb *urb) |
| 54 | { |
| 55 | struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context; |
| 56 | struct hdpvr_device *dev = buf->dev; |
| 57 | |
| 58 | /* marking buffer as received and wake waiting */ |
| 59 | buf->status = BUFSTAT_READY; |
| 60 | wake_up_interruptible(&dev->wait_data); |
| 61 | } |
| 62 | |
| 63 | /*=========================================================================*/ |
| 64 | /* bufffer bits */ |
| 65 | |
| 66 | /* function expects dev->io_mutex to be hold by caller */ |
| 67 | int hdpvr_cancel_queue(struct hdpvr_device *dev) |
| 68 | { |
| 69 | struct hdpvr_buffer *buf; |
| 70 | |
| 71 | list_for_each_entry(buf, &dev->rec_buff_list, buff_list) { |
| 72 | usb_kill_urb(buf->urb); |
| 73 | buf->status = BUFSTAT_AVAILABLE; |
| 74 | } |
| 75 | |
| 76 | list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev); |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int hdpvr_free_queue(struct list_head *q) |
| 82 | { |
| 83 | struct list_head *tmp; |
| 84 | struct list_head *p; |
| 85 | struct hdpvr_buffer *buf; |
| 86 | struct urb *urb; |
| 87 | |
| 88 | for (p = q->next; p != q;) { |
| 89 | buf = list_entry(p, struct hdpvr_buffer, buff_list); |
| 90 | |
| 91 | urb = buf->urb; |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 92 | usb_free_coherent(urb->dev, urb->transfer_buffer_length, |
| 93 | urb->transfer_buffer, urb->transfer_dma); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 94 | usb_free_urb(urb); |
| 95 | tmp = p->next; |
| 96 | list_del(p); |
| 97 | kfree(buf); |
| 98 | p = tmp; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | /* function expects dev->io_mutex to be hold by caller */ |
| 105 | int hdpvr_free_buffers(struct hdpvr_device *dev) |
| 106 | { |
| 107 | hdpvr_cancel_queue(dev); |
| 108 | |
| 109 | hdpvr_free_queue(&dev->free_buff_list); |
| 110 | hdpvr_free_queue(&dev->rec_buff_list); |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | /* function expects dev->io_mutex to be hold by caller */ |
| 116 | int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count) |
| 117 | { |
| 118 | uint i; |
| 119 | int retval = -ENOMEM; |
| 120 | u8 *mem; |
| 121 | struct hdpvr_buffer *buf; |
| 122 | struct urb *urb; |
| 123 | |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 124 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 125 | "allocating %u buffers\n", count); |
| 126 | |
| 127 | for (i = 0; i < count; i++) { |
| 128 | |
| 129 | buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL); |
| 130 | if (!buf) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 131 | v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 132 | goto exit; |
| 133 | } |
| 134 | buf->dev = dev; |
| 135 | |
| 136 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 137 | if (!urb) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 138 | v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n"); |
Julia Lawall | cd0e280 | 2009-11-21 08:49:41 -0300 | [diff] [blame] | 139 | goto exit_urb; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 140 | } |
| 141 | buf->urb = urb; |
| 142 | |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 143 | mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL, |
| 144 | &urb->transfer_dma); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 145 | if (!mem) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 146 | v4l2_err(&dev->v4l2_dev, |
| 147 | "cannot allocate usb transfer buffer\n"); |
Julia Lawall | cd0e280 | 2009-11-21 08:49:41 -0300 | [diff] [blame] | 148 | goto exit_urb_buffer; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | usb_fill_bulk_urb(buf->urb, dev->udev, |
| 152 | usb_rcvbulkpipe(dev->udev, |
| 153 | dev->bulk_in_endpointAddr), |
| 154 | mem, dev->bulk_in_size, |
| 155 | hdpvr_read_bulk_callback, buf); |
| 156 | |
James M McLaren | 4f5c933 | 2010-10-03 19:09:18 -0300 | [diff] [blame] | 157 | buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 158 | buf->status = BUFSTAT_AVAILABLE; |
| 159 | list_add_tail(&buf->buff_list, &dev->free_buff_list); |
| 160 | } |
| 161 | return 0; |
Julia Lawall | cd0e280 | 2009-11-21 08:49:41 -0300 | [diff] [blame] | 162 | exit_urb_buffer: |
| 163 | usb_free_urb(urb); |
| 164 | exit_urb: |
| 165 | kfree(buf); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 166 | exit: |
| 167 | hdpvr_free_buffers(dev); |
| 168 | return retval; |
| 169 | } |
| 170 | |
| 171 | static int hdpvr_submit_buffers(struct hdpvr_device *dev) |
| 172 | { |
| 173 | struct hdpvr_buffer *buf; |
| 174 | struct urb *urb; |
| 175 | int ret = 0, err_count = 0; |
| 176 | |
| 177 | mutex_lock(&dev->io_mutex); |
| 178 | |
| 179 | while (dev->status == STATUS_STREAMING && |
| 180 | !list_empty(&dev->free_buff_list)) { |
| 181 | |
| 182 | buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer, |
| 183 | buff_list); |
| 184 | if (buf->status != BUFSTAT_AVAILABLE) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 185 | v4l2_err(&dev->v4l2_dev, |
Thadeu Lima de Souza Cascardo | 4b512d2 | 2009-04-14 23:14:10 -0300 | [diff] [blame] | 186 | "buffer not marked as available\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 187 | ret = -EFAULT; |
| 188 | goto err; |
| 189 | } |
| 190 | |
| 191 | urb = buf->urb; |
| 192 | urb->status = 0; |
| 193 | urb->actual_length = 0; |
| 194 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 195 | if (ret) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 196 | v4l2_err(&dev->v4l2_dev, |
| 197 | "usb_submit_urb in %s returned %d\n", |
| 198 | __func__, ret); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 199 | if (++err_count > 2) |
| 200 | break; |
| 201 | continue; |
| 202 | } |
| 203 | buf->status = BUFSTAT_INPROGRESS; |
| 204 | list_move_tail(&buf->buff_list, &dev->rec_buff_list); |
| 205 | } |
| 206 | err: |
Janne Grunau | d211bfc | 2009-03-26 20:29:39 -0300 | [diff] [blame] | 207 | print_buffer_status(); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 208 | mutex_unlock(&dev->io_mutex); |
| 209 | return ret; |
| 210 | } |
| 211 | |
| 212 | static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev) |
| 213 | { |
| 214 | struct hdpvr_buffer *buf; |
| 215 | |
| 216 | mutex_lock(&dev->io_mutex); |
| 217 | |
| 218 | if (list_empty(&dev->rec_buff_list)) { |
| 219 | mutex_unlock(&dev->io_mutex); |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer, |
| 224 | buff_list); |
| 225 | mutex_unlock(&dev->io_mutex); |
| 226 | |
| 227 | return buf; |
| 228 | } |
| 229 | |
| 230 | static void hdpvr_transmit_buffers(struct work_struct *work) |
| 231 | { |
| 232 | struct hdpvr_device *dev = container_of(work, struct hdpvr_device, |
| 233 | worker); |
| 234 | |
| 235 | while (dev->status == STATUS_STREAMING) { |
| 236 | |
| 237 | if (hdpvr_submit_buffers(dev)) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 238 | v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 239 | goto error; |
| 240 | } |
| 241 | if (wait_event_interruptible(dev->wait_buffer, |
| 242 | !list_empty(&dev->free_buff_list) || |
| 243 | dev->status != STATUS_STREAMING)) |
| 244 | goto error; |
| 245 | } |
| 246 | |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 247 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 248 | "transmit worker exited\n"); |
| 249 | return; |
| 250 | error: |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 251 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 252 | "transmit buffers errored\n"); |
| 253 | dev->status = STATUS_ERROR; |
| 254 | } |
| 255 | |
| 256 | /* function expects dev->io_mutex to be hold by caller */ |
| 257 | static int hdpvr_start_streaming(struct hdpvr_device *dev) |
| 258 | { |
| 259 | int ret; |
| 260 | struct hdpvr_video_info *vidinf; |
| 261 | |
| 262 | if (dev->status == STATUS_STREAMING) |
| 263 | return 0; |
| 264 | else if (dev->status != STATUS_IDLE) |
| 265 | return -EAGAIN; |
| 266 | |
| 267 | vidinf = get_video_info(dev); |
| 268 | |
| 269 | if (vidinf) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 270 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 271 | "video signal: %dx%d@%dhz\n", vidinf->width, |
| 272 | vidinf->height, vidinf->fps); |
| 273 | kfree(vidinf); |
| 274 | |
| 275 | /* start streaming 2 request */ |
| 276 | ret = usb_control_msg(dev->udev, |
| 277 | usb_sndctrlpipe(dev->udev, 0), |
| 278 | 0xb8, 0x38, 0x1, 0, NULL, 0, 8000); |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 279 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 280 | "encoder start control request returned %d\n", ret); |
| 281 | |
| 282 | hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00); |
| 283 | |
Janne Grunau | afa1595 | 2012-02-02 13:35:21 -0300 | [diff] [blame] | 284 | dev->status = STATUS_STREAMING; |
| 285 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 286 | INIT_WORK(&dev->worker, hdpvr_transmit_buffers); |
| 287 | queue_work(dev->workqueue, &dev->worker); |
| 288 | |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 289 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 290 | "streaming started\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | msleep(250); |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 295 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 296 | "no video signal at input %d\n", dev->options.video_input); |
| 297 | return -EAGAIN; |
| 298 | } |
| 299 | |
| 300 | |
| 301 | /* function expects dev->io_mutex to be hold by caller */ |
| 302 | static int hdpvr_stop_streaming(struct hdpvr_device *dev) |
| 303 | { |
Márton Németh | 85d682b | 2010-01-23 14:44:34 +0100 | [diff] [blame] | 304 | int actual_length; |
| 305 | uint c = 0; |
Janne Grunau | 7d771ff | 2009-03-27 20:21:17 -0300 | [diff] [blame] | 306 | u8 *buf; |
| 307 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 308 | if (dev->status == STATUS_IDLE) |
| 309 | return 0; |
| 310 | else if (dev->status != STATUS_STREAMING) |
| 311 | return -EAGAIN; |
| 312 | |
Janne Grunau | 7d771ff | 2009-03-27 20:21:17 -0300 | [diff] [blame] | 313 | buf = kmalloc(dev->bulk_in_size, GFP_KERNEL); |
| 314 | if (!buf) |
| 315 | v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer " |
| 316 | "for emptying the internal device buffer. " |
| 317 | "Next capture start will be slow\n"); |
| 318 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 319 | dev->status = STATUS_SHUTTING_DOWN; |
| 320 | hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00); |
Janne Grunau | 48f98f7 | 2009-03-26 20:56:06 -0300 | [diff] [blame] | 321 | mutex_unlock(&dev->io_mutex); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 322 | |
| 323 | wake_up_interruptible(&dev->wait_buffer); |
| 324 | msleep(50); |
| 325 | |
| 326 | flush_workqueue(dev->workqueue); |
| 327 | |
Janne Grunau | 48f98f7 | 2009-03-26 20:56:06 -0300 | [diff] [blame] | 328 | mutex_lock(&dev->io_mutex); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 329 | /* kill the still outstanding urbs */ |
| 330 | hdpvr_cancel_queue(dev); |
| 331 | |
Janne Grunau | 7d771ff | 2009-03-27 20:21:17 -0300 | [diff] [blame] | 332 | /* emptying the device buffer beforeshutting it down */ |
| 333 | while (buf && ++c < 500 && |
| 334 | !usb_bulk_msg(dev->udev, |
| 335 | usb_rcvbulkpipe(dev->udev, |
| 336 | dev->bulk_in_endpointAddr), |
| 337 | buf, dev->bulk_in_size, &actual_length, |
| 338 | BULK_URB_TIMEOUT)) { |
Janne Grunau | 7d771ff | 2009-03-27 20:21:17 -0300 | [diff] [blame] | 339 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
| 340 | "%2d: got %d bytes\n", c, actual_length); |
| 341 | } |
| 342 | kfree(buf); |
| 343 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
| 344 | "used %d urbs to empty device buffers\n", c-1); |
| 345 | msleep(10); |
| 346 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 347 | dev->status = STATUS_IDLE; |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /*=======================================================================*/ |
| 354 | /* |
| 355 | * video 4 linux 2 file operations |
| 356 | */ |
| 357 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 358 | static int hdpvr_release(struct file *file) |
| 359 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 360 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 361 | |
| 362 | if (!dev) |
| 363 | return -ENODEV; |
| 364 | |
| 365 | mutex_lock(&dev->io_mutex); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 366 | if (file->private_data == dev->owner) { |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 367 | hdpvr_stop_streaming(dev); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 368 | dev->owner = NULL; |
| 369 | } |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 370 | mutex_unlock(&dev->io_mutex); |
| 371 | |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 372 | return v4l2_fh_release(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * hdpvr_v4l2_read() |
| 377 | * will allocate buffers when called for the first time |
| 378 | */ |
| 379 | static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count, |
| 380 | loff_t *pos) |
| 381 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 382 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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)) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 397 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
| 398 | "start_streaming failed\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 399 | ret = -EIO; |
| 400 | msleep(200); |
| 401 | dev->status = STATUS_IDLE; |
| 402 | mutex_unlock(&dev->io_mutex); |
| 403 | goto err; |
| 404 | } |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 405 | dev->owner = file->private_data; |
Janne Grunau | d211bfc | 2009-03-26 20:29:39 -0300 | [diff] [blame] | 406 | print_buffer_status(); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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)) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 447 | v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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 | |
Janne Grunau | d211bfc | 2009-03-26 20:29:39 -0300 | [diff] [blame] | 466 | print_buffer_status(); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 467 | |
| 468 | mutex_unlock(&dev->io_mutex); |
| 469 | |
| 470 | wake_up_interruptible(&dev->wait_buffer); |
| 471 | |
| 472 | buf = hdpvr_get_next_buffer(dev); |
| 473 | } |
| 474 | } |
| 475 | err: |
| 476 | if (!ret && !buf) |
| 477 | ret = -EAGAIN; |
| 478 | return ret; |
| 479 | } |
| 480 | |
| 481 | static unsigned int hdpvr_poll(struct file *filp, poll_table *wait) |
| 482 | { |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 483 | unsigned long req_events = poll_requested_events(wait); |
Janne Grunau | d2ff3ec | 2009-03-26 14:32:54 -0300 | [diff] [blame] | 484 | struct hdpvr_buffer *buf = NULL; |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 485 | struct hdpvr_device *dev = video_drvdata(filp); |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 486 | unsigned int mask = v4l2_ctrl_poll(filp, wait); |
| 487 | |
| 488 | if (!(req_events & (POLLIN | POLLRDNORM))) |
| 489 | return mask; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 490 | |
| 491 | mutex_lock(&dev->io_mutex); |
| 492 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 493 | if (dev->status == STATUS_IDLE) { |
| 494 | if (hdpvr_start_streaming(dev)) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 495 | v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, |
| 496 | "start_streaming failed\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 497 | dev->status = STATUS_IDLE; |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 498 | } else { |
| 499 | dev->owner = filp->private_data; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 500 | } |
| 501 | |
Janne Grunau | d211bfc | 2009-03-26 20:29:39 -0300 | [diff] [blame] | 502 | print_buffer_status(); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 503 | } |
| 504 | mutex_unlock(&dev->io_mutex); |
| 505 | |
Janne Grunau | d2ff3ec | 2009-03-26 14:32:54 -0300 | [diff] [blame] | 506 | buf = hdpvr_get_next_buffer(dev); |
| 507 | /* only wait if no data is available */ |
| 508 | if (!buf || buf->status != BUFSTAT_READY) { |
| 509 | poll_wait(filp, &dev->wait_data, wait); |
| 510 | buf = hdpvr_get_next_buffer(dev); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 511 | } |
Janne Grunau | d2ff3ec | 2009-03-26 14:32:54 -0300 | [diff] [blame] | 512 | if (buf && buf->status == BUFSTAT_READY) |
| 513 | mask |= POLLIN | POLLRDNORM; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 514 | |
| 515 | return mask; |
| 516 | } |
| 517 | |
| 518 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 519 | static const struct v4l2_file_operations hdpvr_fops = { |
| 520 | .owner = THIS_MODULE, |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 521 | .open = v4l2_fh_open, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 522 | .release = hdpvr_release, |
| 523 | .read = hdpvr_read, |
| 524 | .poll = hdpvr_poll, |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 525 | .unlocked_ioctl = video_ioctl2, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 526 | }; |
| 527 | |
| 528 | /*=======================================================================*/ |
| 529 | /* |
| 530 | * V4L2 ioctl handling |
| 531 | */ |
| 532 | |
| 533 | static int vidioc_querycap(struct file *file, void *priv, |
| 534 | struct v4l2_capability *cap) |
| 535 | { |
| 536 | struct hdpvr_device *dev = video_drvdata(file); |
| 537 | |
| 538 | strcpy(cap->driver, "hdpvr"); |
Lars Hanisch | fdd70c3 | 2010-02-14 08:57:39 -0300 | [diff] [blame] | 539 | strcpy(cap->card, "Hauppauge HD PVR"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 540 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Hans Verkuil | 41022fc | 2013-02-12 09:21:36 -0300 | [diff] [blame^] | 541 | cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO | |
| 542 | V4L2_CAP_READWRITE; |
| 543 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | static int vidioc_s_std(struct file *file, void *private_data, |
Hans Verkuil | 314527acb | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 548 | v4l2_std_id std) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 549 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 550 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 551 | u8 std_type = 1; |
| 552 | |
Hans Verkuil | 314527acb | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 553 | if (std & (V4L2_STD_NTSC | V4L2_STD_PAL_60)) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 554 | std_type = 0; |
| 555 | |
| 556 | return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type); |
| 557 | } |
| 558 | |
| 559 | static const char *iname[] = { |
| 560 | [HDPVR_COMPONENT] = "Component", |
| 561 | [HDPVR_SVIDEO] = "S-Video", |
| 562 | [HDPVR_COMPOSITE] = "Composite", |
| 563 | }; |
| 564 | |
| 565 | static int vidioc_enum_input(struct file *file, void *priv, |
| 566 | struct v4l2_input *i) |
| 567 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 568 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 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 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 590 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 591 | int retval; |
| 592 | |
| 593 | if (index >= HDPVR_VIDEO_INPUTS) |
| 594 | return -EINVAL; |
| 595 | |
| 596 | if (dev->status != STATUS_IDLE) |
| 597 | return -EAGAIN; |
| 598 | |
| 599 | retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1); |
| 600 | if (!retval) |
| 601 | dev->options.video_input = index; |
| 602 | |
| 603 | return retval; |
| 604 | } |
| 605 | |
| 606 | static int vidioc_g_input(struct file *file, void *private_data, |
| 607 | unsigned int *index) |
| 608 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 609 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 610 | |
| 611 | *index = dev->options.video_input; |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | static const char *audio_iname[] = { |
| 617 | [HDPVR_RCA_FRONT] = "RCA front", |
| 618 | [HDPVR_RCA_BACK] = "RCA back", |
| 619 | [HDPVR_SPDIF] = "SPDIF", |
| 620 | }; |
| 621 | |
| 622 | static int vidioc_enumaudio(struct file *file, void *priv, |
| 623 | struct v4l2_audio *audio) |
| 624 | { |
| 625 | unsigned int n; |
| 626 | |
| 627 | n = audio->index; |
| 628 | if (n >= HDPVR_AUDIO_INPUTS) |
| 629 | return -EINVAL; |
| 630 | |
| 631 | audio->capability = V4L2_AUDCAP_STEREO; |
| 632 | |
| 633 | strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1); |
| 634 | audio->name[sizeof(audio->name) - 1] = '\0'; |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | static int vidioc_s_audio(struct file *file, void *private_data, |
Hans Verkuil | 0e8025b9 | 2012-09-04 11:59:31 -0300 | [diff] [blame] | 640 | const struct v4l2_audio *audio) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 641 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 642 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 643 | int retval; |
| 644 | |
| 645 | if (audio->index >= HDPVR_AUDIO_INPUTS) |
| 646 | return -EINVAL; |
| 647 | |
| 648 | if (dev->status != STATUS_IDLE) |
| 649 | return -EAGAIN; |
| 650 | |
| 651 | retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec); |
| 652 | if (!retval) |
| 653 | dev->options.audio_input = audio->index; |
| 654 | |
| 655 | return retval; |
| 656 | } |
| 657 | |
| 658 | static int vidioc_g_audio(struct file *file, void *private_data, |
| 659 | struct v4l2_audio *audio) |
| 660 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 661 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 662 | |
| 663 | audio->index = dev->options.audio_input; |
| 664 | audio->capability = V4L2_AUDCAP_STEREO; |
| 665 | strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name)); |
| 666 | audio->name[sizeof(audio->name) - 1] = '\0'; |
| 667 | return 0; |
| 668 | } |
| 669 | |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 670 | static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 671 | { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 672 | struct hdpvr_device *dev = |
| 673 | container_of(ctrl->handler, struct hdpvr_device, hdl); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 674 | |
| 675 | switch (ctrl->id) { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 676 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: |
| 677 | if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR && |
| 678 | dev->video_bitrate->val >= dev->video_bitrate_peak->val) |
| 679 | dev->video_bitrate_peak->val = |
| 680 | dev->video_bitrate->val + 100000; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 681 | break; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 682 | } |
| 683 | return 0; |
| 684 | } |
| 685 | |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 686 | static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 687 | { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 688 | struct hdpvr_device *dev = |
| 689 | container_of(ctrl->handler, struct hdpvr_device, hdl); |
| 690 | struct hdpvr_options *opt = &dev->options; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 691 | int ret = -EINVAL; |
| 692 | |
| 693 | switch (ctrl->id) { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 694 | case V4L2_CID_BRIGHTNESS: |
| 695 | ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val); |
| 696 | if (ret) |
| 697 | break; |
| 698 | dev->options.brightness = ctrl->val; |
| 699 | return 0; |
| 700 | case V4L2_CID_CONTRAST: |
| 701 | ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val); |
| 702 | if (ret) |
| 703 | break; |
| 704 | dev->options.contrast = ctrl->val; |
| 705 | return 0; |
| 706 | case V4L2_CID_SATURATION: |
| 707 | ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val); |
| 708 | if (ret) |
| 709 | break; |
| 710 | dev->options.saturation = ctrl->val; |
| 711 | return 0; |
| 712 | case V4L2_CID_HUE: |
| 713 | ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val); |
| 714 | if (ret) |
| 715 | break; |
| 716 | dev->options.hue = ctrl->val; |
| 717 | return 0; |
| 718 | case V4L2_CID_SHARPNESS: |
| 719 | ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val); |
| 720 | if (ret) |
| 721 | break; |
| 722 | dev->options.sharpness = ctrl->val; |
| 723 | return 0; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 724 | case V4L2_CID_MPEG_AUDIO_ENCODING: |
| 725 | if (dev->flags & HDPVR_FLAG_AC3_CAP) { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 726 | opt->audio_codec = ctrl->val; |
| 727 | return hdpvr_set_audio(dev, opt->audio_input, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 728 | opt->audio_codec); |
| 729 | } |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 730 | return 0; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 731 | case V4L2_CID_MPEG_VIDEO_ENCODING: |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 732 | return 0; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 733 | /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */ |
| 734 | /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */ |
| 735 | /* opt->gop_mode |= 0x2; */ |
| 736 | /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */ |
| 737 | /* opt->gop_mode); */ |
| 738 | /* } */ |
| 739 | /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */ |
| 740 | /* opt->gop_mode &= ~0x2; */ |
| 741 | /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */ |
| 742 | /* opt->gop_mode); */ |
| 743 | /* } */ |
| 744 | /* break; */ |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 745 | case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: { |
| 746 | uint peak_bitrate = dev->video_bitrate_peak->val / 100000; |
| 747 | uint bitrate = dev->video_bitrate->val / 100000; |
| 748 | |
| 749 | if (ctrl->is_new) { |
| 750 | if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR) |
| 751 | opt->bitrate_mode = HDPVR_CONSTANT; |
| 752 | else |
| 753 | opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 754 | hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE, |
| 755 | opt->bitrate_mode); |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 756 | v4l2_ctrl_activate(dev->video_bitrate_peak, |
| 757 | ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 758 | } |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 759 | |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 760 | if (dev->video_bitrate_peak->is_new || |
| 761 | dev->video_bitrate->is_new) { |
| 762 | opt->bitrate = bitrate; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 763 | opt->peak_bitrate = peak_bitrate; |
| 764 | hdpvr_set_bitrate(dev); |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 765 | } |
| 766 | return 0; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 767 | } |
| 768 | case V4L2_CID_MPEG_STREAM_TYPE: |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 769 | return 0; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 770 | default: |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 771 | break; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 772 | } |
| 773 | return ret; |
| 774 | } |
| 775 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 776 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data, |
| 777 | struct v4l2_fmtdesc *f) |
| 778 | { |
| 779 | |
| 780 | if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 781 | return -EINVAL; |
| 782 | |
| 783 | f->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 784 | strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32); |
| 785 | f->pixelformat = V4L2_PIX_FMT_MPEG; |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data, |
| 791 | struct v4l2_format *f) |
| 792 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 793 | struct hdpvr_device *dev = video_drvdata(file); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 794 | struct hdpvr_video_info *vid_info; |
| 795 | |
| 796 | if (!dev) |
| 797 | return -ENODEV; |
| 798 | |
| 799 | vid_info = get_video_info(dev); |
| 800 | if (!vid_info) |
| 801 | return -EFAULT; |
| 802 | |
| 803 | f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 804 | f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; |
| 805 | f->fmt.pix.width = vid_info->width; |
| 806 | f->fmt.pix.height = vid_info->height; |
| 807 | f->fmt.pix.sizeimage = dev->bulk_in_size; |
| 808 | f->fmt.pix.colorspace = 0; |
| 809 | f->fmt.pix.bytesperline = 0; |
| 810 | f->fmt.pix.field = V4L2_FIELD_ANY; |
| 811 | |
| 812 | kfree(vid_info); |
| 813 | return 0; |
| 814 | } |
| 815 | |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 816 | static int vidioc_encoder_cmd(struct file *filp, void *priv, |
| 817 | struct v4l2_encoder_cmd *a) |
| 818 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 819 | struct hdpvr_device *dev = video_drvdata(filp); |
| 820 | int res = 0; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 821 | |
| 822 | mutex_lock(&dev->io_mutex); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 823 | a->flags = 0; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 824 | |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 825 | switch (a->cmd) { |
| 826 | case V4L2_ENC_CMD_START: |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 827 | if (dev->owner && filp->private_data != dev->owner) { |
| 828 | res = -EBUSY; |
| 829 | break; |
| 830 | } |
| 831 | if (dev->status == STATUS_STREAMING) |
| 832 | break; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 833 | res = hdpvr_start_streaming(dev); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 834 | if (!res) |
| 835 | dev->owner = filp->private_data; |
| 836 | else |
| 837 | dev->status = STATUS_IDLE; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 838 | break; |
| 839 | case V4L2_ENC_CMD_STOP: |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 840 | if (dev->owner && filp->private_data != dev->owner) { |
| 841 | res = -EBUSY; |
| 842 | break; |
| 843 | } |
| 844 | if (dev->status == STATUS_IDLE) |
| 845 | break; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 846 | res = hdpvr_stop_streaming(dev); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 847 | if (!res) |
| 848 | dev->owner = NULL; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 849 | break; |
| 850 | default: |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 851 | v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev, |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 852 | "Unsupported encoder cmd %d\n", a->cmd); |
Janne Grunau | 48f98f7 | 2009-03-26 20:56:06 -0300 | [diff] [blame] | 853 | res = -EINVAL; |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 854 | break; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 855 | } |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 856 | |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 857 | mutex_unlock(&dev->io_mutex); |
| 858 | return res; |
| 859 | } |
| 860 | |
| 861 | static int vidioc_try_encoder_cmd(struct file *filp, void *priv, |
| 862 | struct v4l2_encoder_cmd *a) |
| 863 | { |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 864 | a->flags = 0; |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 865 | switch (a->cmd) { |
| 866 | case V4L2_ENC_CMD_START: |
| 867 | case V4L2_ENC_CMD_STOP: |
| 868 | return 0; |
| 869 | default: |
| 870 | return -EINVAL; |
| 871 | } |
| 872 | } |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 873 | |
| 874 | static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = { |
| 875 | .vidioc_querycap = vidioc_querycap, |
| 876 | .vidioc_s_std = vidioc_s_std, |
| 877 | .vidioc_enum_input = vidioc_enum_input, |
| 878 | .vidioc_g_input = vidioc_g_input, |
| 879 | .vidioc_s_input = vidioc_s_input, |
| 880 | .vidioc_enumaudio = vidioc_enumaudio, |
| 881 | .vidioc_g_audio = vidioc_g_audio, |
| 882 | .vidioc_s_audio = vidioc_s_audio, |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 883 | .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap, |
| 884 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
Janne Grunau | 76717b8 | 2009-03-18 20:57:04 -0300 | [diff] [blame] | 885 | .vidioc_encoder_cmd = vidioc_encoder_cmd, |
| 886 | .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd, |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 887 | .vidioc_log_status = v4l2_ctrl_log_status, |
| 888 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 889 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 890 | }; |
| 891 | |
| 892 | static void hdpvr_device_release(struct video_device *vdev) |
| 893 | { |
| 894 | struct hdpvr_device *dev = video_get_drvdata(vdev); |
| 895 | |
| 896 | hdpvr_delete(dev); |
Hans Verkuil | f2b305c | 2010-05-02 08:01:04 -0300 | [diff] [blame] | 897 | mutex_lock(&dev->io_mutex); |
| 898 | destroy_workqueue(dev->workqueue); |
| 899 | mutex_unlock(&dev->io_mutex); |
| 900 | |
| 901 | v4l2_device_unregister(&dev->v4l2_dev); |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 902 | v4l2_ctrl_handler_free(&dev->hdl); |
Hans Verkuil | f2b305c | 2010-05-02 08:01:04 -0300 | [diff] [blame] | 903 | |
| 904 | /* deregister I2C adapter */ |
Mauro Carvalho Chehab | 54d8090 | 2013-03-21 15:46:18 -0300 | [diff] [blame] | 905 | #if IS_ENABLED(CONFIG_I2C) |
Hans Verkuil | f2b305c | 2010-05-02 08:01:04 -0300 | [diff] [blame] | 906 | mutex_lock(&dev->i2c_mutex); |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame] | 907 | i2c_del_adapter(&dev->i2c_adapter); |
Hans Verkuil | f2b305c | 2010-05-02 08:01:04 -0300 | [diff] [blame] | 908 | mutex_unlock(&dev->i2c_mutex); |
| 909 | #endif /* CONFIG_I2C */ |
| 910 | |
| 911 | kfree(dev->usbc_buf); |
| 912 | kfree(dev); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | static const struct video_device hdpvr_video_template = { |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 916 | .fops = &hdpvr_fops, |
| 917 | .release = hdpvr_device_release, |
| 918 | .ioctl_ops = &hdpvr_ioctl_ops, |
| 919 | .tvnorms = |
| 920 | V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B | |
| 921 | V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I | |
| 922 | V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N | |
| 923 | V4L2_STD_PAL_60, |
Hans Verkuil | 99362e1 | 2009-08-07 07:10:52 -0300 | [diff] [blame] | 924 | .current_norm = V4L2_STD_NTSC | V4L2_STD_PAL_M | |
| 925 | V4L2_STD_PAL_60, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 926 | }; |
| 927 | |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 928 | static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = { |
| 929 | .try_ctrl = hdpvr_try_ctrl, |
| 930 | .s_ctrl = hdpvr_s_ctrl, |
| 931 | }; |
| 932 | |
Janne Grunau | a50ab291 | 2009-03-26 14:40:55 -0300 | [diff] [blame] | 933 | int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent, |
| 934 | int devnum) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 935 | { |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 936 | struct v4l2_ctrl_handler *hdl = &dev->hdl; |
| 937 | bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP; |
| 938 | int res; |
| 939 | |
| 940 | v4l2_ctrl_handler_init(hdl, 11); |
| 941 | if (dev->fw_ver > 0x15) { |
| 942 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 943 | V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80); |
| 944 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 945 | V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40); |
| 946 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 947 | V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40); |
| 948 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 949 | V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf); |
| 950 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 951 | V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80); |
| 952 | } else { |
| 953 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 954 | V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86); |
| 955 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 956 | V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80); |
| 957 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 958 | V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80); |
| 959 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 960 | V4L2_CID_HUE, 0x0, 0xff, 1, 0x80); |
| 961 | v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 962 | V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80); |
| 963 | } |
| 964 | |
| 965 | v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, |
| 966 | V4L2_CID_MPEG_STREAM_TYPE, |
| 967 | V4L2_MPEG_STREAM_TYPE_MPEG2_TS, |
| 968 | 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS); |
| 969 | v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, |
| 970 | V4L2_CID_MPEG_AUDIO_ENCODING, |
| 971 | ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC, |
| 972 | 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC); |
| 973 | v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, |
| 974 | V4L2_CID_MPEG_VIDEO_ENCODING, |
| 975 | V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3, |
| 976 | V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC); |
| 977 | |
| 978 | dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops, |
| 979 | V4L2_CID_MPEG_VIDEO_BITRATE_MODE, |
| 980 | V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0, |
| 981 | V4L2_MPEG_VIDEO_BITRATE_MODE_CBR); |
| 982 | |
| 983 | dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 984 | V4L2_CID_MPEG_VIDEO_BITRATE, |
| 985 | 1000000, 13500000, 100000, 6500000); |
| 986 | dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops, |
| 987 | V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, |
| 988 | 1100000, 20200000, 100000, 9000000); |
| 989 | dev->v4l2_dev.ctrl_handler = hdl; |
| 990 | if (hdl->error) { |
| 991 | res = hdl->error; |
| 992 | v4l2_err(&dev->v4l2_dev, "Could not register controls\n"); |
| 993 | goto error; |
| 994 | } |
| 995 | v4l2_ctrl_cluster(3, &dev->video_mode); |
| 996 | res = v4l2_ctrl_handler_setup(hdl); |
| 997 | if (res < 0) { |
| 998 | v4l2_err(&dev->v4l2_dev, "Could not setup controls\n"); |
| 999 | goto error; |
| 1000 | } |
| 1001 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1002 | /* setup and register video device */ |
| 1003 | dev->video_dev = video_device_alloc(); |
| 1004 | if (!dev->video_dev) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 1005 | v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n"); |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 1006 | res = -ENOMEM; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1007 | goto error; |
| 1008 | } |
| 1009 | |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 1010 | *dev->video_dev = hdpvr_video_template; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1011 | strcpy(dev->video_dev->name, "Hauppauge HD PVR"); |
Hans Verkuil | ede197a | 2013-04-06 06:00:17 -0300 | [diff] [blame] | 1012 | dev->video_dev->v4l2_dev = &dev->v4l2_dev; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1013 | video_set_drvdata(dev->video_dev, dev); |
Hans Verkuil | 65fe42d | 2013-02-12 08:26:59 -0300 | [diff] [blame] | 1014 | set_bit(V4L2_FL_USE_FH_PRIO, &dev->video_dev->flags); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1015 | |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 1016 | res = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum); |
| 1017 | if (res < 0) { |
Janne Grunau | 9ef77ad | 2009-03-27 20:09:40 -0300 | [diff] [blame] | 1018 | v4l2_err(&dev->v4l2_dev, "video_device registration failed\n"); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1019 | goto error; |
| 1020 | } |
| 1021 | |
| 1022 | return 0; |
| 1023 | error: |
Hans Verkuil | 99c77aa | 2013-03-19 09:30:50 -0300 | [diff] [blame] | 1024 | v4l2_ctrl_handler_free(hdl); |
| 1025 | return res; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1026 | } |