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 */
31 #define print_buffer_status() v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,\
32 "%s:%d buffer stat: %d free, %d proc\n",\
34 list_size(&dev->free_buff_list), \
35 list_size(&dev->rec_buff_list))
38 struct hdpvr_device *dev;
41 static uint list_size(struct list_head *list)
43 struct list_head *tmp;
46 list_for_each(tmp, list) {
53 /*=========================================================================*/
55 static void hdpvr_read_bulk_callback(struct urb *urb)
57 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
58 struct hdpvr_device *dev = buf->dev;
60 /* marking buffer as received and wake waiting */
61 buf->status = BUFSTAT_READY;
62 wake_up_interruptible(&dev->wait_data);
65 /*=========================================================================*/
68 /* function expects dev->io_mutex to be hold by caller */
69 int hdpvr_cancel_queue(struct hdpvr_device *dev)
71 struct hdpvr_buffer *buf;
73 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
74 usb_kill_urb(buf->urb);
75 buf->status = BUFSTAT_AVAILABLE;
78 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
83 static int hdpvr_free_queue(struct list_head *q)
85 struct list_head *tmp;
87 struct hdpvr_buffer *buf;
90 for (p = q->next; p != q;) {
91 buf = list_entry(p, struct hdpvr_buffer, buff_list);
94 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
95 urb->transfer_buffer, urb->transfer_dma);
106 /* function expects dev->io_mutex to be hold by caller */
107 int hdpvr_free_buffers(struct hdpvr_device *dev)
109 hdpvr_cancel_queue(dev);
111 hdpvr_free_queue(&dev->free_buff_list);
112 hdpvr_free_queue(&dev->rec_buff_list);
117 /* function expects dev->io_mutex to be hold by caller */
118 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
121 int retval = -ENOMEM;
123 struct hdpvr_buffer *buf;
126 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
127 "allocating %u buffers\n", count);
129 for (i = 0; i < count; i++) {
131 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
133 err("cannot allocate buffer");
138 urb = usb_alloc_urb(0, GFP_KERNEL);
140 err("cannot allocate urb");
145 mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
148 err("cannot allocate usb transfer buffer");
152 usb_fill_bulk_urb(buf->urb, dev->udev,
153 usb_rcvbulkpipe(dev->udev,
154 dev->bulk_in_endpointAddr),
155 mem, dev->bulk_in_size,
156 hdpvr_read_bulk_callback, buf);
158 buf->status = BUFSTAT_AVAILABLE;
159 list_add_tail(&buf->buff_list, &dev->free_buff_list);
163 hdpvr_free_buffers(dev);
167 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
169 struct hdpvr_buffer *buf;
171 int ret = 0, err_count = 0;
173 mutex_lock(&dev->io_mutex);
175 while (dev->status == STATUS_STREAMING &&
176 !list_empty(&dev->free_buff_list)) {
178 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
180 if (buf->status != BUFSTAT_AVAILABLE) {
181 err("buffer not marked as availbale");
188 urb->actual_length = 0;
189 ret = usb_submit_urb(urb, GFP_KERNEL);
191 err("usb_submit_urb in %s returned %d", __func__, ret);
196 buf->status = BUFSTAT_INPROGRESS;
197 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
200 print_buffer_status();
201 mutex_unlock(&dev->io_mutex);
205 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
207 struct hdpvr_buffer *buf;
209 mutex_lock(&dev->io_mutex);
211 if (list_empty(&dev->rec_buff_list)) {
212 mutex_unlock(&dev->io_mutex);
216 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
218 mutex_unlock(&dev->io_mutex);
223 static void hdpvr_transmit_buffers(struct work_struct *work)
225 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
228 while (dev->status == STATUS_STREAMING) {
230 if (hdpvr_submit_buffers(dev)) {
231 v4l2_err(dev->video_dev, "couldn't submit buffers\n");
234 if (wait_event_interruptible(dev->wait_buffer,
235 !list_empty(&dev->free_buff_list) ||
236 dev->status != STATUS_STREAMING))
240 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
241 "transmit worker exited\n");
244 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
245 "transmit buffers errored\n");
246 dev->status = STATUS_ERROR;
249 /* function expects dev->io_mutex to be hold by caller */
250 static int hdpvr_start_streaming(struct hdpvr_device *dev)
253 struct hdpvr_video_info *vidinf;
255 if (dev->status == STATUS_STREAMING)
257 else if (dev->status != STATUS_IDLE)
260 vidinf = get_video_info(dev);
263 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
264 "video signal: %dx%d@%dhz\n", vidinf->width,
265 vidinf->height, vidinf->fps);
268 /* start streaming 2 request */
269 ret = usb_control_msg(dev->udev,
270 usb_sndctrlpipe(dev->udev, 0),
271 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
272 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
273 "encoder start control request returned %d\n", ret);
275 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
277 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
278 queue_work(dev->workqueue, &dev->worker);
280 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
281 "streaming started\n");
282 dev->status = STATUS_STREAMING;
287 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
288 "no video signal at input %d\n", dev->options.video_input);
293 /* function expects dev->io_mutex to be hold by caller */
294 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
296 if (dev->status == STATUS_IDLE)
298 else if (dev->status != STATUS_STREAMING)
301 dev->status = STATUS_SHUTTING_DOWN;
302 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
304 wake_up_interruptible(&dev->wait_buffer);
307 flush_workqueue(dev->workqueue);
309 /* kill the still outstanding urbs */
310 hdpvr_cancel_queue(dev);
312 dev->status = STATUS_IDLE;
318 /*=======================================================================*/
320 * video 4 linux 2 file operations
323 static int hdpvr_open(struct file *file)
325 struct hdpvr_device *dev;
327 int retval = -ENOMEM;
329 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
331 err("open failing with with ENODEV");
336 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
338 err("Out of memory?");
341 /* lock the device to allow correctly handling errors
343 mutex_lock(&dev->io_mutex);
348 /* save our object in the file's private structure */
349 file->private_data = fh;
353 mutex_unlock(&dev->io_mutex);
357 static int hdpvr_release(struct file *file)
359 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
360 struct hdpvr_device *dev = fh->dev;
365 mutex_lock(&dev->io_mutex);
366 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
367 hdpvr_stop_streaming(dev);
369 mutex_unlock(&dev->io_mutex);
376 * will allocate buffers when called for the first time
378 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
381 struct hdpvr_fh *fh = file->private_data;
382 struct hdpvr_device *dev = fh->dev;
383 struct hdpvr_buffer *buf = NULL;
385 unsigned int ret = 0;
394 mutex_lock(&dev->io_mutex);
395 if (dev->status == STATUS_IDLE) {
396 if (hdpvr_start_streaming(dev)) {
397 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
398 "start_streaming failed");
401 dev->status = STATUS_IDLE;
402 mutex_unlock(&dev->io_mutex);
405 print_buffer_status();
407 mutex_unlock(&dev->io_mutex);
409 /* wait for the first buffer */
410 if (!(file->f_flags & O_NONBLOCK)) {
411 if (wait_event_interruptible(dev->wait_data,
412 hdpvr_get_next_buffer(dev)))
416 buf = hdpvr_get_next_buffer(dev);
418 while (count > 0 && buf) {
420 if (buf->status != BUFSTAT_READY &&
421 dev->status != STATUS_DISCONNECTED) {
422 /* return nonblocking */
423 if (file->f_flags & O_NONBLOCK) {
429 if (wait_event_interruptible(dev->wait_data,
430 buf->status == BUFSTAT_READY)) {
436 if (buf->status != BUFSTAT_READY)
439 /* set remaining bytes to copy */
441 rem = urb->actual_length - buf->pos;
442 cnt = rem > count ? count : rem;
444 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
446 err("read: copy_to_user failed");
457 /* finished, take next buffer */
458 if (buf->pos == urb->actual_length) {
459 mutex_lock(&dev->io_mutex);
461 buf->status = BUFSTAT_AVAILABLE;
463 list_move_tail(&buf->buff_list, &dev->free_buff_list);
465 print_buffer_status();
467 mutex_unlock(&dev->io_mutex);
469 wake_up_interruptible(&dev->wait_buffer);
471 buf = hdpvr_get_next_buffer(dev);
480 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
482 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
483 struct hdpvr_device *dev = fh->dev;
484 unsigned int mask = 0;
486 mutex_lock(&dev->io_mutex);
488 if (video_is_unregistered(dev->video_dev))
491 if (dev->status == STATUS_IDLE) {
492 if (hdpvr_start_streaming(dev)) {
493 v4l2_dbg(MSG_BUFFER, hdpvr_debug, dev->video_dev,
494 "start_streaming failed");
495 dev->status = STATUS_IDLE;
498 print_buffer_status();
500 mutex_unlock(&dev->io_mutex);
502 poll_wait(filp, &dev->wait_data, wait);
504 mutex_lock(&dev->io_mutex);
505 if (!list_empty(&dev->rec_buff_list)) {
507 struct hdpvr_buffer *buf = list_entry(dev->rec_buff_list.next,
511 if (buf->status == BUFSTAT_READY)
512 mask |= POLLIN | POLLRDNORM;
514 mutex_unlock(&dev->io_mutex);
520 static const struct v4l2_file_operations hdpvr_fops = {
521 .owner = THIS_MODULE,
523 .release = hdpvr_release,
526 .unlocked_ioctl = video_ioctl2,
529 /*=======================================================================*/
531 * V4L2 ioctl handling
534 static int vidioc_querycap(struct file *file, void *priv,
535 struct v4l2_capability *cap)
537 struct hdpvr_device *dev = video_drvdata(file);
539 strcpy(cap->driver, "hdpvr");
540 strcpy(cap->card, "Haupauge HD PVR");
541 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
542 cap->version = HDPVR_VERSION;
543 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
549 static int vidioc_s_std(struct file *file, void *private_data,
552 struct hdpvr_fh *fh = file->private_data;
553 struct hdpvr_device *dev = fh->dev;
556 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
559 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
562 static const char *iname[] = {
563 [HDPVR_COMPONENT] = "Component",
564 [HDPVR_SVIDEO] = "S-Video",
565 [HDPVR_COMPOSITE] = "Composite",
568 static int vidioc_enum_input(struct file *file, void *priv,
569 struct v4l2_input *i)
571 struct hdpvr_fh *fh = file->private_data;
572 struct hdpvr_device *dev = fh->dev;
576 if (n >= HDPVR_VIDEO_INPUTS)
579 i->type = V4L2_INPUT_TYPE_CAMERA;
581 strncpy(i->name, iname[n], sizeof(i->name) - 1);
582 i->name[sizeof(i->name) - 1] = '\0';
584 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
586 i->std = dev->video_dev->tvnorms;
591 static int vidioc_s_input(struct file *file, void *private_data,
594 struct hdpvr_fh *fh = file->private_data;
595 struct hdpvr_device *dev = fh->dev;
598 if (index >= HDPVR_VIDEO_INPUTS)
601 if (dev->status != STATUS_IDLE)
604 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
606 dev->options.video_input = index;
611 static int vidioc_g_input(struct file *file, void *private_data,
614 struct hdpvr_fh *fh = file->private_data;
615 struct hdpvr_device *dev = fh->dev;
617 *index = dev->options.video_input;
622 static const char *audio_iname[] = {
623 [HDPVR_RCA_FRONT] = "RCA front",
624 [HDPVR_RCA_BACK] = "RCA back",
625 [HDPVR_SPDIF] = "SPDIF",
628 static int vidioc_enumaudio(struct file *file, void *priv,
629 struct v4l2_audio *audio)
634 if (n >= HDPVR_AUDIO_INPUTS)
637 audio->capability = V4L2_AUDCAP_STEREO;
639 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
640 audio->name[sizeof(audio->name) - 1] = '\0';
645 static int vidioc_s_audio(struct file *file, void *private_data,
646 struct v4l2_audio *audio)
648 struct hdpvr_fh *fh = file->private_data;
649 struct hdpvr_device *dev = fh->dev;
652 if (audio->index >= HDPVR_AUDIO_INPUTS)
655 if (dev->status != STATUS_IDLE)
658 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
660 dev->options.audio_input = audio->index;
665 static int vidioc_g_audio(struct file *file, void *private_data,
666 struct v4l2_audio *audio)
668 struct hdpvr_fh *fh = file->private_data;
669 struct hdpvr_device *dev = fh->dev;
671 audio->index = dev->options.audio_input;
672 audio->capability = V4L2_AUDCAP_STEREO;
673 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
674 audio->name[sizeof(audio->name) - 1] = '\0';
678 static const s32 supported_v4l2_ctrls[] = {
684 V4L2_CID_MPEG_AUDIO_ENCODING,
685 V4L2_CID_MPEG_VIDEO_ENCODING,
686 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
687 V4L2_CID_MPEG_VIDEO_BITRATE,
688 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
691 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
697 case V4L2_CID_BRIGHTNESS:
698 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
699 case V4L2_CID_CONTRAST:
700 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
701 case V4L2_CID_SATURATION:
702 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
704 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
705 case V4L2_CID_SHARPNESS:
706 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
707 case V4L2_CID_MPEG_AUDIO_ENCODING:
708 return v4l2_ctrl_query_fill(
709 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
710 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
711 : V4L2_MPEG_AUDIO_ENCODING_AAC,
712 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
713 case V4L2_CID_MPEG_VIDEO_ENCODING:
714 return v4l2_ctrl_query_fill(
715 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
716 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
717 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
719 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
720 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
721 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
722 return v4l2_ctrl_query_fill(
723 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
724 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
725 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
727 case V4L2_CID_MPEG_VIDEO_BITRATE:
728 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
730 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
731 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
733 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
734 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
741 static int vidioc_queryctrl(struct file *file, void *private_data,
742 struct v4l2_queryctrl *qc)
744 struct hdpvr_fh *fh = file->private_data;
745 struct hdpvr_device *dev = fh->dev;
749 memset(qc, 0, sizeof(*qc));
751 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
752 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
754 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
756 if (qc->id < supported_v4l2_ctrls[i])
757 qc->id = supported_v4l2_ctrls[i];
762 if (qc->id == supported_v4l2_ctrls[i])
763 return fill_queryctrl(&dev->options, qc,
764 dev->flags & HDPVR_FLAG_AC3_CAP);
766 if (qc->id < supported_v4l2_ctrls[i])
773 static int vidioc_g_ctrl(struct file *file, void *private_data,
774 struct v4l2_control *ctrl)
776 struct hdpvr_fh *fh = file->private_data;
777 struct hdpvr_device *dev = fh->dev;
780 case V4L2_CID_BRIGHTNESS:
781 ctrl->value = dev->options.brightness;
783 case V4L2_CID_CONTRAST:
784 ctrl->value = dev->options.contrast;
786 case V4L2_CID_SATURATION:
787 ctrl->value = dev->options.saturation;
790 ctrl->value = dev->options.hue;
792 case V4L2_CID_SHARPNESS:
793 ctrl->value = dev->options.sharpness;
801 static int vidioc_s_ctrl(struct file *file, void *private_data,
802 struct v4l2_control *ctrl)
804 struct hdpvr_fh *fh = file->private_data;
805 struct hdpvr_device *dev = fh->dev;
809 case V4L2_CID_BRIGHTNESS:
810 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
812 dev->options.brightness = ctrl->value;
814 case V4L2_CID_CONTRAST:
815 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
817 dev->options.contrast = ctrl->value;
819 case V4L2_CID_SATURATION:
820 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
822 dev->options.saturation = ctrl->value;
825 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
827 dev->options.hue = ctrl->value;
829 case V4L2_CID_SHARPNESS:
830 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
832 dev->options.sharpness = ctrl->value;
842 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
843 struct v4l2_ext_control *ctrl)
846 case V4L2_CID_MPEG_AUDIO_ENCODING:
847 ctrl->value = opt->audio_codec;
849 case V4L2_CID_MPEG_VIDEO_ENCODING:
850 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
852 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
853 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
855 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
856 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
857 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
858 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
860 case V4L2_CID_MPEG_VIDEO_BITRATE:
861 ctrl->value = opt->bitrate * 100000;
863 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
864 ctrl->value = opt->peak_bitrate * 100000;
866 case V4L2_CID_MPEG_STREAM_TYPE:
867 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
875 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
876 struct v4l2_ext_controls *ctrls)
878 struct hdpvr_fh *fh = file->private_data;
879 struct hdpvr_device *dev = fh->dev;
882 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
883 for (i = 0; i < ctrls->count; i++) {
884 struct v4l2_ext_control *ctrl = ctrls->controls + i;
886 err = hdpvr_get_ctrl(&dev->options, ctrl);
888 ctrls->error_idx = i;
900 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
905 case V4L2_CID_MPEG_AUDIO_ENCODING:
906 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
907 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
910 case V4L2_CID_MPEG_VIDEO_ENCODING:
911 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
914 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
915 /* if (ctrl->value == 0 || ctrl->value == 128) */
918 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
919 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
920 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
923 case V4L2_CID_MPEG_VIDEO_BITRATE:
925 uint bitrate = ctrl->value / 100000;
926 if (bitrate >= 10 && bitrate <= 135)
930 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
932 uint peak_bitrate = ctrl->value / 100000;
933 if (peak_bitrate >= 10 && peak_bitrate <= 202)
937 case V4L2_CID_MPEG_STREAM_TYPE:
938 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
947 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
948 struct v4l2_ext_controls *ctrls)
950 struct hdpvr_fh *fh = file->private_data;
951 struct hdpvr_device *dev = fh->dev;
954 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
955 for (i = 0; i < ctrls->count; i++) {
956 struct v4l2_ext_control *ctrl = ctrls->controls + i;
958 err = hdpvr_try_ctrl(ctrl,
959 dev->flags & HDPVR_FLAG_AC3_CAP);
961 ctrls->error_idx = i;
972 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
973 struct v4l2_ext_control *ctrl)
975 struct hdpvr_options *opt = &dev->options;
979 case V4L2_CID_MPEG_AUDIO_ENCODING:
980 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
981 opt->audio_codec = ctrl->value;
982 ret = hdpvr_set_audio(dev, opt->audio_input,
986 case V4L2_CID_MPEG_VIDEO_ENCODING:
988 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
989 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
990 /* opt->gop_mode |= 0x2; */
991 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
992 /* opt->gop_mode); */
994 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
995 /* opt->gop_mode &= ~0x2; */
996 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
997 /* opt->gop_mode); */
1000 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1001 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1002 opt->bitrate_mode != HDPVR_CONSTANT) {
1003 opt->bitrate_mode = HDPVR_CONSTANT;
1004 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1007 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1008 opt->bitrate_mode == HDPVR_CONSTANT) {
1009 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1010 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1014 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1015 uint bitrate = ctrl->value / 100000;
1017 opt->bitrate = bitrate;
1018 if (bitrate >= opt->peak_bitrate)
1019 opt->peak_bitrate = bitrate+1;
1021 hdpvr_set_bitrate(dev);
1024 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1025 uint peak_bitrate = ctrl->value / 100000;
1027 if (opt->bitrate_mode == HDPVR_CONSTANT)
1030 if (opt->bitrate < peak_bitrate) {
1031 opt->peak_bitrate = peak_bitrate;
1032 hdpvr_set_bitrate(dev);
1037 case V4L2_CID_MPEG_STREAM_TYPE:
1045 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1046 struct v4l2_ext_controls *ctrls)
1048 struct hdpvr_fh *fh = file->private_data;
1049 struct hdpvr_device *dev = fh->dev;
1052 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1053 for (i = 0; i < ctrls->count; i++) {
1054 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1056 err = hdpvr_try_ctrl(ctrl,
1057 dev->flags & HDPVR_FLAG_AC3_CAP);
1059 ctrls->error_idx = i;
1062 err = hdpvr_set_ctrl(dev, ctrl);
1064 ctrls->error_idx = i;
1075 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1076 struct v4l2_fmtdesc *f)
1079 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1082 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1083 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1084 f->pixelformat = V4L2_PIX_FMT_MPEG;
1089 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1090 struct v4l2_format *f)
1092 struct hdpvr_fh *fh = file->private_data;
1093 struct hdpvr_device *dev = fh->dev;
1094 struct hdpvr_video_info *vid_info;
1099 vid_info = get_video_info(dev);
1103 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1104 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1105 f->fmt.pix.width = vid_info->width;
1106 f->fmt.pix.height = vid_info->height;
1107 f->fmt.pix.sizeimage = dev->bulk_in_size;
1108 f->fmt.pix.colorspace = 0;
1109 f->fmt.pix.bytesperline = 0;
1110 f->fmt.pix.field = V4L2_FIELD_ANY;
1116 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1117 struct v4l2_encoder_cmd *a)
1119 struct hdpvr_fh *fh = filp->private_data;
1120 struct hdpvr_device *dev = fh->dev;
1123 mutex_lock(&dev->io_mutex);
1125 memset(&a->raw, 0, sizeof(a->raw));
1127 case V4L2_ENC_CMD_START:
1129 res = hdpvr_start_streaming(dev);
1131 case V4L2_ENC_CMD_STOP:
1132 res = hdpvr_stop_streaming(dev);
1135 v4l2_dbg(MSG_INFO, hdpvr_debug, dev->video_dev,
1136 "Unsupported encoder cmd %d\n", a->cmd);
1139 mutex_unlock(&dev->io_mutex);
1143 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1144 struct v4l2_encoder_cmd *a)
1147 case V4L2_ENC_CMD_START:
1148 case V4L2_ENC_CMD_STOP:
1155 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1156 .vidioc_querycap = vidioc_querycap,
1157 .vidioc_s_std = vidioc_s_std,
1158 .vidioc_enum_input = vidioc_enum_input,
1159 .vidioc_g_input = vidioc_g_input,
1160 .vidioc_s_input = vidioc_s_input,
1161 .vidioc_enumaudio = vidioc_enumaudio,
1162 .vidioc_g_audio = vidioc_g_audio,
1163 .vidioc_s_audio = vidioc_s_audio,
1164 .vidioc_queryctrl = vidioc_queryctrl,
1165 .vidioc_g_ctrl = vidioc_g_ctrl,
1166 .vidioc_s_ctrl = vidioc_s_ctrl,
1167 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1168 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1169 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1170 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1171 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1172 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1173 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1176 static void hdpvr_device_release(struct video_device *vdev)
1178 struct hdpvr_device *dev = video_get_drvdata(vdev);
1183 static const struct video_device hdpvr_video_template = {
1184 /* .type = VFL_TYPE_GRABBER, */
1185 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1186 .fops = &hdpvr_fops,
1187 .release = hdpvr_device_release,
1188 .ioctl_ops = &hdpvr_ioctl_ops,
1190 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1191 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1192 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1196 int hdpvr_register_videodev(struct hdpvr_device *dev, int devnum)
1198 /* setup and register video device */
1199 dev->video_dev = video_device_alloc();
1200 if (!dev->video_dev) {
1201 err("video_device_alloc() failed");
1205 *(dev->video_dev) = hdpvr_video_template;
1206 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1207 dev->video_dev->parent = &dev->udev->dev;
1208 video_set_drvdata(dev->video_dev, dev);
1210 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1211 err("V4L2 device registration failed");