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