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