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 available\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
);
378 mutex_unlock(&dev
->io_mutex
);
382 /* save our object in the file's private structure */
383 file
->private_data
= fh
;
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
)) {
523 mutex_unlock(&dev
->io_mutex
);
527 if (dev
->status
== STATUS_IDLE
) {
528 if (hdpvr_start_streaming(dev
)) {
529 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
530 "start_streaming failed\n");
531 dev
->status
= STATUS_IDLE
;
534 print_buffer_status();
536 mutex_unlock(&dev
->io_mutex
);
538 buf
= hdpvr_get_next_buffer(dev
);
539 /* only wait if no data is available */
540 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
541 poll_wait(filp
, &dev
->wait_data
, wait
);
542 buf
= hdpvr_get_next_buffer(dev
);
544 if (buf
&& buf
->status
== BUFSTAT_READY
)
545 mask
|= POLLIN
| POLLRDNORM
;
551 static const struct v4l2_file_operations hdpvr_fops
= {
552 .owner
= THIS_MODULE
,
554 .release
= hdpvr_release
,
557 .unlocked_ioctl
= video_ioctl2
,
560 /*=======================================================================*/
562 * V4L2 ioctl handling
565 static int vidioc_querycap(struct file
*file
, void *priv
,
566 struct v4l2_capability
*cap
)
568 struct hdpvr_device
*dev
= video_drvdata(file
);
570 strcpy(cap
->driver
, "hdpvr");
571 strcpy(cap
->card
, "Haupauge HD PVR");
572 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
573 cap
->version
= HDPVR_VERSION
;
574 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
580 static int vidioc_s_std(struct file
*file
, void *private_data
,
583 struct hdpvr_fh
*fh
= file
->private_data
;
584 struct hdpvr_device
*dev
= fh
->dev
;
587 if (*std
& (V4L2_STD_NTSC
| V4L2_STD_PAL_60
))
590 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
593 static const char *iname
[] = {
594 [HDPVR_COMPONENT
] = "Component",
595 [HDPVR_SVIDEO
] = "S-Video",
596 [HDPVR_COMPOSITE
] = "Composite",
599 static int vidioc_enum_input(struct file
*file
, void *priv
,
600 struct v4l2_input
*i
)
602 struct hdpvr_fh
*fh
= file
->private_data
;
603 struct hdpvr_device
*dev
= fh
->dev
;
607 if (n
>= HDPVR_VIDEO_INPUTS
)
610 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
612 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
613 i
->name
[sizeof(i
->name
) - 1] = '\0';
615 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
617 i
->std
= dev
->video_dev
->tvnorms
;
622 static int vidioc_s_input(struct file
*file
, void *private_data
,
625 struct hdpvr_fh
*fh
= file
->private_data
;
626 struct hdpvr_device
*dev
= fh
->dev
;
629 if (index
>= HDPVR_VIDEO_INPUTS
)
632 if (dev
->status
!= STATUS_IDLE
)
635 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
637 dev
->options
.video_input
= index
;
642 static int vidioc_g_input(struct file
*file
, void *private_data
,
645 struct hdpvr_fh
*fh
= file
->private_data
;
646 struct hdpvr_device
*dev
= fh
->dev
;
648 *index
= dev
->options
.video_input
;
653 static const char *audio_iname
[] = {
654 [HDPVR_RCA_FRONT
] = "RCA front",
655 [HDPVR_RCA_BACK
] = "RCA back",
656 [HDPVR_SPDIF
] = "SPDIF",
659 static int vidioc_enumaudio(struct file
*file
, void *priv
,
660 struct v4l2_audio
*audio
)
665 if (n
>= HDPVR_AUDIO_INPUTS
)
668 audio
->capability
= V4L2_AUDCAP_STEREO
;
670 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
671 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
676 static int vidioc_s_audio(struct file
*file
, void *private_data
,
677 struct v4l2_audio
*audio
)
679 struct hdpvr_fh
*fh
= file
->private_data
;
680 struct hdpvr_device
*dev
= fh
->dev
;
683 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
686 if (dev
->status
!= STATUS_IDLE
)
689 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
691 dev
->options
.audio_input
= audio
->index
;
696 static int vidioc_g_audio(struct file
*file
, void *private_data
,
697 struct v4l2_audio
*audio
)
699 struct hdpvr_fh
*fh
= file
->private_data
;
700 struct hdpvr_device
*dev
= fh
->dev
;
702 audio
->index
= dev
->options
.audio_input
;
703 audio
->capability
= V4L2_AUDCAP_STEREO
;
704 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
705 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
709 static const s32 supported_v4l2_ctrls
[] = {
715 V4L2_CID_MPEG_AUDIO_ENCODING
,
716 V4L2_CID_MPEG_VIDEO_ENCODING
,
717 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
718 V4L2_CID_MPEG_VIDEO_BITRATE
,
719 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
722 static int fill_queryctrl(struct hdpvr_options
*opt
, struct v4l2_queryctrl
*qc
,
728 case V4L2_CID_BRIGHTNESS
:
729 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x86);
730 case V4L2_CID_CONTRAST
:
731 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
732 case V4L2_CID_SATURATION
:
733 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
735 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
736 case V4L2_CID_SHARPNESS
:
737 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
738 case V4L2_CID_MPEG_AUDIO_ENCODING
:
739 return v4l2_ctrl_query_fill(
740 qc
, V4L2_MPEG_AUDIO_ENCODING_AAC
,
741 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
742 : V4L2_MPEG_AUDIO_ENCODING_AAC
,
743 1, V4L2_MPEG_AUDIO_ENCODING_AAC
);
744 case V4L2_CID_MPEG_VIDEO_ENCODING
:
745 return v4l2_ctrl_query_fill(
746 qc
, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
,
747 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 1,
748 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
750 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
751 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
752 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
753 return v4l2_ctrl_query_fill(
754 qc
, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
,
755 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 1,
756 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
758 case V4L2_CID_MPEG_VIDEO_BITRATE
:
759 return v4l2_ctrl_query_fill(qc
, 1000000, 13500000, 100000,
761 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
762 err
= v4l2_ctrl_query_fill(qc
, 1100000, 20200000, 100000,
764 if (!err
&& opt
->bitrate_mode
== HDPVR_CONSTANT
)
765 qc
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
772 static int vidioc_queryctrl(struct file
*file
, void *private_data
,
773 struct v4l2_queryctrl
*qc
)
775 struct hdpvr_fh
*fh
= file
->private_data
;
776 struct hdpvr_device
*dev
= fh
->dev
;
780 memset(qc
, 0, sizeof(*qc
));
782 next
= !!(id
& V4L2_CTRL_FLAG_NEXT_CTRL
);
783 qc
->id
= id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
;
785 for (i
= 0; i
< ARRAY_SIZE(supported_v4l2_ctrls
); i
++) {
787 if (qc
->id
< supported_v4l2_ctrls
[i
])
788 qc
->id
= supported_v4l2_ctrls
[i
];
793 if (qc
->id
== supported_v4l2_ctrls
[i
])
794 return fill_queryctrl(&dev
->options
, qc
,
795 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
797 if (qc
->id
< supported_v4l2_ctrls
[i
])
804 static int vidioc_g_ctrl(struct file
*file
, void *private_data
,
805 struct v4l2_control
*ctrl
)
807 struct hdpvr_fh
*fh
= file
->private_data
;
808 struct hdpvr_device
*dev
= fh
->dev
;
811 case V4L2_CID_BRIGHTNESS
:
812 ctrl
->value
= dev
->options
.brightness
;
814 case V4L2_CID_CONTRAST
:
815 ctrl
->value
= dev
->options
.contrast
;
817 case V4L2_CID_SATURATION
:
818 ctrl
->value
= dev
->options
.saturation
;
821 ctrl
->value
= dev
->options
.hue
;
823 case V4L2_CID_SHARPNESS
:
824 ctrl
->value
= dev
->options
.sharpness
;
832 static int vidioc_s_ctrl(struct file
*file
, void *private_data
,
833 struct v4l2_control
*ctrl
)
835 struct hdpvr_fh
*fh
= file
->private_data
;
836 struct hdpvr_device
*dev
= fh
->dev
;
840 case V4L2_CID_BRIGHTNESS
:
841 retval
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->value
);
843 dev
->options
.brightness
= ctrl
->value
;
845 case V4L2_CID_CONTRAST
:
846 retval
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->value
);
848 dev
->options
.contrast
= ctrl
->value
;
850 case V4L2_CID_SATURATION
:
851 retval
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->value
);
853 dev
->options
.saturation
= ctrl
->value
;
856 retval
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->value
);
858 dev
->options
.hue
= ctrl
->value
;
860 case V4L2_CID_SHARPNESS
:
861 retval
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->value
);
863 dev
->options
.sharpness
= ctrl
->value
;
873 static int hdpvr_get_ctrl(struct hdpvr_options
*opt
,
874 struct v4l2_ext_control
*ctrl
)
877 case V4L2_CID_MPEG_AUDIO_ENCODING
:
878 ctrl
->value
= opt
->audio_codec
;
880 case V4L2_CID_MPEG_VIDEO_ENCODING
:
881 ctrl
->value
= V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
;
883 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
884 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
886 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
887 ctrl
->value
= opt
->bitrate_mode
== HDPVR_CONSTANT
888 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
889 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
;
891 case V4L2_CID_MPEG_VIDEO_BITRATE
:
892 ctrl
->value
= opt
->bitrate
* 100000;
894 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
895 ctrl
->value
= opt
->peak_bitrate
* 100000;
897 case V4L2_CID_MPEG_STREAM_TYPE
:
898 ctrl
->value
= V4L2_MPEG_STREAM_TYPE_MPEG2_TS
;
906 static int vidioc_g_ext_ctrls(struct file
*file
, void *priv
,
907 struct v4l2_ext_controls
*ctrls
)
909 struct hdpvr_fh
*fh
= file
->private_data
;
910 struct hdpvr_device
*dev
= fh
->dev
;
913 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
914 for (i
= 0; i
< ctrls
->count
; i
++) {
915 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
917 err
= hdpvr_get_ctrl(&dev
->options
, ctrl
);
919 ctrls
->error_idx
= i
;
931 static int hdpvr_try_ctrl(struct v4l2_ext_control
*ctrl
, int ac3
)
936 case V4L2_CID_MPEG_AUDIO_ENCODING
:
937 if (ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AAC
||
938 (ac3
&& ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AC3
))
941 case V4L2_CID_MPEG_VIDEO_ENCODING
:
942 if (ctrl
->value
== V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
)
945 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
946 /* if (ctrl->value == 0 || ctrl->value == 128) */
949 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
950 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
||
951 ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
)
954 case V4L2_CID_MPEG_VIDEO_BITRATE
:
956 uint bitrate
= ctrl
->value
/ 100000;
957 if (bitrate
>= 10 && bitrate
<= 135)
961 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
963 uint peak_bitrate
= ctrl
->value
/ 100000;
964 if (peak_bitrate
>= 10 && peak_bitrate
<= 202)
968 case V4L2_CID_MPEG_STREAM_TYPE
:
969 if (ctrl
->value
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
)
978 static int vidioc_try_ext_ctrls(struct file
*file
, void *priv
,
979 struct v4l2_ext_controls
*ctrls
)
981 struct hdpvr_fh
*fh
= file
->private_data
;
982 struct hdpvr_device
*dev
= fh
->dev
;
985 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
986 for (i
= 0; i
< ctrls
->count
; i
++) {
987 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
989 err
= hdpvr_try_ctrl(ctrl
,
990 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
992 ctrls
->error_idx
= i
;
1003 static int hdpvr_set_ctrl(struct hdpvr_device
*dev
,
1004 struct v4l2_ext_control
*ctrl
)
1006 struct hdpvr_options
*opt
= &dev
->options
;
1010 case V4L2_CID_MPEG_AUDIO_ENCODING
:
1011 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
1012 opt
->audio_codec
= ctrl
->value
;
1013 ret
= hdpvr_set_audio(dev
, opt
->audio_input
,
1017 case V4L2_CID_MPEG_VIDEO_ENCODING
:
1019 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1020 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1021 /* opt->gop_mode |= 0x2; */
1022 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1023 /* opt->gop_mode); */
1025 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1026 /* opt->gop_mode &= ~0x2; */
1027 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1028 /* opt->gop_mode); */
1031 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
1032 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
&&
1033 opt
->bitrate_mode
!= HDPVR_CONSTANT
) {
1034 opt
->bitrate_mode
= HDPVR_CONSTANT
;
1035 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1038 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
1039 opt
->bitrate_mode
== HDPVR_CONSTANT
) {
1040 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
1041 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1045 case V4L2_CID_MPEG_VIDEO_BITRATE
: {
1046 uint bitrate
= ctrl
->value
/ 100000;
1048 opt
->bitrate
= bitrate
;
1049 if (bitrate
>= opt
->peak_bitrate
)
1050 opt
->peak_bitrate
= bitrate
+1;
1052 hdpvr_set_bitrate(dev
);
1055 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
: {
1056 uint peak_bitrate
= ctrl
->value
/ 100000;
1058 if (opt
->bitrate_mode
== HDPVR_CONSTANT
)
1061 if (opt
->bitrate
< peak_bitrate
) {
1062 opt
->peak_bitrate
= peak_bitrate
;
1063 hdpvr_set_bitrate(dev
);
1068 case V4L2_CID_MPEG_STREAM_TYPE
:
1076 static int vidioc_s_ext_ctrls(struct file
*file
, void *priv
,
1077 struct v4l2_ext_controls
*ctrls
)
1079 struct hdpvr_fh
*fh
= file
->private_data
;
1080 struct hdpvr_device
*dev
= fh
->dev
;
1083 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
1084 for (i
= 0; i
< ctrls
->count
; i
++) {
1085 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
1087 err
= hdpvr_try_ctrl(ctrl
,
1088 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
1090 ctrls
->error_idx
= i
;
1093 err
= hdpvr_set_ctrl(dev
, ctrl
);
1095 ctrls
->error_idx
= i
;
1106 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
1107 struct v4l2_fmtdesc
*f
)
1110 if (f
->index
!= 0 || f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1113 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1114 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
1115 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
1120 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *private_data
,
1121 struct v4l2_format
*f
)
1123 struct hdpvr_fh
*fh
= file
->private_data
;
1124 struct hdpvr_device
*dev
= fh
->dev
;
1125 struct hdpvr_video_info
*vid_info
;
1130 vid_info
= get_video_info(dev
);
1134 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1135 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1136 f
->fmt
.pix
.width
= vid_info
->width
;
1137 f
->fmt
.pix
.height
= vid_info
->height
;
1138 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1139 f
->fmt
.pix
.colorspace
= 0;
1140 f
->fmt
.pix
.bytesperline
= 0;
1141 f
->fmt
.pix
.field
= V4L2_FIELD_ANY
;
1147 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1148 struct v4l2_encoder_cmd
*a
)
1150 struct hdpvr_fh
*fh
= filp
->private_data
;
1151 struct hdpvr_device
*dev
= fh
->dev
;
1154 mutex_lock(&dev
->io_mutex
);
1156 memset(&a
->raw
, 0, sizeof(a
->raw
));
1158 case V4L2_ENC_CMD_START
:
1160 res
= hdpvr_start_streaming(dev
);
1162 case V4L2_ENC_CMD_STOP
:
1163 res
= hdpvr_stop_streaming(dev
);
1166 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1167 "Unsupported encoder cmd %d\n", a
->cmd
);
1170 mutex_unlock(&dev
->io_mutex
);
1174 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1175 struct v4l2_encoder_cmd
*a
)
1178 case V4L2_ENC_CMD_START
:
1179 case V4L2_ENC_CMD_STOP
:
1186 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1187 .vidioc_querycap
= vidioc_querycap
,
1188 .vidioc_s_std
= vidioc_s_std
,
1189 .vidioc_enum_input
= vidioc_enum_input
,
1190 .vidioc_g_input
= vidioc_g_input
,
1191 .vidioc_s_input
= vidioc_s_input
,
1192 .vidioc_enumaudio
= vidioc_enumaudio
,
1193 .vidioc_g_audio
= vidioc_g_audio
,
1194 .vidioc_s_audio
= vidioc_s_audio
,
1195 .vidioc_queryctrl
= vidioc_queryctrl
,
1196 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1197 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1198 .vidioc_g_ext_ctrls
= vidioc_g_ext_ctrls
,
1199 .vidioc_s_ext_ctrls
= vidioc_s_ext_ctrls
,
1200 .vidioc_try_ext_ctrls
= vidioc_try_ext_ctrls
,
1201 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1202 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1203 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1204 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1207 static void hdpvr_device_release(struct video_device
*vdev
)
1209 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1214 static const struct video_device hdpvr_video_template
= {
1215 /* .type = VFL_TYPE_GRABBER, */
1216 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1217 .fops
= &hdpvr_fops
,
1218 .release
= hdpvr_device_release
,
1219 .ioctl_ops
= &hdpvr_ioctl_ops
,
1221 V4L2_STD_NTSC
| V4L2_STD_SECAM
| V4L2_STD_PAL_B
|
1222 V4L2_STD_PAL_G
| V4L2_STD_PAL_H
| V4L2_STD_PAL_I
|
1223 V4L2_STD_PAL_D
| V4L2_STD_PAL_M
| V4L2_STD_PAL_N
|
1225 .current_norm
= V4L2_STD_NTSC
| V4L2_STD_PAL_M
|
1229 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1232 /* setup and register video device */
1233 dev
->video_dev
= video_device_alloc();
1234 if (!dev
->video_dev
) {
1235 v4l2_err(&dev
->v4l2_dev
, "video_device_alloc() failed\n");
1239 *(dev
->video_dev
) = hdpvr_video_template
;
1240 strcpy(dev
->video_dev
->name
, "Hauppauge HD PVR");
1241 dev
->video_dev
->parent
= parent
;
1242 video_set_drvdata(dev
->video_dev
, dev
);
1244 if (video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
)) {
1245 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");