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