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/workqueue.h>
22 #include <linux/videodev2.h>
23 #include <linux/v4l2-dv-timings.h>
24 #include <media/v4l2-dev.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-dv-timings.h>
27 #include <media/v4l2-ioctl.h>
28 #include <media/v4l2-event.h>
31 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
33 #define print_buffer_status() { \
34 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
35 "%s:%d buffer stat: %d free, %d proc\n", \
37 list_size(&dev->free_buff_list), \
38 list_size(&dev->rec_buff_list)); }
40 static const struct v4l2_dv_timings hdpvr_dv_timings
[] = {
41 V4L2_DV_BT_CEA_720X480I59_94
,
42 V4L2_DV_BT_CEA_720X576I50
,
43 V4L2_DV_BT_CEA_720X480P59_94
,
44 V4L2_DV_BT_CEA_720X576P50
,
45 V4L2_DV_BT_CEA_1280X720P50
,
46 V4L2_DV_BT_CEA_1280X720P60
,
47 V4L2_DV_BT_CEA_1920X1080I50
,
48 V4L2_DV_BT_CEA_1920X1080I60
,
51 /* Use 480i59 as the default timings */
52 #define HDPVR_DEF_DV_TIMINGS_IDX (0)
59 static uint
list_size(struct list_head
*list
)
61 struct list_head
*tmp
;
64 list_for_each(tmp
, list
) {
71 /*=========================================================================*/
73 static void hdpvr_read_bulk_callback(struct urb
*urb
)
75 struct hdpvr_buffer
*buf
= (struct hdpvr_buffer
*)urb
->context
;
76 struct hdpvr_device
*dev
= buf
->dev
;
78 /* marking buffer as received and wake waiting */
79 buf
->status
= BUFSTAT_READY
;
80 wake_up_interruptible(&dev
->wait_data
);
83 /*=========================================================================*/
86 /* function expects dev->io_mutex to be hold by caller */
87 int hdpvr_cancel_queue(struct hdpvr_device
*dev
)
89 struct hdpvr_buffer
*buf
;
91 list_for_each_entry(buf
, &dev
->rec_buff_list
, buff_list
) {
92 usb_kill_urb(buf
->urb
);
93 buf
->status
= BUFSTAT_AVAILABLE
;
96 list_splice_init(&dev
->rec_buff_list
, dev
->free_buff_list
.prev
);
101 static int hdpvr_free_queue(struct list_head
*q
)
103 struct list_head
*tmp
;
105 struct hdpvr_buffer
*buf
;
108 for (p
= q
->next
; p
!= q
;) {
109 buf
= list_entry(p
, struct hdpvr_buffer
, buff_list
);
112 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
113 urb
->transfer_buffer
, urb
->transfer_dma
);
124 /* function expects dev->io_mutex to be hold by caller */
125 int hdpvr_free_buffers(struct hdpvr_device
*dev
)
127 hdpvr_cancel_queue(dev
);
129 hdpvr_free_queue(&dev
->free_buff_list
);
130 hdpvr_free_queue(&dev
->rec_buff_list
);
135 /* function expects dev->io_mutex to be hold by caller */
136 int hdpvr_alloc_buffers(struct hdpvr_device
*dev
, uint count
)
139 int retval
= -ENOMEM
;
141 struct hdpvr_buffer
*buf
;
144 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
145 "allocating %u buffers\n", count
);
147 for (i
= 0; i
< count
; i
++) {
149 buf
= kzalloc(sizeof(struct hdpvr_buffer
), GFP_KERNEL
);
151 v4l2_err(&dev
->v4l2_dev
, "cannot allocate buffer\n");
156 urb
= usb_alloc_urb(0, GFP_KERNEL
);
161 mem
= usb_alloc_coherent(dev
->udev
, dev
->bulk_in_size
, GFP_KERNEL
,
164 v4l2_err(&dev
->v4l2_dev
,
165 "cannot allocate usb transfer buffer\n");
166 goto exit_urb_buffer
;
169 usb_fill_bulk_urb(buf
->urb
, dev
->udev
,
170 usb_rcvbulkpipe(dev
->udev
,
171 dev
->bulk_in_endpointAddr
),
172 mem
, dev
->bulk_in_size
,
173 hdpvr_read_bulk_callback
, buf
);
175 buf
->urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
176 buf
->status
= BUFSTAT_AVAILABLE
;
177 list_add_tail(&buf
->buff_list
, &dev
->free_buff_list
);
185 hdpvr_free_buffers(dev
);
189 static int hdpvr_submit_buffers(struct hdpvr_device
*dev
)
191 struct hdpvr_buffer
*buf
;
193 int ret
= 0, err_count
= 0;
195 mutex_lock(&dev
->io_mutex
);
197 while (dev
->status
== STATUS_STREAMING
&&
198 !list_empty(&dev
->free_buff_list
)) {
200 buf
= list_entry(dev
->free_buff_list
.next
, struct hdpvr_buffer
,
202 if (buf
->status
!= BUFSTAT_AVAILABLE
) {
203 v4l2_err(&dev
->v4l2_dev
,
204 "buffer not marked as available\n");
211 urb
->actual_length
= 0;
212 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
214 v4l2_err(&dev
->v4l2_dev
,
215 "usb_submit_urb in %s returned %d\n",
221 buf
->status
= BUFSTAT_INPROGRESS
;
222 list_move_tail(&buf
->buff_list
, &dev
->rec_buff_list
);
225 print_buffer_status();
226 mutex_unlock(&dev
->io_mutex
);
230 static struct hdpvr_buffer
*hdpvr_get_next_buffer(struct hdpvr_device
*dev
)
232 struct hdpvr_buffer
*buf
;
234 mutex_lock(&dev
->io_mutex
);
236 if (list_empty(&dev
->rec_buff_list
)) {
237 mutex_unlock(&dev
->io_mutex
);
241 buf
= list_entry(dev
->rec_buff_list
.next
, struct hdpvr_buffer
,
243 mutex_unlock(&dev
->io_mutex
);
248 static void hdpvr_transmit_buffers(struct work_struct
*work
)
250 struct hdpvr_device
*dev
= container_of(work
, struct hdpvr_device
,
253 while (dev
->status
== STATUS_STREAMING
) {
255 if (hdpvr_submit_buffers(dev
)) {
256 v4l2_err(&dev
->v4l2_dev
, "couldn't submit buffers\n");
259 if (wait_event_interruptible(dev
->wait_buffer
,
260 !list_empty(&dev
->free_buff_list
) ||
261 dev
->status
!= STATUS_STREAMING
))
265 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
266 "transmit worker exited\n");
269 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
270 "transmit buffers errored\n");
271 dev
->status
= STATUS_ERROR
;
274 /* function expects dev->io_mutex to be hold by caller */
275 static int hdpvr_start_streaming(struct hdpvr_device
*dev
)
278 struct hdpvr_video_info vidinf
;
280 if (dev
->status
== STATUS_STREAMING
)
282 if (dev
->status
!= STATUS_IDLE
)
285 ret
= get_video_info(dev
, &vidinf
);
291 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
292 "no video signal at input %d\n", dev
->options
.video_input
);
296 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
297 "video signal: %dx%d@%dhz\n", vidinf
.width
,
298 vidinf
.height
, vidinf
.fps
);
300 /* start streaming 2 request */
301 ret
= usb_control_msg(dev
->udev
,
302 usb_sndctrlpipe(dev
->udev
, 0),
303 0xb8, 0x38, 0x1, 0, NULL
, 0, 8000);
304 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
305 "encoder start control request returned %d\n", ret
);
309 ret
= hdpvr_config_call(dev
, CTRL_START_STREAMING_VALUE
, 0x00);
313 dev
->status
= STATUS_STREAMING
;
315 INIT_WORK(&dev
->worker
, hdpvr_transmit_buffers
);
316 schedule_work(&dev
->worker
);
318 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
319 "streaming started\n");
325 /* function expects dev->io_mutex to be hold by caller */
326 static int hdpvr_stop_streaming(struct hdpvr_device
*dev
)
332 if (dev
->status
== STATUS_IDLE
)
334 else if (dev
->status
!= STATUS_STREAMING
)
337 buf
= kmalloc(dev
->bulk_in_size
, GFP_KERNEL
);
339 v4l2_err(&dev
->v4l2_dev
, "failed to allocate temporary buffer "
340 "for emptying the internal device buffer. "
341 "Next capture start will be slow\n");
343 dev
->status
= STATUS_SHUTTING_DOWN
;
344 hdpvr_config_call(dev
, CTRL_STOP_STREAMING_VALUE
, 0x00);
345 mutex_unlock(&dev
->io_mutex
);
347 wake_up_interruptible(&dev
->wait_buffer
);
350 flush_work(&dev
->worker
);
352 mutex_lock(&dev
->io_mutex
);
353 /* kill the still outstanding urbs */
354 hdpvr_cancel_queue(dev
);
356 /* emptying the device buffer beforeshutting it down */
357 while (buf
&& ++c
< 500 &&
358 !usb_bulk_msg(dev
->udev
,
359 usb_rcvbulkpipe(dev
->udev
,
360 dev
->bulk_in_endpointAddr
),
361 buf
, dev
->bulk_in_size
, &actual_length
,
363 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
364 "%2d: got %d bytes\n", c
, actual_length
);
367 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
368 "used %d urbs to empty device buffers\n", c
-1);
371 dev
->status
= STATUS_IDLE
;
377 /*=======================================================================*/
379 * video 4 linux 2 file operations
382 static int hdpvr_open(struct file
*file
)
384 struct hdpvr_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
388 fh
->legacy_mode
= true;
389 v4l2_fh_init(&fh
->fh
, video_devdata(file
));
390 v4l2_fh_add(&fh
->fh
);
391 file
->private_data
= fh
;
395 static int hdpvr_release(struct file
*file
)
397 struct hdpvr_device
*dev
= video_drvdata(file
);
399 mutex_lock(&dev
->io_mutex
);
400 if (file
->private_data
== dev
->owner
) {
401 hdpvr_stop_streaming(dev
);
404 mutex_unlock(&dev
->io_mutex
);
406 return v4l2_fh_release(file
);
411 * will allocate buffers when called for the first time
413 static ssize_t
hdpvr_read(struct file
*file
, char __user
*buffer
, size_t count
,
416 struct hdpvr_device
*dev
= video_drvdata(file
);
417 struct hdpvr_buffer
*buf
= NULL
;
419 unsigned int ret
= 0;
425 mutex_lock(&dev
->io_mutex
);
426 if (dev
->status
== STATUS_IDLE
) {
427 if (hdpvr_start_streaming(dev
)) {
428 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
429 "start_streaming failed\n");
432 dev
->status
= STATUS_IDLE
;
433 mutex_unlock(&dev
->io_mutex
);
436 dev
->owner
= file
->private_data
;
437 print_buffer_status();
439 mutex_unlock(&dev
->io_mutex
);
441 /* wait for the first buffer */
442 if (!(file
->f_flags
& O_NONBLOCK
)) {
443 if (wait_event_interruptible(dev
->wait_data
,
444 hdpvr_get_next_buffer(dev
)))
448 buf
= hdpvr_get_next_buffer(dev
);
450 while (count
> 0 && buf
) {
452 if (buf
->status
!= BUFSTAT_READY
&&
453 dev
->status
!= STATUS_DISCONNECTED
) {
454 /* return nonblocking */
455 if (file
->f_flags
& O_NONBLOCK
) {
461 if (wait_event_interruptible(dev
->wait_data
,
462 buf
->status
== BUFSTAT_READY
))
466 if (buf
->status
!= BUFSTAT_READY
)
469 /* set remaining bytes to copy */
471 rem
= urb
->actual_length
- buf
->pos
;
472 cnt
= rem
> count
? count
: rem
;
474 if (copy_to_user(buffer
, urb
->transfer_buffer
+ buf
->pos
,
476 v4l2_err(&dev
->v4l2_dev
, "read: copy_to_user failed\n");
487 /* finished, take next buffer */
488 if (buf
->pos
== urb
->actual_length
) {
489 mutex_lock(&dev
->io_mutex
);
491 buf
->status
= BUFSTAT_AVAILABLE
;
493 list_move_tail(&buf
->buff_list
, &dev
->free_buff_list
);
495 print_buffer_status();
497 mutex_unlock(&dev
->io_mutex
);
499 wake_up_interruptible(&dev
->wait_buffer
);
501 buf
= hdpvr_get_next_buffer(dev
);
510 static unsigned int hdpvr_poll(struct file
*filp
, poll_table
*wait
)
512 unsigned long req_events
= poll_requested_events(wait
);
513 struct hdpvr_buffer
*buf
= NULL
;
514 struct hdpvr_device
*dev
= video_drvdata(filp
);
515 unsigned int mask
= v4l2_ctrl_poll(filp
, wait
);
517 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
520 mutex_lock(&dev
->io_mutex
);
522 if (dev
->status
== STATUS_IDLE
) {
523 if (hdpvr_start_streaming(dev
)) {
524 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
525 "start_streaming failed\n");
526 dev
->status
= STATUS_IDLE
;
528 dev
->owner
= filp
->private_data
;
531 print_buffer_status();
533 mutex_unlock(&dev
->io_mutex
);
535 buf
= hdpvr_get_next_buffer(dev
);
536 /* only wait if no data is available */
537 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
538 poll_wait(filp
, &dev
->wait_data
, wait
);
539 buf
= hdpvr_get_next_buffer(dev
);
541 if (buf
&& buf
->status
== BUFSTAT_READY
)
542 mask
|= POLLIN
| POLLRDNORM
;
548 static const struct v4l2_file_operations hdpvr_fops
= {
549 .owner
= THIS_MODULE
,
551 .release
= hdpvr_release
,
554 .unlocked_ioctl
= video_ioctl2
,
557 /*=======================================================================*/
559 * V4L2 ioctl handling
562 static int vidioc_querycap(struct file
*file
, void *priv
,
563 struct v4l2_capability
*cap
)
565 struct hdpvr_device
*dev
= video_drvdata(file
);
567 strcpy(cap
->driver
, "hdpvr");
568 strcpy(cap
->card
, "Hauppauge HD PVR");
569 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
570 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
|
572 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
576 static int vidioc_s_std(struct file
*file
, void *_fh
,
579 struct hdpvr_device
*dev
= video_drvdata(file
);
580 struct hdpvr_fh
*fh
= _fh
;
583 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
585 if (dev
->status
!= STATUS_IDLE
)
587 if (std
& V4L2_STD_525_60
)
591 dev
->height
= std_type
? 576 : 480;
593 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
596 static int vidioc_g_std(struct file
*file
, void *_fh
,
599 struct hdpvr_device
*dev
= video_drvdata(file
);
600 struct hdpvr_fh
*fh
= _fh
;
602 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
608 static int vidioc_querystd(struct file
*file
, void *_fh
, v4l2_std_id
*a
)
610 struct hdpvr_device
*dev
= video_drvdata(file
);
611 struct hdpvr_video_info vid_info
;
612 struct hdpvr_fh
*fh
= _fh
;
615 *a
= V4L2_STD_UNKNOWN
;
616 if (dev
->options
.video_input
== HDPVR_COMPONENT
)
617 return fh
->legacy_mode
? 0 : -ENODATA
;
618 ret
= get_video_info(dev
, &vid_info
);
619 if (vid_info
.valid
&& vid_info
.width
== 720 &&
620 (vid_info
.height
== 480 || vid_info
.height
== 576)) {
621 *a
= (vid_info
.height
== 480) ?
622 V4L2_STD_525_60
: V4L2_STD_625_50
;
627 static int vidioc_s_dv_timings(struct file
*file
, void *_fh
,
628 struct v4l2_dv_timings
*timings
)
630 struct hdpvr_device
*dev
= video_drvdata(file
);
631 struct hdpvr_fh
*fh
= _fh
;
634 fh
->legacy_mode
= false;
635 if (dev
->options
.video_input
)
637 if (dev
->status
!= STATUS_IDLE
)
639 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++)
640 if (v4l2_match_dv_timings(timings
, hdpvr_dv_timings
+ i
, 0, false))
642 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
644 dev
->cur_dv_timings
= hdpvr_dv_timings
[i
];
645 dev
->width
= hdpvr_dv_timings
[i
].bt
.width
;
646 dev
->height
= hdpvr_dv_timings
[i
].bt
.height
;
650 static int vidioc_g_dv_timings(struct file
*file
, void *_fh
,
651 struct v4l2_dv_timings
*timings
)
653 struct hdpvr_device
*dev
= video_drvdata(file
);
654 struct hdpvr_fh
*fh
= _fh
;
656 fh
->legacy_mode
= false;
657 if (dev
->options
.video_input
)
659 *timings
= dev
->cur_dv_timings
;
663 static int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
664 struct v4l2_dv_timings
*timings
)
666 struct hdpvr_device
*dev
= video_drvdata(file
);
667 struct hdpvr_fh
*fh
= _fh
;
668 struct hdpvr_video_info vid_info
;
673 fh
->legacy_mode
= false;
674 if (dev
->options
.video_input
)
676 ret
= get_video_info(dev
, &vid_info
);
681 interlaced
= vid_info
.fps
<= 30;
682 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++) {
683 const struct v4l2_bt_timings
*bt
= &hdpvr_dv_timings
[i
].bt
;
688 hsize
= V4L2_DV_BT_FRAME_WIDTH(bt
);
689 vsize
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
690 fps
= (unsigned)bt
->pixelclock
/ (hsize
* vsize
);
691 if (bt
->width
!= vid_info
.width
||
692 bt
->height
!= vid_info
.height
||
693 bt
->interlaced
!= interlaced
||
694 (fps
!= vid_info
.fps
&& fps
+ 1 != vid_info
.fps
))
696 *timings
= hdpvr_dv_timings
[i
];
699 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
705 static int vidioc_enum_dv_timings(struct file
*file
, void *_fh
,
706 struct v4l2_enum_dv_timings
*timings
)
708 struct hdpvr_device
*dev
= video_drvdata(file
);
709 struct hdpvr_fh
*fh
= _fh
;
711 fh
->legacy_mode
= false;
712 memset(timings
->reserved
, 0, sizeof(timings
->reserved
));
713 if (dev
->options
.video_input
)
715 if (timings
->index
>= ARRAY_SIZE(hdpvr_dv_timings
))
717 timings
->timings
= hdpvr_dv_timings
[timings
->index
];
721 static int vidioc_dv_timings_cap(struct file
*file
, void *_fh
,
722 struct v4l2_dv_timings_cap
*cap
)
724 struct hdpvr_device
*dev
= video_drvdata(file
);
725 struct hdpvr_fh
*fh
= _fh
;
727 fh
->legacy_mode
= false;
728 if (dev
->options
.video_input
)
730 cap
->type
= V4L2_DV_BT_656_1120
;
731 cap
->bt
.min_width
= 720;
732 cap
->bt
.max_width
= 1920;
733 cap
->bt
.min_height
= 480;
734 cap
->bt
.max_height
= 1080;
735 cap
->bt
.min_pixelclock
= 27000000;
736 cap
->bt
.max_pixelclock
= 74250000;
737 cap
->bt
.standards
= V4L2_DV_BT_STD_CEA861
;
738 cap
->bt
.capabilities
= V4L2_DV_BT_CAP_INTERLACED
| V4L2_DV_BT_CAP_PROGRESSIVE
;
742 static const char *iname
[] = {
743 [HDPVR_COMPONENT
] = "Component",
744 [HDPVR_SVIDEO
] = "S-Video",
745 [HDPVR_COMPOSITE
] = "Composite",
748 static int vidioc_enum_input(struct file
*file
, void *_fh
, struct v4l2_input
*i
)
753 if (n
>= HDPVR_VIDEO_INPUTS
)
756 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
758 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
759 i
->name
[sizeof(i
->name
) - 1] = '\0';
761 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
763 i
->capabilities
= n
? V4L2_IN_CAP_STD
: V4L2_IN_CAP_DV_TIMINGS
;
764 i
->std
= n
? V4L2_STD_ALL
: 0;
769 static int vidioc_s_input(struct file
*file
, void *_fh
,
772 struct hdpvr_device
*dev
= video_drvdata(file
);
775 if (index
>= HDPVR_VIDEO_INPUTS
)
778 if (dev
->status
!= STATUS_IDLE
)
781 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
783 dev
->options
.video_input
= index
;
785 * Unfortunately gstreamer calls ENUMSTD and bails out if it
786 * won't find any formats, even though component input is
787 * selected. This means that we have to leave tvnorms at
788 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
789 * tvnorms is set at the device node level and not at the
792 * Comment this out for now, but if the legacy mode can be
793 * removed in the future, then this code should be enabled
795 dev->video_dev.tvnorms =
796 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
803 static int vidioc_g_input(struct file
*file
, void *private_data
,
806 struct hdpvr_device
*dev
= video_drvdata(file
);
808 *index
= dev
->options
.video_input
;
813 static const char *audio_iname
[] = {
814 [HDPVR_RCA_FRONT
] = "RCA front",
815 [HDPVR_RCA_BACK
] = "RCA back",
816 [HDPVR_SPDIF
] = "SPDIF",
819 static int vidioc_enumaudio(struct file
*file
, void *priv
,
820 struct v4l2_audio
*audio
)
825 if (n
>= HDPVR_AUDIO_INPUTS
)
828 audio
->capability
= V4L2_AUDCAP_STEREO
;
830 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
831 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
836 static int vidioc_s_audio(struct file
*file
, void *private_data
,
837 const struct v4l2_audio
*audio
)
839 struct hdpvr_device
*dev
= video_drvdata(file
);
842 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
845 if (dev
->status
!= STATUS_IDLE
)
848 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
850 dev
->options
.audio_input
= audio
->index
;
855 static int vidioc_g_audio(struct file
*file
, void *private_data
,
856 struct v4l2_audio
*audio
)
858 struct hdpvr_device
*dev
= video_drvdata(file
);
860 audio
->index
= dev
->options
.audio_input
;
861 audio
->capability
= V4L2_AUDCAP_STEREO
;
862 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
863 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
867 static int hdpvr_try_ctrl(struct v4l2_ctrl
*ctrl
)
869 struct hdpvr_device
*dev
=
870 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
873 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
874 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
875 dev
->video_bitrate
->val
>= dev
->video_bitrate_peak
->val
)
876 dev
->video_bitrate_peak
->val
=
877 dev
->video_bitrate
->val
+ 100000;
883 static int hdpvr_s_ctrl(struct v4l2_ctrl
*ctrl
)
885 struct hdpvr_device
*dev
=
886 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
887 struct hdpvr_options
*opt
= &dev
->options
;
891 case V4L2_CID_BRIGHTNESS
:
892 ret
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->val
);
895 dev
->options
.brightness
= ctrl
->val
;
897 case V4L2_CID_CONTRAST
:
898 ret
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->val
);
901 dev
->options
.contrast
= ctrl
->val
;
903 case V4L2_CID_SATURATION
:
904 ret
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->val
);
907 dev
->options
.saturation
= ctrl
->val
;
910 ret
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->val
);
913 dev
->options
.hue
= ctrl
->val
;
915 case V4L2_CID_SHARPNESS
:
916 ret
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->val
);
919 dev
->options
.sharpness
= ctrl
->val
;
921 case V4L2_CID_MPEG_AUDIO_ENCODING
:
922 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
923 opt
->audio_codec
= ctrl
->val
;
924 return hdpvr_set_audio(dev
, opt
->audio_input
+ 1,
928 case V4L2_CID_MPEG_VIDEO_ENCODING
:
930 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
931 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
932 /* opt->gop_mode |= 0x2; */
933 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
934 /* opt->gop_mode); */
936 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
937 /* opt->gop_mode &= ~0x2; */
938 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
939 /* opt->gop_mode); */
942 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
: {
943 uint peak_bitrate
= dev
->video_bitrate_peak
->val
/ 100000;
944 uint bitrate
= dev
->video_bitrate
->val
/ 100000;
947 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
)
948 opt
->bitrate_mode
= HDPVR_CONSTANT
;
950 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
951 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
953 v4l2_ctrl_activate(dev
->video_bitrate_peak
,
954 ctrl
->val
!= V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
957 if (dev
->video_bitrate_peak
->is_new
||
958 dev
->video_bitrate
->is_new
) {
959 opt
->bitrate
= bitrate
;
960 opt
->peak_bitrate
= peak_bitrate
;
961 hdpvr_set_bitrate(dev
);
965 case V4L2_CID_MPEG_STREAM_TYPE
:
973 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
974 struct v4l2_fmtdesc
*f
)
979 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
980 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
981 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
986 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *_fh
,
987 struct v4l2_format
*f
)
989 struct hdpvr_device
*dev
= video_drvdata(file
);
990 struct hdpvr_fh
*fh
= _fh
;
994 * The original driver would always returns the current detected
995 * resolution as the format (and EFAULT if it couldn't be detected).
996 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
997 * better way of doing this, but to stay compatible with existing
998 * applications we assume legacy mode every time an application opens
999 * the device. Only if one of the new DV_TIMINGS ioctls is called
1000 * will the filehandle go into 'normal' mode where g_fmt returns the
1003 if (fh
->legacy_mode
) {
1004 struct hdpvr_video_info vid_info
;
1006 ret
= get_video_info(dev
, &vid_info
);
1009 if (!vid_info
.valid
)
1011 f
->fmt
.pix
.width
= vid_info
.width
;
1012 f
->fmt
.pix
.height
= vid_info
.height
;
1014 f
->fmt
.pix
.width
= dev
->width
;
1015 f
->fmt
.pix
.height
= dev
->height
;
1017 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1018 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1019 f
->fmt
.pix
.bytesperline
= 0;
1020 if (f
->fmt
.pix
.width
== 720) {
1022 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1023 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1026 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
1027 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1032 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1033 struct v4l2_encoder_cmd
*a
)
1035 struct hdpvr_device
*dev
= video_drvdata(filp
);
1038 mutex_lock(&dev
->io_mutex
);
1042 case V4L2_ENC_CMD_START
:
1043 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1047 if (dev
->status
== STATUS_STREAMING
)
1049 res
= hdpvr_start_streaming(dev
);
1051 dev
->owner
= filp
->private_data
;
1053 dev
->status
= STATUS_IDLE
;
1055 case V4L2_ENC_CMD_STOP
:
1056 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1060 if (dev
->status
== STATUS_IDLE
)
1062 res
= hdpvr_stop_streaming(dev
);
1067 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1068 "Unsupported encoder cmd %d\n", a
->cmd
);
1073 mutex_unlock(&dev
->io_mutex
);
1077 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1078 struct v4l2_encoder_cmd
*a
)
1082 case V4L2_ENC_CMD_START
:
1083 case V4L2_ENC_CMD_STOP
:
1090 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1091 .vidioc_querycap
= vidioc_querycap
,
1092 .vidioc_s_std
= vidioc_s_std
,
1093 .vidioc_g_std
= vidioc_g_std
,
1094 .vidioc_querystd
= vidioc_querystd
,
1095 .vidioc_s_dv_timings
= vidioc_s_dv_timings
,
1096 .vidioc_g_dv_timings
= vidioc_g_dv_timings
,
1097 .vidioc_query_dv_timings
= vidioc_query_dv_timings
,
1098 .vidioc_enum_dv_timings
= vidioc_enum_dv_timings
,
1099 .vidioc_dv_timings_cap
= vidioc_dv_timings_cap
,
1100 .vidioc_enum_input
= vidioc_enum_input
,
1101 .vidioc_g_input
= vidioc_g_input
,
1102 .vidioc_s_input
= vidioc_s_input
,
1103 .vidioc_enumaudio
= vidioc_enumaudio
,
1104 .vidioc_g_audio
= vidioc_g_audio
,
1105 .vidioc_s_audio
= vidioc_s_audio
,
1106 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1107 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1108 .vidioc_s_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1109 .vidioc_try_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1110 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1111 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1112 .vidioc_log_status
= v4l2_ctrl_log_status
,
1113 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1114 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1117 static void hdpvr_device_release(struct video_device
*vdev
)
1119 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1122 mutex_lock(&dev
->io_mutex
);
1123 flush_work(&dev
->worker
);
1124 mutex_unlock(&dev
->io_mutex
);
1126 v4l2_device_unregister(&dev
->v4l2_dev
);
1127 v4l2_ctrl_handler_free(&dev
->hdl
);
1129 /* deregister I2C adapter */
1130 #if IS_ENABLED(CONFIG_I2C)
1131 mutex_lock(&dev
->i2c_mutex
);
1132 i2c_del_adapter(&dev
->i2c_adapter
);
1133 mutex_unlock(&dev
->i2c_mutex
);
1134 #endif /* CONFIG_I2C */
1136 kfree(dev
->usbc_buf
);
1140 static const struct video_device hdpvr_video_template
= {
1141 .fops
= &hdpvr_fops
,
1142 .release
= hdpvr_device_release
,
1143 .ioctl_ops
= &hdpvr_ioctl_ops
,
1144 .tvnorms
= V4L2_STD_ALL
,
1147 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops
= {
1148 .try_ctrl
= hdpvr_try_ctrl
,
1149 .s_ctrl
= hdpvr_s_ctrl
,
1152 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1155 struct v4l2_ctrl_handler
*hdl
= &dev
->hdl
;
1156 bool ac3
= dev
->flags
& HDPVR_FLAG_AC3_CAP
;
1159 dev
->cur_std
= V4L2_STD_525_60
;
1162 dev
->cur_dv_timings
= hdpvr_dv_timings
[HDPVR_DEF_DV_TIMINGS_IDX
];
1163 v4l2_ctrl_handler_init(hdl
, 11);
1164 if (dev
->fw_ver
> 0x15) {
1165 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1166 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x80);
1167 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1168 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x40);
1169 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1170 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x40);
1171 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1172 V4L2_CID_HUE
, 0x0, 0x1e, 1, 0xf);
1173 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1174 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1176 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1177 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x86);
1178 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1179 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x80);
1180 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1181 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x80);
1182 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1183 V4L2_CID_HUE
, 0x0, 0xff, 1, 0x80);
1184 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1185 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1188 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1189 V4L2_CID_MPEG_STREAM_TYPE
,
1190 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
,
1191 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS
);
1192 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1193 V4L2_CID_MPEG_AUDIO_ENCODING
,
1194 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
: V4L2_MPEG_AUDIO_ENCODING_AAC
,
1195 0x7, ac3
? dev
->options
.audio_codec
: V4L2_MPEG_AUDIO_ENCODING_AAC
);
1196 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1197 V4L2_CID_MPEG_VIDEO_ENCODING
,
1198 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 0x3,
1199 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
1201 dev
->video_mode
= v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1202 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1203 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 0,
1204 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
1206 dev
->video_bitrate
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1207 V4L2_CID_MPEG_VIDEO_BITRATE
,
1208 1000000, 13500000, 100000, 6500000);
1209 dev
->video_bitrate_peak
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1210 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1211 1100000, 20200000, 100000, 9000000);
1212 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1215 v4l2_err(&dev
->v4l2_dev
, "Could not register controls\n");
1218 v4l2_ctrl_cluster(3, &dev
->video_mode
);
1219 res
= v4l2_ctrl_handler_setup(hdl
);
1221 v4l2_err(&dev
->v4l2_dev
, "Could not setup controls\n");
1225 /* setup and register video device */
1226 dev
->video_dev
= hdpvr_video_template
;
1227 strcpy(dev
->video_dev
.name
, "Hauppauge HD PVR");
1228 dev
->video_dev
.v4l2_dev
= &dev
->v4l2_dev
;
1229 video_set_drvdata(&dev
->video_dev
, dev
);
1231 res
= video_register_device(&dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
);
1233 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");
1239 v4l2_ctrl_handler_free(hdl
);