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-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
);
158 v4l2_err(&dev
->v4l2_dev
, "cannot allocate urb\n");
163 mem
= usb_alloc_coherent(dev
->udev
, dev
->bulk_in_size
, GFP_KERNEL
,
166 v4l2_err(&dev
->v4l2_dev
,
167 "cannot allocate usb transfer buffer\n");
168 goto exit_urb_buffer
;
171 usb_fill_bulk_urb(buf
->urb
, dev
->udev
,
172 usb_rcvbulkpipe(dev
->udev
,
173 dev
->bulk_in_endpointAddr
),
174 mem
, dev
->bulk_in_size
,
175 hdpvr_read_bulk_callback
, buf
);
177 buf
->urb
->transfer_flags
|= URB_NO_TRANSFER_DMA_MAP
;
178 buf
->status
= BUFSTAT_AVAILABLE
;
179 list_add_tail(&buf
->buff_list
, &dev
->free_buff_list
);
187 hdpvr_free_buffers(dev
);
191 static int hdpvr_submit_buffers(struct hdpvr_device
*dev
)
193 struct hdpvr_buffer
*buf
;
195 int ret
= 0, err_count
= 0;
197 mutex_lock(&dev
->io_mutex
);
199 while (dev
->status
== STATUS_STREAMING
&&
200 !list_empty(&dev
->free_buff_list
)) {
202 buf
= list_entry(dev
->free_buff_list
.next
, struct hdpvr_buffer
,
204 if (buf
->status
!= BUFSTAT_AVAILABLE
) {
205 v4l2_err(&dev
->v4l2_dev
,
206 "buffer not marked as available\n");
213 urb
->actual_length
= 0;
214 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
216 v4l2_err(&dev
->v4l2_dev
,
217 "usb_submit_urb in %s returned %d\n",
223 buf
->status
= BUFSTAT_INPROGRESS
;
224 list_move_tail(&buf
->buff_list
, &dev
->rec_buff_list
);
227 print_buffer_status();
228 mutex_unlock(&dev
->io_mutex
);
232 static struct hdpvr_buffer
*hdpvr_get_next_buffer(struct hdpvr_device
*dev
)
234 struct hdpvr_buffer
*buf
;
236 mutex_lock(&dev
->io_mutex
);
238 if (list_empty(&dev
->rec_buff_list
)) {
239 mutex_unlock(&dev
->io_mutex
);
243 buf
= list_entry(dev
->rec_buff_list
.next
, struct hdpvr_buffer
,
245 mutex_unlock(&dev
->io_mutex
);
250 static void hdpvr_transmit_buffers(struct work_struct
*work
)
252 struct hdpvr_device
*dev
= container_of(work
, struct hdpvr_device
,
255 while (dev
->status
== STATUS_STREAMING
) {
257 if (hdpvr_submit_buffers(dev
)) {
258 v4l2_err(&dev
->v4l2_dev
, "couldn't submit buffers\n");
261 if (wait_event_interruptible(dev
->wait_buffer
,
262 !list_empty(&dev
->free_buff_list
) ||
263 dev
->status
!= STATUS_STREAMING
))
267 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
268 "transmit worker exited\n");
271 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
272 "transmit buffers errored\n");
273 dev
->status
= STATUS_ERROR
;
276 /* function expects dev->io_mutex to be hold by caller */
277 static int hdpvr_start_streaming(struct hdpvr_device
*dev
)
280 struct hdpvr_video_info vidinf
;
282 if (dev
->status
== STATUS_STREAMING
)
284 if (dev
->status
!= STATUS_IDLE
)
287 ret
= get_video_info(dev
, &vidinf
);
290 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
291 "no video signal at input %d\n", dev
->options
.video_input
);
295 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
296 "video signal: %dx%d@%dhz\n", vidinf
.width
,
297 vidinf
.height
, vidinf
.fps
);
299 /* start streaming 2 request */
300 ret
= usb_control_msg(dev
->udev
,
301 usb_sndctrlpipe(dev
->udev
, 0),
302 0xb8, 0x38, 0x1, 0, NULL
, 0, 8000);
303 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
304 "encoder start control request returned %d\n", ret
);
308 ret
= hdpvr_config_call(dev
, CTRL_START_STREAMING_VALUE
, 0x00);
312 dev
->status
= STATUS_STREAMING
;
314 INIT_WORK(&dev
->worker
, hdpvr_transmit_buffers
);
315 queue_work(dev
->workqueue
, &dev
->worker
);
317 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
318 "streaming started\n");
324 /* function expects dev->io_mutex to be hold by caller */
325 static int hdpvr_stop_streaming(struct hdpvr_device
*dev
)
331 if (dev
->status
== STATUS_IDLE
)
333 else if (dev
->status
!= STATUS_STREAMING
)
336 buf
= kmalloc(dev
->bulk_in_size
, GFP_KERNEL
);
338 v4l2_err(&dev
->v4l2_dev
, "failed to allocate temporary buffer "
339 "for emptying the internal device buffer. "
340 "Next capture start will be slow\n");
342 dev
->status
= STATUS_SHUTTING_DOWN
;
343 hdpvr_config_call(dev
, CTRL_STOP_STREAMING_VALUE
, 0x00);
344 mutex_unlock(&dev
->io_mutex
);
346 wake_up_interruptible(&dev
->wait_buffer
);
349 flush_workqueue(dev
->workqueue
);
351 mutex_lock(&dev
->io_mutex
);
352 /* kill the still outstanding urbs */
353 hdpvr_cancel_queue(dev
);
355 /* emptying the device buffer beforeshutting it down */
356 while (buf
&& ++c
< 500 &&
357 !usb_bulk_msg(dev
->udev
,
358 usb_rcvbulkpipe(dev
->udev
,
359 dev
->bulk_in_endpointAddr
),
360 buf
, dev
->bulk_in_size
, &actual_length
,
362 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
363 "%2d: got %d bytes\n", c
, actual_length
);
366 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
367 "used %d urbs to empty device buffers\n", c
-1);
370 dev
->status
= STATUS_IDLE
;
376 /*=======================================================================*/
378 * video 4 linux 2 file operations
381 static int hdpvr_open(struct file
*file
)
383 struct hdpvr_fh
*fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
387 fh
->legacy_mode
= true;
388 v4l2_fh_init(&fh
->fh
, video_devdata(file
));
389 v4l2_fh_add(&fh
->fh
);
390 file
->private_data
= fh
;
394 static int hdpvr_release(struct file
*file
)
396 struct hdpvr_device
*dev
= video_drvdata(file
);
398 mutex_lock(&dev
->io_mutex
);
399 if (file
->private_data
== dev
->owner
) {
400 hdpvr_stop_streaming(dev
);
403 mutex_unlock(&dev
->io_mutex
);
405 return v4l2_fh_release(file
);
410 * will allocate buffers when called for the first time
412 static ssize_t
hdpvr_read(struct file
*file
, char __user
*buffer
, size_t count
,
415 struct hdpvr_device
*dev
= video_drvdata(file
);
416 struct hdpvr_buffer
*buf
= NULL
;
418 unsigned int ret
= 0;
424 mutex_lock(&dev
->io_mutex
);
425 if (dev
->status
== STATUS_IDLE
) {
426 if (hdpvr_start_streaming(dev
)) {
427 v4l2_dbg(MSG_INFO
, hdpvr_debug
, &dev
->v4l2_dev
,
428 "start_streaming failed\n");
431 dev
->status
= STATUS_IDLE
;
432 mutex_unlock(&dev
->io_mutex
);
435 dev
->owner
= file
->private_data
;
436 print_buffer_status();
438 mutex_unlock(&dev
->io_mutex
);
440 /* wait for the first buffer */
441 if (!(file
->f_flags
& O_NONBLOCK
)) {
442 if (wait_event_interruptible(dev
->wait_data
,
443 hdpvr_get_next_buffer(dev
)))
447 buf
= hdpvr_get_next_buffer(dev
);
449 while (count
> 0 && buf
) {
451 if (buf
->status
!= BUFSTAT_READY
&&
452 dev
->status
!= STATUS_DISCONNECTED
) {
453 /* return nonblocking */
454 if (file
->f_flags
& O_NONBLOCK
) {
460 if (wait_event_interruptible(dev
->wait_data
,
461 buf
->status
== BUFSTAT_READY
)) {
467 if (buf
->status
!= BUFSTAT_READY
)
470 /* set remaining bytes to copy */
472 rem
= urb
->actual_length
- buf
->pos
;
473 cnt
= rem
> count
? count
: rem
;
475 if (copy_to_user(buffer
, urb
->transfer_buffer
+ buf
->pos
,
477 v4l2_err(&dev
->v4l2_dev
, "read: copy_to_user failed\n");
488 /* finished, take next buffer */
489 if (buf
->pos
== urb
->actual_length
) {
490 mutex_lock(&dev
->io_mutex
);
492 buf
->status
= BUFSTAT_AVAILABLE
;
494 list_move_tail(&buf
->buff_list
, &dev
->free_buff_list
);
496 print_buffer_status();
498 mutex_unlock(&dev
->io_mutex
);
500 wake_up_interruptible(&dev
->wait_buffer
);
502 buf
= hdpvr_get_next_buffer(dev
);
511 static unsigned int hdpvr_poll(struct file
*filp
, poll_table
*wait
)
513 unsigned long req_events
= poll_requested_events(wait
);
514 struct hdpvr_buffer
*buf
= NULL
;
515 struct hdpvr_device
*dev
= video_drvdata(filp
);
516 unsigned int mask
= v4l2_ctrl_poll(filp
, wait
);
518 if (!(req_events
& (POLLIN
| POLLRDNORM
)))
521 mutex_lock(&dev
->io_mutex
);
523 if (dev
->status
== STATUS_IDLE
) {
524 if (hdpvr_start_streaming(dev
)) {
525 v4l2_dbg(MSG_BUFFER
, hdpvr_debug
, &dev
->v4l2_dev
,
526 "start_streaming failed\n");
527 dev
->status
= STATUS_IDLE
;
529 dev
->owner
= filp
->private_data
;
532 print_buffer_status();
534 mutex_unlock(&dev
->io_mutex
);
536 buf
= hdpvr_get_next_buffer(dev
);
537 /* only wait if no data is available */
538 if (!buf
|| buf
->status
!= BUFSTAT_READY
) {
539 poll_wait(filp
, &dev
->wait_data
, wait
);
540 buf
= hdpvr_get_next_buffer(dev
);
542 if (buf
&& buf
->status
== BUFSTAT_READY
)
543 mask
|= POLLIN
| POLLRDNORM
;
549 static const struct v4l2_file_operations hdpvr_fops
= {
550 .owner
= THIS_MODULE
,
552 .release
= hdpvr_release
,
555 .unlocked_ioctl
= video_ioctl2
,
558 /*=======================================================================*/
560 * V4L2 ioctl handling
563 static int vidioc_querycap(struct file
*file
, void *priv
,
564 struct v4l2_capability
*cap
)
566 struct hdpvr_device
*dev
= video_drvdata(file
);
568 strcpy(cap
->driver
, "hdpvr");
569 strcpy(cap
->card
, "Hauppauge HD PVR");
570 usb_make_path(dev
->udev
, cap
->bus_info
, sizeof(cap
->bus_info
));
571 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_AUDIO
|
573 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
577 static int vidioc_s_std(struct file
*file
, void *_fh
,
580 struct hdpvr_device
*dev
= video_drvdata(file
);
581 struct hdpvr_fh
*fh
= _fh
;
584 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
586 if (dev
->status
!= STATUS_IDLE
)
588 if (std
& V4L2_STD_525_60
)
592 dev
->height
= std_type
? 576 : 480;
594 return hdpvr_config_call(dev
, CTRL_VIDEO_STD_TYPE
, std_type
);
597 static int vidioc_g_std(struct file
*file
, void *_fh
,
600 struct hdpvr_device
*dev
= video_drvdata(file
);
601 struct hdpvr_fh
*fh
= _fh
;
603 if (!fh
->legacy_mode
&& dev
->options
.video_input
== HDPVR_COMPONENT
)
609 static int vidioc_querystd(struct file
*file
, void *_fh
, v4l2_std_id
*a
)
611 struct hdpvr_device
*dev
= video_drvdata(file
);
612 struct hdpvr_video_info vid_info
;
613 struct hdpvr_fh
*fh
= _fh
;
616 *a
= V4L2_STD_UNKNOWN
;
617 if (dev
->options
.video_input
== HDPVR_COMPONENT
)
618 return fh
->legacy_mode
? 0 : -ENODATA
;
619 ret
= get_video_info(dev
, &vid_info
);
622 if (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
;
631 static int vidioc_s_dv_timings(struct file
*file
, void *_fh
,
632 struct v4l2_dv_timings
*timings
)
634 struct hdpvr_device
*dev
= video_drvdata(file
);
635 struct hdpvr_fh
*fh
= _fh
;
638 fh
->legacy_mode
= false;
639 if (dev
->options
.video_input
)
641 if (dev
->status
!= STATUS_IDLE
)
643 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++)
644 if (v4l_match_dv_timings(timings
, hdpvr_dv_timings
+ i
, 0))
646 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
648 dev
->cur_dv_timings
= hdpvr_dv_timings
[i
];
649 dev
->width
= hdpvr_dv_timings
[i
].bt
.width
;
650 dev
->height
= hdpvr_dv_timings
[i
].bt
.height
;
654 static int vidioc_g_dv_timings(struct file
*file
, void *_fh
,
655 struct v4l2_dv_timings
*timings
)
657 struct hdpvr_device
*dev
= video_drvdata(file
);
658 struct hdpvr_fh
*fh
= _fh
;
660 fh
->legacy_mode
= false;
661 if (dev
->options
.video_input
)
663 *timings
= dev
->cur_dv_timings
;
667 static int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
668 struct v4l2_dv_timings
*timings
)
670 struct hdpvr_device
*dev
= video_drvdata(file
);
671 struct hdpvr_fh
*fh
= _fh
;
672 struct hdpvr_video_info vid_info
;
677 fh
->legacy_mode
= false;
678 if (dev
->options
.video_input
)
680 ret
= get_video_info(dev
, &vid_info
);
683 interlaced
= vid_info
.fps
<= 30;
684 for (i
= 0; i
< ARRAY_SIZE(hdpvr_dv_timings
); i
++) {
685 const struct v4l2_bt_timings
*bt
= &hdpvr_dv_timings
[i
].bt
;
690 hsize
= bt
->hfrontporch
+ bt
->hsync
+ bt
->hbackporch
+ bt
->width
;
691 vsize
= bt
->vfrontporch
+ bt
->vsync
+ bt
->vbackporch
+
692 bt
->il_vfrontporch
+ bt
->il_vsync
+ bt
->il_vbackporch
+
694 fps
= (unsigned)bt
->pixelclock
/ (hsize
* vsize
);
695 if (bt
->width
!= vid_info
.width
||
696 bt
->height
!= vid_info
.height
||
697 bt
->interlaced
!= interlaced
||
698 (fps
!= vid_info
.fps
&& fps
+ 1 != vid_info
.fps
))
700 *timings
= hdpvr_dv_timings
[i
];
703 if (i
== ARRAY_SIZE(hdpvr_dv_timings
))
709 static int vidioc_enum_dv_timings(struct file
*file
, void *_fh
,
710 struct v4l2_enum_dv_timings
*timings
)
712 struct hdpvr_device
*dev
= video_drvdata(file
);
713 struct hdpvr_fh
*fh
= _fh
;
715 fh
->legacy_mode
= false;
716 memset(timings
->reserved
, 0, sizeof(timings
->reserved
));
717 if (dev
->options
.video_input
)
719 if (timings
->index
>= ARRAY_SIZE(hdpvr_dv_timings
))
721 timings
->timings
= hdpvr_dv_timings
[timings
->index
];
725 static int vidioc_dv_timings_cap(struct file
*file
, void *_fh
,
726 struct v4l2_dv_timings_cap
*cap
)
728 struct hdpvr_device
*dev
= video_drvdata(file
);
729 struct hdpvr_fh
*fh
= _fh
;
731 fh
->legacy_mode
= false;
732 if (dev
->options
.video_input
)
734 cap
->type
= V4L2_DV_BT_656_1120
;
735 cap
->bt
.min_width
= 720;
736 cap
->bt
.max_width
= 1920;
737 cap
->bt
.min_height
= 480;
738 cap
->bt
.max_height
= 1080;
739 cap
->bt
.min_pixelclock
= 27000000;
740 cap
->bt
.max_pixelclock
= 74250000;
741 cap
->bt
.standards
= V4L2_DV_BT_STD_CEA861
;
742 cap
->bt
.capabilities
= V4L2_DV_BT_CAP_INTERLACED
| V4L2_DV_BT_CAP_PROGRESSIVE
;
746 static const char *iname
[] = {
747 [HDPVR_COMPONENT
] = "Component",
748 [HDPVR_SVIDEO
] = "S-Video",
749 [HDPVR_COMPOSITE
] = "Composite",
752 static int vidioc_enum_input(struct file
*file
, void *_fh
, struct v4l2_input
*i
)
757 if (n
>= HDPVR_VIDEO_INPUTS
)
760 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
762 strncpy(i
->name
, iname
[n
], sizeof(i
->name
) - 1);
763 i
->name
[sizeof(i
->name
) - 1] = '\0';
765 i
->audioset
= 1<<HDPVR_RCA_FRONT
| 1<<HDPVR_RCA_BACK
| 1<<HDPVR_SPDIF
;
767 i
->capabilities
= n
? V4L2_IN_CAP_STD
: V4L2_IN_CAP_DV_TIMINGS
;
768 i
->std
= n
? V4L2_STD_ALL
: 0;
773 static int vidioc_s_input(struct file
*file
, void *_fh
,
776 struct hdpvr_device
*dev
= video_drvdata(file
);
779 if (index
>= HDPVR_VIDEO_INPUTS
)
782 if (dev
->status
!= STATUS_IDLE
)
785 retval
= hdpvr_config_call(dev
, CTRL_VIDEO_INPUT_VALUE
, index
+1);
787 dev
->options
.video_input
= index
;
789 * Unfortunately gstreamer calls ENUMSTD and bails out if it
790 * won't find any formats, even though component input is
791 * selected. This means that we have to leave tvnorms at
792 * V4L2_STD_ALL. We cannot use the 'legacy' trick since
793 * tvnorms is set at the device node level and not at the
796 * Comment this out for now, but if the legacy mode can be
797 * removed in the future, then this code should be enabled
799 dev->video_dev->tvnorms =
800 (index != HDPVR_COMPONENT) ? V4L2_STD_ALL : 0;
807 static int vidioc_g_input(struct file
*file
, void *private_data
,
810 struct hdpvr_device
*dev
= video_drvdata(file
);
812 *index
= dev
->options
.video_input
;
817 static const char *audio_iname
[] = {
818 [HDPVR_RCA_FRONT
] = "RCA front",
819 [HDPVR_RCA_BACK
] = "RCA back",
820 [HDPVR_SPDIF
] = "SPDIF",
823 static int vidioc_enumaudio(struct file
*file
, void *priv
,
824 struct v4l2_audio
*audio
)
829 if (n
>= HDPVR_AUDIO_INPUTS
)
832 audio
->capability
= V4L2_AUDCAP_STEREO
;
834 strncpy(audio
->name
, audio_iname
[n
], sizeof(audio
->name
) - 1);
835 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
840 static int vidioc_s_audio(struct file
*file
, void *private_data
,
841 const struct v4l2_audio
*audio
)
843 struct hdpvr_device
*dev
= video_drvdata(file
);
846 if (audio
->index
>= HDPVR_AUDIO_INPUTS
)
849 if (dev
->status
!= STATUS_IDLE
)
852 retval
= hdpvr_set_audio(dev
, audio
->index
+1, dev
->options
.audio_codec
);
854 dev
->options
.audio_input
= audio
->index
;
859 static int vidioc_g_audio(struct file
*file
, void *private_data
,
860 struct v4l2_audio
*audio
)
862 struct hdpvr_device
*dev
= video_drvdata(file
);
864 audio
->index
= dev
->options
.audio_input
;
865 audio
->capability
= V4L2_AUDCAP_STEREO
;
866 strncpy(audio
->name
, audio_iname
[audio
->index
], sizeof(audio
->name
));
867 audio
->name
[sizeof(audio
->name
) - 1] = '\0';
871 static int hdpvr_try_ctrl(struct v4l2_ctrl
*ctrl
)
873 struct hdpvr_device
*dev
=
874 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
877 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
:
878 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_VBR
&&
879 dev
->video_bitrate
->val
>= dev
->video_bitrate_peak
->val
)
880 dev
->video_bitrate_peak
->val
=
881 dev
->video_bitrate
->val
+ 100000;
887 static int hdpvr_s_ctrl(struct v4l2_ctrl
*ctrl
)
889 struct hdpvr_device
*dev
=
890 container_of(ctrl
->handler
, struct hdpvr_device
, hdl
);
891 struct hdpvr_options
*opt
= &dev
->options
;
895 case V4L2_CID_BRIGHTNESS
:
896 ret
= hdpvr_config_call(dev
, CTRL_BRIGHTNESS
, ctrl
->val
);
899 dev
->options
.brightness
= ctrl
->val
;
901 case V4L2_CID_CONTRAST
:
902 ret
= hdpvr_config_call(dev
, CTRL_CONTRAST
, ctrl
->val
);
905 dev
->options
.contrast
= ctrl
->val
;
907 case V4L2_CID_SATURATION
:
908 ret
= hdpvr_config_call(dev
, CTRL_SATURATION
, ctrl
->val
);
911 dev
->options
.saturation
= ctrl
->val
;
914 ret
= hdpvr_config_call(dev
, CTRL_HUE
, ctrl
->val
);
917 dev
->options
.hue
= ctrl
->val
;
919 case V4L2_CID_SHARPNESS
:
920 ret
= hdpvr_config_call(dev
, CTRL_SHARPNESS
, ctrl
->val
);
923 dev
->options
.sharpness
= ctrl
->val
;
925 case V4L2_CID_MPEG_AUDIO_ENCODING
:
926 if (dev
->flags
& HDPVR_FLAG_AC3_CAP
) {
927 opt
->audio_codec
= ctrl
->val
;
928 return hdpvr_set_audio(dev
, opt
->audio_input
,
932 case V4L2_CID_MPEG_VIDEO_ENCODING
:
934 /* case V4L2_CID_MPEG_VIDEO_B_FRAMES: */
935 /* if (ctrl->value == 0 && !(opt->gop_mode & 0x2)) { */
936 /* opt->gop_mode |= 0x2; */
937 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
938 /* opt->gop_mode); */
940 /* if (ctrl->value == 128 && opt->gop_mode & 0x2) { */
941 /* opt->gop_mode &= ~0x2; */
942 /* hdpvr_config_call(dev, CTRL_GOP_MODE_VALUE, */
943 /* opt->gop_mode); */
946 case V4L2_CID_MPEG_VIDEO_BITRATE_MODE
: {
947 uint peak_bitrate
= dev
->video_bitrate_peak
->val
/ 100000;
948 uint bitrate
= dev
->video_bitrate
->val
/ 100000;
951 if (ctrl
->val
== V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
)
952 opt
->bitrate_mode
= HDPVR_CONSTANT
;
954 opt
->bitrate_mode
= HDPVR_VARIABLE_AVERAGE
;
955 hdpvr_config_call(dev
, CTRL_BITRATE_MODE_VALUE
,
957 v4l2_ctrl_activate(dev
->video_bitrate_peak
,
958 ctrl
->val
!= V4L2_MPEG_VIDEO_BITRATE_MODE_CBR
);
961 if (dev
->video_bitrate_peak
->is_new
||
962 dev
->video_bitrate
->is_new
) {
963 opt
->bitrate
= bitrate
;
964 opt
->peak_bitrate
= peak_bitrate
;
965 hdpvr_set_bitrate(dev
);
969 case V4L2_CID_MPEG_STREAM_TYPE
:
977 static int vidioc_enum_fmt_vid_cap(struct file
*file
, void *private_data
,
978 struct v4l2_fmtdesc
*f
)
983 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
984 strncpy(f
->description
, "MPEG2-TS with AVC/AAC streams", 32);
985 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
990 static int vidioc_g_fmt_vid_cap(struct file
*file
, void *_fh
,
991 struct v4l2_format
*f
)
993 struct hdpvr_device
*dev
= video_drvdata(file
);
994 struct hdpvr_fh
*fh
= _fh
;
998 * The original driver would always returns the current detected
999 * resolution as the format (and EFAULT if it couldn't be detected).
1000 * With the introduction of VIDIOC_QUERY_DV_TIMINGS there is now a
1001 * better way of doing this, but to stay compatible with existing
1002 * applications we assume legacy mode every time an application opens
1003 * the device. Only if one of the new DV_TIMINGS ioctls is called
1004 * will the filehandle go into 'normal' mode where g_fmt returns the
1007 if (fh
->legacy_mode
) {
1008 struct hdpvr_video_info vid_info
;
1010 ret
= get_video_info(dev
, &vid_info
);
1013 f
->fmt
.pix
.width
= vid_info
.width
;
1014 f
->fmt
.pix
.height
= vid_info
.height
;
1016 f
->fmt
.pix
.width
= dev
->width
;
1017 f
->fmt
.pix
.height
= dev
->height
;
1019 f
->fmt
.pix
.pixelformat
= V4L2_PIX_FMT_MPEG
;
1020 f
->fmt
.pix
.sizeimage
= dev
->bulk_in_size
;
1021 f
->fmt
.pix
.bytesperline
= 0;
1022 f
->fmt
.pix
.priv
= 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_SMPTE240M
;
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 destroy_workqueue(dev
->workqueue
);
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, 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
= video_device_alloc();
1230 if (!dev
->video_dev
) {
1231 v4l2_err(&dev
->v4l2_dev
, "video_device_alloc() failed\n");
1236 *dev
->video_dev
= hdpvr_video_template
;
1237 strcpy(dev
->video_dev
->name
, "Hauppauge HD PVR");
1238 dev
->video_dev
->v4l2_dev
= &dev
->v4l2_dev
;
1239 video_set_drvdata(dev
->video_dev
, dev
);
1240 set_bit(V4L2_FL_USE_FH_PRIO
, &dev
->video_dev
->flags
);
1242 res
= video_register_device(dev
->video_dev
, VFL_TYPE_GRABBER
, devnum
);
1244 v4l2_err(&dev
->v4l2_dev
, "video_device registration failed\n");
1250 v4l2_ctrl_handler_free(hdl
);