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