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 queue_work(dev
->workqueue
, &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_workqueue(dev
->workqueue
);
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
)) {
471 if (buf
->status
!= BUFSTAT_READY
)
474 /* set remaining bytes to copy */
476 rem
= urb
->actual_length
- buf
->pos
;
477 cnt
= rem
> count
? count
: rem
;
479 if (copy_to_user(buffer
, urb
->transfer_buffer
+ buf
->pos
,
481 v4l2_err(&dev
->v4l2_dev
, "read: copy_to_user failed\n");
492 /* finished, take next buffer */
493 if (buf
->pos
== urb
->actual_length
) {
494 mutex_lock(&dev
->io_mutex
);
496 buf
->status
= BUFSTAT_AVAILABLE
;
498 list_move_tail(&buf
->buff_list
, &dev
->free_buff_list
);
500 print_buffer_status();
502 mutex_unlock(&dev
->io_mutex
);
504 wake_up_interruptible(&dev
->wait_buffer
);
506 buf
= hdpvr_get_next_buffer(dev
);
515 static unsigned int hdpvr_poll(struct file
*filp
, poll_table
*wait
)
517 unsigned long req_events
= poll_requested_events(wait
);
518 struct hdpvr_buffer
*buf
= NULL
;
519 struct hdpvr_device
*dev
= video_drvdata(filp
);
520 unsigned int mask
= v4l2_ctrl_poll(filp
, wait
);
522 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
525 mutex_lock(&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
;
533 dev
->owner
= filp
->private_data
;
536 print_buffer_status();
538 mutex_unlock(&dev
->io_mutex
);
540 buf
= hdpvr_get_next_buffer(dev
);
541 /* only wait if no data is available */
542 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
543 poll_wait(filp
, &dev
->wait_data
, wait
);
544 buf
= hdpvr_get_next_buffer(dev
);
546 if (buf
&& buf
->status
== BUFSTAT_READY
)
547 mask
|= POLLIN
| POLLRDNORM
;
553 static const struct v4l2_file_operations hdpvr_fops
= {
554 .owner
= THIS_MODULE
,
556 .release
= hdpvr_release
,
559 .unlocked_ioctl
= video_ioctl2
,
562 /*=======================================================================*/
564 * V4L2 ioctl handling
567 static int vidioc_querycap(struct file
*file
, void *priv
,
568 struct v4l2_capability
*cap
)
570 struct hdpvr_device
*dev
= video_drvdata(file
);
572 strcpy(cap
->driver
, "hdpvr");
573 strcpy(cap
->card
, "Hauppauge HD PVR");
574 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
575 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
|
577 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
581 static int vidioc_s_std(struct file
*file
, void *_fh
,
584 struct hdpvr_device
*dev
= video_drvdata(file
);
585 struct hdpvr_fh
*fh
= _fh
;
588 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
590 if (dev
->status
!= STATUS_IDLE
)
592 if (std
& V4L2_STD_525_60
)
596 dev
->height
= std_type
? 576 : 480;
598 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
601 static int vidioc_g_std(struct file
*file
, void *_fh
,
604 struct hdpvr_device
*dev
= video_drvdata(file
);
605 struct hdpvr_fh
*fh
= _fh
;
607 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
613 static int vidioc_querystd(struct file
*file
, void *_fh
, v4l2_std_id
*a
)
615 struct hdpvr_device
*dev
= video_drvdata(file
);
616 struct hdpvr_video_info vid_info
;
617 struct hdpvr_fh
*fh
= _fh
;
620 *a
= V4L2_STD_UNKNOWN
;
621 if (dev
->options
.video_input
== HDPVR_COMPONENT
)
622 return fh
->legacy_mode
? 0 : -ENODATA
;
623 ret
= get_video_info(dev
, &vid_info
);
624 if (vid_info
.valid
&& vid_info
.width
== 720 &&
625 (vid_info
.height
== 480 || vid_info
.height
== 576)) {
626 *a
= (vid_info
.height
== 480) ?
627 V4L2_STD_525_60
: V4L2_STD_625_50
;
632 static int vidioc_s_dv_timings(struct file
*file
, void *_fh
,
633 struct v4l2_dv_timings
*timings
)
635 struct hdpvr_device
*dev
= video_drvdata(file
);
636 struct hdpvr_fh
*fh
= _fh
;
639 fh
->legacy_mode
= false;
640 if (dev
->options
.video_input
)
642 if (dev
->status
!= STATUS_IDLE
)
644 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++)
645 if (v4l2_match_dv_timings(timings
, hdpvr_dv_timings
+ i
, 0))
647 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
649 dev
->cur_dv_timings
= hdpvr_dv_timings
[i
];
650 dev
->width
= hdpvr_dv_timings
[i
].bt
.width
;
651 dev
->height
= hdpvr_dv_timings
[i
].bt
.height
;
655 static int vidioc_g_dv_timings(struct file
*file
, void *_fh
,
656 struct v4l2_dv_timings
*timings
)
658 struct hdpvr_device
*dev
= video_drvdata(file
);
659 struct hdpvr_fh
*fh
= _fh
;
661 fh
->legacy_mode
= false;
662 if (dev
->options
.video_input
)
664 *timings
= dev
->cur_dv_timings
;
668 static int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
669 struct v4l2_dv_timings
*timings
)
671 struct hdpvr_device
*dev
= video_drvdata(file
);
672 struct hdpvr_fh
*fh
= _fh
;
673 struct hdpvr_video_info vid_info
;
678 fh
->legacy_mode
= false;
679 if (dev
->options
.video_input
)
681 ret
= get_video_info(dev
, &vid_info
);
686 interlaced
= vid_info
.fps
<= 30;
687 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++) {
688 const struct v4l2_bt_timings
*bt
= &hdpvr_dv_timings
[i
].bt
;
693 hsize
= V4L2_DV_BT_FRAME_WIDTH(bt
);
694 vsize
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
695 fps
= (unsigned)bt
->pixelclock
/ (hsize
* vsize
);
696 if (bt
->width
!= vid_info
.width
||
697 bt
->height
!= vid_info
.height
||
698 bt
->interlaced
!= interlaced
||
699 (fps
!= vid_info
.fps
&& fps
+ 1 != vid_info
.fps
))
701 *timings
= hdpvr_dv_timings
[i
];
704 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
710 static int vidioc_enum_dv_timings(struct file
*file
, void *_fh
,
711 struct v4l2_enum_dv_timings
*timings
)
713 struct hdpvr_device
*dev
= video_drvdata(file
);
714 struct hdpvr_fh
*fh
= _fh
;
716 fh
->legacy_mode
= false;
717 memset(timings
->reserved
, 0, sizeof(timings
->reserved
));
718 if (dev
->options
.video_input
)
720 if (timings
->index
>= ARRAY_SIZE(hdpvr_dv_timings
))
722 timings
->timings
= hdpvr_dv_timings
[timings
->index
];
726 static int vidioc_dv_timings_cap(struct file
*file
, void *_fh
,
727 struct v4l2_dv_timings_cap
*cap
)
729 struct hdpvr_device
*dev
= video_drvdata(file
);
730 struct hdpvr_fh
*fh
= _fh
;
732 fh
->legacy_mode
= false;
733 if (dev
->options
.video_input
)
735 cap
->type
= V4L2_DV_BT_656_1120
;
736 cap
->bt
.min_width
= 720;
737 cap
->bt
.max_width
= 1920;
738 cap
->bt
.min_height
= 480;
739 cap
->bt
.max_height
= 1080;
740 cap
->bt
.min_pixelclock
= 27000000;
741 cap
->bt
.max_pixelclock
= 74250000;
742 cap
->bt
.standards
= V4L2_DV_BT_STD_CEA861
;
743 cap
->bt
.capabilities
= V4L2_DV_BT_CAP_INTERLACED
| V4L2_DV_BT_CAP_PROGRESSIVE
;
747 static const char *iname
[] = {
748 [HDPVR_COMPONENT
] = "Component",
749 [HDPVR_SVIDEO
] = "S-Video",
750 [HDPVR_COMPOSITE
] = "Composite",
753 static int vidioc_enum_input(struct file
*file
, void *_fh
, struct v4l2_input
*i
)
758 if (n
>= HDPVR_VIDEO_INPUTS
)
761 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
763 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
764 i
->name
[sizeof(i
->name
) - 1] = '\0';
766 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
768 i
->capabilities
= n
? V4L2_IN_CAP_STD
: V4L2_IN_CAP_DV_TIMINGS
;
769 i
->std
= n
? V4L2_STD_ALL
: 0;
774 static int vidioc_s_input(struct file
*file
, void *_fh
,
777 struct hdpvr_device
*dev
= video_drvdata(file
);
780 if (index
>= HDPVR_VIDEO_INPUTS
)
783 if (dev
->status
!= STATUS_IDLE
)
786 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
788 dev
->options
.video_input
= index
;
790 * Unfortunately gstreamer calls ENUMSTD and bails out if it
791 * won't find any formats, even though component input is
792 * selected. This means that we have to leave tvnorms at
793 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
794 * tvnorms is set at the device node level and not at the
797 * Comment this out for now, but if the legacy mode can be
798 * removed in the future, then this code should be enabled
800 dev->video_dev->tvnorms =
801 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
808 static int vidioc_g_input(struct file
*file
, void *private_data
,
811 struct hdpvr_device
*dev
= video_drvdata(file
);
813 *index
= dev
->options
.video_input
;
818 static const char *audio_iname
[] = {
819 [HDPVR_RCA_FRONT
] = "RCA front",
820 [HDPVR_RCA_BACK
] = "RCA back",
821 [HDPVR_SPDIF
] = "SPDIF",
824 static int vidioc_enumaudio(struct file
*file
, void *priv
,
825 struct v4l2_audio
*audio
)
830 if (n
>= HDPVR_AUDIO_INPUTS
)
833 audio
->capability
= V4L2_AUDCAP_STEREO
;
835 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
836 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
841 static int vidioc_s_audio(struct file
*file
, void *private_data
,
842 const struct v4l2_audio
*audio
)
844 struct hdpvr_device
*dev
= video_drvdata(file
);
847 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
850 if (dev
->status
!= STATUS_IDLE
)
853 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
855 dev
->options
.audio_input
= audio
->index
;
860 static int vidioc_g_audio(struct file
*file
, void *private_data
,
861 struct v4l2_audio
*audio
)
863 struct hdpvr_device
*dev
= video_drvdata(file
);
865 audio
->index
= dev
->options
.audio_input
;
866 audio
->capability
= V4L2_AUDCAP_STEREO
;
867 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
868 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
872 static int hdpvr_try_ctrl(struct v4l2_ctrl
*ctrl
)
874 struct hdpvr_device
*dev
=
875 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
878 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
879 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
880 dev
->video_bitrate
->val
>= dev
->video_bitrate_peak
->val
)
881 dev
->video_bitrate_peak
->val
=
882 dev
->video_bitrate
->val
+ 100000;
888 static int hdpvr_s_ctrl(struct v4l2_ctrl
*ctrl
)
890 struct hdpvr_device
*dev
=
891 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
892 struct hdpvr_options
*opt
= &dev
->options
;
896 case V4L2_CID_BRIGHTNESS
:
897 ret
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->val
);
900 dev
->options
.brightness
= ctrl
->val
;
902 case V4L2_CID_CONTRAST
:
903 ret
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->val
);
906 dev
->options
.contrast
= ctrl
->val
;
908 case V4L2_CID_SATURATION
:
909 ret
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->val
);
912 dev
->options
.saturation
= ctrl
->val
;
915 ret
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->val
);
918 dev
->options
.hue
= ctrl
->val
;
920 case V4L2_CID_SHARPNESS
:
921 ret
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->val
);
924 dev
->options
.sharpness
= ctrl
->val
;
926 case V4L2_CID_MPEG_AUDIO_ENCODING
:
927 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
928 opt
->audio_codec
= ctrl
->val
;
929 return hdpvr_set_audio(dev
, opt
->audio_input
,
933 case V4L2_CID_MPEG_VIDEO_ENCODING
:
935 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
936 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
937 /* opt->gop_mode |= 0x2; */
938 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
939 /* opt->gop_mode); */
941 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
942 /* opt->gop_mode &= ~0x2; */
943 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
944 /* opt->gop_mode); */
947 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
: {
948 uint peak_bitrate
= dev
->video_bitrate_peak
->val
/ 100000;
949 uint bitrate
= dev
->video_bitrate
->val
/ 100000;
952 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
)
953 opt
->bitrate_mode
= HDPVR_CONSTANT
;
955 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
956 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
958 v4l2_ctrl_activate(dev
->video_bitrate_peak
,
959 ctrl
->val
!= V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
962 if (dev
->video_bitrate_peak
->is_new
||
963 dev
->video_bitrate
->is_new
) {
964 opt
->bitrate
= bitrate
;
965 opt
->peak_bitrate
= peak_bitrate
;
966 hdpvr_set_bitrate(dev
);
970 case V4L2_CID_MPEG_STREAM_TYPE
:
978 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
979 struct v4l2_fmtdesc
*f
)
984 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
985 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
986 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
991 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *_fh
,
992 struct v4l2_format
*f
)
994 struct hdpvr_device
*dev
= video_drvdata(file
);
995 struct hdpvr_fh
*fh
= _fh
;
999 * The original driver would always returns the current detected
1000 * resolution as the format (and EFAULT if it couldn't be detected).
1001 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
1002 * better way of doing this, but to stay compatible with existing
1003 * applications we assume legacy mode every time an application opens
1004 * the device. Only if one of the new DV_TIMINGS ioctls is called
1005 * will the filehandle go into 'normal' mode where g_fmt returns the
1008 if (fh
->legacy_mode
) {
1009 struct hdpvr_video_info vid_info
;
1011 ret
= get_video_info(dev
, &vid_info
);
1014 if (!vid_info
.valid
)
1016 f
->fmt
.pix
.width
= vid_info
.width
;
1017 f
->fmt
.pix
.height
= vid_info
.height
;
1019 f
->fmt
.pix
.width
= dev
->width
;
1020 f
->fmt
.pix
.height
= dev
->height
;
1022 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1023 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1024 f
->fmt
.pix
.bytesperline
= 0;
1025 f
->fmt
.pix
.priv
= 0;
1026 if (f
->fmt
.pix
.width
== 720) {
1028 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
1029 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
1032 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE240M
;
1033 f
->fmt
.pix
.field
= V4L2_FIELD_NONE
;
1038 static int vidioc_encoder_cmd(struct file
*filp
, void *priv
,
1039 struct v4l2_encoder_cmd
*a
)
1041 struct hdpvr_device
*dev
= video_drvdata(filp
);
1044 mutex_lock(&dev
->io_mutex
);
1048 case V4L2_ENC_CMD_START
:
1049 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1053 if (dev
->status
== STATUS_STREAMING
)
1055 res
= hdpvr_start_streaming(dev
);
1057 dev
->owner
= filp
->private_data
;
1059 dev
->status
= STATUS_IDLE
;
1061 case V4L2_ENC_CMD_STOP
:
1062 if (dev
->owner
&& filp
->private_data
!= dev
->owner
) {
1066 if (dev
->status
== STATUS_IDLE
)
1068 res
= hdpvr_stop_streaming(dev
);
1073 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
1074 "Unsupported encoder cmd %d\n", a
->cmd
);
1079 mutex_unlock(&dev
->io_mutex
);
1083 static int vidioc_try_encoder_cmd(struct file
*filp
, void *priv
,
1084 struct v4l2_encoder_cmd
*a
)
1088 case V4L2_ENC_CMD_START
:
1089 case V4L2_ENC_CMD_STOP
:
1096 static const struct v4l2_ioctl_ops hdpvr_ioctl_ops
= {
1097 .vidioc_querycap
= vidioc_querycap
,
1098 .vidioc_s_std
= vidioc_s_std
,
1099 .vidioc_g_std
= vidioc_g_std
,
1100 .vidioc_querystd
= vidioc_querystd
,
1101 .vidioc_s_dv_timings
= vidioc_s_dv_timings
,
1102 .vidioc_g_dv_timings
= vidioc_g_dv_timings
,
1103 .vidioc_query_dv_timings
= vidioc_query_dv_timings
,
1104 .vidioc_enum_dv_timings
= vidioc_enum_dv_timings
,
1105 .vidioc_dv_timings_cap
= vidioc_dv_timings_cap
,
1106 .vidioc_enum_input
= vidioc_enum_input
,
1107 .vidioc_g_input
= vidioc_g_input
,
1108 .vidioc_s_input
= vidioc_s_input
,
1109 .vidioc_enumaudio
= vidioc_enumaudio
,
1110 .vidioc_g_audio
= vidioc_g_audio
,
1111 .vidioc_s_audio
= vidioc_s_audio
,
1112 .vidioc_enum_fmt_vid_cap
= vidioc_enum_fmt_vid_cap
,
1113 .vidioc_g_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1114 .vidioc_s_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1115 .vidioc_try_fmt_vid_cap
= vidioc_g_fmt_vid_cap
,
1116 .vidioc_encoder_cmd
= vidioc_encoder_cmd
,
1117 .vidioc_try_encoder_cmd
= vidioc_try_encoder_cmd
,
1118 .vidioc_log_status
= v4l2_ctrl_log_status
,
1119 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
1120 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
1123 static void hdpvr_device_release(struct video_device
*vdev
)
1125 struct hdpvr_device
*dev
= video_get_drvdata(vdev
);
1128 mutex_lock(&dev
->io_mutex
);
1129 destroy_workqueue(dev
->workqueue
);
1130 mutex_unlock(&dev
->io_mutex
);
1132 v4l2_device_unregister(&dev
->v4l2_dev
);
1133 v4l2_ctrl_handler_free(&dev
->hdl
);
1135 /* deregister I2C adapter */
1136 #if IS_ENABLED(CONFIG_I2C)
1137 mutex_lock(&dev
->i2c_mutex
);
1138 i2c_del_adapter(&dev
->i2c_adapter
);
1139 mutex_unlock(&dev
->i2c_mutex
);
1140 #endif /* CONFIG_I2C */
1142 kfree(dev
->usbc_buf
);
1146 static const struct video_device hdpvr_video_template
= {
1147 .fops
= &hdpvr_fops
,
1148 .release
= hdpvr_device_release
,
1149 .ioctl_ops
= &hdpvr_ioctl_ops
,
1150 .tvnorms
= V4L2_STD_ALL
,
1153 static const struct v4l2_ctrl_ops hdpvr_ctrl_ops
= {
1154 .try_ctrl
= hdpvr_try_ctrl
,
1155 .s_ctrl
= hdpvr_s_ctrl
,
1158 int hdpvr_register_videodev(struct hdpvr_device
*dev
, struct device
*parent
,
1161 struct v4l2_ctrl_handler
*hdl
= &dev
->hdl
;
1162 bool ac3
= dev
->flags
& HDPVR_FLAG_AC3_CAP
;
1165 dev
->cur_std
= V4L2_STD_525_60
;
1168 dev
->cur_dv_timings
= hdpvr_dv_timings
[HDPVR_DEF_DV_TIMINGS_IDX
];
1169 v4l2_ctrl_handler_init(hdl
, 11);
1170 if (dev
->fw_ver
> 0x15) {
1171 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1172 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x80);
1173 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1174 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x40);
1175 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1176 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x40);
1177 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1178 V4L2_CID_HUE
, 0x0, 0x1e, 1, 0xf);
1179 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1180 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1182 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1183 V4L2_CID_BRIGHTNESS
, 0x0, 0xff, 1, 0x86);
1184 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1185 V4L2_CID_CONTRAST
, 0x0, 0xff, 1, 0x80);
1186 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1187 V4L2_CID_SATURATION
, 0x0, 0xff, 1, 0x80);
1188 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1189 V4L2_CID_HUE
, 0x0, 0xff, 1, 0x80);
1190 v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1191 V4L2_CID_SHARPNESS
, 0x0, 0xff, 1, 0x80);
1194 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1195 V4L2_CID_MPEG_STREAM_TYPE
,
1196 V4L2_MPEG_STREAM_TYPE_MPEG2_TS
,
1197 0x1, V4L2_MPEG_STREAM_TYPE_MPEG2_TS
);
1198 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1199 V4L2_CID_MPEG_AUDIO_ENCODING
,
1200 ac3
? V4L2_MPEG_AUDIO_ENCODING_AC3
: V4L2_MPEG_AUDIO_ENCODING_AAC
,
1201 0x7, V4L2_MPEG_AUDIO_ENCODING_AAC
);
1202 v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1203 V4L2_CID_MPEG_VIDEO_ENCODING
,
1204 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
, 0x3,
1205 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC
);
1207 dev
->video_mode
= v4l2_ctrl_new_std_menu(hdl
, &hdpvr_ctrl_ops
,
1208 V4L2_CID_MPEG_VIDEO_BITRATE_MODE
,
1209 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
, 0,
1210 V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
1212 dev
->video_bitrate
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1213 V4L2_CID_MPEG_VIDEO_BITRATE
,
1214 1000000, 13500000, 100000, 6500000);
1215 dev
->video_bitrate_peak
= v4l2_ctrl_new_std(hdl
, &hdpvr_ctrl_ops
,
1216 V4L2_CID_MPEG_VIDEO_BITRATE_PEAK
,
1217 1100000, 20200000, 100000, 9000000);
1218 dev
->v4l2_dev
.ctrl_handler
= hdl
;
1221 v4l2_err(&dev
->v4l2_dev
, "Could not register controls\n");
1224 v4l2_ctrl_cluster(3, &dev
->video_mode
);
1225 res
= v4l2_ctrl_handler_setup(hdl
);
1227 v4l2_err(&dev
->v4l2_dev
, "Could not setup controls\n");
1231 /* setup and register video device */
1232 dev
->video_dev
= video_device_alloc();
1233 if (!dev
->video_dev
) {
1234 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
->v4l2_dev
= &dev
->v4l2_dev
;
1242 video_set_drvdata(dev
->video_dev
, dev
);
1243 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->video_dev
->flags
);
1245 res
= video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
);
1247 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");
1253 v4l2_ctrl_handler_free(hdl
);