2 * Hauppage 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/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/version.h>
21 #include <linux/workqueue.h>
23 #include <linux/videodev2.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-ioctl.h>
29 #define BULK_URB_TIMEOUT 1250 /* 1.25 seconds */
32 struct hdpvr_device *dev;
35 static uint list_size(struct list_head *list)
37 struct list_head *tmp;
40 list_for_each(tmp, list) {
47 /*=========================================================================*/
49 static void hdpvr_read_bulk_callback(struct urb *urb)
51 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
52 struct hdpvr_device *dev = buf->dev;
54 /* marking buffer as received and wake waiting */
55 buf->status = BUFSTAT_READY;
56 wake_up_interruptible(&dev->wait_data);
59 /*=========================================================================*/
62 /* function expects dev->io_mutex to be hold by caller */
63 int hdpvr_cancel_queue(struct hdpvr_device *dev)
65 struct hdpvr_buffer *buf;
67 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
68 usb_kill_urb(buf->urb);
69 buf->status = BUFSTAT_AVAILABLE;
72 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
77 static int hdpvr_free_queue(struct list_head *q)
79 struct list_head *tmp;
81 struct hdpvr_buffer *buf;
84 for (p = q->next; p != q;) {
85 buf = list_entry(p, struct hdpvr_buffer, buff_list);
88 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
89 urb->transfer_buffer, urb->transfer_dma);
100 /* function expects dev->io_mutex to be hold by caller */
101 int hdpvr_free_buffers(struct hdpvr_device *dev)
103 hdpvr_cancel_queue(dev);
105 hdpvr_free_queue(&dev->free_buff_list);
106 hdpvr_free_queue(&dev->rec_buff_list);
111 /* function expects dev->io_mutex to be hold by caller */
112 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
115 int retval = -ENOMEM;
117 struct hdpvr_buffer *buf;
120 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
121 "allocating %u buffers\n", count);
123 for (i = 0; i < count; i++) {
125 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
127 err("cannot allocate buffer");
132 urb = usb_alloc_urb(0, GFP_KERNEL);
134 err("cannot allocate urb");
139 mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
142 err("cannot allocate usb transfer buffer");
146 usb_fill_bulk_urb(buf->urb, dev->udev,
147 usb_rcvbulkpipe(dev->udev,
148 dev->bulk_in_endpointAddr),
149 mem, dev->bulk_in_size,
150 hdpvr_read_bulk_callback, buf);
152 buf->status = BUFSTAT_AVAILABLE;
153 list_add_tail(&buf->buff_list, &dev->free_buff_list);
157 hdpvr_free_buffers(dev);
161 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
163 struct hdpvr_buffer *buf;
165 int ret = 0, err_count = 0;
167 mutex_lock(&dev->io_mutex);
169 while (dev->status == STATUS_STREAMING &&
170 !list_empty(&dev->free_buff_list)) {
172 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
174 if (buf->status != BUFSTAT_AVAILABLE) {
175 err("buffer not marked as availbale");
182 urb->actual_length = 0;
183 ret = usb_submit_urb(urb, GFP_KERNEL);
185 err("usb_submit_urb in %s returned %d", __func__, ret);
190 buf->status = BUFSTAT_INPROGRESS;
191 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
194 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
195 "buffer queue stat: %d free, %d proc\n",
196 list_size(&dev->free_buff_list),
197 list_size(&dev->rec_buff_list));
198 mutex_unlock(&dev->io_mutex);
202 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
204 struct hdpvr_buffer *buf;
206 mutex_lock(&dev->io_mutex);
208 if (list_empty(&dev->rec_buff_list)) {
209 mutex_unlock(&dev->io_mutex);
213 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
215 mutex_unlock(&dev->io_mutex);
220 static void hdpvr_transmit_buffers(struct work_struct *work)
222 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
225 while (dev->status == STATUS_STREAMING) {
227 if (hdpvr_submit_buffers(dev)) {
228 v4l2_err(dev->video_dev, "couldn't submit buffers\n");
231 if (wait_event_interruptible(dev->wait_buffer,
232 !list_empty(&dev->free_buff_list) ||
233 dev->status != STATUS_STREAMING))
237 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
238 "transmit worker exited\n");
241 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
242 "transmit buffers errored\n");
243 dev->status = STATUS_ERROR;
246 /* function expects dev->io_mutex to be hold by caller */
247 static int hdpvr_start_streaming(struct hdpvr_device *dev)
250 struct hdpvr_video_info *vidinf;
252 if (dev->status == STATUS_STREAMING)
254 else if (dev->status != STATUS_IDLE)
257 vidinf = get_video_info(dev);
260 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
261 "video signal: %dx%d@%dhz\n", vidinf->width,
262 vidinf->height, vidinf->fps);
265 /* start streaming 2 request */
266 ret = usb_control_msg(dev->udev,
267 usb_sndctrlpipe(dev->udev, 0),
268 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
269 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
270 "encoder start control request returned %d\n", ret);
272 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
274 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
275 queue_work(dev->workqueue, &dev->worker);
277 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
278 "streaming started\n");
279 dev->status = STATUS_STREAMING;
284 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
285 "no video signal at input %d\n", dev->options.video_input);
290 /* function expects dev->io_mutex to be hold by caller */
291 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
293 if (dev->status == STATUS_IDLE)
295 else if (dev->status != STATUS_STREAMING)
298 dev->status = STATUS_SHUTTING_DOWN;
299 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
301 wake_up_interruptible(&dev->wait_buffer);
304 flush_workqueue(dev->workqueue);
306 /* kill the still outstanding urbs */
307 hdpvr_cancel_queue(dev);
309 dev->status = STATUS_IDLE;
315 /*=======================================================================*/
317 * video 4 linux 2 file operations
320 static int hdpvr_open(struct file *file)
322 struct hdpvr_device *dev;
324 int retval = -ENOMEM;
326 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
328 err("open failing with with ENODEV");
333 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
335 err("Out of memory?");
338 /* lock the device to allow correctly handling errors
340 mutex_lock(&dev->io_mutex);
345 /* save our object in the file's private structure */
346 file->private_data = fh;
350 mutex_unlock(&dev->io_mutex);
354 static int hdpvr_release(struct file *file)
356 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
357 struct hdpvr_device *dev = fh->dev;
362 mutex_lock(&dev->io_mutex);
363 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
364 hdpvr_stop_streaming(dev);
366 mutex_unlock(&dev->io_mutex);
373 * will allocate buffers when called for the first time
375 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
378 struct hdpvr_fh *fh = file->private_data;
379 struct hdpvr_device *dev = fh->dev;
380 struct hdpvr_buffer *buf = NULL;
382 unsigned int ret = 0;
391 mutex_lock(&dev->io_mutex);
392 if (dev->status == STATUS_IDLE) {
393 if (hdpvr_start_streaming(dev)) {
394 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
395 "start_streaming failed");
398 dev->status = STATUS_IDLE;
399 mutex_unlock(&dev->io_mutex);
403 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
404 "buffer queue stat: %d free, %d proc\n",
405 list_size(&dev->free_buff_list),
406 list_size(&dev->rec_buff_list));
408 mutex_unlock(&dev->io_mutex);
410 /* wait for the first buffer */
411 if (!(file->f_flags & O_NONBLOCK)) {
412 if (wait_event_interruptible(dev->wait_data,
413 hdpvr_get_next_buffer(dev)))
417 buf = hdpvr_get_next_buffer(dev);
419 while (count > 0 && buf) {
421 if (buf->status != BUFSTAT_READY &&
422 dev->status != STATUS_DISCONNECTED) {
423 /* return nonblocking */
424 if (file->f_flags & O_NONBLOCK) {
430 if (wait_event_interruptible(dev->wait_data,
431 buf->status == BUFSTAT_READY)) {
437 if (buf->status != BUFSTAT_READY)
440 /* set remaining bytes to copy */
442 rem = urb->actual_length - buf->pos;
443 cnt = rem > count ? count : rem;
445 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
447 err("read: copy_to_user failed");
458 /* finished, take next buffer */
459 if (buf->pos == urb->actual_length) {
460 mutex_lock(&dev->io_mutex);
462 buf->status = BUFSTAT_AVAILABLE;
464 list_move_tail(&buf->buff_list, &dev->free_buff_list);
466 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
467 "buffer queue stat: %d free, %d proc\n",
468 list_size(&dev->free_buff_list),
469 list_size(&dev->rec_buff_list));
471 mutex_unlock(&dev->io_mutex);
473 wake_up_interruptible(&dev->wait_buffer);
475 buf = hdpvr_get_next_buffer(dev);
484 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
486 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
487 struct hdpvr_device *dev = fh->dev;
488 unsigned int mask = 0;
490 mutex_lock(&dev->io_mutex);
492 if (video_is_unregistered(dev->video_dev))
495 if (dev->status == STATUS_IDLE) {
496 if (hdpvr_start_streaming(dev)) {
497 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
498 "start_streaming failed");
499 dev->status = STATUS_IDLE;
502 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
503 "buffer queue stat: %d free, %d proc\n",
504 list_size(&dev->free_buff_list),
505 list_size(&dev->rec_buff_list));
507 mutex_unlock(&dev->io_mutex);
509 poll_wait(filp, &dev->wait_data, wait);
511 mutex_lock(&dev->io_mutex);
512 if (!list_empty(&dev->rec_buff_list)) {
514 struct hdpvr_buffer *buf = list_entry(dev->rec_buff_list.next,
518 if (buf->status == BUFSTAT_READY)
519 mask |= POLLIN | POLLRDNORM;
521 mutex_unlock(&dev->io_mutex);
527 static long hdpvr_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
529 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
530 struct hdpvr_device *dev = fh->dev;
533 if (video_is_unregistered(dev->video_dev))
536 mutex_lock(&dev->io_mutex);
538 case VIDIOC_TRY_ENCODER_CMD:
539 case VIDIOC_ENCODER_CMD: {
540 struct v4l2_encoder_cmd *enc = (struct v4l2_encoder_cmd *)arg;
541 int try = cmd == VIDIOC_TRY_ENCODER_CMD;
543 memset(&enc->raw, 0, sizeof(enc->raw));
545 case V4L2_ENC_CMD_START:
549 res = hdpvr_start_streaming(dev);
551 case V4L2_ENC_CMD_STOP:
554 res = hdpvr_stop_streaming(dev);
557 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
558 "Unsupported encoder cmd %d\n", enc->cmd);
564 res = video_ioctl2(filp, cmd, arg);
566 mutex_unlock(&dev->io_mutex);
570 static const struct v4l2_file_operations hdpvr_fops = {
571 .owner = THIS_MODULE,
573 .release = hdpvr_release,
576 .unlocked_ioctl = hdpvr_ioctl,
579 /*=======================================================================*/
581 * V4L2 ioctl handling
584 static int vidioc_querycap(struct file *file, void *priv,
585 struct v4l2_capability *cap)
587 struct hdpvr_device *dev = video_drvdata(file);
589 strcpy(cap->driver, "hdpvr");
590 strcpy(cap->card, "Haupauge HD PVR");
591 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
592 cap->version = HDPVR_VERSION;
593 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
599 static int vidioc_s_std(struct file *file, void *private_data,
602 struct hdpvr_fh *fh = file->private_data;
603 struct hdpvr_device *dev = fh->dev;
606 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
609 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
612 static const char *iname[] = {
613 [HDPVR_COMPONENT] = "Component",
614 [HDPVR_SVIDEO] = "S-Video",
615 [HDPVR_COMPOSITE] = "Composite",
618 static int vidioc_enum_input(struct file *file, void *priv,
619 struct v4l2_input *i)
621 struct hdpvr_fh *fh = file->private_data;
622 struct hdpvr_device *dev = fh->dev;
626 if (n >= HDPVR_VIDEO_INPUTS)
629 i->type = V4L2_INPUT_TYPE_CAMERA;
631 strncpy(i->name, iname[n], sizeof(i->name) - 1);
632 i->name[sizeof(i->name) - 1] = '\0';
634 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
636 i->std = dev->video_dev->tvnorms;
641 static int vidioc_s_input(struct file *file, void *private_data,
644 struct hdpvr_fh *fh = file->private_data;
645 struct hdpvr_device *dev = fh->dev;
648 if (index >= HDPVR_VIDEO_INPUTS)
651 if (dev->status != STATUS_IDLE)
654 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
656 dev->options.video_input = index;
661 static int vidioc_g_input(struct file *file, void *private_data,
664 struct hdpvr_fh *fh = file->private_data;
665 struct hdpvr_device *dev = fh->dev;
667 *index = dev->options.video_input;
672 static const char *audio_iname[] = {
673 [HDPVR_RCA_FRONT] = "RCA front",
674 [HDPVR_RCA_BACK] = "RCA back",
675 [HDPVR_SPDIF] = "SPDIF",
678 static int vidioc_enumaudio(struct file *file, void *priv,
679 struct v4l2_audio *audio)
684 if (n >= HDPVR_AUDIO_INPUTS)
687 audio->capability = V4L2_AUDCAP_STEREO;
689 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
690 audio->name[sizeof(audio->name) - 1] = '\0';
695 static int vidioc_s_audio(struct file *file, void *private_data,
696 struct v4l2_audio *audio)
698 struct hdpvr_fh *fh = file->private_data;
699 struct hdpvr_device *dev = fh->dev;
702 if (audio->index >= HDPVR_AUDIO_INPUTS)
705 if (dev->status != STATUS_IDLE)
708 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
710 dev->options.audio_input = audio->index;
715 static int vidioc_g_audio(struct file *file, void *private_data,
716 struct v4l2_audio *audio)
718 struct hdpvr_fh *fh = file->private_data;
719 struct hdpvr_device *dev = fh->dev;
721 audio->index = dev->options.audio_input;
722 audio->capability = V4L2_AUDCAP_STEREO;
723 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
724 audio->name[sizeof(audio->name) - 1] = '\0';
728 static const s32 supported_v4l2_ctrls[] = {
734 V4L2_CID_MPEG_AUDIO_ENCODING,
735 V4L2_CID_MPEG_VIDEO_ENCODING,
736 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
737 V4L2_CID_MPEG_VIDEO_BITRATE,
738 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
741 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
747 case V4L2_CID_BRIGHTNESS:
748 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
749 case V4L2_CID_CONTRAST:
750 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
751 case V4L2_CID_SATURATION:
752 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
754 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
755 case V4L2_CID_SHARPNESS:
756 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
757 case V4L2_CID_MPEG_AUDIO_ENCODING:
758 return v4l2_ctrl_query_fill(
759 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
760 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
761 : V4L2_MPEG_AUDIO_ENCODING_AAC,
762 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
763 case V4L2_CID_MPEG_VIDEO_ENCODING:
764 return v4l2_ctrl_query_fill(
765 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
766 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
767 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
769 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
770 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
771 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
772 return v4l2_ctrl_query_fill(
773 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
774 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
775 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
777 case V4L2_CID_MPEG_VIDEO_BITRATE:
778 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
780 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
781 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
783 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
784 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
791 static int vidioc_queryctrl(struct file *file, void *private_data,
792 struct v4l2_queryctrl *qc)
794 struct hdpvr_fh *fh = file->private_data;
795 struct hdpvr_device *dev = fh->dev;
799 memset(qc, 0, sizeof(*qc));
801 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
802 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
804 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
806 if (qc->id < supported_v4l2_ctrls[i])
807 qc->id = supported_v4l2_ctrls[i];
812 if (qc->id == supported_v4l2_ctrls[i])
813 return fill_queryctrl(&dev->options, qc,
814 dev->flags & HDPVR_FLAG_AC3_CAP);
816 if (qc->id < supported_v4l2_ctrls[i])
823 static int vidioc_g_ctrl(struct file *file, void *private_data,
824 struct v4l2_control *ctrl)
826 struct hdpvr_fh *fh = file->private_data;
827 struct hdpvr_device *dev = fh->dev;
830 case V4L2_CID_BRIGHTNESS:
831 ctrl->value = dev->options.brightness;
833 case V4L2_CID_CONTRAST:
834 ctrl->value = dev->options.contrast;
836 case V4L2_CID_SATURATION:
837 ctrl->value = dev->options.saturation;
840 ctrl->value = dev->options.hue;
842 case V4L2_CID_SHARPNESS:
843 ctrl->value = dev->options.sharpness;
851 static int vidioc_s_ctrl(struct file *file, void *private_data,
852 struct v4l2_control *ctrl)
854 struct hdpvr_fh *fh = file->private_data;
855 struct hdpvr_device *dev = fh->dev;
859 case V4L2_CID_BRIGHTNESS:
860 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
862 dev->options.brightness = ctrl->value;
864 case V4L2_CID_CONTRAST:
865 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
867 dev->options.contrast = ctrl->value;
869 case V4L2_CID_SATURATION:
870 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
872 dev->options.saturation = ctrl->value;
875 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
877 dev->options.hue = ctrl->value;
879 case V4L2_CID_SHARPNESS:
880 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
882 dev->options.sharpness = ctrl->value;
892 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
893 struct v4l2_ext_control *ctrl)
896 case V4L2_CID_MPEG_AUDIO_ENCODING:
897 ctrl->value = opt->audio_codec;
899 case V4L2_CID_MPEG_VIDEO_ENCODING:
900 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
902 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
903 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
905 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
906 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
907 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
908 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
910 case V4L2_CID_MPEG_VIDEO_BITRATE:
911 ctrl->value = opt->bitrate * 100000;
913 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
914 ctrl->value = opt->peak_bitrate * 100000;
916 case V4L2_CID_MPEG_STREAM_TYPE:
917 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
925 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
926 struct v4l2_ext_controls *ctrls)
928 struct hdpvr_fh *fh = file->private_data;
929 struct hdpvr_device *dev = fh->dev;
932 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
933 for (i = 0; i < ctrls->count; i++) {
934 struct v4l2_ext_control *ctrl = ctrls->controls + i;
936 err = hdpvr_get_ctrl(&dev->options, ctrl);
938 ctrls->error_idx = i;
950 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
955 case V4L2_CID_MPEG_AUDIO_ENCODING:
956 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
957 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
960 case V4L2_CID_MPEG_VIDEO_ENCODING:
961 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
964 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
965 /* if (ctrl->value == 0 || ctrl->value == 128) */
968 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
969 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
970 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
973 case V4L2_CID_MPEG_VIDEO_BITRATE:
975 uint bitrate = ctrl->value / 100000;
976 if (bitrate >= 10 && bitrate <= 135)
980 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
982 uint peak_bitrate = ctrl->value / 100000;
983 if (peak_bitrate >= 10 && peak_bitrate <= 202)
987 case V4L2_CID_MPEG_STREAM_TYPE:
988 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
997 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
998 struct v4l2_ext_controls *ctrls)
1000 struct hdpvr_fh *fh = file->private_data;
1001 struct hdpvr_device *dev = fh->dev;
1004 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1005 for (i = 0; i < ctrls->count; i++) {
1006 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1008 err = hdpvr_try_ctrl(ctrl,
1009 dev->flags & HDPVR_FLAG_AC3_CAP);
1011 ctrls->error_idx = i;
1022 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1023 struct v4l2_ext_control *ctrl)
1025 struct hdpvr_options *opt = &dev->options;
1029 case V4L2_CID_MPEG_AUDIO_ENCODING:
1030 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1031 opt->audio_codec = ctrl->value;
1032 ret = hdpvr_set_audio(dev, opt->audio_input,
1036 case V4L2_CID_MPEG_VIDEO_ENCODING:
1038 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1039 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1040 /* opt->gop_mode |= 0x2; */
1041 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1042 /* opt->gop_mode); */
1044 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1045 /* opt->gop_mode &= ~0x2; */
1046 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1047 /* opt->gop_mode); */
1050 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1051 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1052 opt->bitrate_mode != HDPVR_CONSTANT) {
1053 opt->bitrate_mode = HDPVR_CONSTANT;
1054 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1057 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1058 opt->bitrate_mode == HDPVR_CONSTANT) {
1059 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1060 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1064 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1065 uint bitrate = ctrl->value / 100000;
1067 opt->bitrate = bitrate;
1068 if (bitrate >= opt->peak_bitrate)
1069 opt->peak_bitrate = bitrate+1;
1071 hdpvr_set_bitrate(dev);
1074 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1075 uint peak_bitrate = ctrl->value / 100000;
1077 if (opt->bitrate_mode == HDPVR_CONSTANT)
1080 if (opt->bitrate < peak_bitrate) {
1081 opt->peak_bitrate = peak_bitrate;
1082 hdpvr_set_bitrate(dev);
1087 case V4L2_CID_MPEG_STREAM_TYPE:
1095 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1096 struct v4l2_ext_controls *ctrls)
1098 struct hdpvr_fh *fh = file->private_data;
1099 struct hdpvr_device *dev = fh->dev;
1102 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1103 for (i = 0; i < ctrls->count; i++) {
1104 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1106 err = hdpvr_try_ctrl(ctrl,
1107 dev->flags & HDPVR_FLAG_AC3_CAP);
1109 ctrls->error_idx = i;
1112 err = hdpvr_set_ctrl(dev, ctrl);
1114 ctrls->error_idx = i;
1125 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1126 struct v4l2_fmtdesc *f)
1129 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1132 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1133 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1134 f->pixelformat = V4L2_PIX_FMT_MPEG;
1139 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1140 struct v4l2_format *f)
1142 struct hdpvr_fh *fh = file->private_data;
1143 struct hdpvr_device *dev = fh->dev;
1144 struct hdpvr_video_info *vid_info;
1149 vid_info = get_video_info(dev);
1153 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1154 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1155 f->fmt.pix.width = vid_info->width;
1156 f->fmt.pix.height = vid_info->height;
1157 f->fmt.pix.sizeimage = dev->bulk_in_size;
1158 f->fmt.pix.colorspace = 0;
1159 f->fmt.pix.bytesperline = 0;
1160 f->fmt.pix.field = V4L2_FIELD_ANY;
1167 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1168 .vidioc_querycap = vidioc_querycap,
1169 .vidioc_s_std = vidioc_s_std,
1170 .vidioc_enum_input = vidioc_enum_input,
1171 .vidioc_g_input = vidioc_g_input,
1172 .vidioc_s_input = vidioc_s_input,
1173 .vidioc_enumaudio = vidioc_enumaudio,
1174 .vidioc_g_audio = vidioc_g_audio,
1175 .vidioc_s_audio = vidioc_s_audio,
1176 .vidioc_queryctrl = vidioc_queryctrl,
1177 .vidioc_g_ctrl = vidioc_g_ctrl,
1178 .vidioc_s_ctrl = vidioc_s_ctrl,
1179 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1180 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1181 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1182 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1183 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1186 static void hdpvr_device_release(struct video_device *vdev)
1188 struct hdpvr_device *dev = video_get_drvdata(vdev);
1193 static const struct video_device hdpvr_video_template = {
1194 /* .type = VFL_TYPE_GRABBER, */
1195 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1196 .fops = &hdpvr_fops,
1197 .release = hdpvr_device_release,
1198 .ioctl_ops = &hdpvr_ioctl_ops,
1200 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1201 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1202 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1206 int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1208 /* setup and register video device */
1209 dev->video_dev = video_device_alloc();
1210 if (!dev->video_dev) {
1211 err("video_device_alloc() failed");
1215 *(dev->video_dev) = hdpvr_video_template;
1216 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1217 dev->video_dev->parent = &dev->udev->dev;
1218 video_set_drvdata(dev->video_dev, dev);
1220 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1221 err("V4L2 device registration failed");