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 	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 */
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 
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 
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  */
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 					     hdpvr_get_next_buffer(dev)))
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 			/* return nonblocking */
453 			if (file->f_flags & O_NONBLOCK) {
454 				if (!ret)
455 					ret = -EAGAIN;
456 				goto err;
457 			}
458 
459 			if (wait_event_interruptible(dev->wait_data,
460 					      buf->status == BUFSTAT_READY))
461 				return -ERESTARTSYS;
462 		}
463 
464 		if (buf->status != BUFSTAT_READY)
465 			break;
466 
467 		/* set remaining bytes to copy */
468 		urb = buf->urb;
469 		rem = urb->actual_length - buf->pos;
470 		cnt = rem > count ? count : rem;
471 
472 		if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
473 				 cnt)) {
474 			v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
475 			if (!ret)
476 				ret = -EFAULT;
477 			goto err;
478 		}
479 
480 		buf->pos += cnt;
481 		count -= cnt;
482 		buffer += cnt;
483 		ret += cnt;
484 
485 		/* finished, take next buffer */
486 		if (buf->pos == urb->actual_length) {
487 			mutex_lock(&dev->io_mutex);
488 			buf->pos = 0;
489 			buf->status = BUFSTAT_AVAILABLE;
490 
491 			list_move_tail(&buf->buff_list, &dev->free_buff_list);
492 
493 			print_buffer_status();
494 
495 			mutex_unlock(&dev->io_mutex);
496 
497 			wake_up_interruptible(&dev->wait_buffer);
498 
499 			buf = hdpvr_get_next_buffer(dev);
500 		}
501 	}
502 err:
503 	if (!ret && !buf)
504 		ret = -EAGAIN;
505 	return ret;
506 }
507 
508 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
509 {
510 	unsigned long req_events = poll_requested_events(wait);
511 	struct hdpvr_buffer *buf = NULL;
512 	struct hdpvr_device *dev = video_drvdata(filp);
513 	unsigned int mask = v4l2_ctrl_poll(filp, wait);
514 
515 	if (!(req_events & (POLLIN | POLLRDNORM)))
516 		return mask;
517 
518 	mutex_lock(&dev->io_mutex);
519 
520 	if (dev->status == STATUS_IDLE) {
521 		if (hdpvr_start_streaming(dev)) {
522 			v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
523 				 "start_streaming failed\n");
524 			dev->status = STATUS_IDLE;
525 		} else {
526 			dev->owner = filp->private_data;
527 		}
528 
529 		print_buffer_status();
530 	}
531 	mutex_unlock(&dev->io_mutex);
532 
533 	buf = hdpvr_get_next_buffer(dev);
534 	/* only wait if no data is available */
535 	if (!buf || buf->status != BUFSTAT_READY) {
536 		poll_wait(filp, &dev->wait_data, wait);
537 		buf = hdpvr_get_next_buffer(dev);
538 	}
539 	if (buf && buf->status == BUFSTAT_READY)
540 		mask |= POLLIN | POLLRDNORM;
541 
542 	return mask;
543 }
544 
545 
546 static const struct v4l2_file_operations hdpvr_fops = {
547 	.owner		= THIS_MODULE,
548 	.open		= hdpvr_open,
549 	.release	= hdpvr_release,
550 	.read		= hdpvr_read,
551 	.poll		= hdpvr_poll,
552 	.unlocked_ioctl	= video_ioctl2,
553 };
554 
555 /*=======================================================================*/
556 /*
557  * V4L2 ioctl handling
558  */
559 
560 static int vidioc_querycap(struct file *file, void  *priv,
561 			   struct v4l2_capability *cap)
562 {
563 	struct hdpvr_device *dev = video_drvdata(file);
564 
565 	strcpy(cap->driver, "hdpvr");
566 	strcpy(cap->card, "Hauppauge HD PVR");
567 	usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
568 	cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
569 			    V4L2_CAP_READWRITE;
570 	cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
571 	return 0;
572 }
573 
574 static int vidioc_s_std(struct file *file, void *_fh,
575 			v4l2_std_id std)
576 {
577 	struct hdpvr_device *dev = video_drvdata(file);
578 	struct hdpvr_fh *fh = _fh;
579 	u8 std_type = 1;
580 
581 	if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
582 		return -ENODATA;
583 	if (dev->status != STATUS_IDLE)
584 		return -EBUSY;
585 	if (std & V4L2_STD_525_60)
586 		std_type = 0;
587 	dev->cur_std = std;
588 	dev->width = 720;
589 	dev->height = std_type ? 576 : 480;
590 
591 	return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
592 }
593 
594 static int vidioc_g_std(struct file *file, void *_fh,
595 			v4l2_std_id *std)
596 {
597 	struct hdpvr_device *dev = video_drvdata(file);
598 	struct hdpvr_fh *fh = _fh;
599 
600 	if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
601 		return -ENODATA;
602 	*std = dev->cur_std;
603 	return 0;
604 }
605 
606 static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
607 {
608 	struct hdpvr_device *dev = video_drvdata(file);
609 	struct hdpvr_video_info vid_info;
610 	struct hdpvr_fh *fh = _fh;
611 	int ret;
612 
613 	*a = V4L2_STD_UNKNOWN;
614 	if (dev->options.video_input == HDPVR_COMPONENT)
615 		return fh->legacy_mode ? 0 : -ENODATA;
616 	ret = get_video_info(dev, &vid_info);
617 	if (vid_info.valid && vid_info.width == 720 &&
618 	    (vid_info.height == 480 || vid_info.height == 576)) {
619 		*a = (vid_info.height == 480) ?
620 			V4L2_STD_525_60 : V4L2_STD_625_50;
621 	}
622 	return ret;
623 }
624 
625 static int vidioc_s_dv_timings(struct file *file, void *_fh,
626 				    struct v4l2_dv_timings *timings)
627 {
628 	struct hdpvr_device *dev = video_drvdata(file);
629 	struct hdpvr_fh *fh = _fh;
630 	int i;
631 
632 	fh->legacy_mode = false;
633 	if (dev->options.video_input)
634 		return -ENODATA;
635 	if (dev->status != STATUS_IDLE)
636 		return -EBUSY;
637 	for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
638 		if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
639 			break;
640 	if (i == ARRAY_SIZE(hdpvr_dv_timings))
641 		return -EINVAL;
642 	dev->cur_dv_timings = hdpvr_dv_timings[i];
643 	dev->width = hdpvr_dv_timings[i].bt.width;
644 	dev->height = hdpvr_dv_timings[i].bt.height;
645 	return 0;
646 }
647 
648 static int vidioc_g_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 
654 	fh->legacy_mode = false;
655 	if (dev->options.video_input)
656 		return -ENODATA;
657 	*timings = dev->cur_dv_timings;
658 	return 0;
659 }
660 
661 static int vidioc_query_dv_timings(struct file *file, void *_fh,
662 				    struct v4l2_dv_timings *timings)
663 {
664 	struct hdpvr_device *dev = video_drvdata(file);
665 	struct hdpvr_fh *fh = _fh;
666 	struct hdpvr_video_info vid_info;
667 	bool interlaced;
668 	int ret = 0;
669 	int i;
670 
671 	fh->legacy_mode = false;
672 	if (dev->options.video_input)
673 		return -ENODATA;
674 	ret = get_video_info(dev, &vid_info);
675 	if (ret)
676 		return ret;
677 	if (!vid_info.valid)
678 		return -ENOLCK;
679 	interlaced = vid_info.fps <= 30;
680 	for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
681 		const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
682 		unsigned hsize;
683 		unsigned vsize;
684 		unsigned fps;
685 
686 		hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
687 		vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
688 		fps = (unsigned)bt->pixelclock / (hsize * vsize);
689 		if (bt->width != vid_info.width ||
690 		    bt->height != vid_info.height ||
691 		    bt->interlaced != interlaced ||
692 		    (fps != vid_info.fps && fps + 1 != vid_info.fps))
693 			continue;
694 		*timings = hdpvr_dv_timings[i];
695 		break;
696 	}
697 	if (i == ARRAY_SIZE(hdpvr_dv_timings))
698 		ret = -ERANGE;
699 
700 	return ret;
701 }
702 
703 static int vidioc_enum_dv_timings(struct file *file, void *_fh,
704 				    struct v4l2_enum_dv_timings *timings)
705 {
706 	struct hdpvr_device *dev = video_drvdata(file);
707 	struct hdpvr_fh *fh = _fh;
708 
709 	fh->legacy_mode = false;
710 	memset(timings->reserved, 0, sizeof(timings->reserved));
711 	if (dev->options.video_input)
712 		return -ENODATA;
713 	if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
714 		return -EINVAL;
715 	timings->timings = hdpvr_dv_timings[timings->index];
716 	return 0;
717 }
718 
719 static int vidioc_dv_timings_cap(struct file *file, void *_fh,
720 				    struct v4l2_dv_timings_cap *cap)
721 {
722 	struct hdpvr_device *dev = video_drvdata(file);
723 	struct hdpvr_fh *fh = _fh;
724 
725 	fh->legacy_mode = false;
726 	if (dev->options.video_input)
727 		return -ENODATA;
728 	cap->type = V4L2_DV_BT_656_1120;
729 	cap->bt.min_width = 720;
730 	cap->bt.max_width = 1920;
731 	cap->bt.min_height = 480;
732 	cap->bt.max_height = 1080;
733 	cap->bt.min_pixelclock = 27000000;
734 	cap->bt.max_pixelclock = 74250000;
735 	cap->bt.standards = V4L2_DV_BT_STD_CEA861;
736 	cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
737 	return 0;
738 }
739 
740 static const char *iname[] = {
741 	[HDPVR_COMPONENT] = "Component",
742 	[HDPVR_SVIDEO]    = "S-Video",
743 	[HDPVR_COMPOSITE] = "Composite",
744 };
745 
746 static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
747 {
748 	unsigned int n;
749 
750 	n = i->index;
751 	if (n >= HDPVR_VIDEO_INPUTS)
752 		return -EINVAL;
753 
754 	i->type = V4L2_INPUT_TYPE_CAMERA;
755 
756 	strncpy(i->name, iname[n], sizeof(i->name) - 1);
757 	i->name[sizeof(i->name) - 1] = '\0';
758 
759 	i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
760 
761 	i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
762 	i->std = n ? V4L2_STD_ALL : 0;
763 
764 	return 0;
765 }
766 
767 static int vidioc_s_input(struct file *file, void *_fh,
768 			  unsigned int index)
769 {
770 	struct hdpvr_device *dev = video_drvdata(file);
771 	int retval;
772 
773 	if (index >= HDPVR_VIDEO_INPUTS)
774 		return -EINVAL;
775 
776 	if (dev->status != STATUS_IDLE)
777 		return -EBUSY;
778 
779 	retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
780 	if (!retval) {
781 		dev->options.video_input = index;
782 		/*
783 		 * Unfortunately gstreamer calls ENUMSTD and bails out if it
784 		 * won't find any formats, even though component input is
785 		 * selected. This means that we have to leave tvnorms at
786 		 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
787 		 * tvnorms is set at the device node level and not at the
788 		 * filehandle level.
789 		 *
790 		 * Comment this out for now, but if the legacy mode can be
791 		 * removed in the future, then this code should be enabled
792 		 * again.
793 		dev->video_dev.tvnorms =
794 			(index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
795 		 */
796 	}
797 
798 	return retval;
799 }
800 
801 static int vidioc_g_input(struct file *file, void *private_data,
802 			  unsigned int *index)
803 {
804 	struct hdpvr_device *dev = video_drvdata(file);
805 
806 	*index = dev->options.video_input;
807 	return 0;
808 }
809 
810 
811 static const char *audio_iname[] = {
812 	[HDPVR_RCA_FRONT] = "RCA front",
813 	[HDPVR_RCA_BACK]  = "RCA back",
814 	[HDPVR_SPDIF]     = "SPDIF",
815 };
816 
817 static int vidioc_enumaudio(struct file *file, void *priv,
818 				struct v4l2_audio *audio)
819 {
820 	unsigned int n;
821 
822 	n = audio->index;
823 	if (n >= HDPVR_AUDIO_INPUTS)
824 		return -EINVAL;
825 
826 	audio->capability = V4L2_AUDCAP_STEREO;
827 
828 	strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
829 	audio->name[sizeof(audio->name) - 1] = '\0';
830 
831 	return 0;
832 }
833 
834 static int vidioc_s_audio(struct file *file, void *private_data,
835 			  const struct v4l2_audio *audio)
836 {
837 	struct hdpvr_device *dev = video_drvdata(file);
838 	int retval;
839 
840 	if (audio->index >= HDPVR_AUDIO_INPUTS)
841 		return -EINVAL;
842 
843 	if (dev->status != STATUS_IDLE)
844 		return -EBUSY;
845 
846 	retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
847 	if (!retval)
848 		dev->options.audio_input = audio->index;
849 
850 	return retval;
851 }
852 
853 static int vidioc_g_audio(struct file *file, void *private_data,
854 			  struct v4l2_audio *audio)
855 {
856 	struct hdpvr_device *dev = video_drvdata(file);
857 
858 	audio->index = dev->options.audio_input;
859 	audio->capability = V4L2_AUDCAP_STEREO;
860 	strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
861 	audio->name[sizeof(audio->name) - 1] = '\0';
862 	return 0;
863 }
864 
865 static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
866 {
867 	struct hdpvr_device *dev =
868 		container_of(ctrl->handler, struct hdpvr_device, hdl);
869 
870 	switch (ctrl->id) {
871 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
872 		if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
873 		    dev->video_bitrate->val >= dev->video_bitrate_peak->val)
874 			dev->video_bitrate_peak->val =
875 					dev->video_bitrate->val + 100000;
876 		break;
877 	}
878 	return 0;
879 }
880 
881 static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
882 {
883 	struct hdpvr_device *dev =
884 		container_of(ctrl->handler, struct hdpvr_device, hdl);
885 	struct hdpvr_options *opt = &dev->options;
886 	int ret = -EINVAL;
887 
888 	switch (ctrl->id) {
889 	case V4L2_CID_BRIGHTNESS:
890 		ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
891 		if (ret)
892 			break;
893 		dev->options.brightness = ctrl->val;
894 		return 0;
895 	case V4L2_CID_CONTRAST:
896 		ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
897 		if (ret)
898 			break;
899 		dev->options.contrast = ctrl->val;
900 		return 0;
901 	case V4L2_CID_SATURATION:
902 		ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
903 		if (ret)
904 			break;
905 		dev->options.saturation = ctrl->val;
906 		return 0;
907 	case V4L2_CID_HUE:
908 		ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
909 		if (ret)
910 			break;
911 		dev->options.hue = ctrl->val;
912 		return 0;
913 	case V4L2_CID_SHARPNESS:
914 		ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
915 		if (ret)
916 			break;
917 		dev->options.sharpness = ctrl->val;
918 		return 0;
919 	case V4L2_CID_MPEG_AUDIO_ENCODING:
920 		if (dev->flags & HDPVR_FLAG_AC3_CAP) {
921 			opt->audio_codec = ctrl->val;
922 			return hdpvr_set_audio(dev, opt->audio_input + 1,
923 					      opt->audio_codec);
924 		}
925 		return 0;
926 	case V4L2_CID_MPEG_VIDEO_ENCODING:
927 		return 0;
928 /* 	case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
929 /* 		if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
930 /* 			opt->gop_mode |= 0x2; */
931 /* 			hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
932 /* 					  opt->gop_mode); */
933 /* 		} */
934 /* 		if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
935 /* 			opt->gop_mode &= ~0x2; */
936 /* 			hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
937 /* 					  opt->gop_mode); */
938 /* 		} */
939 /* 		break; */
940 	case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
941 		uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
942 		uint bitrate = dev->video_bitrate->val / 100000;
943 
944 		if (ctrl->is_new) {
945 			if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
946 				opt->bitrate_mode = HDPVR_CONSTANT;
947 			else
948 				opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
949 			hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
950 					  opt->bitrate_mode);
951 			v4l2_ctrl_activate(dev->video_bitrate_peak,
952 				ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
953 		}
954 
955 		if (dev->video_bitrate_peak->is_new ||
956 		    dev->video_bitrate->is_new) {
957 			opt->bitrate = bitrate;
958 			opt->peak_bitrate = peak_bitrate;
959 			hdpvr_set_bitrate(dev);
960 		}
961 		return 0;
962 	}
963 	case V4L2_CID_MPEG_STREAM_TYPE:
964 		return 0;
965 	default:
966 		break;
967 	}
968 	return ret;
969 }
970 
971 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
972 				    struct v4l2_fmtdesc *f)
973 {
974 	if (f->index != 0)
975 		return -EINVAL;
976 
977 	f->flags = V4L2_FMT_FLAG_COMPRESSED;
978 	strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
979 	f->pixelformat = V4L2_PIX_FMT_MPEG;
980 
981 	return 0;
982 }
983 
984 static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
985 				struct v4l2_format *f)
986 {
987 	struct hdpvr_device *dev = video_drvdata(file);
988 	struct hdpvr_fh *fh = _fh;
989 	int ret;
990 
991 	/*
992 	 * The original driver would always returns the current detected
993 	 * resolution as the format (and EFAULT if it couldn't be detected).
994 	 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
995 	 * better way of doing this, but to stay compatible with existing
996 	 * applications we assume legacy mode every time an application opens
997 	 * the device. Only if one of the new DV_TIMINGS ioctls is called
998 	 * will the filehandle go into 'normal' mode where g_fmt returns the
999 	 * last set format.
1000 	 */
1001 	if (fh->legacy_mode) {
1002 		struct hdpvr_video_info vid_info;
1003 
1004 		ret = get_video_info(dev, &vid_info);
1005 		if (ret < 0)
1006 			return ret;
1007 		if (!vid_info.valid)
1008 			return -EFAULT;
1009 		f->fmt.pix.width = vid_info.width;
1010 		f->fmt.pix.height = vid_info.height;
1011 	} else {
1012 		f->fmt.pix.width = dev->width;
1013 		f->fmt.pix.height = dev->height;
1014 	}
1015 	f->fmt.pix.pixelformat	= V4L2_PIX_FMT_MPEG;
1016 	f->fmt.pix.sizeimage	= dev->bulk_in_size;
1017 	f->fmt.pix.bytesperline	= 0;
1018 	if (f->fmt.pix.width == 720) {
1019 		/* SDTV formats */
1020 		f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1021 		f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1022 	} else {
1023 		/* HDTV formats */
1024 		f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
1025 		f->fmt.pix.field = V4L2_FIELD_NONE;
1026 	}
1027 	return 0;
1028 }
1029 
1030 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1031 			       struct v4l2_encoder_cmd *a)
1032 {
1033 	struct hdpvr_device *dev = video_drvdata(filp);
1034 	int res = 0;
1035 
1036 	mutex_lock(&dev->io_mutex);
1037 	a->flags = 0;
1038 
1039 	switch (a->cmd) {
1040 	case V4L2_ENC_CMD_START:
1041 		if (dev->owner && filp->private_data != dev->owner) {
1042 			res = -EBUSY;
1043 			break;
1044 		}
1045 		if (dev->status == STATUS_STREAMING)
1046 			break;
1047 		res = hdpvr_start_streaming(dev);
1048 		if (!res)
1049 			dev->owner = filp->private_data;
1050 		else
1051 			dev->status = STATUS_IDLE;
1052 		break;
1053 	case V4L2_ENC_CMD_STOP:
1054 		if (dev->owner && filp->private_data != dev->owner) {
1055 			res = -EBUSY;
1056 			break;
1057 		}
1058 		if (dev->status == STATUS_IDLE)
1059 			break;
1060 		res = hdpvr_stop_streaming(dev);
1061 		if (!res)
1062 			dev->owner = NULL;
1063 		break;
1064 	default:
1065 		v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1066 			 "Unsupported encoder cmd %d\n", a->cmd);
1067 		res = -EINVAL;
1068 		break;
1069 	}
1070 
1071 	mutex_unlock(&dev->io_mutex);
1072 	return res;
1073 }
1074 
1075 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1076 					struct v4l2_encoder_cmd *a)
1077 {
1078 	a->flags = 0;
1079 	switch (a->cmd) {
1080 	case V4L2_ENC_CMD_START:
1081 	case V4L2_ENC_CMD_STOP:
1082 		return 0;
1083 	default:
1084 		return -EINVAL;
1085 	}
1086 }
1087 
1088 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1089 	.vidioc_querycap	= vidioc_querycap,
1090 	.vidioc_s_std		= vidioc_s_std,
1091 	.vidioc_g_std		= vidioc_g_std,
1092 	.vidioc_querystd	= vidioc_querystd,
1093 	.vidioc_s_dv_timings	= vidioc_s_dv_timings,
1094 	.vidioc_g_dv_timings	= vidioc_g_dv_timings,
1095 	.vidioc_query_dv_timings= vidioc_query_dv_timings,
1096 	.vidioc_enum_dv_timings	= vidioc_enum_dv_timings,
1097 	.vidioc_dv_timings_cap	= vidioc_dv_timings_cap,
1098 	.vidioc_enum_input	= vidioc_enum_input,
1099 	.vidioc_g_input		= vidioc_g_input,
1100 	.vidioc_s_input		= vidioc_s_input,
1101 	.vidioc_enumaudio	= vidioc_enumaudio,
1102 	.vidioc_g_audio		= vidioc_g_audio,
1103 	.vidioc_s_audio		= vidioc_s_audio,
1104 	.vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
1105 	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1106 	.vidioc_s_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1107 	.vidioc_try_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1108 	.vidioc_encoder_cmd	= vidioc_encoder_cmd,
1109 	.vidioc_try_encoder_cmd	= vidioc_try_encoder_cmd,
1110 	.vidioc_log_status	= v4l2_ctrl_log_status,
1111 	.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1112 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1113 };
1114 
1115 static void hdpvr_device_release(struct video_device *vdev)
1116 {
1117 	struct hdpvr_device *dev = video_get_drvdata(vdev);
1118 
1119 	hdpvr_delete(dev);
1120 	mutex_lock(&dev->io_mutex);
1121 	flush_work(&dev->worker);
1122 	mutex_unlock(&dev->io_mutex);
1123 
1124 	v4l2_device_unregister(&dev->v4l2_dev);
1125 	v4l2_ctrl_handler_free(&dev->hdl);
1126 
1127 	/* deregister I2C adapter */
1128 #if IS_ENABLED(CONFIG_I2C)
1129 	mutex_lock(&dev->i2c_mutex);
1130 	i2c_del_adapter(&dev->i2c_adapter);
1131 	mutex_unlock(&dev->i2c_mutex);
1132 #endif /* CONFIG_I2C */
1133 
1134 	kfree(dev->usbc_buf);
1135 	kfree(dev);
1136 }
1137 
1138 static const struct video_device hdpvr_video_template = {
1139 	.fops			= &hdpvr_fops,
1140 	.release		= hdpvr_device_release,
1141 	.ioctl_ops 		= &hdpvr_ioctl_ops,
1142 	.tvnorms		= V4L2_STD_ALL,
1143 };
1144 
1145 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
1146 	.try_ctrl = hdpvr_try_ctrl,
1147 	.s_ctrl = hdpvr_s_ctrl,
1148 };
1149 
1150 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1151 			    int devnum)
1152 {
1153 	struct v4l2_ctrl_handler *hdl = &dev->hdl;
1154 	bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
1155 	int res;
1156 
1157 	dev->cur_std = V4L2_STD_525_60;
1158 	dev->width = 720;
1159 	dev->height = 480;
1160 	dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
1161 	v4l2_ctrl_handler_init(hdl, 11);
1162 	if (dev->fw_ver > 0x15) {
1163 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1164 			V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
1165 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1166 			V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
1167 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1168 			V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
1169 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1170 			V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
1171 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1172 			V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1173 	} else {
1174 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1175 			V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
1176 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1177 			V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
1178 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1179 			V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
1180 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1181 			V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
1182 		v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1183 			V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1184 	}
1185 
1186 	v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1187 		V4L2_CID_MPEG_STREAM_TYPE,
1188 		V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1189 		0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1190 	v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1191 		V4L2_CID_MPEG_AUDIO_ENCODING,
1192 		ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
1193 		0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
1194 	v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1195 		V4L2_CID_MPEG_VIDEO_ENCODING,
1196 		V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1197 		V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1198 
1199 	dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1200 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1201 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1202 		V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1203 
1204 	dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1205 		V4L2_CID_MPEG_VIDEO_BITRATE,
1206 		1000000, 13500000, 100000, 6500000);
1207 	dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1208 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1209 		1100000, 20200000, 100000, 9000000);
1210 	dev->v4l2_dev.ctrl_handler = hdl;
1211 	if (hdl->error) {
1212 		res = hdl->error;
1213 		v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1214 		goto error;
1215 	}
1216 	v4l2_ctrl_cluster(3, &dev->video_mode);
1217 	res = v4l2_ctrl_handler_setup(hdl);
1218 	if (res < 0) {
1219 		v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1220 		goto error;
1221 	}
1222 
1223 	/* setup and register video device */
1224 	dev->video_dev = hdpvr_video_template;
1225 	strcpy(dev->video_dev.name, "Hauppauge HD PVR");
1226 	dev->video_dev.v4l2_dev = &dev->v4l2_dev;
1227 	video_set_drvdata(&dev->video_dev, dev);
1228 
1229 	res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum);
1230 	if (res < 0) {
1231 		v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1232 		goto error;
1233 	}
1234 
1235 	return 0;
1236 error:
1237 	v4l2_ctrl_handler_free(hdl);
1238 	return res;
1239 }
1240