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