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/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 const struct v4l2_file_operations hdpvr_fops = {
528 .owner = THIS_MODULE,
530 .release = hdpvr_release,
533 .unlocked_ioctl = video_ioctl2,
536 /*=======================================================================*/
538 * V4L2 ioctl handling
541 static int vidioc_querycap(struct file *file, void *priv,
542 struct v4l2_capability *cap)
544 struct hdpvr_device *dev = video_drvdata(file);
546 strcpy(cap->driver, "hdpvr");
547 strcpy(cap->card, "Haupauge HD PVR");
548 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
549 cap->version = HDPVR_VERSION;
550 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
556 static int vidioc_s_std(struct file *file, void *private_data,
559 struct hdpvr_fh *fh = file->private_data;
560 struct hdpvr_device *dev = fh->dev;
563 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
566 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
569 static const char *iname[] = {
570 [HDPVR_COMPONENT] = "Component",
571 [HDPVR_SVIDEO] = "S-Video",
572 [HDPVR_COMPOSITE] = "Composite",
575 static int vidioc_enum_input(struct file *file, void *priv,
576 struct v4l2_input *i)
578 struct hdpvr_fh *fh = file->private_data;
579 struct hdpvr_device *dev = fh->dev;
583 if (n >= HDPVR_VIDEO_INPUTS)
586 i->type = V4L2_INPUT_TYPE_CAMERA;
588 strncpy(i->name, iname[n], sizeof(i->name) - 1);
589 i->name[sizeof(i->name) - 1] = '\0';
591 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
593 i->std = dev->video_dev->tvnorms;
598 static int vidioc_s_input(struct file *file, void *private_data,
601 struct hdpvr_fh *fh = file->private_data;
602 struct hdpvr_device *dev = fh->dev;
605 if (index >= HDPVR_VIDEO_INPUTS)
608 if (dev->status != STATUS_IDLE)
611 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
613 dev->options.video_input = index;
618 static int vidioc_g_input(struct file *file, void *private_data,
621 struct hdpvr_fh *fh = file->private_data;
622 struct hdpvr_device *dev = fh->dev;
624 *index = dev->options.video_input;
629 static const char *audio_iname[] = {
630 [HDPVR_RCA_FRONT] = "RCA front",
631 [HDPVR_RCA_BACK] = "RCA back",
632 [HDPVR_SPDIF] = "SPDIF",
635 static int vidioc_enumaudio(struct file *file, void *priv,
636 struct v4l2_audio *audio)
641 if (n >= HDPVR_AUDIO_INPUTS)
644 audio->capability = V4L2_AUDCAP_STEREO;
646 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
647 audio->name[sizeof(audio->name) - 1] = '\0';
652 static int vidioc_s_audio(struct file *file, void *private_data,
653 struct v4l2_audio *audio)
655 struct hdpvr_fh *fh = file->private_data;
656 struct hdpvr_device *dev = fh->dev;
659 if (audio->index >= HDPVR_AUDIO_INPUTS)
662 if (dev->status != STATUS_IDLE)
665 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
667 dev->options.audio_input = audio->index;
672 static int vidioc_g_audio(struct file *file, void *private_data,
673 struct v4l2_audio *audio)
675 struct hdpvr_fh *fh = file->private_data;
676 struct hdpvr_device *dev = fh->dev;
678 audio->index = dev->options.audio_input;
679 audio->capability = V4L2_AUDCAP_STEREO;
680 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
681 audio->name[sizeof(audio->name) - 1] = '\0';
685 static const s32 supported_v4l2_ctrls[] = {
691 V4L2_CID_MPEG_AUDIO_ENCODING,
692 V4L2_CID_MPEG_VIDEO_ENCODING,
693 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
694 V4L2_CID_MPEG_VIDEO_BITRATE,
695 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
698 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
704 case V4L2_CID_BRIGHTNESS:
705 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
706 case V4L2_CID_CONTRAST:
707 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
708 case V4L2_CID_SATURATION:
709 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
711 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
712 case V4L2_CID_SHARPNESS:
713 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
714 case V4L2_CID_MPEG_AUDIO_ENCODING:
715 return v4l2_ctrl_query_fill(
716 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
717 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
718 : V4L2_MPEG_AUDIO_ENCODING_AAC,
719 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
720 case V4L2_CID_MPEG_VIDEO_ENCODING:
721 return v4l2_ctrl_query_fill(
722 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
723 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
724 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
726 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
727 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
728 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
729 return v4l2_ctrl_query_fill(
730 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
731 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
732 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
734 case V4L2_CID_MPEG_VIDEO_BITRATE:
735 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
737 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
738 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
740 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
741 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
748 static int vidioc_queryctrl(struct file *file, void *private_data,
749 struct v4l2_queryctrl *qc)
751 struct hdpvr_fh *fh = file->private_data;
752 struct hdpvr_device *dev = fh->dev;
756 memset(qc, 0, sizeof(*qc));
758 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
759 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
761 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
763 if (qc->id < supported_v4l2_ctrls[i])
764 qc->id = supported_v4l2_ctrls[i];
769 if (qc->id == supported_v4l2_ctrls[i])
770 return fill_queryctrl(&dev->options, qc,
771 dev->flags & HDPVR_FLAG_AC3_CAP);
773 if (qc->id < supported_v4l2_ctrls[i])
780 static int vidioc_g_ctrl(struct file *file, void *private_data,
781 struct v4l2_control *ctrl)
783 struct hdpvr_fh *fh = file->private_data;
784 struct hdpvr_device *dev = fh->dev;
787 case V4L2_CID_BRIGHTNESS:
788 ctrl->value = dev->options.brightness;
790 case V4L2_CID_CONTRAST:
791 ctrl->value = dev->options.contrast;
793 case V4L2_CID_SATURATION:
794 ctrl->value = dev->options.saturation;
797 ctrl->value = dev->options.hue;
799 case V4L2_CID_SHARPNESS:
800 ctrl->value = dev->options.sharpness;
808 static int vidioc_s_ctrl(struct file *file, void *private_data,
809 struct v4l2_control *ctrl)
811 struct hdpvr_fh *fh = file->private_data;
812 struct hdpvr_device *dev = fh->dev;
816 case V4L2_CID_BRIGHTNESS:
817 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
819 dev->options.brightness = ctrl->value;
821 case V4L2_CID_CONTRAST:
822 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
824 dev->options.contrast = ctrl->value;
826 case V4L2_CID_SATURATION:
827 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
829 dev->options.saturation = ctrl->value;
832 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
834 dev->options.hue = ctrl->value;
836 case V4L2_CID_SHARPNESS:
837 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
839 dev->options.sharpness = ctrl->value;
849 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
850 struct v4l2_ext_control *ctrl)
853 case V4L2_CID_MPEG_AUDIO_ENCODING:
854 ctrl->value = opt->audio_codec;
856 case V4L2_CID_MPEG_VIDEO_ENCODING:
857 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
859 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
860 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
862 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
863 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
864 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
865 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
867 case V4L2_CID_MPEG_VIDEO_BITRATE:
868 ctrl->value = opt->bitrate * 100000;
870 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
871 ctrl->value = opt->peak_bitrate * 100000;
873 case V4L2_CID_MPEG_STREAM_TYPE:
874 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
882 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
883 struct v4l2_ext_controls *ctrls)
885 struct hdpvr_fh *fh = file->private_data;
886 struct hdpvr_device *dev = fh->dev;
889 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
890 for (i = 0; i < ctrls->count; i++) {
891 struct v4l2_ext_control *ctrl = ctrls->controls + i;
893 err = hdpvr_get_ctrl(&dev->options, ctrl);
895 ctrls->error_idx = i;
907 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
912 case V4L2_CID_MPEG_AUDIO_ENCODING:
913 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
914 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
917 case V4L2_CID_MPEG_VIDEO_ENCODING:
918 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
921 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
922 /* if (ctrl->value == 0 || ctrl->value == 128) */
925 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
926 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
927 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
930 case V4L2_CID_MPEG_VIDEO_BITRATE:
932 uint bitrate = ctrl->value / 100000;
933 if (bitrate >= 10 && bitrate <= 135)
937 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
939 uint peak_bitrate = ctrl->value / 100000;
940 if (peak_bitrate >= 10 && peak_bitrate <= 202)
944 case V4L2_CID_MPEG_STREAM_TYPE:
945 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
954 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
955 struct v4l2_ext_controls *ctrls)
957 struct hdpvr_fh *fh = file->private_data;
958 struct hdpvr_device *dev = fh->dev;
961 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
962 for (i = 0; i < ctrls->count; i++) {
963 struct v4l2_ext_control *ctrl = ctrls->controls + i;
965 err = hdpvr_try_ctrl(ctrl,
966 dev->flags & HDPVR_FLAG_AC3_CAP);
968 ctrls->error_idx = i;
979 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
980 struct v4l2_ext_control *ctrl)
982 struct hdpvr_options *opt = &dev->options;
986 case V4L2_CID_MPEG_AUDIO_ENCODING:
987 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
988 opt->audio_codec = ctrl->value;
989 ret = hdpvr_set_audio(dev, opt->audio_input,
993 case V4L2_CID_MPEG_VIDEO_ENCODING:
995 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
996 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
997 /* opt->gop_mode |= 0x2; */
998 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
999 /* opt->gop_mode); */
1001 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1002 /* opt->gop_mode &= ~0x2; */
1003 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1004 /* opt->gop_mode); */
1007 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1008 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1009 opt->bitrate_mode != HDPVR_CONSTANT) {
1010 opt->bitrate_mode = HDPVR_CONSTANT;
1011 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1014 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1015 opt->bitrate_mode == HDPVR_CONSTANT) {
1016 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1017 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1021 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1022 uint bitrate = ctrl->value / 100000;
1024 opt->bitrate = bitrate;
1025 if (bitrate >= opt->peak_bitrate)
1026 opt->peak_bitrate = bitrate+1;
1028 hdpvr_set_bitrate(dev);
1031 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1032 uint peak_bitrate = ctrl->value / 100000;
1034 if (opt->bitrate_mode == HDPVR_CONSTANT)
1037 if (opt->bitrate < peak_bitrate) {
1038 opt->peak_bitrate = peak_bitrate;
1039 hdpvr_set_bitrate(dev);
1044 case V4L2_CID_MPEG_STREAM_TYPE:
1052 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1053 struct v4l2_ext_controls *ctrls)
1055 struct hdpvr_fh *fh = file->private_data;
1056 struct hdpvr_device *dev = fh->dev;
1059 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1060 for (i = 0; i < ctrls->count; i++) {
1061 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1063 err = hdpvr_try_ctrl(ctrl,
1064 dev->flags & HDPVR_FLAG_AC3_CAP);
1066 ctrls->error_idx = i;
1069 err = hdpvr_set_ctrl(dev, ctrl);
1071 ctrls->error_idx = i;
1082 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1083 struct v4l2_fmtdesc *f)
1086 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1089 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1090 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1091 f->pixelformat = V4L2_PIX_FMT_MPEG;
1096 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1097 struct v4l2_format *f)
1099 struct hdpvr_fh *fh = file->private_data;
1100 struct hdpvr_device *dev = fh->dev;
1101 struct hdpvr_video_info *vid_info;
1106 vid_info = get_video_info(dev);
1110 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1111 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1112 f->fmt.pix.width = vid_info->width;
1113 f->fmt.pix.height = vid_info->height;
1114 f->fmt.pix.sizeimage = dev->bulk_in_size;
1115 f->fmt.pix.colorspace = 0;
1116 f->fmt.pix.bytesperline = 0;
1117 f->fmt.pix.field = V4L2_FIELD_ANY;
1123 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1124 struct v4l2_encoder_cmd *a)
1126 struct hdpvr_fh *fh = filp->private_data;
1127 struct hdpvr_device *dev = fh->dev;
1130 mutex_lock(&dev->io_mutex);
1132 memset(&a->raw, 0, sizeof(a->raw));
1134 case V4L2_ENC_CMD_START:
1136 res = hdpvr_start_streaming(dev);
1138 case V4L2_ENC_CMD_STOP:
1139 res = hdpvr_stop_streaming(dev);
1142 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
1143 "Unsupported encoder cmd %d\n", a->cmd);
1146 mutex_unlock(&dev->io_mutex);
1150 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1151 struct v4l2_encoder_cmd *a)
1154 case V4L2_ENC_CMD_START:
1155 case V4L2_ENC_CMD_STOP:
1162 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1163 .vidioc_querycap = vidioc_querycap,
1164 .vidioc_s_std = vidioc_s_std,
1165 .vidioc_enum_input = vidioc_enum_input,
1166 .vidioc_g_input = vidioc_g_input,
1167 .vidioc_s_input = vidioc_s_input,
1168 .vidioc_enumaudio = vidioc_enumaudio,
1169 .vidioc_g_audio = vidioc_g_audio,
1170 .vidioc_s_audio = vidioc_s_audio,
1171 .vidioc_queryctrl = vidioc_queryctrl,
1172 .vidioc_g_ctrl = vidioc_g_ctrl,
1173 .vidioc_s_ctrl = vidioc_s_ctrl,
1174 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1175 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1176 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1177 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1178 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1179 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1180 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1183 static void hdpvr_device_release(struct video_device *vdev)
1185 struct hdpvr_device *dev = video_get_drvdata(vdev);
1190 static const struct video_device hdpvr_video_template = {
1191 /* .type = VFL_TYPE_GRABBER, */
1192 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1193 .fops = &hdpvr_fops,
1194 .release = hdpvr_device_release,
1195 .ioctl_ops = &hdpvr_ioctl_ops,
1197 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1198 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1199 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1203 int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1205 /* setup and register video device */
1206 dev->video_dev = video_device_alloc();
1207 if (!dev->video_dev) {
1208 err("video_device_alloc() failed");
1212 *(dev->video_dev) = hdpvr_video_template;
1213 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1214 dev->video_dev->parent = &dev->udev->dev;
1215 video_set_drvdata(dev->video_dev, dev);
1217 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1218 err("V4L2 device registration failed");