2 * Hauppauge HD PVR USB driver - video 4 linux 2 interface
4 * Copyright (C) 2008 Janne Grunau (j@jannau.net)
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.
12 #include <linux/kernel.h>
13 #include <linux/kconfig.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/uaccess.h>
19 #include <linux/usb.h>
20 #include <linux/mutex.h>
21 #include <linux/workqueue.h>
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-dv-timings.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-dv-timings.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-event.h>
32 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
34 #define print_buffer_status() { \
35 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
36 "%s:%d buffer stat: %d free, %d proc\n", \
38 list_size(&dev->free_buff_list), \
39 list_size(&dev->rec_buff_list)); }
41 static const struct v4l2_dv_timings hdpvr_dv_timings[] = {
42 V4L2_DV_BT_CEA_720X480I59_94,
43 V4L2_DV_BT_CEA_720X576I50,
44 V4L2_DV_BT_CEA_720X480P59_94,
45 V4L2_DV_BT_CEA_720X576P50,
46 V4L2_DV_BT_CEA_1280X720P50,
47 V4L2_DV_BT_CEA_1280X720P60,
48 V4L2_DV_BT_CEA_1920X1080I50,
49 V4L2_DV_BT_CEA_1920X1080I60,
52 /* Use 480i59 as the default timings */
53 #define HDPVR_DEF_DV_TIMINGS_IDX (0)
60 static uint list_size(struct list_head *list)
62 struct list_head *tmp;
65 list_for_each(tmp, list) {
72 /*=========================================================================*/
74 static void hdpvr_read_bulk_callback(struct urb *urb)
76 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
77 struct hdpvr_device *dev = buf->dev;
79 /* marking buffer as received and wake waiting */
80 buf->status = BUFSTAT_READY;
81 wake_up_interruptible(&dev->wait_data);
84 /*=========================================================================*/
87 /* function expects dev->io_mutex to be hold by caller */
88 int hdpvr_cancel_queue(struct hdpvr_device *dev)
90 struct hdpvr_buffer *buf;
92 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
93 usb_kill_urb(buf->urb);
94 buf->status = BUFSTAT_AVAILABLE;
97 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
102 static int hdpvr_free_queue(struct list_head *q)
104 struct list_head *tmp;
106 struct hdpvr_buffer *buf;
109 for (p = q->next; p != q;) {
110 buf = list_entry(p, struct hdpvr_buffer, buff_list);
113 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
114 urb->transfer_buffer, urb->transfer_dma);
125 /* function expects dev->io_mutex to be hold by caller */
126 int hdpvr_free_buffers(struct hdpvr_device *dev)
128 hdpvr_cancel_queue(dev);
130 hdpvr_free_queue(&dev->free_buff_list);
131 hdpvr_free_queue(&dev->rec_buff_list);
136 /* function expects dev->io_mutex to be hold by caller */
137 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
140 int retval = -ENOMEM;
142 struct hdpvr_buffer *buf;
145 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
146 "allocating %u buffers\n", count);
148 for (i = 0; i < count; i++) {
150 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
152 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
157 urb = usb_alloc_urb(0, GFP_KERNEL);
162 mem = usb_alloc_coherent(dev->udev, dev->bulk_in_size, GFP_KERNEL,
165 v4l2_err(&dev->v4l2_dev,
166 "cannot allocate usb transfer buffer\n");
167 goto exit_urb_buffer;
170 usb_fill_bulk_urb(buf->urb, dev->udev,
171 usb_rcvbulkpipe(dev->udev,
172 dev->bulk_in_endpointAddr),
173 mem, dev->bulk_in_size,
174 hdpvr_read_bulk_callback, buf);
176 buf->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
177 buf->status = BUFSTAT_AVAILABLE;
178 list_add_tail(&buf->buff_list, &dev->free_buff_list);
186 hdpvr_free_buffers(dev);
190 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
192 struct hdpvr_buffer *buf;
194 int ret = 0, err_count = 0;
196 mutex_lock(&dev->io_mutex);
198 while (dev->status == STATUS_STREAMING &&
199 !list_empty(&dev->free_buff_list)) {
201 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
203 if (buf->status != BUFSTAT_AVAILABLE) {
204 v4l2_err(&dev->v4l2_dev,
205 "buffer not marked as available\n");
212 urb->actual_length = 0;
213 ret = usb_submit_urb(urb, GFP_KERNEL);
215 v4l2_err(&dev->v4l2_dev,
216 "usb_submit_urb in %s returned %d\n",
222 buf->status = BUFSTAT_INPROGRESS;
223 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
226 print_buffer_status();
227 mutex_unlock(&dev->io_mutex);
231 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
233 struct hdpvr_buffer *buf;
235 mutex_lock(&dev->io_mutex);
237 if (list_empty(&dev->rec_buff_list)) {
238 mutex_unlock(&dev->io_mutex);
242 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
244 mutex_unlock(&dev->io_mutex);
249 static void hdpvr_transmit_buffers(struct work_struct *work)
251 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
254 while (dev->status == STATUS_STREAMING) {
256 if (hdpvr_submit_buffers(dev)) {
257 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
260 if (wait_event_interruptible(dev->wait_buffer,
261 !list_empty(&dev->free_buff_list) ||
262 dev->status != STATUS_STREAMING))
266 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
267 "transmit worker exited\n");
270 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
271 "transmit buffers errored\n");
272 dev->status = STATUS_ERROR;
275 /* function expects dev->io_mutex to be hold by caller */
276 static int hdpvr_start_streaming(struct hdpvr_device *dev)
279 struct hdpvr_video_info vidinf;
281 if (dev->status == STATUS_STREAMING)
283 if (dev->status != STATUS_IDLE)
286 ret = get_video_info(dev, &vidinf);
292 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
293 "no video signal at input %d\n", dev->options.video_input);
297 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
298 "video signal: %dx%d@%dhz\n", vidinf.width,
299 vidinf.height, vidinf.fps);
301 /* start streaming 2 request */
302 ret = usb_control_msg(dev->udev,
303 usb_sndctrlpipe(dev->udev, 0),
304 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
305 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
306 "encoder start control request returned %d\n", ret);
310 ret = hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
314 dev->status = STATUS_STREAMING;
316 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
317 schedule_work(&dev->worker);
319 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
320 "streaming started\n");
326 /* function expects dev->io_mutex to be hold by caller */
327 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
333 if (dev->status == STATUS_IDLE)
335 else if (dev->status != STATUS_STREAMING)
338 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
340 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
341 "for emptying the internal device buffer. "
342 "Next capture start will be slow\n");
344 dev->status = STATUS_SHUTTING_DOWN;
345 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
346 mutex_unlock(&dev->io_mutex);
348 wake_up_interruptible(&dev->wait_buffer);
351 flush_work(&dev->worker);
353 mutex_lock(&dev->io_mutex);
354 /* kill the still outstanding urbs */
355 hdpvr_cancel_queue(dev);
357 /* emptying the device buffer beforeshutting it down */
358 while (buf && ++c < 500 &&
359 !usb_bulk_msg(dev->udev,
360 usb_rcvbulkpipe(dev->udev,
361 dev->bulk_in_endpointAddr),
362 buf, dev->bulk_in_size, &actual_length,
364 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
365 "%2d: got %d bytes\n", c, actual_length);
368 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
369 "used %d urbs to empty device buffers\n", c-1);
372 dev->status = STATUS_IDLE;
378 /*=======================================================================*/
380 * video 4 linux 2 file operations
383 static int hdpvr_open(struct file *file)
385 struct hdpvr_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
389 fh->legacy_mode = true;
390 v4l2_fh_init(&fh->fh, video_devdata(file));
391 v4l2_fh_add(&fh->fh);
392 file->private_data = fh;
396 static int hdpvr_release(struct file *file)
398 struct hdpvr_device *dev = video_drvdata(file);
400 mutex_lock(&dev->io_mutex);
401 if (file->private_data == dev->owner) {
402 hdpvr_stop_streaming(dev);
405 mutex_unlock(&dev->io_mutex);
407 return v4l2_fh_release(file);
412 * will allocate buffers when called for the first time
414 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
417 struct hdpvr_device *dev = video_drvdata(file);
418 struct hdpvr_buffer *buf = NULL;
420 unsigned int ret = 0;
426 mutex_lock(&dev->io_mutex);
427 if (dev->status == STATUS_IDLE) {
428 if (hdpvr_start_streaming(dev)) {
429 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
430 "start_streaming failed\n");
433 dev->status = STATUS_IDLE;
434 mutex_unlock(&dev->io_mutex);
437 dev->owner = file->private_data;
438 print_buffer_status();
440 mutex_unlock(&dev->io_mutex);
442 /* wait for the first buffer */
443 if (!(file->f_flags & O_NONBLOCK)) {
444 if (wait_event_interruptible(dev->wait_data,
445 hdpvr_get_next_buffer(dev)))
449 buf = hdpvr_get_next_buffer(dev);
451 while (count > 0 && buf) {
453 if (buf->status != BUFSTAT_READY &&
454 dev->status != STATUS_DISCONNECTED) {
455 /* return nonblocking */
456 if (file->f_flags & O_NONBLOCK) {
462 if (wait_event_interruptible(dev->wait_data,
463 buf->status == BUFSTAT_READY))
467 if (buf->status != BUFSTAT_READY)
470 /* set remaining bytes to copy */
472 rem = urb->actual_length - buf->pos;
473 cnt = rem > count ? count : rem;
475 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
477 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
488 /* finished, take next buffer */
489 if (buf->pos == urb->actual_length) {
490 mutex_lock(&dev->io_mutex);
492 buf->status = BUFSTAT_AVAILABLE;
494 list_move_tail(&buf->buff_list, &dev->free_buff_list);
496 print_buffer_status();
498 mutex_unlock(&dev->io_mutex);
500 wake_up_interruptible(&dev->wait_buffer);
502 buf = hdpvr_get_next_buffer(dev);
511 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
513 unsigned long req_events = poll_requested_events(wait);
514 struct hdpvr_buffer *buf = NULL;
515 struct hdpvr_device *dev = video_drvdata(filp);
516 unsigned int mask = v4l2_ctrl_poll(filp, wait);
518 if (!(req_events & (POLLIN | POLLRDNORM)))
521 mutex_lock(&dev->io_mutex);
523 if (dev->status == STATUS_IDLE) {
524 if (hdpvr_start_streaming(dev)) {
525 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
526 "start_streaming failed\n");
527 dev->status = STATUS_IDLE;
529 dev->owner = filp->private_data;
532 print_buffer_status();
534 mutex_unlock(&dev->io_mutex);
536 buf = hdpvr_get_next_buffer(dev);
537 /* only wait if no data is available */
538 if (!buf || buf->status != BUFSTAT_READY) {
539 poll_wait(filp, &dev->wait_data, wait);
540 buf = hdpvr_get_next_buffer(dev);
542 if (buf && buf->status == BUFSTAT_READY)
543 mask |= POLLIN | POLLRDNORM;
549 static const struct v4l2_file_operations hdpvr_fops = {
550 .owner = THIS_MODULE,
552 .release = hdpvr_release,
555 .unlocked_ioctl = video_ioctl2,
558 /*=======================================================================*/
560 * V4L2 ioctl handling
563 static int vidioc_querycap(struct file *file, void *priv,
564 struct v4l2_capability *cap)
566 struct hdpvr_device *dev = video_drvdata(file);
568 strcpy(cap->driver, "hdpvr");
569 strcpy(cap->card, "Hauppauge HD PVR");
570 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
571 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_AUDIO |
573 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
577 static int vidioc_s_std(struct file *file, void *_fh,
580 struct hdpvr_device *dev = video_drvdata(file);
581 struct hdpvr_fh *fh = _fh;
584 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
586 if (dev->status != STATUS_IDLE)
588 if (std & V4L2_STD_525_60)
592 dev->height = std_type ? 576 : 480;
594 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
597 static int vidioc_g_std(struct file *file, void *_fh,
600 struct hdpvr_device *dev = video_drvdata(file);
601 struct hdpvr_fh *fh = _fh;
603 if (!fh->legacy_mode && dev->options.video_input == HDPVR_COMPONENT)
609 static int vidioc_querystd(struct file *file, void *_fh, v4l2_std_id *a)
611 struct hdpvr_device *dev = video_drvdata(file);
612 struct hdpvr_video_info vid_info;
613 struct hdpvr_fh *fh = _fh;
616 *a = V4L2_STD_UNKNOWN;
617 if (dev->options.video_input == HDPVR_COMPONENT)
618 return fh->legacy_mode ? 0 : -ENODATA;
619 ret = get_video_info(dev, &vid_info);
620 if (vid_info.valid && vid_info.width == 720 &&
621 (vid_info.height == 480 || vid_info.height == 576)) {
622 *a = (vid_info.height == 480) ?
623 V4L2_STD_525_60 : V4L2_STD_625_50;
628 static int vidioc_s_dv_timings(struct file *file, void *_fh,
629 struct v4l2_dv_timings *timings)
631 struct hdpvr_device *dev = video_drvdata(file);
632 struct hdpvr_fh *fh = _fh;
635 fh->legacy_mode = false;
636 if (dev->options.video_input)
638 if (dev->status != STATUS_IDLE)
640 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++)
641 if (v4l2_match_dv_timings(timings, hdpvr_dv_timings + i, 0, false))
643 if (i == ARRAY_SIZE(hdpvr_dv_timings))
645 dev->cur_dv_timings = hdpvr_dv_timings[i];
646 dev->width = hdpvr_dv_timings[i].bt.width;
647 dev->height = hdpvr_dv_timings[i].bt.height;
651 static int vidioc_g_dv_timings(struct file *file, void *_fh,
652 struct v4l2_dv_timings *timings)
654 struct hdpvr_device *dev = video_drvdata(file);
655 struct hdpvr_fh *fh = _fh;
657 fh->legacy_mode = false;
658 if (dev->options.video_input)
660 *timings = dev->cur_dv_timings;
664 static int vidioc_query_dv_timings(struct file *file, void *_fh,
665 struct v4l2_dv_timings *timings)
667 struct hdpvr_device *dev = video_drvdata(file);
668 struct hdpvr_fh *fh = _fh;
669 struct hdpvr_video_info vid_info;
674 fh->legacy_mode = false;
675 if (dev->options.video_input)
677 ret = get_video_info(dev, &vid_info);
682 interlaced = vid_info.fps <= 30;
683 for (i = 0; i < ARRAY_SIZE(hdpvr_dv_timings); i++) {
684 const struct v4l2_bt_timings *bt = &hdpvr_dv_timings[i].bt;
689 hsize = V4L2_DV_BT_FRAME_WIDTH(bt);
690 vsize = V4L2_DV_BT_FRAME_HEIGHT(bt);
691 fps = (unsigned)bt->pixelclock / (hsize * vsize);
692 if (bt->width != vid_info.width ||
693 bt->height != vid_info.height ||
694 bt->interlaced != interlaced ||
695 (fps != vid_info.fps && fps + 1 != vid_info.fps))
697 *timings = hdpvr_dv_timings[i];
700 if (i == ARRAY_SIZE(hdpvr_dv_timings))
706 static int vidioc_enum_dv_timings(struct file *file, void *_fh,
707 struct v4l2_enum_dv_timings *timings)
709 struct hdpvr_device *dev = video_drvdata(file);
710 struct hdpvr_fh *fh = _fh;
712 fh->legacy_mode = false;
713 memset(timings->reserved, 0, sizeof(timings->reserved));
714 if (dev->options.video_input)
716 if (timings->index >= ARRAY_SIZE(hdpvr_dv_timings))
718 timings->timings = hdpvr_dv_timings[timings->index];
722 static int vidioc_dv_timings_cap(struct file *file, void *_fh,
723 struct v4l2_dv_timings_cap *cap)
725 struct hdpvr_device *dev = video_drvdata(file);
726 struct hdpvr_fh *fh = _fh;
728 fh->legacy_mode = false;
729 if (dev->options.video_input)
731 cap->type = V4L2_DV_BT_656_1120;
732 cap->bt.min_width = 720;
733 cap->bt.max_width = 1920;
734 cap->bt.min_height = 480;
735 cap->bt.max_height = 1080;
736 cap->bt.min_pixelclock = 27000000;
737 cap->bt.max_pixelclock = 74250000;
738 cap->bt.standards = V4L2_DV_BT_STD_CEA861;
739 cap->bt.capabilities = V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE;
743 static const char *iname[] = {
744 [HDPVR_COMPONENT] = "Component",
745 [HDPVR_SVIDEO] = "S-Video",
746 [HDPVR_COMPOSITE] = "Composite",
749 static int vidioc_enum_input(struct file *file, void *_fh, struct v4l2_input *i)
754 if (n >= HDPVR_VIDEO_INPUTS)
757 i->type = V4L2_INPUT_TYPE_CAMERA;
759 strncpy(i->name, iname[n], sizeof(i->name) - 1);
760 i->name[sizeof(i->name) - 1] = '\0';
762 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
764 i->capabilities = n ? V4L2_IN_CAP_STD : V4L2_IN_CAP_DV_TIMINGS;
765 i->std = n ? V4L2_STD_ALL : 0;
770 static int vidioc_s_input(struct file *file, void *_fh,
773 struct hdpvr_device *dev = video_drvdata(file);
776 if (index >= HDPVR_VIDEO_INPUTS)
779 if (dev->status != STATUS_IDLE)
782 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
784 dev->options.video_input = index;
786 * Unfortunately gstreamer calls ENUMSTD and bails out if it
787 * won't find any formats, even though component input is
788 * selected. This means that we have to leave tvnorms at
789 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
790 * tvnorms is set at the device node level and not at the
793 * Comment this out for now, but if the legacy mode can be
794 * removed in the future, then this code should be enabled
796 dev->video_dev.tvnorms =
797 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
804 static int vidioc_g_input(struct file *file, void *private_data,
807 struct hdpvr_device *dev = video_drvdata(file);
809 *index = dev->options.video_input;
814 static const char *audio_iname[] = {
815 [HDPVR_RCA_FRONT] = "RCA front",
816 [HDPVR_RCA_BACK] = "RCA back",
817 [HDPVR_SPDIF] = "SPDIF",
820 static int vidioc_enumaudio(struct file *file, void *priv,
821 struct v4l2_audio *audio)
826 if (n >= HDPVR_AUDIO_INPUTS)
829 audio->capability = V4L2_AUDCAP_STEREO;
831 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
832 audio->name[sizeof(audio->name) - 1] = '\0';
837 static int vidioc_s_audio(struct file *file, void *private_data,
838 const struct v4l2_audio *audio)
840 struct hdpvr_device *dev = video_drvdata(file);
843 if (audio->index >= HDPVR_AUDIO_INPUTS)
846 if (dev->status != STATUS_IDLE)
849 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
851 dev->options.audio_input = audio->index;
856 static int vidioc_g_audio(struct file *file, void *private_data,
857 struct v4l2_audio *audio)
859 struct hdpvr_device *dev = video_drvdata(file);
861 audio->index = dev->options.audio_input;
862 audio->capability = V4L2_AUDCAP_STEREO;
863 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
864 audio->name[sizeof(audio->name) - 1] = '\0';
868 static int hdpvr_try_ctrl(struct v4l2_ctrl *ctrl)
870 struct hdpvr_device *dev =
871 container_of(ctrl->handler, struct hdpvr_device, hdl);
874 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
875 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
876 dev->video_bitrate->val >= dev->video_bitrate_peak->val)
877 dev->video_bitrate_peak->val =
878 dev->video_bitrate->val + 100000;
884 static int hdpvr_s_ctrl(struct v4l2_ctrl *ctrl)
886 struct hdpvr_device *dev =
887 container_of(ctrl->handler, struct hdpvr_device, hdl);
888 struct hdpvr_options *opt = &dev->options;
892 case V4L2_CID_BRIGHTNESS:
893 ret = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->val);
896 dev->options.brightness = ctrl->val;
898 case V4L2_CID_CONTRAST:
899 ret = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->val);
902 dev->options.contrast = ctrl->val;
904 case V4L2_CID_SATURATION:
905 ret = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->val);
908 dev->options.saturation = ctrl->val;
911 ret = hdpvr_config_call(dev, CTRL_HUE, ctrl->val);
914 dev->options.hue = ctrl->val;
916 case V4L2_CID_SHARPNESS:
917 ret = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->val);
920 dev->options.sharpness = ctrl->val;
922 case V4L2_CID_MPEG_AUDIO_ENCODING:
923 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
924 opt->audio_codec = ctrl->val;
925 return hdpvr_set_audio(dev, opt->audio_input + 1,
929 case V4L2_CID_MPEG_VIDEO_ENCODING:
931 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
932 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
933 /* opt->gop_mode |= 0x2; */
934 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
935 /* opt->gop_mode); */
937 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
938 /* opt->gop_mode &= ~0x2; */
939 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
940 /* opt->gop_mode); */
943 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE: {
944 uint peak_bitrate = dev->video_bitrate_peak->val / 100000;
945 uint bitrate = dev->video_bitrate->val / 100000;
948 if (ctrl->val == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR)
949 opt->bitrate_mode = HDPVR_CONSTANT;
951 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
952 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
954 v4l2_ctrl_activate(dev->video_bitrate_peak,
955 ctrl->val != V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
958 if (dev->video_bitrate_peak->is_new ||
959 dev->video_bitrate->is_new) {
960 opt->bitrate = bitrate;
961 opt->peak_bitrate = peak_bitrate;
962 hdpvr_set_bitrate(dev);
966 case V4L2_CID_MPEG_STREAM_TYPE:
974 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
975 struct v4l2_fmtdesc *f)
980 f->flags = V4L2_FMT_FLAG_COMPRESSED;
981 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
982 f->pixelformat = V4L2_PIX_FMT_MPEG;
987 static int vidioc_g_fmt_vid_cap(struct file *file, void *_fh,
988 struct v4l2_format *f)
990 struct hdpvr_device *dev = video_drvdata(file);
991 struct hdpvr_fh *fh = _fh;
995 * The original driver would always returns the current detected
996 * resolution as the format (and EFAULT if it couldn't be detected).
997 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
998 * better way of doing this, but to stay compatible with existing
999 * applications we assume legacy mode every time an application opens
1000 * the device. Only if one of the new DV_TIMINGS ioctls is called
1001 * will the filehandle go into 'normal' mode where g_fmt returns the
1004 if (fh->legacy_mode) {
1005 struct hdpvr_video_info vid_info;
1007 ret = get_video_info(dev, &vid_info);
1010 if (!vid_info.valid)
1012 f->fmt.pix.width = vid_info.width;
1013 f->fmt.pix.height = vid_info.height;
1015 f->fmt.pix.width = dev->width;
1016 f->fmt.pix.height = dev->height;
1018 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1019 f->fmt.pix.sizeimage = dev->bulk_in_size;
1020 f->fmt.pix.bytesperline = 0;
1021 if (f->fmt.pix.width == 720) {
1023 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1024 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1027 f->fmt.pix.colorspace = V4L2_COLORSPACE_REC709;
1028 f->fmt.pix.field = V4L2_FIELD_NONE;
1033 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1034 struct v4l2_encoder_cmd *a)
1036 struct hdpvr_device *dev = video_drvdata(filp);
1039 mutex_lock(&dev->io_mutex);
1043 case V4L2_ENC_CMD_START:
1044 if (dev->owner && filp->private_data != dev->owner) {
1048 if (dev->status == STATUS_STREAMING)
1050 res = hdpvr_start_streaming(dev);
1052 dev->owner = filp->private_data;
1054 dev->status = STATUS_IDLE;
1056 case V4L2_ENC_CMD_STOP:
1057 if (dev->owner && filp->private_data != dev->owner) {
1061 if (dev->status == STATUS_IDLE)
1063 res = hdpvr_stop_streaming(dev);
1068 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1069 "Unsupported encoder cmd %d\n", a->cmd);
1074 mutex_unlock(&dev->io_mutex);
1078 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1079 struct v4l2_encoder_cmd *a)
1083 case V4L2_ENC_CMD_START:
1084 case V4L2_ENC_CMD_STOP:
1091 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1092 .vidioc_querycap = vidioc_querycap,
1093 .vidioc_s_std = vidioc_s_std,
1094 .vidioc_g_std = vidioc_g_std,
1095 .vidioc_querystd = vidioc_querystd,
1096 .vidioc_s_dv_timings = vidioc_s_dv_timings,
1097 .vidioc_g_dv_timings = vidioc_g_dv_timings,
1098 .vidioc_query_dv_timings= vidioc_query_dv_timings,
1099 .vidioc_enum_dv_timings = vidioc_enum_dv_timings,
1100 .vidioc_dv_timings_cap = vidioc_dv_timings_cap,
1101 .vidioc_enum_input = vidioc_enum_input,
1102 .vidioc_g_input = vidioc_g_input,
1103 .vidioc_s_input = vidioc_s_input,
1104 .vidioc_enumaudio = vidioc_enumaudio,
1105 .vidioc_g_audio = vidioc_g_audio,
1106 .vidioc_s_audio = vidioc_s_audio,
1107 .vidioc_enum_fmt_vid_cap= vidioc_enum_fmt_vid_cap,
1108 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1109 .vidioc_s_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1110 .vidioc_try_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1111 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1112 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1113 .vidioc_log_status = v4l2_ctrl_log_status,
1114 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1115 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1118 static void hdpvr_device_release(struct video_device *vdev)
1120 struct hdpvr_device *dev = video_get_drvdata(vdev);
1123 mutex_lock(&dev->io_mutex);
1124 flush_work(&dev->worker);
1125 mutex_unlock(&dev->io_mutex);
1127 v4l2_device_unregister(&dev->v4l2_dev);
1128 v4l2_ctrl_handler_free(&dev->hdl);
1130 /* deregister I2C adapter */
1131 #if IS_ENABLED(CONFIG_I2C)
1132 mutex_lock(&dev->i2c_mutex);
1133 i2c_del_adapter(&dev->i2c_adapter);
1134 mutex_unlock(&dev->i2c_mutex);
1135 #endif /* CONFIG_I2C */
1137 kfree(dev->usbc_buf);
1141 static const struct video_device hdpvr_video_template = {
1142 .fops = &hdpvr_fops,
1143 .release = hdpvr_device_release,
1144 .ioctl_ops = &hdpvr_ioctl_ops,
1145 .tvnorms = V4L2_STD_ALL,
1148 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops = {
1149 .try_ctrl = hdpvr_try_ctrl,
1150 .s_ctrl = hdpvr_s_ctrl,
1153 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1156 struct v4l2_ctrl_handler *hdl = &dev->hdl;
1157 bool ac3 = dev->flags & HDPVR_FLAG_AC3_CAP;
1160 dev->cur_std = V4L2_STD_525_60;
1163 dev->cur_dv_timings = hdpvr_dv_timings[HDPVR_DEF_DV_TIMINGS_IDX];
1164 v4l2_ctrl_handler_init(hdl, 11);
1165 if (dev->fw_ver > 0x15) {
1166 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1167 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x80);
1168 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1169 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x40);
1170 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1171 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x40);
1172 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1173 V4L2_CID_HUE, 0x0, 0x1e, 1, 0xf);
1174 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1175 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1177 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1178 V4L2_CID_BRIGHTNESS, 0x0, 0xff, 1, 0x86);
1179 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1180 V4L2_CID_CONTRAST, 0x0, 0xff, 1, 0x80);
1181 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1182 V4L2_CID_SATURATION, 0x0, 0xff, 1, 0x80);
1183 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1184 V4L2_CID_HUE, 0x0, 0xff, 1, 0x80);
1185 v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1186 V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x80);
1189 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1190 V4L2_CID_MPEG_STREAM_TYPE,
1191 V4L2_MPEG_STREAM_TYPE_MPEG2_TS,
1192 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS);
1193 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1194 V4L2_CID_MPEG_AUDIO_ENCODING,
1195 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3 : V4L2_MPEG_AUDIO_ENCODING_AAC,
1196 0x7, ac3 ? dev->options.audio_codec : V4L2_MPEG_AUDIO_ENCODING_AAC);
1197 v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1198 V4L2_CID_MPEG_VIDEO_ENCODING,
1199 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 0x3,
1200 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1202 dev->video_mode = v4l2_ctrl_new_std_menu(hdl, &hdpvr_ctrl_ops,
1203 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
1204 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 0,
1205 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
1207 dev->video_bitrate = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1208 V4L2_CID_MPEG_VIDEO_BITRATE,
1209 1000000, 13500000, 100000, 6500000);
1210 dev->video_bitrate_peak = v4l2_ctrl_new_std(hdl, &hdpvr_ctrl_ops,
1211 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
1212 1100000, 20200000, 100000, 9000000);
1213 dev->v4l2_dev.ctrl_handler = hdl;
1216 v4l2_err(&dev->v4l2_dev, "Could not register controls\n");
1219 v4l2_ctrl_cluster(3, &dev->video_mode);
1220 res = v4l2_ctrl_handler_setup(hdl);
1222 v4l2_err(&dev->v4l2_dev, "Could not setup controls\n");
1226 /* setup and register video device */
1227 dev->video_dev = hdpvr_video_template;
1228 strcpy(dev->video_dev.name, "Hauppauge HD PVR");
1229 dev->video_dev.v4l2_dev = &dev->v4l2_dev;
1230 video_set_drvdata(&dev->video_dev, dev);
1232 res = video_register_device(&dev->video_dev, VFL_TYPE_GRABBER, devnum);
1234 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");
1240 v4l2_ctrl_handler_free(hdl);