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