4 * Copyright (C) 2005 Mike Isely <isely@pobox.com>
5 * Copyright (C) 2004 Aurelien Alleaume <slts@free.fr>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <linux/kernel.h>
23 #include <linux/version.h>
24 #include "pvrusb2-context.h"
25 #include "pvrusb2-hdw.h"
27 #include "pvrusb2-debug.h"
28 #include "pvrusb2-v4l2.h"
29 #include "pvrusb2-ioread.h"
30 #include <linux/videodev2.h>
31 #include <media/v4l2-dev.h>
32 #include <media/v4l2-common.h>
33 #include <media/v4l2-ioctl.h>
39 struct pvr2_v4l2_dev
{
40 struct video_device devbase
; /* MUST be first! */
41 struct pvr2_v4l2
*v4lp
;
42 struct pvr2_context_stream
*stream
;
43 /* Information about this device: */
44 enum pvr2_config config
; /* Expected stream format */
45 int v4l_type
; /* V4L defined type for this device node */
46 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
50 struct pvr2_channel channel
;
51 struct pvr2_v4l2_dev
*dev_info
;
52 enum v4l2_priority prio
;
53 struct pvr2_ioread
*rhp
;
55 struct pvr2_v4l2
*vhead
;
56 struct pvr2_v4l2_fh
*vnext
;
57 struct pvr2_v4l2_fh
*vprev
;
58 wait_queue_head_t wait_data
;
60 /* Map contiguous ordinal value to input id */
61 unsigned char *input_map
;
62 unsigned int input_cnt
;
66 struct pvr2_channel channel
;
67 struct pvr2_v4l2_fh
*vfirst
;
68 struct pvr2_v4l2_fh
*vlast
;
70 struct v4l2_prio_state prio
;
72 /* streams - Note that these must be separately, individually,
73 * allocated pointers. This is because the v4l core is going to
74 * manage their deletion - separately, individually... */
75 struct pvr2_v4l2_dev
*dev_video
;
76 struct pvr2_v4l2_dev
*dev_radio
;
79 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
80 module_param_array(video_nr
, int, NULL
, 0444);
81 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
82 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
83 module_param_array(radio_nr
, int, NULL
, 0444);
84 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
85 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
86 module_param_array(vbi_nr
, int, NULL
, 0444);
87 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
89 static struct v4l2_capability pvr_capability
={
91 .card
= "Hauppauge WinTV pvr-usb2",
93 .version
= KERNEL_VERSION(0, 9, 0),
94 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
95 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
100 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
103 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
104 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
105 .description
= "MPEG1/2",
106 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
107 // breaks when I do that.
108 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
109 .reserved
= { 0, 0, 0, 0 }
113 #define PVR_FORMAT_PIX 0
114 #define PVR_FORMAT_VBI 1
116 static struct v4l2_format pvr_format
[] = {
118 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
123 // This should really be V4L2_PIX_FMT_MPEG,
124 // but xawtv breaks when I do that.
125 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
126 .field
= V4L2_FIELD_INTERLACED
,
127 .bytesperline
= 0, // doesn't make sense
129 //FIXME : Don't know what to put here...
130 .sizeimage
= (32*1024),
131 .colorspace
= 0, // doesn't make sense here
137 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
140 .sampling_rate
= 27000000,
142 .samples_per_line
= 1443,
143 .sample_format
= V4L2_PIX_FMT_GREY
,
157 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
160 static long pvr2_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
162 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
163 struct pvr2_v4l2
*vp
= fh
->vhead
;
164 struct pvr2_v4l2_dev
*dev_info
= fh
->dev_info
;
165 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
168 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
169 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),cmd
);
172 if (!pvr2_hdw_dev_ok(hdw
)) {
173 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
174 "ioctl failed - bad or no context");
184 case VIDIOC_S_FREQUENCY
:
185 ret
= v4l2_prio_check(&vp
->prio
, &fh
->prio
);
191 case VIDIOC_QUERYCAP
:
193 struct v4l2_capability
*cap
= arg
;
195 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
196 strlcpy(cap
->bus_info
,pvr2_hdw_get_bus_info(hdw
),
197 sizeof(cap
->bus_info
));
198 strlcpy(cap
->card
,pvr2_hdw_get_desc(hdw
),sizeof(cap
->card
));
204 case VIDIOC_G_PRIORITY
:
206 enum v4l2_priority
*p
= arg
;
208 *p
= v4l2_prio_max(&vp
->prio
);
213 case VIDIOC_S_PRIORITY
:
215 enum v4l2_priority
*prio
= arg
;
217 ret
= v4l2_prio_change(&vp
->prio
, &fh
->prio
, *prio
);
223 struct v4l2_standard
*vs
= (struct v4l2_standard
*)arg
;
225 ret
= pvr2_hdw_get_stdenum_value(hdw
,vs
,idx
+1);
232 ret
= pvr2_ctrl_get_value(
233 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
234 *(v4l2_std_id
*)arg
= val
;
240 ret
= pvr2_ctrl_set_value(
241 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
242 *(v4l2_std_id
*)arg
);
246 case VIDIOC_ENUMINPUT
:
248 struct pvr2_ctrl
*cptr
;
249 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
250 struct v4l2_input tmp
;
254 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
256 memset(&tmp
,0,sizeof(tmp
));
257 tmp
.index
= vi
->index
;
259 if (vi
->index
>= fh
->input_cnt
) {
263 val
= fh
->input_map
[vi
->index
];
265 case PVR2_CVAL_INPUT_TV
:
266 case PVR2_CVAL_INPUT_DTV
:
267 case PVR2_CVAL_INPUT_RADIO
:
268 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
270 case PVR2_CVAL_INPUT_SVIDEO
:
271 case PVR2_CVAL_INPUT_COMPOSITE
:
272 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
281 pvr2_ctrl_get_valname(cptr
,val
,
282 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
285 /* Don't bother with audioset, since this driver currently
286 always switches the audio whenever the video is
289 /* Handling std is a tougher problem. It doesn't make
290 sense in cases where a device might be multi-standard.
291 We could just copy out the current value for the
292 standard, but it can change over time. For now just
295 memcpy(vi
, &tmp
, sizeof(tmp
));
304 struct pvr2_ctrl
*cptr
;
305 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
307 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
309 ret
= pvr2_ctrl_get_value(cptr
,&val
);
311 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
312 if (fh
->input_map
[idx
] == val
) {
322 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
323 if (vi
->index
>= fh
->input_cnt
) {
327 ret
= pvr2_ctrl_set_value(
328 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
329 fh
->input_map
[vi
->index
]);
333 case VIDIOC_ENUMAUDIO
:
335 /* pkt: FIXME: We are returning one "fake" input here
336 which could very well be called "whatever_we_like".
337 This is for apps that want to see an audio input
338 just to feel comfortable, as well as to test if
339 it can do stereo or sth. There is actually no guarantee
340 that the actual audio input cannot change behind the app's
341 back, but most applications should not mind that either.
343 Hopefully, mplayer people will work with us on this (this
344 whole mess is to support mplayer pvr://), or Hans will come
345 up with a more standard way to say "we have inputs but we
346 don 't want you to change them independent of video" which
349 struct v4l2_audio
*vin
= arg
;
351 if (vin
->index
> 0) break;
352 strncpy(vin
->name
, "PVRUSB2 Audio",14);
353 vin
->capability
= V4L2_AUDCAP_STEREO
;
361 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
362 struct v4l2_audio
*vin
= arg
;
363 memset(vin
,0,sizeof(*vin
));
365 strncpy(vin
->name
, "PVRUSB2 Audio",14);
366 vin
->capability
= V4L2_AUDCAP_STEREO
;
378 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
380 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
382 pvr2_hdw_execute_tuner_poll(hdw
);
383 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
389 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
394 ret
= pvr2_ctrl_set_value(
395 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
400 case VIDIOC_S_FREQUENCY
:
402 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
404 struct v4l2_tuner vt
;
406 struct pvr2_ctrl
*ctrlp
;
407 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
409 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
410 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
412 if (vf
->type
== V4L2_TUNER_RADIO
) {
413 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
414 pvr2_ctrl_set_value(ctrlp
,
415 PVR2_CVAL_INPUT_RADIO
);
418 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
419 pvr2_ctrl_set_value(ctrlp
,
424 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
429 ret
= pvr2_ctrl_set_value(
430 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
434 case VIDIOC_G_FREQUENCY
:
436 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
439 struct v4l2_tuner vt
;
440 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
442 ret
= pvr2_ctrl_get_value(
443 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
447 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
449 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
450 vf
->type
= V4L2_TUNER_RADIO
;
452 vf
->type
= V4L2_TUNER_ANALOG_TV
;
454 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
455 val
= (val
* 2) / 125;
463 case VIDIOC_ENUM_FMT
:
465 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
467 /* Only one format is supported : mpeg.*/
471 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
478 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
481 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
482 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
483 sizeof(struct v4l2_format
));
486 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
488 vf
->fmt
.pix
.width
= val
;
491 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
493 vf
->fmt
.pix
.height
= val
;
496 case V4L2_BUF_TYPE_VBI_CAPTURE
:
497 // ????? Still need to figure out to do VBI correctly
510 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
514 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
516 struct pvr2_ctrl
*hcp
,*vcp
;
517 int h
= vf
->fmt
.pix
.height
;
518 int w
= vf
->fmt
.pix
.width
;
519 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
520 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
522 lmin
= pvr2_ctrl_get_min(hcp
);
523 lmax
= pvr2_ctrl_get_max(hcp
);
524 pvr2_ctrl_get_def(hcp
, &ldef
);
527 } else if (w
< lmin
) {
529 } else if (w
> lmax
) {
532 lmin
= pvr2_ctrl_get_min(vcp
);
533 lmax
= pvr2_ctrl_get_max(vcp
);
534 pvr2_ctrl_get_def(vcp
, &ldef
);
537 } else if (h
< lmin
) {
539 } else if (h
> lmax
) {
543 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
544 sizeof(struct v4l2_format
));
545 vf
->fmt
.pix
.width
= w
;
546 vf
->fmt
.pix
.height
= h
;
548 if (cmd
== VIDIOC_S_FMT
) {
549 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
550 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
553 case V4L2_BUF_TYPE_VBI_CAPTURE
:
554 // ????? Still need to figure out to do VBI correctly
564 case VIDIOC_STREAMON
:
566 if (!fh
->dev_info
->stream
) {
567 /* No stream defined for this node. This means
568 that we're not currently allowed to stream from
573 ret
= pvr2_hdw_set_stream_type(hdw
,dev_info
->config
);
574 if (ret
< 0) return ret
;
575 ret
= pvr2_hdw_set_streaming(hdw
,!0);
579 case VIDIOC_STREAMOFF
:
581 if (!fh
->dev_info
->stream
) {
582 /* No stream defined for this node. This means
583 that we're not currently allowed to stream from
588 ret
= pvr2_hdw_set_streaming(hdw
,0);
592 case VIDIOC_QUERYCTRL
:
594 struct pvr2_ctrl
*cptr
;
596 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
598 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
599 cptr
= pvr2_hdw_get_ctrl_nextv4l(
600 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
601 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
603 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
606 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
607 "QUERYCTRL id=0x%x not implemented here",
613 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
614 "QUERYCTRL id=0x%x mapping name=%s (%s)",
615 vc
->id
,pvr2_ctrl_get_name(cptr
),
616 pvr2_ctrl_get_desc(cptr
));
617 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
618 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
619 pvr2_ctrl_get_def(cptr
, &val
);
620 vc
->default_value
= val
;
621 switch (pvr2_ctrl_get_type(cptr
)) {
623 vc
->type
= V4L2_CTRL_TYPE_MENU
;
625 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
629 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
635 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
636 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
637 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
641 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
642 "QUERYCTRL id=0x%x name=%s not mappable",
643 vc
->id
,pvr2_ctrl_get_name(cptr
));
650 case VIDIOC_QUERYMENU
:
652 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
653 unsigned int cnt
= 0;
654 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
656 vm
->name
,sizeof(vm
->name
)-1,
664 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
666 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
674 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
675 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
680 case VIDIOC_G_EXT_CTRLS
:
682 struct v4l2_ext_controls
*ctls
=
683 (struct v4l2_ext_controls
*)arg
;
684 struct v4l2_ext_control
*ctrl
;
688 for (idx
= 0; idx
< ctls
->count
; idx
++) {
689 ctrl
= ctls
->controls
+ idx
;
690 ret
= pvr2_ctrl_get_value(
691 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
693 ctls
->error_idx
= idx
;
696 /* Ensure that if read as a 64 bit value, the user
697 will still get a hopefully sane value */
704 case VIDIOC_S_EXT_CTRLS
:
706 struct v4l2_ext_controls
*ctls
=
707 (struct v4l2_ext_controls
*)arg
;
708 struct v4l2_ext_control
*ctrl
;
711 for (idx
= 0; idx
< ctls
->count
; idx
++) {
712 ctrl
= ctls
->controls
+ idx
;
713 ret
= pvr2_ctrl_set_value(
714 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
717 ctls
->error_idx
= idx
;
724 case VIDIOC_TRY_EXT_CTRLS
:
726 struct v4l2_ext_controls
*ctls
=
727 (struct v4l2_ext_controls
*)arg
;
728 struct v4l2_ext_control
*ctrl
;
729 struct pvr2_ctrl
*pctl
;
731 /* For the moment just validate that the requested control
734 for (idx
= 0; idx
< ctls
->count
; idx
++) {
735 ctrl
= ctls
->controls
+ idx
;
736 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
739 ctls
->error_idx
= idx
;
748 struct v4l2_cropcap
*cap
= (struct v4l2_cropcap
*)arg
;
749 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
753 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
754 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
759 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
761 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
765 ret
= pvr2_ctrl_get_value(
766 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
772 ret
= pvr2_ctrl_get_value(
773 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
779 ret
= pvr2_ctrl_get_value(
780 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
786 ret
= pvr2_ctrl_get_value(
787 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
792 crop
->c
.height
= val
;
796 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
797 struct v4l2_cropcap cap
;
798 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
802 cap
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
803 ret
= pvr2_ctrl_set_value(
804 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
810 ret
= pvr2_ctrl_set_value(
811 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
817 ret
= pvr2_ctrl_set_value(
818 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
824 ret
= pvr2_ctrl_set_value(
825 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
832 case VIDIOC_LOG_STATUS
:
834 pvr2_hdw_trigger_module_log(hdw
);
838 #ifdef CONFIG_VIDEO_ADV_DEBUG
839 case VIDIOC_DBG_S_REGISTER
:
840 case VIDIOC_DBG_G_REGISTER
:
843 struct v4l2_dbg_register
*req
= (struct v4l2_dbg_register
*)arg
;
844 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
845 ret
= pvr2_hdw_register_access(
846 hdw
, &req
->match
, req
->reg
,
847 cmd
== VIDIOC_DBG_S_REGISTER
, &val
);
848 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
854 ret
= v4l_compat_translate_ioctl(file
, cmd
,
855 arg
, pvr2_v4l2_do_ioctl
);
858 pvr2_hdw_commit_ctl(hdw
);
861 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
862 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
863 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
865 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
866 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
867 "pvr2_v4l2_do_ioctl failure, ret=%ld"
868 " command was:", ret
);
869 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
874 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
875 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
881 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
883 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
884 enum pvr2_config cfg
= dip
->config
;
886 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
892 /* Actual deallocation happens later when all internal references
894 video_unregister_device(&dip
->devbase
);
896 printk(KERN_INFO
"pvrusb2: unregistered device %s [%s]\n",
897 video_device_node_name(&dip
->devbase
),
898 pvr2_config_get_name(cfg
));
903 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
906 if (!dip
->devbase
.parent
) return;
907 dip
->devbase
.parent
= NULL
;
908 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
912 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
915 pvr2_v4l2_dev_destroy(vp
->dev_video
);
916 vp
->dev_video
= NULL
;
919 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
920 vp
->dev_radio
= NULL
;
923 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
924 pvr2_channel_done(&vp
->channel
);
929 static void pvr2_video_device_release(struct video_device
*vdev
)
931 struct pvr2_v4l2_dev
*dev
;
932 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
937 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
939 struct pvr2_v4l2
*vp
;
940 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
941 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
942 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
943 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
944 if (vp
->vfirst
) return;
945 pvr2_v4l2_destroy_no_lock(vp
);
949 static long pvr2_v4l2_ioctl(struct file
*file
,
950 unsigned int cmd
, unsigned long arg
)
953 return video_usercopy(file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
957 static int pvr2_v4l2_release(struct file
*file
)
959 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
960 struct pvr2_v4l2
*vp
= fhp
->vhead
;
961 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
963 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
966 struct pvr2_stream
*sp
;
967 pvr2_hdw_set_streaming(hdw
,0);
968 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
969 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
970 pvr2_ioread_destroy(fhp
->rhp
);
974 v4l2_prio_close(&vp
->prio
, &fhp
->prio
);
975 file
->private_data
= NULL
;
978 fhp
->vnext
->vprev
= fhp
->vprev
;
980 vp
->vlast
= fhp
->vprev
;
983 fhp
->vprev
->vnext
= fhp
->vnext
;
985 vp
->vfirst
= fhp
->vnext
;
990 pvr2_channel_done(&fhp
->channel
);
991 pvr2_trace(PVR2_TRACE_STRUCT
,
992 "Destroying pvr_v4l2_fh id=%p",fhp
);
993 if (fhp
->input_map
) {
994 kfree(fhp
->input_map
);
995 fhp
->input_map
= NULL
;
998 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
999 pvr2_v4l2_destroy_no_lock(vp
);
1005 static int pvr2_v4l2_open(struct file
*file
)
1007 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1008 struct pvr2_v4l2_fh
*fhp
;
1009 struct pvr2_v4l2
*vp
;
1010 struct pvr2_hdw
*hdw
;
1011 unsigned int input_mask
= 0;
1012 unsigned int input_cnt
,idx
;
1015 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1018 hdw
= vp
->channel
.hdw
;
1020 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1022 if (!pvr2_hdw_dev_ok(hdw
)) {
1023 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1024 "pvr2_v4l2_open: hardware not ready");
1028 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1033 init_waitqueue_head(&fhp
->wait_data
);
1034 fhp
->dev_info
= dip
;
1036 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1037 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1039 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1040 /* Opening device as a radio, legal input selection subset
1041 is just the radio. */
1042 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1044 /* Opening the main V4L device, legal input selection
1045 subset includes all analog inputs. */
1046 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1047 (1 << PVR2_CVAL_INPUT_TV
) |
1048 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1049 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1051 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1053 pvr2_channel_done(&fhp
->channel
);
1054 pvr2_trace(PVR2_TRACE_STRUCT
,
1055 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1062 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1064 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1065 if (input_mask
& (1 << idx
)) input_cnt
++;
1067 fhp
->input_cnt
= input_cnt
;
1068 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1069 if (!fhp
->input_map
) {
1070 pvr2_channel_done(&fhp
->channel
);
1071 pvr2_trace(PVR2_TRACE_STRUCT
,
1072 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1078 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1079 if (!(input_mask
& (1 << idx
))) continue;
1080 fhp
->input_map
[input_cnt
++] = idx
;
1084 fhp
->vprev
= vp
->vlast
;
1086 vp
->vlast
->vnext
= fhp
;
1094 file
->private_data
= fhp
;
1095 v4l2_prio_open(&vp
->prio
,&fhp
->prio
);
1097 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1103 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1105 wake_up(&fhp
->wait_data
);
1108 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1111 struct pvr2_stream
*sp
;
1112 struct pvr2_hdw
*hdw
;
1113 if (fh
->rhp
) return 0;
1115 if (!fh
->dev_info
->stream
) {
1116 /* No stream defined for this node. This means that we're
1117 not currently allowed to stream from this node. */
1121 /* First read() attempt. Try to claim the stream and start
1123 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1124 fh
->dev_info
->stream
)) != 0) {
1125 /* Someone else must already have it */
1129 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->dev_info
->stream
);
1131 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1135 hdw
= fh
->channel
.mc_head
->hdw
;
1136 sp
= fh
->dev_info
->stream
->stream
;
1137 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1138 pvr2_hdw_set_stream_type(hdw
,fh
->dev_info
->config
);
1139 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1140 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1144 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1145 char __user
*buff
, size_t count
, loff_t
*ppos
)
1147 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1150 if (fh
->fw_mode_flag
) {
1151 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1155 unsigned int offs
= *ppos
;
1157 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1158 if (!tbuf
) return -ENOMEM
;
1162 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1163 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1169 if (copy_to_user(buff
,tbuf
,c2
)) {
1184 ret
= pvr2_v4l2_iosetup(fh
);
1191 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1192 if (ret
>= 0) break;
1193 if (ret
!= -EAGAIN
) break;
1194 if (file
->f_flags
& O_NONBLOCK
) break;
1195 /* Doing blocking I/O. Wait here. */
1196 ret
= wait_event_interruptible(
1198 pvr2_ioread_avail(fh
->rhp
) >= 0);
1206 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1208 unsigned int mask
= 0;
1209 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1212 if (fh
->fw_mode_flag
) {
1213 mask
|= POLLIN
| POLLRDNORM
;
1218 ret
= pvr2_v4l2_iosetup(fh
);
1219 if (ret
) return POLLERR
;
1222 poll_wait(file
,&fh
->wait_data
,wait
);
1224 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1225 mask
|= POLLIN
| POLLRDNORM
;
1232 static const struct v4l2_file_operations vdev_fops
= {
1233 .owner
= THIS_MODULE
,
1234 .open
= pvr2_v4l2_open
,
1235 .release
= pvr2_v4l2_release
,
1236 .read
= pvr2_v4l2_read
,
1237 .ioctl
= pvr2_v4l2_ioctl
,
1238 .poll
= pvr2_v4l2_poll
,
1242 static struct video_device vdev_template
= {
1247 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1248 struct pvr2_v4l2
*vp
,
1251 struct usb_device
*usbdev
;
1257 usbdev
= pvr2_hdw_get_dev(vp
->channel
.mc_head
->hdw
);
1258 dip
->v4l_type
= v4l_type
;
1260 case VFL_TYPE_GRABBER
:
1261 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1262 dip
->config
= pvr2_config_mpeg
;
1263 dip
->minor_type
= pvr2_v4l_type_video
;
1266 pr_err(KBUILD_MODNAME
1267 ": Failed to set up pvrusb2 v4l video dev"
1268 " due to missing stream instance\n");
1273 dip
->config
= pvr2_config_vbi
;
1274 dip
->minor_type
= pvr2_v4l_type_vbi
;
1277 case VFL_TYPE_RADIO
:
1278 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1279 dip
->config
= pvr2_config_mpeg
;
1280 dip
->minor_type
= pvr2_v4l_type_radio
;
1284 /* Bail out (this should be impossible) */
1285 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1286 " due to unrecognized config\n");
1290 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1291 dip
->devbase
.release
= pvr2_video_device_release
;
1294 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1295 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1296 mindevnum
= nr_ptr
[unit_number
];
1298 dip
->devbase
.parent
= &usbdev
->dev
;
1299 if ((video_register_device(&dip
->devbase
,
1300 dip
->v4l_type
, mindevnum
) < 0) &&
1301 (video_register_device(&dip
->devbase
,
1302 dip
->v4l_type
, -1) < 0)) {
1303 pr_err(KBUILD_MODNAME
1304 ": Failed to register pvrusb2 v4l device\n");
1307 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1308 video_device_node_name(&dip
->devbase
),
1309 pvr2_config_get_name(dip
->config
));
1311 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1312 dip
->minor_type
,dip
->devbase
.minor
);
1316 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1318 struct pvr2_v4l2
*vp
;
1320 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1322 pvr2_channel_init(&vp
->channel
,mnp
);
1323 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1325 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1327 /* register streams */
1328 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1329 if (!vp
->dev_video
) goto fail
;
1330 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1331 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1332 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1333 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1334 if (!vp
->dev_radio
) goto fail
;
1335 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1340 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1341 pvr2_v4l2_destroy_no_lock(vp
);
1346 Stuff for Emacs to see, in order to encourage consistent editing style:
1347 *** Local Variables: ***
1349 *** fill-column: 75 ***
1350 *** tab-width: 8 ***
1351 *** c-basic-offset: 8 ***