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() { \
32 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
33 "%s:%d buffer stat: %d free, %d proc\n", \
35 list_size(&dev->free_buff_list), \
36 list_size(&dev->rec_buff_list)); }
39 struct hdpvr_device *dev;
42 static uint list_size(struct list_head *list)
44 struct list_head *tmp;
47 list_for_each(tmp, list) {
54 /*=========================================================================*/
56 static void hdpvr_read_bulk_callback(struct urb *urb)
58 struct hdpvr_buffer *buf = (struct hdpvr_buffer *)urb->context;
59 struct hdpvr_device *dev = buf->dev;
61 /* marking buffer as received and wake waiting */
62 buf->status = BUFSTAT_READY;
63 wake_up_interruptible(&dev->wait_data);
66 /*=========================================================================*/
69 /* function expects dev->io_mutex to be hold by caller */
70 int hdpvr_cancel_queue(struct hdpvr_device *dev)
72 struct hdpvr_buffer *buf;
74 list_for_each_entry(buf, &dev->rec_buff_list, buff_list) {
75 usb_kill_urb(buf->urb);
76 buf->status = BUFSTAT_AVAILABLE;
79 list_splice_init(&dev->rec_buff_list, dev->free_buff_list.prev);
84 static int hdpvr_free_queue(struct list_head *q)
86 struct list_head *tmp;
88 struct hdpvr_buffer *buf;
91 for (p = q->next; p != q;) {
92 buf = list_entry(p, struct hdpvr_buffer, buff_list);
95 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
96 urb->transfer_buffer, urb->transfer_dma);
107 /* function expects dev->io_mutex to be hold by caller */
108 int hdpvr_free_buffers(struct hdpvr_device *dev)
110 hdpvr_cancel_queue(dev);
112 hdpvr_free_queue(&dev->free_buff_list);
113 hdpvr_free_queue(&dev->rec_buff_list);
118 /* function expects dev->io_mutex to be hold by caller */
119 int hdpvr_alloc_buffers(struct hdpvr_device *dev, uint count)
122 int retval = -ENOMEM;
124 struct hdpvr_buffer *buf;
127 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
128 "allocating %u buffers\n", count);
130 for (i = 0; i < count; i++) {
132 buf = kzalloc(sizeof(struct hdpvr_buffer), GFP_KERNEL);
134 v4l2_err(&dev->v4l2_dev, "cannot allocate buffer\n");
139 urb = usb_alloc_urb(0, GFP_KERNEL);
141 v4l2_err(&dev->v4l2_dev, "cannot allocate urb\n");
146 mem = usb_buffer_alloc(dev->udev, dev->bulk_in_size, GFP_KERNEL,
149 v4l2_err(&dev->v4l2_dev,
150 "cannot allocate usb transfer buffer\n");
154 usb_fill_bulk_urb(buf->urb, dev->udev,
155 usb_rcvbulkpipe(dev->udev,
156 dev->bulk_in_endpointAddr),
157 mem, dev->bulk_in_size,
158 hdpvr_read_bulk_callback, buf);
160 buf->status = BUFSTAT_AVAILABLE;
161 list_add_tail(&buf->buff_list, &dev->free_buff_list);
165 hdpvr_free_buffers(dev);
169 static int hdpvr_submit_buffers(struct hdpvr_device *dev)
171 struct hdpvr_buffer *buf;
173 int ret = 0, err_count = 0;
175 mutex_lock(&dev->io_mutex);
177 while (dev->status == STATUS_STREAMING &&
178 !list_empty(&dev->free_buff_list)) {
180 buf = list_entry(dev->free_buff_list.next, struct hdpvr_buffer,
182 if (buf->status != BUFSTAT_AVAILABLE) {
183 v4l2_err(&dev->v4l2_dev,
184 "buffer not marked as availbale\n");
191 urb->actual_length = 0;
192 ret = usb_submit_urb(urb, GFP_KERNEL);
194 v4l2_err(&dev->v4l2_dev,
195 "usb_submit_urb in %s returned %d\n",
201 buf->status = BUFSTAT_INPROGRESS;
202 list_move_tail(&buf->buff_list, &dev->rec_buff_list);
205 print_buffer_status();
206 mutex_unlock(&dev->io_mutex);
210 static struct hdpvr_buffer *hdpvr_get_next_buffer(struct hdpvr_device *dev)
212 struct hdpvr_buffer *buf;
214 mutex_lock(&dev->io_mutex);
216 if (list_empty(&dev->rec_buff_list)) {
217 mutex_unlock(&dev->io_mutex);
221 buf = list_entry(dev->rec_buff_list.next, struct hdpvr_buffer,
223 mutex_unlock(&dev->io_mutex);
228 static void hdpvr_transmit_buffers(struct work_struct *work)
230 struct hdpvr_device *dev = container_of(work, struct hdpvr_device,
233 while (dev->status == STATUS_STREAMING) {
235 if (hdpvr_submit_buffers(dev)) {
236 v4l2_err(&dev->v4l2_dev, "couldn't submit buffers\n");
239 if (wait_event_interruptible(dev->wait_buffer,
240 !list_empty(&dev->free_buff_list) ||
241 dev->status != STATUS_STREAMING))
245 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
246 "transmit worker exited\n");
249 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
250 "transmit buffers errored\n");
251 dev->status = STATUS_ERROR;
254 /* function expects dev->io_mutex to be hold by caller */
255 static int hdpvr_start_streaming(struct hdpvr_device *dev)
258 struct hdpvr_video_info *vidinf;
260 if (dev->status == STATUS_STREAMING)
262 else if (dev->status != STATUS_IDLE)
265 vidinf = get_video_info(dev);
268 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
269 "video signal: %dx%d@%dhz\n", vidinf->width,
270 vidinf->height, vidinf->fps);
273 /* start streaming 2 request */
274 ret = usb_control_msg(dev->udev,
275 usb_sndctrlpipe(dev->udev, 0),
276 0xb8, 0x38, 0x1, 0, NULL, 0, 8000);
277 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
278 "encoder start control request returned %d\n", ret);
280 hdpvr_config_call(dev, CTRL_START_STREAMING_VALUE, 0x00);
282 INIT_WORK(&dev->worker, hdpvr_transmit_buffers);
283 queue_work(dev->workqueue, &dev->worker);
285 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
286 "streaming started\n");
287 dev->status = STATUS_STREAMING;
292 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
293 "no video signal at input %d\n", dev->options.video_input);
298 /* function expects dev->io_mutex to be hold by caller */
299 static int hdpvr_stop_streaming(struct hdpvr_device *dev)
301 uint actual_length, c = 0;
304 if (dev->status == STATUS_IDLE)
306 else if (dev->status != STATUS_STREAMING)
309 buf = kmalloc(dev->bulk_in_size, GFP_KERNEL);
311 v4l2_err(&dev->v4l2_dev, "failed to allocate temporary buffer "
312 "for emptying the internal device buffer. "
313 "Next capture start will be slow\n");
315 dev->status = STATUS_SHUTTING_DOWN;
316 hdpvr_config_call(dev, CTRL_STOP_STREAMING_VALUE, 0x00);
317 mutex_unlock(&dev->io_mutex);
319 wake_up_interruptible(&dev->wait_buffer);
322 flush_workqueue(dev->workqueue);
324 mutex_lock(&dev->io_mutex);
325 /* kill the still outstanding urbs */
326 hdpvr_cancel_queue(dev);
328 /* emptying the device buffer beforeshutting it down */
329 while (buf && ++c < 500 &&
330 !usb_bulk_msg(dev->udev,
331 usb_rcvbulkpipe(dev->udev,
332 dev->bulk_in_endpointAddr),
333 buf, dev->bulk_in_size, &actual_length,
337 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
338 "%2d: got %d bytes\n", c, actual_length);
341 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
342 "used %d urbs to empty device buffers\n", c-1);
345 dev->status = STATUS_IDLE;
351 /*=======================================================================*/
353 * video 4 linux 2 file operations
356 static int hdpvr_open(struct file *file)
358 struct hdpvr_device *dev;
360 int retval = -ENOMEM;
362 dev = (struct hdpvr_device *)video_get_drvdata(video_devdata(file));
364 v4l2_err(&dev->v4l2_dev, "open failing with with ENODEV\n");
369 fh = kzalloc(sizeof(struct hdpvr_fh), GFP_KERNEL);
371 v4l2_err(&dev->v4l2_dev, "Out of memory\n");
374 /* lock the device to allow correctly handling errors
376 mutex_lock(&dev->io_mutex);
381 /* save our object in the file's private structure */
382 file->private_data = fh;
386 mutex_unlock(&dev->io_mutex);
390 static int hdpvr_release(struct file *file)
392 struct hdpvr_fh *fh = (struct hdpvr_fh *)file->private_data;
393 struct hdpvr_device *dev = fh->dev;
398 mutex_lock(&dev->io_mutex);
399 if (!(--dev->open_count) && dev->status == STATUS_STREAMING)
400 hdpvr_stop_streaming(dev);
402 mutex_unlock(&dev->io_mutex);
409 * will allocate buffers when called for the first time
411 static ssize_t hdpvr_read(struct file *file, char __user *buffer, size_t count,
414 struct hdpvr_fh *fh = file->private_data;
415 struct hdpvr_device *dev = fh->dev;
416 struct hdpvr_buffer *buf = NULL;
418 unsigned int ret = 0;
427 mutex_lock(&dev->io_mutex);
428 if (dev->status == STATUS_IDLE) {
429 if (hdpvr_start_streaming(dev)) {
430 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
431 "start_streaming failed\n");
434 dev->status = STATUS_IDLE;
435 mutex_unlock(&dev->io_mutex);
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)) {
469 if (buf->status != BUFSTAT_READY)
472 /* set remaining bytes to copy */
474 rem = urb->actual_length - buf->pos;
475 cnt = rem > count ? count : rem;
477 if (copy_to_user(buffer, urb->transfer_buffer + buf->pos,
479 v4l2_err(&dev->v4l2_dev, "read: copy_to_user failed\n");
490 /* finished, take next buffer */
491 if (buf->pos == urb->actual_length) {
492 mutex_lock(&dev->io_mutex);
494 buf->status = BUFSTAT_AVAILABLE;
496 list_move_tail(&buf->buff_list, &dev->free_buff_list);
498 print_buffer_status();
500 mutex_unlock(&dev->io_mutex);
502 wake_up_interruptible(&dev->wait_buffer);
504 buf = hdpvr_get_next_buffer(dev);
513 static unsigned int hdpvr_poll(struct file *filp, poll_table *wait)
515 struct hdpvr_buffer *buf = NULL;
516 struct hdpvr_fh *fh = (struct hdpvr_fh *)filp->private_data;
517 struct hdpvr_device *dev = fh->dev;
518 unsigned int mask = 0;
520 mutex_lock(&dev->io_mutex);
522 if (video_is_unregistered(dev->video_dev))
525 if (dev->status == STATUS_IDLE) {
526 if (hdpvr_start_streaming(dev)) {
527 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev,
528 "start_streaming failed\n");
529 dev->status = STATUS_IDLE;
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, "Haupauge HD PVR");
570 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
571 cap->version = HDPVR_VERSION;
572 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
578 static int vidioc_s_std(struct file *file, void *private_data,
581 struct hdpvr_fh *fh = file->private_data;
582 struct hdpvr_device *dev = fh->dev;
585 if (*std & (V4L2_STD_NTSC | V4L2_STD_PAL_60))
588 return hdpvr_config_call(dev, CTRL_VIDEO_STD_TYPE, std_type);
591 static const char *iname[] = {
592 [HDPVR_COMPONENT] = "Component",
593 [HDPVR_SVIDEO] = "S-Video",
594 [HDPVR_COMPOSITE] = "Composite",
597 static int vidioc_enum_input(struct file *file, void *priv,
598 struct v4l2_input *i)
600 struct hdpvr_fh *fh = file->private_data;
601 struct hdpvr_device *dev = fh->dev;
605 if (n >= HDPVR_VIDEO_INPUTS)
608 i->type = V4L2_INPUT_TYPE_CAMERA;
610 strncpy(i->name, iname[n], sizeof(i->name) - 1);
611 i->name[sizeof(i->name) - 1] = '\0';
613 i->audioset = 1<<HDPVR_RCA_FRONT | 1<<HDPVR_RCA_BACK | 1<<HDPVR_SPDIF;
615 i->std = dev->video_dev->tvnorms;
620 static int vidioc_s_input(struct file *file, void *private_data,
623 struct hdpvr_fh *fh = file->private_data;
624 struct hdpvr_device *dev = fh->dev;
627 if (index >= HDPVR_VIDEO_INPUTS)
630 if (dev->status != STATUS_IDLE)
633 retval = hdpvr_config_call(dev, CTRL_VIDEO_INPUT_VALUE, index+1);
635 dev->options.video_input = index;
640 static int vidioc_g_input(struct file *file, void *private_data,
643 struct hdpvr_fh *fh = file->private_data;
644 struct hdpvr_device *dev = fh->dev;
646 *index = dev->options.video_input;
651 static const char *audio_iname[] = {
652 [HDPVR_RCA_FRONT] = "RCA front",
653 [HDPVR_RCA_BACK] = "RCA back",
654 [HDPVR_SPDIF] = "SPDIF",
657 static int vidioc_enumaudio(struct file *file, void *priv,
658 struct v4l2_audio *audio)
663 if (n >= HDPVR_AUDIO_INPUTS)
666 audio->capability = V4L2_AUDCAP_STEREO;
668 strncpy(audio->name, audio_iname[n], sizeof(audio->name) - 1);
669 audio->name[sizeof(audio->name) - 1] = '\0';
674 static int vidioc_s_audio(struct file *file, void *private_data,
675 struct v4l2_audio *audio)
677 struct hdpvr_fh *fh = file->private_data;
678 struct hdpvr_device *dev = fh->dev;
681 if (audio->index >= HDPVR_AUDIO_INPUTS)
684 if (dev->status != STATUS_IDLE)
687 retval = hdpvr_set_audio(dev, audio->index+1, dev->options.audio_codec);
689 dev->options.audio_input = audio->index;
694 static int vidioc_g_audio(struct file *file, void *private_data,
695 struct v4l2_audio *audio)
697 struct hdpvr_fh *fh = file->private_data;
698 struct hdpvr_device *dev = fh->dev;
700 audio->index = dev->options.audio_input;
701 audio->capability = V4L2_AUDCAP_STEREO;
702 strncpy(audio->name, audio_iname[audio->index], sizeof(audio->name));
703 audio->name[sizeof(audio->name) - 1] = '\0';
707 static const s32 supported_v4l2_ctrls[] = {
713 V4L2_CID_MPEG_AUDIO_ENCODING,
714 V4L2_CID_MPEG_VIDEO_ENCODING,
715 V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
716 V4L2_CID_MPEG_VIDEO_BITRATE,
717 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
720 static int fill_queryctrl(struct hdpvr_options *opt, struct v4l2_queryctrl *qc,
726 case V4L2_CID_BRIGHTNESS:
727 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x86);
728 case V4L2_CID_CONTRAST:
729 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
730 case V4L2_CID_SATURATION:
731 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
733 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
734 case V4L2_CID_SHARPNESS:
735 return v4l2_ctrl_query_fill(qc, 0x0, 0xff, 1, 0x80);
736 case V4L2_CID_MPEG_AUDIO_ENCODING:
737 return v4l2_ctrl_query_fill(
738 qc, V4L2_MPEG_AUDIO_ENCODING_AAC,
739 ac3 ? V4L2_MPEG_AUDIO_ENCODING_AC3
740 : V4L2_MPEG_AUDIO_ENCODING_AAC,
741 1, V4L2_MPEG_AUDIO_ENCODING_AAC);
742 case V4L2_CID_MPEG_VIDEO_ENCODING:
743 return v4l2_ctrl_query_fill(
744 qc, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC,
745 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 1,
746 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
748 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
749 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
750 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
751 return v4l2_ctrl_query_fill(
752 qc, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR,
753 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR, 1,
754 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR);
756 case V4L2_CID_MPEG_VIDEO_BITRATE:
757 return v4l2_ctrl_query_fill(qc, 1000000, 13500000, 100000,
759 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
760 err = v4l2_ctrl_query_fill(qc, 1100000, 20200000, 100000,
762 if (!err && opt->bitrate_mode == HDPVR_CONSTANT)
763 qc->flags |= V4L2_CTRL_FLAG_INACTIVE;
770 static int vidioc_queryctrl(struct file *file, void *private_data,
771 struct v4l2_queryctrl *qc)
773 struct hdpvr_fh *fh = file->private_data;
774 struct hdpvr_device *dev = fh->dev;
778 memset(qc, 0, sizeof(*qc));
780 next = !!(id & V4L2_CTRL_FLAG_NEXT_CTRL);
781 qc->id = id & ~V4L2_CTRL_FLAG_NEXT_CTRL;
783 for (i = 0; i < ARRAY_SIZE(supported_v4l2_ctrls); i++) {
785 if (qc->id < supported_v4l2_ctrls[i])
786 qc->id = supported_v4l2_ctrls[i];
791 if (qc->id == supported_v4l2_ctrls[i])
792 return fill_queryctrl(&dev->options, qc,
793 dev->flags & HDPVR_FLAG_AC3_CAP);
795 if (qc->id < supported_v4l2_ctrls[i])
802 static int vidioc_g_ctrl(struct file *file, void *private_data,
803 struct v4l2_control *ctrl)
805 struct hdpvr_fh *fh = file->private_data;
806 struct hdpvr_device *dev = fh->dev;
809 case V4L2_CID_BRIGHTNESS:
810 ctrl->value = dev->options.brightness;
812 case V4L2_CID_CONTRAST:
813 ctrl->value = dev->options.contrast;
815 case V4L2_CID_SATURATION:
816 ctrl->value = dev->options.saturation;
819 ctrl->value = dev->options.hue;
821 case V4L2_CID_SHARPNESS:
822 ctrl->value = dev->options.sharpness;
830 static int vidioc_s_ctrl(struct file *file, void *private_data,
831 struct v4l2_control *ctrl)
833 struct hdpvr_fh *fh = file->private_data;
834 struct hdpvr_device *dev = fh->dev;
838 case V4L2_CID_BRIGHTNESS:
839 retval = hdpvr_config_call(dev, CTRL_BRIGHTNESS, ctrl->value);
841 dev->options.brightness = ctrl->value;
843 case V4L2_CID_CONTRAST:
844 retval = hdpvr_config_call(dev, CTRL_CONTRAST, ctrl->value);
846 dev->options.contrast = ctrl->value;
848 case V4L2_CID_SATURATION:
849 retval = hdpvr_config_call(dev, CTRL_SATURATION, ctrl->value);
851 dev->options.saturation = ctrl->value;
854 retval = hdpvr_config_call(dev, CTRL_HUE, ctrl->value);
856 dev->options.hue = ctrl->value;
858 case V4L2_CID_SHARPNESS:
859 retval = hdpvr_config_call(dev, CTRL_SHARPNESS, ctrl->value);
861 dev->options.sharpness = ctrl->value;
871 static int hdpvr_get_ctrl(struct hdpvr_options *opt,
872 struct v4l2_ext_control *ctrl)
875 case V4L2_CID_MPEG_AUDIO_ENCODING:
876 ctrl->value = opt->audio_codec;
878 case V4L2_CID_MPEG_VIDEO_ENCODING:
879 ctrl->value = V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC;
881 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
882 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
884 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
885 ctrl->value = opt->bitrate_mode == HDPVR_CONSTANT
886 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
887 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR;
889 case V4L2_CID_MPEG_VIDEO_BITRATE:
890 ctrl->value = opt->bitrate * 100000;
892 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
893 ctrl->value = opt->peak_bitrate * 100000;
895 case V4L2_CID_MPEG_STREAM_TYPE:
896 ctrl->value = V4L2_MPEG_STREAM_TYPE_MPEG2_TS;
904 static int vidioc_g_ext_ctrls(struct file *file, void *priv,
905 struct v4l2_ext_controls *ctrls)
907 struct hdpvr_fh *fh = file->private_data;
908 struct hdpvr_device *dev = fh->dev;
911 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
912 for (i = 0; i < ctrls->count; i++) {
913 struct v4l2_ext_control *ctrl = ctrls->controls + i;
915 err = hdpvr_get_ctrl(&dev->options, ctrl);
917 ctrls->error_idx = i;
929 static int hdpvr_try_ctrl(struct v4l2_ext_control *ctrl, int ac3)
934 case V4L2_CID_MPEG_AUDIO_ENCODING:
935 if (ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AAC ||
936 (ac3 && ctrl->value == V4L2_MPEG_AUDIO_ENCODING_AC3))
939 case V4L2_CID_MPEG_VIDEO_ENCODING:
940 if (ctrl->value == V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC)
943 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
944 /* if (ctrl->value == 0 || ctrl->value == 128) */
947 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
948 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR ||
949 ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR)
952 case V4L2_CID_MPEG_VIDEO_BITRATE:
954 uint bitrate = ctrl->value / 100000;
955 if (bitrate >= 10 && bitrate <= 135)
959 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK:
961 uint peak_bitrate = ctrl->value / 100000;
962 if (peak_bitrate >= 10 && peak_bitrate <= 202)
966 case V4L2_CID_MPEG_STREAM_TYPE:
967 if (ctrl->value == V4L2_MPEG_STREAM_TYPE_MPEG2_TS)
976 static int vidioc_try_ext_ctrls(struct file *file, void *priv,
977 struct v4l2_ext_controls *ctrls)
979 struct hdpvr_fh *fh = file->private_data;
980 struct hdpvr_device *dev = fh->dev;
983 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
984 for (i = 0; i < ctrls->count; i++) {
985 struct v4l2_ext_control *ctrl = ctrls->controls + i;
987 err = hdpvr_try_ctrl(ctrl,
988 dev->flags & HDPVR_FLAG_AC3_CAP);
990 ctrls->error_idx = i;
1001 static int hdpvr_set_ctrl(struct hdpvr_device *dev,
1002 struct v4l2_ext_control *ctrl)
1004 struct hdpvr_options *opt = &dev->options;
1008 case V4L2_CID_MPEG_AUDIO_ENCODING:
1009 if (dev->flags & HDPVR_FLAG_AC3_CAP) {
1010 opt->audio_codec = ctrl->value;
1011 ret = hdpvr_set_audio(dev, opt->audio_input,
1015 case V4L2_CID_MPEG_VIDEO_ENCODING:
1017 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1018 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1019 /* opt->gop_mode |= 0x2; */
1020 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1021 /* opt->gop_mode); */
1023 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1024 /* opt->gop_mode &= ~0x2; */
1025 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1026 /* opt->gop_mode); */
1029 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE:
1030 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_CBR &&
1031 opt->bitrate_mode != HDPVR_CONSTANT) {
1032 opt->bitrate_mode = HDPVR_CONSTANT;
1033 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1036 if (ctrl->value == V4L2_MPEG_VIDEO_BITRATE_MODE_VBR &&
1037 opt->bitrate_mode == HDPVR_CONSTANT) {
1038 opt->bitrate_mode = HDPVR_VARIABLE_AVERAGE;
1039 hdpvr_config_call(dev, CTRL_BITRATE_MODE_VALUE,
1043 case V4L2_CID_MPEG_VIDEO_BITRATE: {
1044 uint bitrate = ctrl->value / 100000;
1046 opt->bitrate = bitrate;
1047 if (bitrate >= opt->peak_bitrate)
1048 opt->peak_bitrate = bitrate+1;
1050 hdpvr_set_bitrate(dev);
1053 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK: {
1054 uint peak_bitrate = ctrl->value / 100000;
1056 if (opt->bitrate_mode == HDPVR_CONSTANT)
1059 if (opt->bitrate < peak_bitrate) {
1060 opt->peak_bitrate = peak_bitrate;
1061 hdpvr_set_bitrate(dev);
1066 case V4L2_CID_MPEG_STREAM_TYPE:
1074 static int vidioc_s_ext_ctrls(struct file *file, void *priv,
1075 struct v4l2_ext_controls *ctrls)
1077 struct hdpvr_fh *fh = file->private_data;
1078 struct hdpvr_device *dev = fh->dev;
1081 if (ctrls->ctrl_class == V4L2_CTRL_CLASS_MPEG) {
1082 for (i = 0; i < ctrls->count; i++) {
1083 struct v4l2_ext_control *ctrl = ctrls->controls + i;
1085 err = hdpvr_try_ctrl(ctrl,
1086 dev->flags & HDPVR_FLAG_AC3_CAP);
1088 ctrls->error_idx = i;
1091 err = hdpvr_set_ctrl(dev, ctrl);
1093 ctrls->error_idx = i;
1104 static int vidioc_enum_fmt_vid_cap(struct file *file, void *private_data,
1105 struct v4l2_fmtdesc *f)
1108 if (f->index != 0 || f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1111 f->flags = V4L2_FMT_FLAG_COMPRESSED;
1112 strncpy(f->description, "MPEG2-TS with AVC/AAC streams", 32);
1113 f->pixelformat = V4L2_PIX_FMT_MPEG;
1118 static int vidioc_g_fmt_vid_cap(struct file *file, void *private_data,
1119 struct v4l2_format *f)
1121 struct hdpvr_fh *fh = file->private_data;
1122 struct hdpvr_device *dev = fh->dev;
1123 struct hdpvr_video_info *vid_info;
1128 vid_info = get_video_info(dev);
1132 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1133 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
1134 f->fmt.pix.width = vid_info->width;
1135 f->fmt.pix.height = vid_info->height;
1136 f->fmt.pix.sizeimage = dev->bulk_in_size;
1137 f->fmt.pix.colorspace = 0;
1138 f->fmt.pix.bytesperline = 0;
1139 f->fmt.pix.field = V4L2_FIELD_ANY;
1145 static int vidioc_encoder_cmd(struct file *filp, void *priv,
1146 struct v4l2_encoder_cmd *a)
1148 struct hdpvr_fh *fh = filp->private_data;
1149 struct hdpvr_device *dev = fh->dev;
1152 mutex_lock(&dev->io_mutex);
1154 memset(&a->raw, 0, sizeof(a->raw));
1156 case V4L2_ENC_CMD_START:
1158 res = hdpvr_start_streaming(dev);
1160 case V4L2_ENC_CMD_STOP:
1161 res = hdpvr_stop_streaming(dev);
1164 v4l2_dbg(MSG_INFO, hdpvr_debug, &dev->v4l2_dev,
1165 "Unsupported encoder cmd %d\n", a->cmd);
1168 mutex_unlock(&dev->io_mutex);
1172 static int vidioc_try_encoder_cmd(struct file *filp, void *priv,
1173 struct v4l2_encoder_cmd *a)
1176 case V4L2_ENC_CMD_START:
1177 case V4L2_ENC_CMD_STOP:
1184 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops = {
1185 .vidioc_querycap = vidioc_querycap,
1186 .vidioc_s_std = vidioc_s_std,
1187 .vidioc_enum_input = vidioc_enum_input,
1188 .vidioc_g_input = vidioc_g_input,
1189 .vidioc_s_input = vidioc_s_input,
1190 .vidioc_enumaudio = vidioc_enumaudio,
1191 .vidioc_g_audio = vidioc_g_audio,
1192 .vidioc_s_audio = vidioc_s_audio,
1193 .vidioc_queryctrl = vidioc_queryctrl,
1194 .vidioc_g_ctrl = vidioc_g_ctrl,
1195 .vidioc_s_ctrl = vidioc_s_ctrl,
1196 .vidioc_g_ext_ctrls = vidioc_g_ext_ctrls,
1197 .vidioc_s_ext_ctrls = vidioc_s_ext_ctrls,
1198 .vidioc_try_ext_ctrls = vidioc_try_ext_ctrls,
1199 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1200 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
1201 .vidioc_encoder_cmd = vidioc_encoder_cmd,
1202 .vidioc_try_encoder_cmd = vidioc_try_encoder_cmd,
1205 static void hdpvr_device_release(struct video_device *vdev)
1207 struct hdpvr_device *dev = video_get_drvdata(vdev);
1212 static const struct video_device hdpvr_video_template = {
1213 /* .type = VFL_TYPE_GRABBER, */
1214 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1215 .fops = &hdpvr_fops,
1216 .release = hdpvr_device_release,
1217 .ioctl_ops = &hdpvr_ioctl_ops,
1219 V4L2_STD_NTSC | V4L2_STD_SECAM | V4L2_STD_PAL_B |
1220 V4L2_STD_PAL_G | V4L2_STD_PAL_H | V4L2_STD_PAL_I |
1221 V4L2_STD_PAL_D | V4L2_STD_PAL_M | V4L2_STD_PAL_N |
1225 int hdpvr_register_videodev(struct hdpvr_device *dev, struct device *parent,
1228 /* setup and register video device */
1229 dev->video_dev = video_device_alloc();
1230 if (!dev->video_dev) {
1231 v4l2_err(&dev->v4l2_dev, "video_device_alloc() failed\n");
1235 *(dev->video_dev) = hdpvr_video_template;
1236 strcpy(dev->video_dev->name, "Hauppauge HD PVR");
1237 dev->video_dev->parent = parent;
1238 video_set_drvdata(dev->video_dev, dev);
1240 if (video_register_device(dev->video_dev, VFL_TYPE_GRABBER, devnum)) {
1241 v4l2_err(&dev->v4l2_dev, "video_device registration failed\n");