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/kconfig.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/uaccess.h>
19 #include <linux/usb.h>
20 #include <linux/mutex.h>
21 #include <linux/workqueue.h>
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-dv-timings.h>
25 #include <media/v4l2-dev.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-dv-timings.h>
28 #include <media/v4l2-ioctl.h>
29 #include <media/v4l2-event.h>
32 #define BULK_URB_TIMEOUT 90 /* 0.09 seconds */
34 #define print_buffer_status() { \
35 v4l2_dbg(MSG_BUFFER, hdpvr_debug, &dev->v4l2_dev, \
36 "%s:%d buffer stat: %d free, %d proc\n", \
38 list_size(&dev->free_buff_list), \
39 list_size(&dev->rec_buff_list)); }
41 static const struct v4l2_dv_timings hdpvr_dv_timings
[] = {
42 V4L2_DV_BT_CEA_720X480I59_94
,
43 V4L2_DV_BT_CEA_720X576I50
,
44 V4L2_DV_BT_CEA_720X480P59_94
,
45 V4L2_DV_BT_CEA_720X576P50
,
46 V4L2_DV_BT_CEA_1280X720P50
,
47 V4L2_DV_BT_CEA_1280X720P60
,
48 V4L2_DV_BT_CEA_1920X1080I50
,
49 V4L2_DV_BT_CEA_1920X1080I60
,
52 /* Use 480i59 as the default timings */
53 #define HDPVR_DEF_DV_TIMINGS_IDX (0)
60 static uint
list_size(struct list_head
*list
)
62 struct list_head
*tmp
;
65 list_for_each(tmp
, list
) {
72 /*=========================================================================*/
74 static void hdpvr_read_bulk_callback(struct urb
*urb
)
76 struct hdpvr_buffer
*buf
= (struct hdpvr_buffer
*)urb
->context
;
77 struct hdpvr_device
*dev
= buf
->dev
;
79 /* marking buffer as received and wake waiting */
80 buf
->status
= BUFSTAT_READY
;
81 wake_up_interruptible(&dev
->wait_data
);
84 /*=========================================================================*/
87 /* function expects dev->io_mutex to be hold by caller */
88 int hdpvr_cancel_queue(struct hdpvr_device
*dev
)
90 struct hdpvr_buffer
*buf
;
92 list_for_each_entry(buf
, &dev
->rec_buff_list
, buff_list
) {
93 usb_kill_urb(buf
->urb
);
94 buf
->status
= BUFSTAT_AVAILABLE
;
97 list_splice_init(&dev
->rec_buff_list
, dev
->free_buff_list
.prev
);
102 static int hdpvr_free_queue(struct list_head
*q
)
104 struct list_head
*tmp
;
106 struct hdpvr_buffer
*buf
;
109 for (p
= q
->next
; p
!= q
;) {
110 buf
= list_entry(p
, struct hdpvr_buffer
, buff_list
);
113 usb_free_coherent(urb
->dev
, urb
->transfer_buffer_length
,
114 urb
->transfer_buffer
, urb
->transfer_dma
);
125 /* function expects dev->io_mutex to be hold by caller */
126 int hdpvr_free_buffers(struct hdpvr_device
*dev
)
128 hdpvr_cancel_queue(dev
);
130 hdpvr_free_queue(&dev
->free_buff_list
);
131 hdpvr_free_queue(&dev
->rec_buff_list
);
136 /* function expects dev->io_mutex to be hold by caller */
137 int hdpvr_alloc_buffers(struct hdpvr_device
*dev
, uint count
)
140 int retval
= -ENOMEM
;
142 struct hdpvr_buffer
*buf
;
145 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
146 "allocating %u buffers\n", count
);
148 for (i
= 0; i
< count
; i
++) {
150 buf
= kzalloc(sizeof(struct hdpvr_buffer
), GFP_KERNEL
);
152 v4l2_err(&dev
->v4l2_dev
, "cannot allocate buffer\n");
157 urb
= usb_alloc_urb(0, GFP_KERNEL
);
159 v4l2_err(&dev
->v4l2_dev
, "cannot allocate urb\n");
164 mem
= usb_alloc_coherent(dev
->udev
, dev
->bulk_in_size
, GFP_KERNEL
,
167 v4l2_err(&dev
->v4l2_dev
,
168 "cannot allocate usb transfer buffer\n");
169 goto exit_urb_buffer
;
172 usb_fill_bulk_urb(buf
->urb
, dev
->udev
,
173 usb_rcvbulkpipe(dev
->udev
,
174 dev
->bulk_in_endpointAddr
),
175 mem
, dev
->bulk_in_size
,
176 hdpvr_read_bulk_callback
, buf
);
178 buf
->urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
179 buf
->status
= BUFSTAT_AVAILABLE
;
180 list_add_tail(&buf
->buff_list
, &dev
->free_buff_list
);
188 hdpvr_free_buffers(dev
);
192 static int hdpvr_submit_buffers(struct hdpvr_device
*dev
)
194 struct hdpvr_buffer
*buf
;
196 int ret
= 0, err_count
= 0;
198 mutex_lock(&dev
->io_mutex
);
200 while (dev
->status
== STATUS_STREAMING
&&
201 !list_empty(&dev
->free_buff_list
)) {
203 buf
= list_entry(dev
->free_buff_list
.next
, struct hdpvr_buffer
,
205 if (buf
->status
!= BUFSTAT_AVAILABLE
) {
206 v4l2_err(&dev
->v4l2_dev
,
207 "buffer not marked as available\n");
214 urb
->actual_length
= 0;
215 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
217 v4l2_err(&dev
->v4l2_dev
,
218 "usb_submit_urb in %s returned %d\n",
224 buf
->status
= BUFSTAT_INPROGRESS
;
225 list_move_tail(&buf
->buff_list
, &dev
->rec_buff_list
);
228 print_buffer_status();
229 mutex_unlock(&dev
->io_mutex
);
233 static struct hdpvr_buffer
*hdpvr_get_next_buffer(struct hdpvr_device
*dev
)
235 struct hdpvr_buffer
*buf
;
237 mutex_lock(&dev
->io_mutex
);
239 if (list_empty(&dev
->rec_buff_list
)) {
240 mutex_unlock(&dev
->io_mutex
);
244 buf
= list_entry(dev
->rec_buff_list
.next
, struct hdpvr_buffer
,
246 mutex_unlock(&dev
->io_mutex
);
251 static void hdpvr_transmit_buffers(struct work_struct
*work
)
253 struct hdpvr_device
*dev
= container_of(work
, struct hdpvr_device
,
256 while (dev
->status
== STATUS_STREAMING
) {
258 if (hdpvr_submit_buffers(dev
)) {
259 v4l2_err(&dev
->v4l2_dev
, "couldn't submit buffers\n");
262 if (wait_event_interruptible(dev
->wait_buffer
,
263 !list_empty(&dev
->free_buff_list
) ||
264 dev
->status
!= STATUS_STREAMING
))
268 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
269 "transmit worker exited\n");
272 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
273 "transmit buffers errored\n");
274 dev
->status
= STATUS_ERROR
;
277 /* function expects dev->io_mutex to be hold by caller */
278 static int hdpvr_start_streaming(struct hdpvr_device
*dev
)
281 struct hdpvr_video_info vidinf
;
283 if (dev
->status
== STATUS_STREAMING
)
285 if (dev
->status
!= STATUS_IDLE
)
288 ret
= get_video_info(dev
, &vidinf
);
294 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
295 "no video signal at input %d\n", dev
->options
.video_input
);
299 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
300 "video signal: %dx%d@%dhz\n", vidinf
.width
,
301 vidinf
.height
, vidinf
.fps
);
303 /* start streaming 2 request */
304 ret
= usb_control_msg(dev
->udev
,
305 usb_sndctrlpipe(dev
->udev
, 0),
306 0xb8, 0x38, 0x1, 0, NULL
, 0, 8000);
307 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
308 "encoder start control request returned %d\n", ret
);
312 ret
= hdpvr_config_call(dev
, CTRL_START_STREAMING_VALUE
, 0x00);
316 dev
->status
= STATUS_STREAMING
;
318 INIT_WORK(&dev
->worker
, hdpvr_transmit_buffers
);
319 schedule_work(&dev
->worker
);
321 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
322 "streaming started\n");
328 /* function expects dev->io_mutex to be hold by caller */
329 static int hdpvr_stop_streaming(struct hdpvr_device
*dev
)
335 if (dev
->status
== STATUS_IDLE
)
337 else if (dev
->status
!= STATUS_STREAMING
)
340 buf
= kmalloc(dev
->bulk_in_size
, GFP_KERNEL
);
342 v4l2_err(&dev
->v4l2_dev
, "failed to allocate temporary buffer "
343 "for emptying the internal device buffer. "
344 "Next capture start will be slow\n");
346 dev
->status
= STATUS_SHUTTING_DOWN
;
347 hdpvr_config_call(dev
, CTRL_STOP_STREAMING_VALUE
, 0x00);
348 mutex_unlock(&dev
->io_mutex
);
350 wake_up_interruptible(&dev
->wait_buffer
);
353 flush_work(&dev
->worker
);
355 mutex_lock(&dev
->io_mutex
);
356 /* kill the still outstanding urbs */
357 hdpvr_cancel_queue(dev
);
359 /* emptying the device buffer beforeshutting it down */
360 while (buf
&& ++c
< 500 &&
361 !usb_bulk_msg(dev
->udev
,
362 usb_rcvbulkpipe(dev
->udev
,
363 dev
->bulk_in_endpointAddr
),
364 buf
, dev
->bulk_in_size
, &actual_length
,
366 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
367 "%2d: got %d bytes\n", c
, actual_length
);
370 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
371 "used %d urbs to empty device buffers\n", c
-1);
374 dev
->status
= STATUS_IDLE
;
380 /*=======================================================================*/
382 * video 4 linux 2 file operations
385 static int hdpvr_open(struct file
*file
)
387 struct hdpvr_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
391 fh
->legacy_mode
= true;
392 v4l2_fh_init(&fh
->fh
, video_devdata(file
));
393 v4l2_fh_add(&fh
->fh
);
394 file
->private_data
= fh
;
398 static int hdpvr_release(struct file
*file
)
400 struct hdpvr_device
*dev
= video_drvdata(file
);
402 mutex_lock(&dev
->io_mutex
);
403 if (file
->private_data
== dev
->owner
) {
404 hdpvr_stop_streaming(dev
);
407 mutex_unlock(&dev
->io_mutex
);
409 return v4l2_fh_release(file
);
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_device
*dev
= video_drvdata(file
);
420 struct hdpvr_buffer
*buf
= NULL
;
422 unsigned int ret
= 0;
428 mutex_lock(&dev
->io_mutex
);
429 if (dev
->status
== STATUS_IDLE
) {
430 if (hdpvr_start_streaming(dev
)) {
431 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
432 "start_streaming failed\n");
435 dev
->status
= STATUS_IDLE
;
436 mutex_unlock(&dev
->io_mutex
);
439 dev
->owner
= file
->private_data
;
440 print_buffer_status();
442 mutex_unlock(&dev
->io_mutex
);
444 /* wait for the first buffer */
445 if (!(file
->f_flags
& O_NONBLOCK
)) {
446 if (wait_event_interruptible(dev
->wait_data
,
447 hdpvr_get_next_buffer(dev
)))
451 buf
= hdpvr_get_next_buffer(dev
);
453 while (count
> 0 && buf
) {
455 if (buf
->status
!= BUFSTAT_READY
&&
456 dev
->status
!= STATUS_DISCONNECTED
) {
457 /* return nonblocking */
458 if (file
->f_flags
& O_NONBLOCK
) {
464 if (wait_event_interruptible(dev
->wait_data
,
465 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 unsigned long req_events
= poll_requested_events(wait
);
516 struct hdpvr_buffer
*buf
= NULL
;
517 struct hdpvr_device
*dev
= video_drvdata(filp
);
518 unsigned int mask
= v4l2_ctrl_poll(filp
, wait
);
520 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
523 mutex_lock(&dev
->io_mutex
);
525 if (dev
->status
== STATUS_IDLE
) {
526 if (hdpvr_start_streaming(dev
)) {
527 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
528 "start_streaming failed\n");
529 dev
->status
= STATUS_IDLE
;
531 dev
->owner
= filp
->private_data
;
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
, "Hauppauge HD PVR");
572 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
573 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
|
575 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
579 static int vidioc_s_std(struct file
*file
, void *_fh
,
582 struct hdpvr_device
*dev
= video_drvdata(file
);
583 struct hdpvr_fh
*fh
= _fh
;
586 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
588 if (dev
->status
!= STATUS_IDLE
)
590 if (std
& V4L2_STD_525_60
)
594 dev
->height
= std_type
? 576 : 480;
596 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
599 static int vidioc_g_std(struct file
*file
, void *_fh
,
602 struct hdpvr_device
*dev
= video_drvdata(file
);
603 struct hdpvr_fh
*fh
= _fh
;
605 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
611 static int vidioc_querystd(struct file
*file
, void *_fh
, v4l2_std_id
*a
)
613 struct hdpvr_device
*dev
= video_drvdata(file
);
614 struct hdpvr_video_info vid_info
;
615 struct hdpvr_fh
*fh
= _fh
;
618 *a
= V4L2_STD_UNKNOWN
;
619 if (dev
->options
.video_input
== HDPVR_COMPONENT
)
620 return fh
->legacy_mode
? 0 : -ENODATA
;
621 ret
= get_video_info(dev
, &vid_info
);
622 if (vid_info
.valid
&& vid_info
.width
== 720 &&
623 (vid_info
.height
== 480 || vid_info
.height
== 576)) {
624 *a
= (vid_info
.height
== 480) ?
625 V4L2_STD_525_60
: V4L2_STD_625_50
;
630 static int vidioc_s_dv_timings(struct file
*file
, void *_fh
,
631 struct v4l2_dv_timings
*timings
)
633 struct hdpvr_device
*dev
= video_drvdata(file
);
634 struct hdpvr_fh
*fh
= _fh
;
637 fh
->legacy_mode
= false;
638 if (dev
->options
.video_input
)
640 if (dev
->status
!= STATUS_IDLE
)
642 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++)
643 if (v4l2_match_dv_timings(timings
, hdpvr_dv_timings
+ i
, 0, false))
645 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
647 dev
->cur_dv_timings
= hdpvr_dv_timings
[i
];
648 dev
->width
= hdpvr_dv_timings
[i
].bt
.width
;
649 dev
->height
= hdpvr_dv_timings
[i
].bt
.height
;
653 static int vidioc_g_dv_timings(struct file
*file
, void *_fh
,
654 struct v4l2_dv_timings
*timings
)
656 struct hdpvr_device
*dev
= video_drvdata(file
);
657 struct hdpvr_fh
*fh
= _fh
;
659 fh
->legacy_mode
= false;
660 if (dev
->options
.video_input
)
662 *timings
= dev
->cur_dv_timings
;
666 static int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
667 struct v4l2_dv_timings
*timings
)
669 struct hdpvr_device
*dev
= video_drvdata(file
);
670 struct hdpvr_fh
*fh
= _fh
;
671 struct hdpvr_video_info vid_info
;
676 fh
->legacy_mode
= false;
677 if (dev
->options
.video_input
)
679 ret
= get_video_info(dev
, &vid_info
);
684 interlaced
= vid_info
.fps
<= 30;
685 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++) {
686 const struct v4l2_bt_timings
*bt
= &hdpvr_dv_timings
[i
].bt
;
691 hsize
= V4L2_DV_BT_FRAME_WIDTH(bt
);
692 vsize
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
693 fps
= (unsigned)bt
->pixelclock
/ (hsize
* vsize
);
694 if (bt
->width
!= vid_info
.width
||
695 bt
->height
!= vid_info
.height
||
696 bt
->interlaced
!= interlaced
||
697 (fps
!= vid_info
.fps
&& fps
+ 1 != vid_info
.fps
))
699 *timings
= hdpvr_dv_timings
[i
];
702 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
708 static int vidioc_enum_dv_timings(struct file
*file
, void *_fh
,
709 struct v4l2_enum_dv_timings
*timings
)
711 struct hdpvr_device
*dev
= video_drvdata(file
);
712 struct hdpvr_fh
*fh
= _fh
;
714 fh
->legacy_mode
= false;
715 memset(timings
->reserved
, 0, sizeof(timings
->reserved
));
716 if (dev
->options
.video_input
)
718 if (timings
->index
>= ARRAY_SIZE(hdpvr_dv_timings
))
720 timings
->timings
= hdpvr_dv_timings
[timings
->index
];
724 static int vidioc_dv_timings_cap(struct file
*file
, void *_fh
,
725 struct v4l2_dv_timings_cap
*cap
)
727 struct hdpvr_device
*dev
= video_drvdata(file
);
728 struct hdpvr_fh
*fh
= _fh
;
730 fh
->legacy_mode
= false;
731 if (dev
->options
.video_input
)
733 cap
->type
= V4L2_DV_BT_656_1120
;
734 cap
->bt
.min_width
= 720;
735 cap
->bt
.max_width
= 1920;
736 cap
->bt
.min_height
= 480;
737 cap
->bt
.max_height
= 1080;
738 cap
->bt
.min_pixelclock
= 27000000;
739 cap
->bt
.max_pixelclock
= 74250000;
740 cap
->bt
.standards
= V4L2_DV_BT_STD_CEA861
;
741 cap
->bt
.capabilities
= V4L2_DV_BT_CAP_INTERLACED
| V4L2_DV_BT_CAP_PROGRESSIVE
;
745 static const char *iname
[] = {
746 [HDPVR_COMPONENT
] = "Component",
747 [HDPVR_SVIDEO
] = "S-Video",
748 [HDPVR_COMPOSITE
] = "Composite",
751 static int vidioc_enum_input(struct file
*file
, void *_fh
, struct v4l2_input
*i
)
756 if (n
>= HDPVR_VIDEO_INPUTS
)
759 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
761 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
762 i
->name
[sizeof(i
->name
) - 1] = '\0';
764 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
766 i
->capabilities
= n
? V4L2_IN_CAP_STD
: V4L2_IN_CAP_DV_TIMINGS
;
767 i
->std
= n
? V4L2_STD_ALL
: 0;
772 static int vidioc_s_input(struct file
*file
, void *_fh
,
775 struct hdpvr_device
*dev
= video_drvdata(file
);
778 if (index
>= HDPVR_VIDEO_INPUTS
)
781 if (dev
->status
!= STATUS_IDLE
)
784 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
786 dev
->options
.video_input
= index
;
788 * Unfortunately gstreamer calls ENUMSTD and bails out if it
789 * won't find any formats, even though component input is
790 * selected. This means that we have to leave tvnorms at
791 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
792 * tvnorms is set at the device node level and not at the
795 * Comment this out for now, but if the legacy mode can be
796 * removed in the future, then this code should be enabled
798 dev->video_dev.tvnorms =
799 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
806 static int vidioc_g_input(struct file
*file
, void *private_data
,
809 struct hdpvr_device
*dev
= video_drvdata(file
);
811 *index
= dev
->options
.video_input
;
816 static const char *audio_iname
[] = {
817 [HDPVR_RCA_FRONT
] = "RCA front",
818 [HDPVR_RCA_BACK
] = "RCA back",
819 [HDPVR_SPDIF
] = "SPDIF",
822 static int vidioc_enumaudio(struct file
*file
, void *priv
,
823 struct v4l2_audio
*audio
)
828 if (n
>= HDPVR_AUDIO_INPUTS
)
831 audio
->capability
= V4L2_AUDCAP_STEREO
;
833 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
834 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
839 static int vidioc_s_audio(struct file
*file
, void *private_data
,
840 const struct v4l2_audio
*audio
)
842 struct hdpvr_device
*dev
= video_drvdata(file
);
845 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
848 if (dev
->status
!= STATUS_IDLE
)
851 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
853 dev
->options
.audio_input
= audio
->index
;
858 static int vidioc_g_audio(struct file
*file
, void *private_data
,
859 struct v4l2_audio
*audio
)
861 struct hdpvr_device
*dev
= video_drvdata(file
);
863 audio
->index
= dev
->options
.audio_input
;
864 audio
->capability
= V4L2_AUDCAP_STEREO
;
865 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
866 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
870 static int hdpvr_try_ctrl(struct v4l2_ctrl
*ctrl
)
872 struct hdpvr_device
*dev
=
873 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
876 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
877 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
878 dev
->video_bitrate
->val
>= dev
->video_bitrate_peak
->val
)
879 dev
->video_bitrate_peak
->val
=
880 dev
->video_bitrate
->val
+ 100000;
886 static int hdpvr_s_ctrl(struct v4l2_ctrl
*ctrl
)
888 struct hdpvr_device
*dev
=
889 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
890 struct hdpvr_options
*opt
= &dev
->options
;
894 case V4L2_CID_BRIGHTNESS
:
895 ret
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->val
);
898 dev
->options
.brightness
= ctrl
->val
;
900 case V4L2_CID_CONTRAST
:
901 ret
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->val
);
904 dev
->options
.contrast
= ctrl
->val
;
906 case V4L2_CID_SATURATION
:
907 ret
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->val
);
910 dev
->options
.saturation
= ctrl
->val
;
913 ret
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->val
);
916 dev
->options
.hue
= ctrl
->val
;
918 case V4L2_CID_SHARPNESS
:
919 ret
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->val
);
922 dev
->options
.sharpness
= ctrl
->val
;
924 case V4L2_CID_MPEG_AUDIO_ENCODING
:
925 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
926 opt
->audio_codec
= ctrl
->val
;
927 return hdpvr_set_audio(dev
, opt
->audio_input
+ 1,
931 case V4L2_CID_MPEG_VIDEO_ENCODING
:
933 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
934 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
935 /* opt->gop_mode |= 0x2; */
936 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
937 /* opt->gop_mode); */
939 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
940 /* opt->gop_mode &= ~0x2; */
941 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
942 /* opt->gop_mode); */
945 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
: {
946 uint peak_bitrate
= dev
->video_bitrate_peak
->val
/ 100000;
947 uint bitrate
= dev
->video_bitrate
->val
/ 100000;
950 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
)
951 opt
->bitrate_mode
= HDPVR_CONSTANT
;
953 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
954 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
956 v4l2_ctrl_activate(dev
->video_bitrate_peak
,
957 ctrl
->val
!= V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
960 if (dev
->video_bitrate_peak
->is_new
||
961 dev
->video_bitrate
->is_new
) {
962 opt
->bitrate
= bitrate
;
963 opt
->peak_bitrate
= peak_bitrate
;
964 hdpvr_set_bitrate(dev
);
968 case V4L2_CID_MPEG_STREAM_TYPE
:
976 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
977 struct v4l2_fmtdesc
*f
)
982 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
983 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
984 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
989 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *_fh
,
990 struct v4l2_format
*f
)
992 struct hdpvr_device
*dev
= video_drvdata(file
);
993 struct hdpvr_fh
*fh
= _fh
;
997 * The original driver would always returns the current detected
998 * resolution as the format (and EFAULT if it couldn't be detected).
999 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
1000 * better way of doing this, but to stay compatible with existing
1001 * applications we assume legacy mode every time an application opens
1002 * the device. Only if one of the new DV_TIMINGS ioctls is called
1003 * will the filehandle go into 'normal' mode where g_fmt returns the
1006 if (fh
->legacy_mode
) {
1007 struct hdpvr_video_info vid_info
;
1009 ret
= get_video_info(dev
, &vid_info
);
1012 if (!vid_info
.valid
)
1014 f
->fmt
.pix
.width
= vid_info
.width
;
1015 f
->fmt
.pix
.height
= vid_info
.height
;
1017 f
->fmt
.pix
.width
= dev
->width
;
1018 f
->fmt
.pix
.height
= dev
->height
;
1020 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1021 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1022 f
->fmt
.pix
.bytesperline
= 0;
1023 if (f
->fmt
.pix
.width
== 720) {
1025 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1026 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1029 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_REC709
;
1030 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1035 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1036 struct v4l2_encoder_cmd
*a
)
1038 struct hdpvr_device
*dev
= video_drvdata(filp
);
1041 mutex_lock(&dev
->io_mutex
);
1045 case V4L2_ENC_CMD_START
:
1046 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1050 if (dev
->status
== STATUS_STREAMING
)
1052 res
= hdpvr_start_streaming(dev
);
1054 dev
->owner
= filp
->private_data
;
1056 dev
->status
= STATUS_IDLE
;
1058 case V4L2_ENC_CMD_STOP
:
1059 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1063 if (dev
->status
== STATUS_IDLE
)
1065 res
= hdpvr_stop_streaming(dev
);
1070 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1071 "Unsupported encoder cmd %d\n", a
->cmd
);
1076 mutex_unlock(&dev
->io_mutex
);
1080 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1081 struct v4l2_encoder_cmd
*a
)
1085 case V4L2_ENC_CMD_START
:
1086 case V4L2_ENC_CMD_STOP
:
1093 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1094 .vidioc_querycap
= vidioc_querycap
,
1095 .vidioc_s_std
= vidioc_s_std
,
1096 .vidioc_g_std
= vidioc_g_std
,
1097 .vidioc_querystd
= vidioc_querystd
,
1098 .vidioc_s_dv_timings
= vidioc_s_dv_timings
,
1099 .vidioc_g_dv_timings
= vidioc_g_dv_timings
,
1100 .vidioc_query_dv_timings
= vidioc_query_dv_timings
,
1101 .vidioc_enum_dv_timings
= vidioc_enum_dv_timings
,
1102 .vidioc_dv_timings_cap
= vidioc_dv_timings_cap
,
1103 .vidioc_enum_input
= vidioc_enum_input
,
1104 .vidioc_g_input
= vidioc_g_input
,
1105 .vidioc_s_input
= vidioc_s_input
,
1106 .vidioc_enumaudio
= vidioc_enumaudio
,
1107 .vidioc_g_audio
= vidioc_g_audio
,
1108 .vidioc_s_audio
= vidioc_s_audio
,
1109 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1110 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1111 .vidioc_s_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1112 .vidioc_try_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1113 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1114 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1115 .vidioc_log_status
= v4l2_ctrl_log_status
,
1116 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1117 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1120 static void hdpvr_device_release(struct video_device
*vdev
)
1122 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1125 mutex_lock(&dev
->io_mutex
);
1126 flush_work(&dev
->worker
);
1127 mutex_unlock(&dev
->io_mutex
);
1129 v4l2_device_unregister(&dev
->v4l2_dev
);
1130 v4l2_ctrl_handler_free(&dev
->hdl
);
1132 /* deregister I2C adapter */
1133 #if IS_ENABLED(CONFIG_I2C)
1134 mutex_lock(&dev
->i2c_mutex
);
1135 i2c_del_adapter(&dev
->i2c_adapter
);
1136 mutex_unlock(&dev
->i2c_mutex
);
1137 #endif /* CONFIG_I2C */
1139 kfree(dev
->usbc_buf
);
1143 static const struct video_device hdpvr_video_template
= {
1144 .fops
= &hdpvr_fops
,
1145 .release
= hdpvr_device_release
,
1146 .ioctl_ops
= &hdpvr_ioctl_ops
,
1147 .tvnorms
= V4L2_STD_ALL
,
1150 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops
= {
1151 .try_ctrl
= hdpvr_try_ctrl
,
1152 .s_ctrl
= hdpvr_s_ctrl
,
1155 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1158 struct v4l2_ctrl_handler
*hdl
= &dev
->hdl
;
1159 bool ac3
= dev
->flags
& HDPVR_FLAG_AC3_CAP
;
1162 dev
->cur_std
= V4L2_STD_525_60
;
1165 dev
->cur_dv_timings
= hdpvr_dv_timings
[HDPVR_DEF_DV_TIMINGS_IDX
];
1166 v4l2_ctrl_handler_init(hdl
, 11);
1167 if (dev
->fw_ver
> 0x15) {
1168 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1169 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x80);
1170 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1171 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x40);
1172 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1173 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x40);
1174 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1175 V4L2_CID_HUE
, 0x0, 0x1e, 1, 0xf);
1176 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1177 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1179 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1180 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x86);
1181 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1182 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x80);
1183 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1184 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x80);
1185 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1186 V4L2_CID_HUE
, 0x0, 0xff, 1, 0x80);
1187 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1188 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1191 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1192 V4L2_CID_MPEG_STREAM_TYPE
,
1193 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
,
1194 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS
);
1195 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1196 V4L2_CID_MPEG_AUDIO_ENCODING
,
1197 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
: V4L2_MPEG_AUDIO_ENCODING_AAC
,
1198 0x7, ac3
? dev
->options
.audio_codec
: V4L2_MPEG_AUDIO_ENCODING_AAC
);
1199 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1200 V4L2_CID_MPEG_VIDEO_ENCODING
,
1201 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 0x3,
1202 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
1204 dev
->video_mode
= v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1205 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1206 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 0,
1207 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
1209 dev
->video_bitrate
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1210 V4L2_CID_MPEG_VIDEO_BITRATE
,
1211 1000000, 13500000, 100000, 6500000);
1212 dev
->video_bitrate_peak
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1213 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1214 1100000, 20200000, 100000, 9000000);
1215 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1218 v4l2_err(&dev
->v4l2_dev
, "Could not register controls\n");
1221 v4l2_ctrl_cluster(3, &dev
->video_mode
);
1222 res
= v4l2_ctrl_handler_setup(hdl
);
1224 v4l2_err(&dev
->v4l2_dev
, "Could not setup controls\n");
1228 /* setup and register video device */
1229 dev
->video_dev
= hdpvr_video_template
;
1230 strcpy(dev
->video_dev
.name
, "Hauppauge HD PVR");
1231 dev
->video_dev
.v4l2_dev
= &dev
->v4l2_dev
;
1232 video_set_drvdata(&dev
->video_dev
, dev
);
1234 res
= video_register_device(&dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
);
1236 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");
1242 v4l2_ctrl_handler_free(hdl
);