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