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_free_coherent(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_alloc_coherent(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
->urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
161 buf
->status
= BUFSTAT_AVAILABLE
;
162 list_add_tail(&buf
->buff_list
, &dev
->free_buff_list
);
170 hdpvr_free_buffers(dev
);
174 static int hdpvr_submit_buffers(struct hdpvr_device
*dev
)
176 struct hdpvr_buffer
*buf
;
178 int ret
= 0, err_count
= 0;
180 mutex_lock(&dev
->io_mutex
);
182 while (dev
->status
== STATUS_STREAMING
&&
183 !list_empty(&dev
->free_buff_list
)) {
185 buf
= list_entry(dev
->free_buff_list
.next
, struct hdpvr_buffer
,
187 if (buf
->status
!= BUFSTAT_AVAILABLE
) {
188 v4l2_err(&dev
->v4l2_dev
,
189 "buffer not marked as available\n");
196 urb
->actual_length
= 0;
197 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
199 v4l2_err(&dev
->v4l2_dev
,
200 "usb_submit_urb in %s returned %d\n",
206 buf
->status
= BUFSTAT_INPROGRESS
;
207 list_move_tail(&buf
->buff_list
, &dev
->rec_buff_list
);
210 print_buffer_status();
211 mutex_unlock(&dev
->io_mutex
);
215 static struct hdpvr_buffer
*hdpvr_get_next_buffer(struct hdpvr_device
*dev
)
217 struct hdpvr_buffer
*buf
;
219 mutex_lock(&dev
->io_mutex
);
221 if (list_empty(&dev
->rec_buff_list
)) {
222 mutex_unlock(&dev
->io_mutex
);
226 buf
= list_entry(dev
->rec_buff_list
.next
, struct hdpvr_buffer
,
228 mutex_unlock(&dev
->io_mutex
);
233 static void hdpvr_transmit_buffers(struct work_struct
*work
)
235 struct hdpvr_device
*dev
= container_of(work
, struct hdpvr_device
,
238 while (dev
->status
== STATUS_STREAMING
) {
240 if (hdpvr_submit_buffers(dev
)) {
241 v4l2_err(&dev
->v4l2_dev
, "couldn't submit buffers\n");
244 if (wait_event_interruptible(dev
->wait_buffer
,
245 !list_empty(&dev
->free_buff_list
) ||
246 dev
->status
!= STATUS_STREAMING
))
250 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
251 "transmit worker exited\n");
254 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
255 "transmit buffers errored\n");
256 dev
->status
= STATUS_ERROR
;
259 /* function expects dev->io_mutex to be hold by caller */
260 static int hdpvr_start_streaming(struct hdpvr_device
*dev
)
263 struct hdpvr_video_info
*vidinf
;
265 if (dev
->status
== STATUS_STREAMING
)
267 else if (dev
->status
!= STATUS_IDLE
)
270 vidinf
= get_video_info(dev
);
273 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
274 "video signal: %dx%d@%dhz\n", vidinf
->width
,
275 vidinf
->height
, vidinf
->fps
);
278 /* start streaming 2 request */
279 ret
= usb_control_msg(dev
->udev
,
280 usb_sndctrlpipe(dev
->udev
, 0),
281 0xb8, 0x38, 0x1, 0, NULL
, 0, 8000);
282 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
283 "encoder start control request returned %d\n", ret
);
285 hdpvr_config_call(dev
, CTRL_START_STREAMING_VALUE
, 0x00);
287 INIT_WORK(&dev
->worker
, hdpvr_transmit_buffers
);
288 queue_work(dev
->workqueue
, &dev
->worker
);
290 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
291 "streaming started\n");
292 dev
->status
= STATUS_STREAMING
;
297 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
298 "no video signal at input %d\n", dev
->options
.video_input
);
303 /* function expects dev->io_mutex to be hold by caller */
304 static int hdpvr_stop_streaming(struct hdpvr_device
*dev
)
310 if (dev
->status
== STATUS_IDLE
)
312 else if (dev
->status
!= STATUS_STREAMING
)
315 buf
= kmalloc(dev
->bulk_in_size
, GFP_KERNEL
);
317 v4l2_err(&dev
->v4l2_dev
, "failed to allocate temporary buffer "
318 "for emptying the internal device buffer. "
319 "Next capture start will be slow\n");
321 dev
->status
= STATUS_SHUTTING_DOWN
;
322 hdpvr_config_call(dev
, CTRL_STOP_STREAMING_VALUE
, 0x00);
323 mutex_unlock(&dev
->io_mutex
);
325 wake_up_interruptible(&dev
->wait_buffer
);
328 flush_workqueue(dev
->workqueue
);
330 mutex_lock(&dev
->io_mutex
);
331 /* kill the still outstanding urbs */
332 hdpvr_cancel_queue(dev
);
334 /* emptying the device buffer beforeshutting it down */
335 while (buf
&& ++c
< 500 &&
336 !usb_bulk_msg(dev
->udev
,
337 usb_rcvbulkpipe(dev
->udev
,
338 dev
->bulk_in_endpointAddr
),
339 buf
, dev
->bulk_in_size
, &actual_length
,
343 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
344 "%2d: got %d bytes\n", c
, actual_length
);
347 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
348 "used %d urbs to empty device buffers\n", c
-1);
351 dev
->status
= STATUS_IDLE
;
357 /*=======================================================================*/
359 * video 4 linux 2 file operations
362 static int hdpvr_open(struct file
*file
)
364 struct hdpvr_device
*dev
;
366 int retval
= -ENOMEM
;
368 dev
= (struct hdpvr_device
*)video_get_drvdata(video_devdata(file
));
370 pr_err("open failing with with ENODEV\n");
375 fh
= kzalloc(sizeof(struct hdpvr_fh
), GFP_KERNEL
);
377 v4l2_err(&dev
->v4l2_dev
, "Out of memory\n");
380 /* lock the device to allow correctly handling errors
382 mutex_lock(&dev
->io_mutex
);
384 mutex_unlock(&dev
->io_mutex
);
388 /* save our object in the file's private structure */
389 file
->private_data
= fh
;
396 static int hdpvr_release(struct file
*file
)
398 struct hdpvr_fh
*fh
= file
->private_data
;
399 struct hdpvr_device
*dev
= fh
->dev
;
404 mutex_lock(&dev
->io_mutex
);
405 if (!(--dev
->open_count
) && dev
->status
== STATUS_STREAMING
)
406 hdpvr_stop_streaming(dev
);
408 mutex_unlock(&dev
->io_mutex
);
415 * will allocate buffers when called for the first time
417 static ssize_t
hdpvr_read(struct file
*file
, char __user
*buffer
, size_t count
,
420 struct hdpvr_fh
*fh
= file
->private_data
;
421 struct hdpvr_device
*dev
= fh
->dev
;
422 struct hdpvr_buffer
*buf
= NULL
;
424 unsigned int ret
= 0;
433 mutex_lock(&dev
->io_mutex
);
434 if (dev
->status
== STATUS_IDLE
) {
435 if (hdpvr_start_streaming(dev
)) {
436 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
437 "start_streaming failed\n");
440 dev
->status
= STATUS_IDLE
;
441 mutex_unlock(&dev
->io_mutex
);
444 print_buffer_status();
446 mutex_unlock(&dev
->io_mutex
);
448 /* wait for the first buffer */
449 if (!(file
->f_flags
& O_NONBLOCK
)) {
450 if (wait_event_interruptible(dev
->wait_data
,
451 hdpvr_get_next_buffer(dev
)))
455 buf
= hdpvr_get_next_buffer(dev
);
457 while (count
> 0 && buf
) {
459 if (buf
->status
!= BUFSTAT_READY
&&
460 dev
->status
!= STATUS_DISCONNECTED
) {
461 /* return nonblocking */
462 if (file
->f_flags
& O_NONBLOCK
) {
468 if (wait_event_interruptible(dev
->wait_data
,
469 buf
->status
== BUFSTAT_READY
)) {
475 if (buf
->status
!= BUFSTAT_READY
)
478 /* set remaining bytes to copy */
480 rem
= urb
->actual_length
- buf
->pos
;
481 cnt
= rem
> count
? count
: rem
;
483 if (copy_to_user(buffer
, urb
->transfer_buffer
+ buf
->pos
,
485 v4l2_err(&dev
->v4l2_dev
, "read: copy_to_user failed\n");
496 /* finished, take next buffer */
497 if (buf
->pos
== urb
->actual_length
) {
498 mutex_lock(&dev
->io_mutex
);
500 buf
->status
= BUFSTAT_AVAILABLE
;
502 list_move_tail(&buf
->buff_list
, &dev
->free_buff_list
);
504 print_buffer_status();
506 mutex_unlock(&dev
->io_mutex
);
508 wake_up_interruptible(&dev
->wait_buffer
);
510 buf
= hdpvr_get_next_buffer(dev
);
519 static unsigned int hdpvr_poll(struct file
*filp
, poll_table
*wait
)
521 struct hdpvr_buffer
*buf
= NULL
;
522 struct hdpvr_fh
*fh
= filp
->private_data
;
523 struct hdpvr_device
*dev
= fh
->dev
;
524 unsigned int mask
= 0;
526 mutex_lock(&dev
->io_mutex
);
528 if (!video_is_registered(dev
->video_dev
)) {
529 mutex_unlock(&dev
->io_mutex
);
533 if (dev
->status
== STATUS_IDLE
) {
534 if (hdpvr_start_streaming(dev
)) {
535 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
536 "start_streaming failed\n");
537 dev
->status
= STATUS_IDLE
;
540 print_buffer_status();
542 mutex_unlock(&dev
->io_mutex
);
544 buf
= hdpvr_get_next_buffer(dev
);
545 /* only wait if no data is available */
546 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
547 poll_wait(filp
, &dev
->wait_data
, wait
);
548 buf
= hdpvr_get_next_buffer(dev
);
550 if (buf
&& buf
->status
== BUFSTAT_READY
)
551 mask
|= POLLIN
| POLLRDNORM
;
557 static const struct v4l2_file_operations hdpvr_fops
= {
558 .owner
= THIS_MODULE
,
560 .release
= hdpvr_release
,
563 .unlocked_ioctl
= video_ioctl2
,
566 /*=======================================================================*/
568 * V4L2 ioctl handling
571 static int vidioc_querycap(struct file
*file
, void *priv
,
572 struct v4l2_capability
*cap
)
574 struct hdpvr_device
*dev
= video_drvdata(file
);
576 strcpy(cap
->driver
, "hdpvr");
577 strcpy(cap
->card
, "Hauppauge HD PVR");
578 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
579 cap
->version
= HDPVR_VERSION
;
580 cap
->capabilities
= V4L2_CAP_VIDEO_CAPTURE
|
586 static int vidioc_s_std(struct file
*file
, void *private_data
,
589 struct hdpvr_fh
*fh
= file
->private_data
;
590 struct hdpvr_device
*dev
= fh
->dev
;
593 if (*std
& (V4L2_STD_NTSC
| V4L2_STD_PAL_60
))
596 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
599 static const char *iname
[] = {
600 [HDPVR_COMPONENT
] = "Component",
601 [HDPVR_SVIDEO
] = "S-Video",
602 [HDPVR_COMPOSITE
] = "Composite",
605 static int vidioc_enum_input(struct file
*file
, void *priv
,
606 struct v4l2_input
*i
)
608 struct hdpvr_fh
*fh
= file
->private_data
;
609 struct hdpvr_device
*dev
= fh
->dev
;
613 if (n
>= HDPVR_VIDEO_INPUTS
)
616 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
618 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
619 i
->name
[sizeof(i
->name
) - 1] = '\0';
621 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
623 i
->std
= dev
->video_dev
->tvnorms
;
628 static int vidioc_s_input(struct file
*file
, void *private_data
,
631 struct hdpvr_fh
*fh
= file
->private_data
;
632 struct hdpvr_device
*dev
= fh
->dev
;
635 if (index
>= HDPVR_VIDEO_INPUTS
)
638 if (dev
->status
!= STATUS_IDLE
)
641 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
643 dev
->options
.video_input
= index
;
648 static int vidioc_g_input(struct file
*file
, void *private_data
,
651 struct hdpvr_fh
*fh
= file
->private_data
;
652 struct hdpvr_device
*dev
= fh
->dev
;
654 *index
= dev
->options
.video_input
;
659 static const char *audio_iname
[] = {
660 [HDPVR_RCA_FRONT
] = "RCA front",
661 [HDPVR_RCA_BACK
] = "RCA back",
662 [HDPVR_SPDIF
] = "SPDIF",
665 static int vidioc_enumaudio(struct file
*file
, void *priv
,
666 struct v4l2_audio
*audio
)
671 if (n
>= HDPVR_AUDIO_INPUTS
)
674 audio
->capability
= V4L2_AUDCAP_STEREO
;
676 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
677 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
682 static int vidioc_s_audio(struct file
*file
, void *private_data
,
683 struct v4l2_audio
*audio
)
685 struct hdpvr_fh
*fh
= file
->private_data
;
686 struct hdpvr_device
*dev
= fh
->dev
;
689 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
692 if (dev
->status
!= STATUS_IDLE
)
695 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
697 dev
->options
.audio_input
= audio
->index
;
702 static int vidioc_g_audio(struct file
*file
, void *private_data
,
703 struct v4l2_audio
*audio
)
705 struct hdpvr_fh
*fh
= file
->private_data
;
706 struct hdpvr_device
*dev
= fh
->dev
;
708 audio
->index
= dev
->options
.audio_input
;
709 audio
->capability
= V4L2_AUDCAP_STEREO
;
710 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
711 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
715 static const s32 supported_v4l2_ctrls
[] = {
721 V4L2_CID_MPEG_AUDIO_ENCODING
,
722 V4L2_CID_MPEG_VIDEO_ENCODING
,
723 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
724 V4L2_CID_MPEG_VIDEO_BITRATE
,
725 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
728 static int fill_queryctrl(struct hdpvr_options
*opt
, struct v4l2_queryctrl
*qc
,
734 case V4L2_CID_BRIGHTNESS
:
735 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x86);
736 case V4L2_CID_CONTRAST
:
737 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
738 case V4L2_CID_SATURATION
:
739 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
741 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
742 case V4L2_CID_SHARPNESS
:
743 return v4l2_ctrl_query_fill(qc
, 0x0, 0xff, 1, 0x80);
744 case V4L2_CID_MPEG_AUDIO_ENCODING
:
745 return v4l2_ctrl_query_fill(
746 qc
, V4L2_MPEG_AUDIO_ENCODING_AAC
,
747 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
748 : V4L2_MPEG_AUDIO_ENCODING_AAC
,
749 1, V4L2_MPEG_AUDIO_ENCODING_AAC
);
750 case V4L2_CID_MPEG_VIDEO_ENCODING
:
751 return v4l2_ctrl_query_fill(
752 qc
, V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
,
753 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 1,
754 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
756 /* case V4L2_CID_MPEG_VIDEO_? maybe keyframe interval: */
757 /* return v4l2_ctrl_query_fill(qc, 0, 128, 128, 0); */
758 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
759 return v4l2_ctrl_query_fill(
760 qc
, V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
,
761 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 1,
762 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
764 case V4L2_CID_MPEG_VIDEO_BITRATE
:
765 return v4l2_ctrl_query_fill(qc
, 1000000, 13500000, 100000,
767 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
768 err
= v4l2_ctrl_query_fill(qc
, 1100000, 20200000, 100000,
770 if (!err
&& opt
->bitrate_mode
== HDPVR_CONSTANT
)
771 qc
->flags
|= V4L2_CTRL_FLAG_INACTIVE
;
778 static int vidioc_queryctrl(struct file
*file
, void *private_data
,
779 struct v4l2_queryctrl
*qc
)
781 struct hdpvr_fh
*fh
= file
->private_data
;
782 struct hdpvr_device
*dev
= fh
->dev
;
786 memset(qc
, 0, sizeof(*qc
));
788 next
= !!(id
& V4L2_CTRL_FLAG_NEXT_CTRL
);
789 qc
->id
= id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
;
791 for (i
= 0; i
< ARRAY_SIZE(supported_v4l2_ctrls
); i
++) {
793 if (qc
->id
< supported_v4l2_ctrls
[i
])
794 qc
->id
= supported_v4l2_ctrls
[i
];
799 if (qc
->id
== supported_v4l2_ctrls
[i
])
800 return fill_queryctrl(&dev
->options
, qc
,
801 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
803 if (qc
->id
< supported_v4l2_ctrls
[i
])
810 static int vidioc_g_ctrl(struct file
*file
, void *private_data
,
811 struct v4l2_control
*ctrl
)
813 struct hdpvr_fh
*fh
= file
->private_data
;
814 struct hdpvr_device
*dev
= fh
->dev
;
817 case V4L2_CID_BRIGHTNESS
:
818 ctrl
->value
= dev
->options
.brightness
;
820 case V4L2_CID_CONTRAST
:
821 ctrl
->value
= dev
->options
.contrast
;
823 case V4L2_CID_SATURATION
:
824 ctrl
->value
= dev
->options
.saturation
;
827 ctrl
->value
= dev
->options
.hue
;
829 case V4L2_CID_SHARPNESS
:
830 ctrl
->value
= dev
->options
.sharpness
;
838 static int vidioc_s_ctrl(struct file
*file
, void *private_data
,
839 struct v4l2_control
*ctrl
)
841 struct hdpvr_fh
*fh
= file
->private_data
;
842 struct hdpvr_device
*dev
= fh
->dev
;
846 case V4L2_CID_BRIGHTNESS
:
847 retval
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->value
);
849 dev
->options
.brightness
= ctrl
->value
;
851 case V4L2_CID_CONTRAST
:
852 retval
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->value
);
854 dev
->options
.contrast
= ctrl
->value
;
856 case V4L2_CID_SATURATION
:
857 retval
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->value
);
859 dev
->options
.saturation
= ctrl
->value
;
862 retval
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->value
);
864 dev
->options
.hue
= ctrl
->value
;
866 case V4L2_CID_SHARPNESS
:
867 retval
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->value
);
869 dev
->options
.sharpness
= ctrl
->value
;
879 static int hdpvr_get_ctrl(struct hdpvr_options
*opt
,
880 struct v4l2_ext_control
*ctrl
)
883 case V4L2_CID_MPEG_AUDIO_ENCODING
:
884 ctrl
->value
= opt
->audio_codec
;
886 case V4L2_CID_MPEG_VIDEO_ENCODING
:
887 ctrl
->value
= V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
;
889 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
890 /* ctrl->value = (opt->gop_mode & 0x2) ? 0 : 128; */
892 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
893 ctrl
->value
= opt
->bitrate_mode
== HDPVR_CONSTANT
894 ? V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
895 : V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
;
897 case V4L2_CID_MPEG_VIDEO_BITRATE
:
898 ctrl
->value
= opt
->bitrate
* 100000;
900 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
901 ctrl
->value
= opt
->peak_bitrate
* 100000;
903 case V4L2_CID_MPEG_STREAM_TYPE
:
904 ctrl
->value
= V4L2_MPEG_STREAM_TYPE_MPEG2_TS
;
912 static int vidioc_g_ext_ctrls(struct file
*file
, void *priv
,
913 struct v4l2_ext_controls
*ctrls
)
915 struct hdpvr_fh
*fh
= file
->private_data
;
916 struct hdpvr_device
*dev
= fh
->dev
;
919 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
920 for (i
= 0; i
< ctrls
->count
; i
++) {
921 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
923 err
= hdpvr_get_ctrl(&dev
->options
, ctrl
);
925 ctrls
->error_idx
= i
;
937 static int hdpvr_try_ctrl(struct v4l2_ext_control
*ctrl
, int ac3
)
942 case V4L2_CID_MPEG_AUDIO_ENCODING
:
943 if (ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AAC
||
944 (ac3
&& ctrl
->value
== V4L2_MPEG_AUDIO_ENCODING_AC3
))
947 case V4L2_CID_MPEG_VIDEO_ENCODING
:
948 if (ctrl
->value
== V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
)
951 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
952 /* if (ctrl->value == 0 || ctrl->value == 128) */
955 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
956 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
||
957 ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
)
960 case V4L2_CID_MPEG_VIDEO_BITRATE
:
962 uint bitrate
= ctrl
->value
/ 100000;
963 if (bitrate
>= 10 && bitrate
<= 135)
967 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
:
969 uint peak_bitrate
= ctrl
->value
/ 100000;
970 if (peak_bitrate
>= 10 && peak_bitrate
<= 202)
974 case V4L2_CID_MPEG_STREAM_TYPE
:
975 if (ctrl
->value
== V4L2_MPEG_STREAM_TYPE_MPEG2_TS
)
984 static int vidioc_try_ext_ctrls(struct file
*file
, void *priv
,
985 struct v4l2_ext_controls
*ctrls
)
987 struct hdpvr_fh
*fh
= file
->private_data
;
988 struct hdpvr_device
*dev
= fh
->dev
;
991 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
992 for (i
= 0; i
< ctrls
->count
; i
++) {
993 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
995 err
= hdpvr_try_ctrl(ctrl
,
996 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
998 ctrls
->error_idx
= i
;
1009 static int hdpvr_set_ctrl(struct hdpvr_device
*dev
,
1010 struct v4l2_ext_control
*ctrl
)
1012 struct hdpvr_options
*opt
= &dev
->options
;
1016 case V4L2_CID_MPEG_AUDIO_ENCODING
:
1017 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
1018 opt
->audio_codec
= ctrl
->value
;
1019 ret
= hdpvr_set_audio(dev
, opt
->audio_input
,
1023 case V4L2_CID_MPEG_VIDEO_ENCODING
:
1025 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
1026 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
1027 /* opt->gop_mode |= 0x2; */
1028 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1029 /* opt->gop_mode); */
1031 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
1032 /* opt->gop_mode &= ~0x2; */
1033 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
1034 /* opt->gop_mode); */
1037 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
1038 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
&&
1039 opt
->bitrate_mode
!= HDPVR_CONSTANT
) {
1040 opt
->bitrate_mode
= HDPVR_CONSTANT
;
1041 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1044 if (ctrl
->value
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
1045 opt
->bitrate_mode
== HDPVR_CONSTANT
) {
1046 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
1047 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
1051 case V4L2_CID_MPEG_VIDEO_BITRATE
: {
1052 uint bitrate
= ctrl
->value
/ 100000;
1054 opt
->bitrate
= bitrate
;
1055 if (bitrate
>= opt
->peak_bitrate
)
1056 opt
->peak_bitrate
= bitrate
+1;
1058 hdpvr_set_bitrate(dev
);
1061 case V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
: {
1062 uint peak_bitrate
= ctrl
->value
/ 100000;
1064 if (opt
->bitrate_mode
== HDPVR_CONSTANT
)
1067 if (opt
->bitrate
< peak_bitrate
) {
1068 opt
->peak_bitrate
= peak_bitrate
;
1069 hdpvr_set_bitrate(dev
);
1074 case V4L2_CID_MPEG_STREAM_TYPE
:
1082 static int vidioc_s_ext_ctrls(struct file
*file
, void *priv
,
1083 struct v4l2_ext_controls
*ctrls
)
1085 struct hdpvr_fh
*fh
= file
->private_data
;
1086 struct hdpvr_device
*dev
= fh
->dev
;
1089 if (ctrls
->ctrl_class
== V4L2_CTRL_CLASS_MPEG
) {
1090 for (i
= 0; i
< ctrls
->count
; i
++) {
1091 struct v4l2_ext_control
*ctrl
= ctrls
->controls
+ i
;
1093 err
= hdpvr_try_ctrl(ctrl
,
1094 dev
->flags
& HDPVR_FLAG_AC3_CAP
);
1096 ctrls
->error_idx
= i
;
1099 err
= hdpvr_set_ctrl(dev
, ctrl
);
1101 ctrls
->error_idx
= i
;
1112 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
1113 struct v4l2_fmtdesc
*f
)
1116 if (f
->index
!= 0 || f
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1119 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
1120 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
1121 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
1126 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *private_data
,
1127 struct v4l2_format
*f
)
1129 struct hdpvr_fh
*fh
= file
->private_data
;
1130 struct hdpvr_device
*dev
= fh
->dev
;
1131 struct hdpvr_video_info
*vid_info
;
1136 vid_info
= get_video_info(dev
);
1140 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1141 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1142 f
->fmt
.pix
.width
= vid_info
->width
;
1143 f
->fmt
.pix
.height
= vid_info
->height
;
1144 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1145 f
->fmt
.pix
.colorspace
= 0;
1146 f
->fmt
.pix
.bytesperline
= 0;
1147 f
->fmt
.pix
.field
= V4L2_FIELD_ANY
;
1153 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1154 struct v4l2_encoder_cmd
*a
)
1156 struct hdpvr_fh
*fh
= filp
->private_data
;
1157 struct hdpvr_device
*dev
= fh
->dev
;
1160 mutex_lock(&dev
->io_mutex
);
1162 memset(&a
->raw
, 0, sizeof(a
->raw
));
1164 case V4L2_ENC_CMD_START
:
1166 res
= hdpvr_start_streaming(dev
);
1168 case V4L2_ENC_CMD_STOP
:
1169 res
= hdpvr_stop_streaming(dev
);
1172 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1173 "Unsupported encoder cmd %d\n", a
->cmd
);
1176 mutex_unlock(&dev
->io_mutex
);
1180 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1181 struct v4l2_encoder_cmd
*a
)
1184 case V4L2_ENC_CMD_START
:
1185 case V4L2_ENC_CMD_STOP
:
1192 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1193 .vidioc_querycap
= vidioc_querycap
,
1194 .vidioc_s_std
= vidioc_s_std
,
1195 .vidioc_enum_input
= vidioc_enum_input
,
1196 .vidioc_g_input
= vidioc_g_input
,
1197 .vidioc_s_input
= vidioc_s_input
,
1198 .vidioc_enumaudio
= vidioc_enumaudio
,
1199 .vidioc_g_audio
= vidioc_g_audio
,
1200 .vidioc_s_audio
= vidioc_s_audio
,
1201 .vidioc_queryctrl
= vidioc_queryctrl
,
1202 .vidioc_g_ctrl
= vidioc_g_ctrl
,
1203 .vidioc_s_ctrl
= vidioc_s_ctrl
,
1204 .vidioc_g_ext_ctrls
= vidioc_g_ext_ctrls
,
1205 .vidioc_s_ext_ctrls
= vidioc_s_ext_ctrls
,
1206 .vidioc_try_ext_ctrls
= vidioc_try_ext_ctrls
,
1207 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1208 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1209 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1210 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1213 static void hdpvr_device_release(struct video_device
*vdev
)
1215 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1218 mutex_lock(&dev
->io_mutex
);
1219 destroy_workqueue(dev
->workqueue
);
1220 mutex_unlock(&dev
->io_mutex
);
1222 v4l2_device_unregister(&dev
->v4l2_dev
);
1224 /* deregister I2C adapter */
1226 mutex_lock(&dev
->i2c_mutex
);
1227 if (dev
->i2c_adapter
)
1228 i2c_del_adapter(dev
->i2c_adapter
);
1229 kfree(dev
->i2c_adapter
);
1230 dev
->i2c_adapter
= NULL
;
1231 mutex_unlock(&dev
->i2c_mutex
);
1232 #endif /* CONFIG_I2C */
1234 kfree(dev
->usbc_buf
);
1238 static const struct video_device hdpvr_video_template
= {
1239 /* .type = VFL_TYPE_GRABBER, */
1240 /* .type2 = VID_TYPE_CAPTURE | VID_TYPE_MPEG_ENCODER, */
1241 .fops
= &hdpvr_fops
,
1242 .release
= hdpvr_device_release
,
1243 .ioctl_ops
= &hdpvr_ioctl_ops
,
1245 V4L2_STD_NTSC
| V4L2_STD_SECAM
| V4L2_STD_PAL_B
|
1246 V4L2_STD_PAL_G
| V4L2_STD_PAL_H
| V4L2_STD_PAL_I
|
1247 V4L2_STD_PAL_D
| V4L2_STD_PAL_M
| V4L2_STD_PAL_N
|
1249 .current_norm
= V4L2_STD_NTSC
| V4L2_STD_PAL_M
|
1253 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1256 /* setup and register video device */
1257 dev
->video_dev
= video_device_alloc();
1258 if (!dev
->video_dev
) {
1259 v4l2_err(&dev
->v4l2_dev
, "video_device_alloc() failed\n");
1263 *(dev
->video_dev
) = hdpvr_video_template
;
1264 strcpy(dev
->video_dev
->name
, "Hauppauge HD PVR");
1265 dev
->video_dev
->parent
= parent
;
1266 video_set_drvdata(dev
->video_dev
, dev
);
1268 if (video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
)) {
1269 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");