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