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");
151 goto exit_urb_buffer
;
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
);
169 hdpvr_free_buffers(dev
);
173 static int hdpvr_submit_buffers(struct hdpvr_device
*dev
)
175 struct hdpvr_buffer
*buf
;
177 int ret
= 0, err_count
= 0;
179 mutex_lock(&dev
->io_mutex
);
181 while (dev
->status
== STATUS_STREAMING
&&
182 !list_empty(&dev
->free_buff_list
)) {
184 buf
= list_entry(dev
->free_buff_list
.next
, struct hdpvr_buffer
,
186 if (buf
->status
!= BUFSTAT_AVAILABLE
) {
187 v4l2_err(&dev
->v4l2_dev
,
188 "buffer not marked as available\n");
195 urb
->actual_length
= 0;
196 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
198 v4l2_err(&dev
->v4l2_dev
,
199 "usb_submit_urb in %s returned %d\n",
205 buf
->status
= BUFSTAT_INPROGRESS
;
206 list_move_tail(&buf
->buff_list
, &dev
->rec_buff_list
);
209 print_buffer_status();
210 mutex_unlock(&dev
->io_mutex
);
214 static struct hdpvr_buffer
*hdpvr_get_next_buffer(struct hdpvr_device
*dev
)
216 struct hdpvr_buffer
*buf
;
218 mutex_lock(&dev
->io_mutex
);
220 if (list_empty(&dev
->rec_buff_list
)) {
221 mutex_unlock(&dev
->io_mutex
);
225 buf
= list_entry(dev
->rec_buff_list
.next
, struct hdpvr_buffer
,
227 mutex_unlock(&dev
->io_mutex
);
232 static void hdpvr_transmit_buffers(struct work_struct
*work
)
234 struct hdpvr_device
*dev
= container_of(work
, struct hdpvr_device
,
237 while (dev
->status
== STATUS_STREAMING
) {
239 if (hdpvr_submit_buffers(dev
)) {
240 v4l2_err(&dev
->v4l2_dev
, "couldn't submit buffers\n");
243 if (wait_event_interruptible(dev
->wait_buffer
,
244 !list_empty(&dev
->free_buff_list
) ||
245 dev
->status
!= STATUS_STREAMING
))
249 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
250 "transmit worker exited\n");
253 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
254 "transmit buffers errored\n");
255 dev
->status
= STATUS_ERROR
;
258 /* function expects dev->io_mutex to be hold by caller */
259 static int hdpvr_start_streaming(struct hdpvr_device
*dev
)
262 struct hdpvr_video_info
*vidinf
;
264 if (dev
->status
== STATUS_STREAMING
)
266 else if (dev
->status
!= STATUS_IDLE
)
269 vidinf
= get_video_info(dev
);
272 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
273 "video signal: %dx%d@%dhz\n", vidinf
->width
,
274 vidinf
->height
, vidinf
->fps
);
277 /* start streaming 2 request */
278 ret
= usb_control_msg(dev
->udev
,
279 usb_sndctrlpipe(dev
->udev
, 0),
280 0xb8, 0x38, 0x1, 0, NULL
, 0, 8000);
281 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
282 "encoder start control request returned %d\n", ret
);
284 hdpvr_config_call(dev
, CTRL_START_STREAMING_VALUE
, 0x00);
286 INIT_WORK(&dev
->worker
, hdpvr_transmit_buffers
);
287 queue_work(dev
->workqueue
, &dev
->worker
);
289 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
290 "streaming started\n");
291 dev
->status
= STATUS_STREAMING
;
296 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
297 "no video signal at input %d\n", dev
->options
.video_input
);
302 /* function expects dev->io_mutex to be hold by caller */
303 static int hdpvr_stop_streaming(struct hdpvr_device
*dev
)
309 if (dev
->status
== STATUS_IDLE
)
311 else if (dev
->status
!= STATUS_STREAMING
)
314 buf
= kmalloc(dev
->bulk_in_size
, GFP_KERNEL
);
316 v4l2_err(&dev
->v4l2_dev
, "failed to allocate temporary buffer "
317 "for emptying the internal device buffer. "
318 "Next capture start will be slow\n");
320 dev
->status
= STATUS_SHUTTING_DOWN
;
321 hdpvr_config_call(dev
, CTRL_STOP_STREAMING_VALUE
, 0x00);
322 mutex_unlock(&dev
->io_mutex
);
324 wake_up_interruptible(&dev
->wait_buffer
);
327 flush_workqueue(dev
->workqueue
);
329 mutex_lock(&dev
->io_mutex
);
330 /* kill the still outstanding urbs */
331 hdpvr_cancel_queue(dev
);
333 /* emptying the device buffer beforeshutting it down */
334 while (buf
&& ++c
< 500 &&
335 !usb_bulk_msg(dev
->udev
,
336 usb_rcvbulkpipe(dev
->udev
,
337 dev
->bulk_in_endpointAddr
),
338 buf
, dev
->bulk_in_size
, &actual_length
,
342 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
343 "%2d: got %d bytes\n", c
, actual_length
);
346 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
347 "used %d urbs to empty device buffers\n", c
-1);
350 dev
->status
= STATUS_IDLE
;
356 /*=======================================================================*/
358 * video 4 linux 2 file operations
361 static int hdpvr_open(struct file
*file
)
363 struct hdpvr_device
*dev
;
365 int retval
= -ENOMEM
;
367 dev
= (struct hdpvr_device
*)video_get_drvdata(video_devdata(file
));
369 v4l2_err(&dev
->v4l2_dev
, "open failing with with ENODEV\n");
374 fh
= kzalloc(sizeof(struct hdpvr_fh
), GFP_KERNEL
);
376 v4l2_err(&dev
->v4l2_dev
, "Out of memory\n");
379 /* lock the device to allow correctly handling errors
381 mutex_lock(&dev
->io_mutex
);
383 mutex_unlock(&dev
->io_mutex
);
387 /* save our object in the file's private structure */
388 file
->private_data
= fh
;
395 static int hdpvr_release(struct file
*file
)
397 struct hdpvr_fh
*fh
= (struct hdpvr_fh
*)file
->private_data
;
398 struct hdpvr_device
*dev
= fh
->dev
;
403 mutex_lock(&dev
->io_mutex
);
404 if (!(--dev
->open_count
) && dev
->status
== STATUS_STREAMING
)
405 hdpvr_stop_streaming(dev
);
407 mutex_unlock(&dev
->io_mutex
);
414 * will allocate buffers when called for the first time
416 static ssize_t
hdpvr_read(struct file
*file
, char __user
*buffer
, size_t count
,
419 struct hdpvr_fh
*fh
= file
->private_data
;
420 struct hdpvr_device
*dev
= fh
->dev
;
421 struct hdpvr_buffer
*buf
= NULL
;
423 unsigned int ret
= 0;
432 mutex_lock(&dev
->io_mutex
);
433 if (dev
->status
== STATUS_IDLE
) {
434 if (hdpvr_start_streaming(dev
)) {
435 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
436 "start_streaming failed\n");
439 dev
->status
= STATUS_IDLE
;
440 mutex_unlock(&dev
->io_mutex
);
443 print_buffer_status();
445 mutex_unlock(&dev
->io_mutex
);
447 /* wait for the first buffer */
448 if (!(file
->f_flags
& O_NONBLOCK
)) {
449 if (wait_event_interruptible(dev
->wait_data
,
450 hdpvr_get_next_buffer(dev
)))
454 buf
= hdpvr_get_next_buffer(dev
);
456 while (count
> 0 && buf
) {
458 if (buf
->status
!= BUFSTAT_READY
&&
459 dev
->status
!= STATUS_DISCONNECTED
) {
460 /* return nonblocking */
461 if (file
->f_flags
& O_NONBLOCK
) {
467 if (wait_event_interruptible(dev
->wait_data
,
468 buf
->status
== BUFSTAT_READY
)) {
474 if (buf
->status
!= BUFSTAT_READY
)
477 /* set remaining bytes to copy */
479 rem
= urb
->actual_length
- buf
->pos
;
480 cnt
= rem
> count
? count
: rem
;
482 if (copy_to_user(buffer
, urb
->transfer_buffer
+ buf
->pos
,
484 v4l2_err(&dev
->v4l2_dev
, "read: copy_to_user failed\n");
495 /* finished, take next buffer */
496 if (buf
->pos
== urb
->actual_length
) {
497 mutex_lock(&dev
->io_mutex
);
499 buf
->status
= BUFSTAT_AVAILABLE
;
501 list_move_tail(&buf
->buff_list
, &dev
->free_buff_list
);
503 print_buffer_status();
505 mutex_unlock(&dev
->io_mutex
);
507 wake_up_interruptible(&dev
->wait_buffer
);
509 buf
= hdpvr_get_next_buffer(dev
);
518 static unsigned int hdpvr_poll(struct file
*filp
, poll_table
*wait
)
520 struct hdpvr_buffer
*buf
= NULL
;
521 struct hdpvr_fh
*fh
= (struct hdpvr_fh
*)filp
->private_data
;
522 struct hdpvr_device
*dev
= fh
->dev
;
523 unsigned int mask
= 0;
525 mutex_lock(&dev
->io_mutex
);
527 if (!video_is_registered(dev
->video_dev
)) {
528 mutex_unlock(&dev
->io_mutex
);
532 if (dev
->status
== STATUS_IDLE
) {
533 if (hdpvr_start_streaming(dev
)) {
534 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
535 "start_streaming failed\n");
536 dev
->status
= STATUS_IDLE
;
539 print_buffer_status();
541 mutex_unlock(&dev
->io_mutex
);
543 buf
= hdpvr_get_next_buffer(dev
);
544 /* only wait if no data is available */
545 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
546 poll_wait(filp
, &dev
->wait_data
, wait
);
547 buf
= hdpvr_get_next_buffer(dev
);
549 if (buf
&& buf
->status
== BUFSTAT_READY
)
550 mask
|= POLLIN
| POLLRDNORM
;
556 static const struct v4l2_file_operations hdpvr_fops
= {
557 .owner
= THIS_MODULE
,
559 .release
= hdpvr_release
,
562 .unlocked_ioctl
= video_ioctl2
,
565 /*=======================================================================*/
567 * V4L2 ioctl handling
570 static int vidioc_querycap(struct file
*file
, void *priv
,
571 struct v4l2_capability
*cap
)
573 struct hdpvr_device
*dev
= video_drvdata(file
);
575 strcpy(cap
->driver
, "hdpvr");
576 strcpy(cap
->card
, "Hauppauge HD PVR");
577 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
578 cap
->version
= HDPVR_VERSION
;
579 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
585 static int vidioc_s_std(struct file
*file
, void *private_data
,
588 struct hdpvr_fh
*fh
= file
->private_data
;
589 struct hdpvr_device
*dev
= fh
->dev
;
592 if (*std
& (V4L2_STD_NTSC
| V4L2_STD_PAL_60
))
595 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
598 static const char *iname
[] = {
599 [HDPVR_COMPONENT
] = "Component",
600 [HDPVR_SVIDEO
] = "S-Video",
601 [HDPVR_COMPOSITE
] = "Composite",
604 static int vidioc_enum_input(struct file
*file
, void *priv
,
605 struct v4l2_input
*i
)
607 struct hdpvr_fh
*fh
= file
->private_data
;
608 struct hdpvr_device
*dev
= fh
->dev
;
612 if (n
>= HDPVR_VIDEO_INPUTS
)
615 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
617 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
618 i
->name
[sizeof(i
->name
) - 1] = '\0';
620 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
622 i
->std
= dev
->video_dev
->tvnorms
;
627 static int vidioc_s_input(struct file
*file
, void *private_data
,
630 struct hdpvr_fh
*fh
= file
->private_data
;
631 struct hdpvr_device
*dev
= fh
->dev
;
634 if (index
>= HDPVR_VIDEO_INPUTS
)
637 if (dev
->status
!= STATUS_IDLE
)
640 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
642 dev
->options
.video_input
= index
;
647 static int vidioc_g_input(struct file
*file
, void *private_data
,
650 struct hdpvr_fh
*fh
= file
->private_data
;
651 struct hdpvr_device
*dev
= fh
->dev
;
653 *index
= dev
->options
.video_input
;
658 static const char *audio_iname
[] = {
659 [HDPVR_RCA_FRONT
] = "RCA front",
660 [HDPVR_RCA_BACK
] = "RCA back",
661 [HDPVR_SPDIF
] = "SPDIF",
664 static int vidioc_enumaudio(struct file
*file
, void *priv
,
665 struct v4l2_audio
*audio
)
670 if (n
>= HDPVR_AUDIO_INPUTS
)
673 audio
->capability
= V4L2_AUDCAP_STEREO
;
675 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
676 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
681 static int vidioc_s_audio(struct file
*file
, void *private_data
,
682 struct v4l2_audio
*audio
)
684 struct hdpvr_fh
*fh
= file
->private_data
;
685 struct hdpvr_device
*dev
= fh
->dev
;
688 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
691 if (dev
->status
!= STATUS_IDLE
)
694 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
696 dev
->options
.audio_input
= audio
->index
;
701 static int vidioc_g_audio(struct file
*file
, void *private_data
,
702 struct v4l2_audio
*audio
)
704 struct hdpvr_fh
*fh
= file
->private_data
;
705 struct hdpvr_device
*dev
= fh
->dev
;
707 audio
->index
= dev
->options
.audio_input
;
708 audio
->capability
= V4L2_AUDCAP_STEREO
;
709 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
710 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
714 static const s32 supported_v4l2_ctrls
[] = {
720 V4L2_CID_MPEG_AUDIO_ENCODING
,
721 V4L2_CID_MPEG_VIDEO_ENCODING
,
722 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
723 V4L2_CID_MPEG_VIDEO_BITRATE
,
724 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
727 static int fill_queryctrl(struct hdpvr_options
*opt
, struct v4l2_queryctrl
*qc
,
733 case V4L2_CID_BRIGHTNESS
:
734 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x86);
735 case V4L2_CID_CONTRAST
:
736 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
737 case V4L2_CID_SATURATION
:
738 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
740 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
741 case V4L2_CID_SHARPNESS
:
742 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
743 case V4L2_CID_MPEG_AUDIO_ENCODING
:
744 return v4l2_ctrl_query_fill(
745 qc
, V4L2_MPEG_AUDIO_ENCODING_AAC
,
746 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
747 : V4L2_MPEG_AUDIO_ENCODING_AAC
,
748 1, V4L2_MPEG_AUDIO_ENCODING_AAC
);
749 case V4L2_CID_MPEG_VIDEO_ENCODING
:
750 return v4l2_ctrl_query_fill(
751 qc
, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
,
752 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 1,
753 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
755 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
756 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
757 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
758 return v4l2_ctrl_query_fill(
759 qc
, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
,
760 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 1,
761 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
763 case V4L2_CID_MPEG_VIDEO_BITRATE
:
764 return v4l2_ctrl_query_fill(qc
, 1000000, 13500000, 100000,
766 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
767 err
= v4l2_ctrl_query_fill(qc
, 1100000, 20200000, 100000,
769 if (!err
&& opt
->bitrate_mode
== HDPVR_CONSTANT
)
770 qc
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
777 static int vidioc_queryctrl(struct file
*file
, void *private_data
,
778 struct v4l2_queryctrl
*qc
)
780 struct hdpvr_fh
*fh
= file
->private_data
;
781 struct hdpvr_device
*dev
= fh
->dev
;
785 memset(qc
, 0, sizeof(*qc
));
787 next
= !!(id
& V4L2_CTRL_FLAG_NEXT_CTRL
);
788 qc
->id
= id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
;
790 for (i
= 0; i
< ARRAY_SIZE(supported_v4l2_ctrls
); i
++) {
792 if (qc
->id
< supported_v4l2_ctrls
[i
])
793 qc
->id
= supported_v4l2_ctrls
[i
];
798 if (qc
->id
== supported_v4l2_ctrls
[i
])
799 return fill_queryctrl(&dev
->options
, qc
,
800 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
802 if (qc
->id
< supported_v4l2_ctrls
[i
])
809 static int vidioc_g_ctrl(struct file
*file
, void *private_data
,
810 struct v4l2_control
*ctrl
)
812 struct hdpvr_fh
*fh
= file
->private_data
;
813 struct hdpvr_device
*dev
= fh
->dev
;
816 case V4L2_CID_BRIGHTNESS
:
817 ctrl
->value
= dev
->options
.brightness
;
819 case V4L2_CID_CONTRAST
:
820 ctrl
->value
= dev
->options
.contrast
;
822 case V4L2_CID_SATURATION
:
823 ctrl
->value
= dev
->options
.saturation
;
826 ctrl
->value
= dev
->options
.hue
;
828 case V4L2_CID_SHARPNESS
:
829 ctrl
->value
= dev
->options
.sharpness
;
837 static int vidioc_s_ctrl(struct file
*file
, void *private_data
,
838 struct v4l2_control
*ctrl
)
840 struct hdpvr_fh
*fh
= file
->private_data
;
841 struct hdpvr_device
*dev
= fh
->dev
;
845 case V4L2_CID_BRIGHTNESS
:
846 retval
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->value
);
848 dev
->options
.brightness
= ctrl
->value
;
850 case V4L2_CID_CONTRAST
:
851 retval
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->value
);
853 dev
->options
.contrast
= ctrl
->value
;
855 case V4L2_CID_SATURATION
:
856 retval
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->value
);
858 dev
->options
.saturation
= ctrl
->value
;
861 retval
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->value
);
863 dev
->options
.hue
= ctrl
->value
;
865 case V4L2_CID_SHARPNESS
:
866 retval
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->value
);
868 dev
->options
.sharpness
= ctrl
->value
;
878 static int hdpvr_get_ctrl(struct hdpvr_options
*opt
,
879 struct v4l2_ext_control
*ctrl
)
882 case V4L2_CID_MPEG_AUDIO_ENCODING
:
883 ctrl
->value
= opt
->audio_codec
;
885 case V4L2_CID_MPEG_VIDEO_ENCODING
:
886 ctrl
->value
= V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
;
888 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
889 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
891 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
892 ctrl
->value
= opt
->bitrate_mode
== HDPVR_CONSTANT
893 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
894 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
;
896 case V4L2_CID_MPEG_VIDEO_BITRATE
:
897 ctrl
->value
= opt
->bitrate
* 100000;
899 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
900 ctrl
->value
= opt
->peak_bitrate
* 100000;
902 case V4L2_CID_MPEG_STREAM_TYPE
:
903 ctrl
->value
= V4L2_MPEG_STREAM_TYPE_MPEG2_TS
;
911 static int vidioc_g_ext_ctrls(struct file
*file
, void *priv
,
912 struct v4l2_ext_controls
*ctrls
)
914 struct hdpvr_fh
*fh
= file
->private_data
;
915 struct hdpvr_device
*dev
= fh
->dev
;
918 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
919 for (i
= 0; i
< ctrls
->count
; i
++) {
920 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
922 err
= hdpvr_get_ctrl(&dev
->options
, ctrl
);
924 ctrls
->error_idx
= i
;
936 static int hdpvr_try_ctrl(struct v4l2_ext_control
*ctrl
, int ac3
)
941 case V4L2_CID_MPEG_AUDIO_ENCODING
:
942 if (ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AAC
||
943 (ac3
&& ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AC3
))
946 case V4L2_CID_MPEG_VIDEO_ENCODING
:
947 if (ctrl
->value
== V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
)
950 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
951 /* if (ctrl->value == 0 || ctrl->value == 128) */
954 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
955 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
||
956 ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
)
959 case V4L2_CID_MPEG_VIDEO_BITRATE
:
961 uint bitrate
= ctrl
->value
/ 100000;
962 if (bitrate
>= 10 && bitrate
<= 135)
966 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
968 uint peak_bitrate
= ctrl
->value
/ 100000;
969 if (peak_bitrate
>= 10 && peak_bitrate
<= 202)
973 case V4L2_CID_MPEG_STREAM_TYPE
:
974 if (ctrl
->value
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
)
983 static int vidioc_try_ext_ctrls(struct file
*file
, void *priv
,
984 struct v4l2_ext_controls
*ctrls
)
986 struct hdpvr_fh
*fh
= file
->private_data
;
987 struct hdpvr_device
*dev
= fh
->dev
;
990 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
991 for (i
= 0; i
< ctrls
->count
; i
++) {
992 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
994 err
= hdpvr_try_ctrl(ctrl
,
995 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
997 ctrls
->error_idx
= i
;
1008 static int hdpvr_set_ctrl(struct hdpvr_device
*dev
,
1009 struct v4l2_ext_control
*ctrl
)
1011 struct hdpvr_options
*opt
= &dev
->options
;
1015 case V4L2_CID_MPEG_AUDIO_ENCODING
:
1016 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
1017 opt
->audio_codec
= ctrl
->value
;
1018 ret
= hdpvr_set_audio(dev
, opt
->audio_input
,
1022 case V4L2_CID_MPEG_VIDEO_ENCODING
:
1024 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1025 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1026 /* opt->gop_mode |= 0x2; */
1027 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1028 /* opt->gop_mode); */
1030 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1031 /* opt->gop_mode &= ~0x2; */
1032 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1033 /* opt->gop_mode); */
1036 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
1037 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
&&
1038 opt
->bitrate_mode
!= HDPVR_CONSTANT
) {
1039 opt
->bitrate_mode
= HDPVR_CONSTANT
;
1040 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1043 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
1044 opt
->bitrate_mode
== HDPVR_CONSTANT
) {
1045 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
1046 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1050 case V4L2_CID_MPEG_VIDEO_BITRATE
: {
1051 uint bitrate
= ctrl
->value
/ 100000;
1053 opt
->bitrate
= bitrate
;
1054 if (bitrate
>= opt
->peak_bitrate
)
1055 opt
->peak_bitrate
= bitrate
+1;
1057 hdpvr_set_bitrate(dev
);
1060 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
: {
1061 uint peak_bitrate
= ctrl
->value
/ 100000;
1063 if (opt
->bitrate_mode
== HDPVR_CONSTANT
)
1066 if (opt
->bitrate
< peak_bitrate
) {
1067 opt
->peak_bitrate
= peak_bitrate
;
1068 hdpvr_set_bitrate(dev
);
1073 case V4L2_CID_MPEG_STREAM_TYPE
:
1081 static int vidioc_s_ext_ctrls(struct file
*file
, void *priv
,
1082 struct v4l2_ext_controls
*ctrls
)
1084 struct hdpvr_fh
*fh
= file
->private_data
;
1085 struct hdpvr_device
*dev
= fh
->dev
;
1088 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
1089 for (i
= 0; i
< ctrls
->count
; i
++) {
1090 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
1092 err
= hdpvr_try_ctrl(ctrl
,
1093 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
1095 ctrls
->error_idx
= i
;
1098 err
= hdpvr_set_ctrl(dev
, ctrl
);
1100 ctrls
->error_idx
= i
;
1111 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
1112 struct v4l2_fmtdesc
*f
)
1115 if (f
->index
!= 0 || f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1118 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1119 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
1120 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
1125 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *private_data
,
1126 struct v4l2_format
*f
)
1128 struct hdpvr_fh
*fh
= file
->private_data
;
1129 struct hdpvr_device
*dev
= fh
->dev
;
1130 struct hdpvr_video_info
*vid_info
;
1135 vid_info
= get_video_info(dev
);
1139 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1140 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1141 f
->fmt
.pix
.width
= vid_info
->width
;
1142 f
->fmt
.pix
.height
= vid_info
->height
;
1143 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1144 f
->fmt
.pix
.colorspace
= 0;
1145 f
->fmt
.pix
.bytesperline
= 0;
1146 f
->fmt
.pix
.field
= V4L2_FIELD_ANY
;
1152 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1153 struct v4l2_encoder_cmd
*a
)
1155 struct hdpvr_fh
*fh
= filp
->private_data
;
1156 struct hdpvr_device
*dev
= fh
->dev
;
1159 mutex_lock(&dev
->io_mutex
);
1161 memset(&a
->raw
, 0, sizeof(a
->raw
));
1163 case V4L2_ENC_CMD_START
:
1165 res
= hdpvr_start_streaming(dev
);
1167 case V4L2_ENC_CMD_STOP
:
1168 res
= hdpvr_stop_streaming(dev
);
1171 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1172 "Unsupported encoder cmd %d\n", a
->cmd
);
1175 mutex_unlock(&dev
->io_mutex
);
1179 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1180 struct v4l2_encoder_cmd
*a
)
1183 case V4L2_ENC_CMD_START
:
1184 case V4L2_ENC_CMD_STOP
:
1191 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1192 .vidioc_querycap
= vidioc_querycap
,
1193 .vidioc_s_std
= vidioc_s_std
,
1194 .vidioc_enum_input
= vidioc_enum_input
,
1195 .vidioc_g_input
= vidioc_g_input
,
1196 .vidioc_s_input
= vidioc_s_input
,
1197 .vidioc_enumaudio
= vidioc_enumaudio
,
1198 .vidioc_g_audio
= vidioc_g_audio
,
1199 .vidioc_s_audio
= vidioc_s_audio
,
1200 .vidioc_queryctrl
= vidioc_queryctrl
,
1201 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1202 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1203 .vidioc_g_ext_ctrls
= vidioc_g_ext_ctrls
,
1204 .vidioc_s_ext_ctrls
= vidioc_s_ext_ctrls
,
1205 .vidioc_try_ext_ctrls
= vidioc_try_ext_ctrls
,
1206 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1207 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1208 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1209 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1212 static void hdpvr_device_release(struct video_device
*vdev
)
1214 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1219 static const struct video_device hdpvr_video_template
= {
1220 /* .type = VFL_TYPE_GRABBER, */
1221 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1222 .fops
= &hdpvr_fops
,
1223 .release
= hdpvr_device_release
,
1224 .ioctl_ops
= &hdpvr_ioctl_ops
,
1226 V4L2_STD_NTSC
| V4L2_STD_SECAM
| V4L2_STD_PAL_B
|
1227 V4L2_STD_PAL_G
| V4L2_STD_PAL_H
| V4L2_STD_PAL_I
|
1228 V4L2_STD_PAL_D
| V4L2_STD_PAL_M
| V4L2_STD_PAL_N
|
1230 .current_norm
= V4L2_STD_NTSC
| V4L2_STD_PAL_M
|
1234 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1237 /* setup and register video device */
1238 dev
->video_dev
= video_device_alloc();
1239 if (!dev
->video_dev
) {
1240 v4l2_err(&dev
->v4l2_dev
, "video_device_alloc() failed\n");
1244 *(dev
->video_dev
) = hdpvr_video_template
;
1245 strcpy(dev
->video_dev
->name
, "Hauppauge HD PVR");
1246 dev
->video_dev
->parent
= parent
;
1247 video_set_drvdata(dev
->video_dev
, dev
);
1249 if (video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
)) {
1250 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");