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/slab.h>
24 #include <linux/version.h>
25 #include "pvrusb2-context.h"
26 #include "pvrusb2-hdw.h"
28 #include "pvrusb2-debug.h"
29 #include "pvrusb2-v4l2.h"
30 #include "pvrusb2-ioread.h"
31 #include <linux/videodev2.h>
32 #include <linux/module.h>
33 #include <media/v4l2-dev.h>
34 #include <media/v4l2-common.h>
35 #include <media/v4l2-ioctl.h>
41 struct pvr2_v4l2_dev
{
42 struct video_device devbase
; /* MUST be first! */
43 struct pvr2_v4l2
*v4lp
;
44 struct pvr2_context_stream
*stream
;
45 /* Information about this device: */
46 enum pvr2_config config
; /* Expected stream format */
47 int v4l_type
; /* V4L defined type for this device node */
48 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
52 struct pvr2_channel channel
;
53 struct pvr2_v4l2_dev
*pdi
;
54 enum v4l2_priority prio
;
55 struct pvr2_ioread
*rhp
;
57 struct pvr2_v4l2
*vhead
;
58 struct pvr2_v4l2_fh
*vnext
;
59 struct pvr2_v4l2_fh
*vprev
;
60 wait_queue_head_t wait_data
;
62 /* Map contiguous ordinal value to input id */
63 unsigned char *input_map
;
64 unsigned int input_cnt
;
68 struct pvr2_channel channel
;
69 struct pvr2_v4l2_fh
*vfirst
;
70 struct pvr2_v4l2_fh
*vlast
;
72 struct v4l2_prio_state prio
;
74 /* streams - Note that these must be separately, individually,
75 * allocated pointers. This is because the v4l core is going to
76 * manage their deletion - separately, individually... */
77 struct pvr2_v4l2_dev
*dev_video
;
78 struct pvr2_v4l2_dev
*dev_radio
;
81 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
82 module_param_array(video_nr
, int, NULL
, 0444);
83 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
84 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
85 module_param_array(radio_nr
, int, NULL
, 0444);
86 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
87 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
88 module_param_array(vbi_nr
, int, NULL
, 0444);
89 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
91 static struct v4l2_capability pvr_capability
={
93 .card
= "Hauppauge WinTV pvr-usb2",
95 .version
= LINUX_VERSION_CODE
,
96 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
97 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
102 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
105 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
106 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
107 .description
= "MPEG1/2",
108 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
109 // breaks when I do that.
110 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
111 .reserved
= { 0, 0, 0, 0 }
115 #define PVR_FORMAT_PIX 0
116 #define PVR_FORMAT_VBI 1
118 static struct v4l2_format pvr_format
[] = {
120 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
125 // This should really be V4L2_PIX_FMT_MPEG,
126 // but xawtv breaks when I do that.
127 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
128 .field
= V4L2_FIELD_INTERLACED
,
129 .bytesperline
= 0, // doesn't make sense
131 //FIXME : Don't know what to put here...
132 .sizeimage
= (32*1024),
133 .colorspace
= 0, // doesn't make sense here
139 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
142 .sampling_rate
= 27000000,
144 .samples_per_line
= 1443,
145 .sample_format
= V4L2_PIX_FMT_GREY
,
159 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
162 static long pvr2_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
164 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
165 struct pvr2_v4l2
*vp
= fh
->vhead
;
166 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
167 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
170 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
171 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),cmd
);
174 if (!pvr2_hdw_dev_ok(hdw
)) {
175 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
176 "ioctl failed - bad or no context");
186 case VIDIOC_S_FREQUENCY
:
187 ret
= v4l2_prio_check(&vp
->prio
, fh
->prio
);
193 case VIDIOC_QUERYCAP
:
195 struct v4l2_capability
*cap
= arg
;
197 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
198 strlcpy(cap
->bus_info
,pvr2_hdw_get_bus_info(hdw
),
199 sizeof(cap
->bus_info
));
200 strlcpy(cap
->card
,pvr2_hdw_get_desc(hdw
),sizeof(cap
->card
));
206 case VIDIOC_G_PRIORITY
:
208 enum v4l2_priority
*p
= arg
;
210 *p
= v4l2_prio_max(&vp
->prio
);
215 case VIDIOC_S_PRIORITY
:
217 enum v4l2_priority
*prio
= arg
;
219 ret
= v4l2_prio_change(&vp
->prio
, &fh
->prio
, *prio
);
225 struct v4l2_standard
*vs
= (struct v4l2_standard
*)arg
;
227 ret
= pvr2_hdw_get_stdenum_value(hdw
,vs
,idx
+1);
234 ret
= pvr2_ctrl_get_value(
235 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
236 *(v4l2_std_id
*)arg
= val
;
242 ret
= pvr2_ctrl_set_value(
243 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
244 *(v4l2_std_id
*)arg
);
248 case VIDIOC_ENUMINPUT
:
250 struct pvr2_ctrl
*cptr
;
251 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
252 struct v4l2_input tmp
;
256 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
258 memset(&tmp
,0,sizeof(tmp
));
259 tmp
.index
= vi
->index
;
261 if (vi
->index
>= fh
->input_cnt
) {
265 val
= fh
->input_map
[vi
->index
];
267 case PVR2_CVAL_INPUT_TV
:
268 case PVR2_CVAL_INPUT_DTV
:
269 case PVR2_CVAL_INPUT_RADIO
:
270 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
272 case PVR2_CVAL_INPUT_SVIDEO
:
273 case PVR2_CVAL_INPUT_COMPOSITE
:
274 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
283 pvr2_ctrl_get_valname(cptr
,val
,
284 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
287 /* Don't bother with audioset, since this driver currently
288 always switches the audio whenever the video is
291 /* Handling std is a tougher problem. It doesn't make
292 sense in cases where a device might be multi-standard.
293 We could just copy out the current value for the
294 standard, but it can change over time. For now just
297 memcpy(vi
, &tmp
, sizeof(tmp
));
306 struct pvr2_ctrl
*cptr
;
307 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
309 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
311 ret
= pvr2_ctrl_get_value(cptr
,&val
);
313 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
314 if (fh
->input_map
[idx
] == val
) {
324 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
325 if (vi
->index
>= fh
->input_cnt
) {
329 ret
= pvr2_ctrl_set_value(
330 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
331 fh
->input_map
[vi
->index
]);
335 case VIDIOC_ENUMAUDIO
:
337 /* pkt: FIXME: We are returning one "fake" input here
338 which could very well be called "whatever_we_like".
339 This is for apps that want to see an audio input
340 just to feel comfortable, as well as to test if
341 it can do stereo or sth. There is actually no guarantee
342 that the actual audio input cannot change behind the app's
343 back, but most applications should not mind that either.
345 Hopefully, mplayer people will work with us on this (this
346 whole mess is to support mplayer pvr://), or Hans will come
347 up with a more standard way to say "we have inputs but we
348 don 't want you to change them independent of video" which
351 struct v4l2_audio
*vin
= arg
;
353 if (vin
->index
> 0) break;
354 strncpy(vin
->name
, "PVRUSB2 Audio",14);
355 vin
->capability
= V4L2_AUDCAP_STEREO
;
363 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
364 struct v4l2_audio
*vin
= arg
;
365 memset(vin
,0,sizeof(*vin
));
367 strncpy(vin
->name
, "PVRUSB2 Audio",14);
368 vin
->capability
= V4L2_AUDCAP_STEREO
;
375 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
377 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
379 pvr2_hdw_execute_tuner_poll(hdw
);
380 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
386 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
391 ret
= pvr2_ctrl_set_value(
392 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
397 case VIDIOC_S_FREQUENCY
:
399 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
401 struct v4l2_tuner vt
;
403 struct pvr2_ctrl
*ctrlp
;
404 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
406 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
407 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
409 if (vf
->type
== V4L2_TUNER_RADIO
) {
410 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
411 pvr2_ctrl_set_value(ctrlp
,
412 PVR2_CVAL_INPUT_RADIO
);
415 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
416 pvr2_ctrl_set_value(ctrlp
,
421 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
426 ret
= pvr2_ctrl_set_value(
427 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
431 case VIDIOC_G_FREQUENCY
:
433 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
436 struct v4l2_tuner vt
;
437 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
439 ret
= pvr2_ctrl_get_value(
440 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
444 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
446 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
447 vf
->type
= V4L2_TUNER_RADIO
;
449 vf
->type
= V4L2_TUNER_ANALOG_TV
;
451 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
452 val
= (val
* 2) / 125;
460 case VIDIOC_ENUM_FMT
:
462 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
464 /* Only one format is supported : mpeg.*/
468 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
475 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
478 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
479 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
480 sizeof(struct v4l2_format
));
483 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
485 vf
->fmt
.pix
.width
= val
;
488 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
490 vf
->fmt
.pix
.height
= val
;
493 case V4L2_BUF_TYPE_VBI_CAPTURE
:
494 // ????? Still need to figure out to do VBI correctly
507 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
511 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
513 struct pvr2_ctrl
*hcp
,*vcp
;
514 int h
= vf
->fmt
.pix
.height
;
515 int w
= vf
->fmt
.pix
.width
;
516 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
517 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
519 lmin
= pvr2_ctrl_get_min(hcp
);
520 lmax
= pvr2_ctrl_get_max(hcp
);
521 pvr2_ctrl_get_def(hcp
, &ldef
);
524 } else if (w
< lmin
) {
526 } else if (w
> lmax
) {
529 lmin
= pvr2_ctrl_get_min(vcp
);
530 lmax
= pvr2_ctrl_get_max(vcp
);
531 pvr2_ctrl_get_def(vcp
, &ldef
);
534 } else if (h
< lmin
) {
536 } else if (h
> lmax
) {
540 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
541 sizeof(struct v4l2_format
));
542 vf
->fmt
.pix
.width
= w
;
543 vf
->fmt
.pix
.height
= h
;
545 if (cmd
== VIDIOC_S_FMT
) {
546 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
547 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
550 case V4L2_BUF_TYPE_VBI_CAPTURE
:
551 // ????? Still need to figure out to do VBI correctly
561 case VIDIOC_STREAMON
:
563 if (!fh
->pdi
->stream
) {
564 /* No stream defined for this node. This means
565 that we're not currently allowed to stream from
570 ret
= pvr2_hdw_set_stream_type(hdw
,pdi
->config
);
571 if (ret
< 0) return ret
;
572 ret
= pvr2_hdw_set_streaming(hdw
,!0);
576 case VIDIOC_STREAMOFF
:
578 if (!fh
->pdi
->stream
) {
579 /* No stream defined for this node. This means
580 that we're not currently allowed to stream from
585 ret
= pvr2_hdw_set_streaming(hdw
,0);
589 case VIDIOC_QUERYCTRL
:
591 struct pvr2_ctrl
*cptr
;
593 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
595 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
596 cptr
= pvr2_hdw_get_ctrl_nextv4l(
597 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
598 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
600 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
603 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
604 "QUERYCTRL id=0x%x not implemented here",
610 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
611 "QUERYCTRL id=0x%x mapping name=%s (%s)",
612 vc
->id
,pvr2_ctrl_get_name(cptr
),
613 pvr2_ctrl_get_desc(cptr
));
614 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
615 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
616 pvr2_ctrl_get_def(cptr
, &val
);
617 vc
->default_value
= val
;
618 switch (pvr2_ctrl_get_type(cptr
)) {
620 vc
->type
= V4L2_CTRL_TYPE_MENU
;
622 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
626 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
632 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
633 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
634 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
638 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
639 "QUERYCTRL id=0x%x name=%s not mappable",
640 vc
->id
,pvr2_ctrl_get_name(cptr
));
647 case VIDIOC_QUERYMENU
:
649 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
650 unsigned int cnt
= 0;
651 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
653 vm
->name
,sizeof(vm
->name
)-1,
661 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
663 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
671 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
672 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
677 case VIDIOC_G_EXT_CTRLS
:
679 struct v4l2_ext_controls
*ctls
=
680 (struct v4l2_ext_controls
*)arg
;
681 struct v4l2_ext_control
*ctrl
;
685 for (idx
= 0; idx
< ctls
->count
; idx
++) {
686 ctrl
= ctls
->controls
+ idx
;
687 ret
= pvr2_ctrl_get_value(
688 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
690 ctls
->error_idx
= idx
;
693 /* Ensure that if read as a 64 bit value, the user
694 will still get a hopefully sane value */
701 case VIDIOC_S_EXT_CTRLS
:
703 struct v4l2_ext_controls
*ctls
=
704 (struct v4l2_ext_controls
*)arg
;
705 struct v4l2_ext_control
*ctrl
;
708 for (idx
= 0; idx
< ctls
->count
; idx
++) {
709 ctrl
= ctls
->controls
+ idx
;
710 ret
= pvr2_ctrl_set_value(
711 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
714 ctls
->error_idx
= idx
;
721 case VIDIOC_TRY_EXT_CTRLS
:
723 struct v4l2_ext_controls
*ctls
=
724 (struct v4l2_ext_controls
*)arg
;
725 struct v4l2_ext_control
*ctrl
;
726 struct pvr2_ctrl
*pctl
;
728 /* For the moment just validate that the requested control
731 for (idx
= 0; idx
< ctls
->count
; idx
++) {
732 ctrl
= ctls
->controls
+ idx
;
733 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
736 ctls
->error_idx
= idx
;
745 struct v4l2_cropcap
*cap
= (struct v4l2_cropcap
*)arg
;
746 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
750 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
751 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
756 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
758 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
762 ret
= pvr2_ctrl_get_value(
763 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
769 ret
= pvr2_ctrl_get_value(
770 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
776 ret
= pvr2_ctrl_get_value(
777 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
783 ret
= pvr2_ctrl_get_value(
784 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
789 crop
->c
.height
= val
;
793 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
794 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
798 ret
= pvr2_ctrl_set_value(
799 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
805 ret
= pvr2_ctrl_set_value(
806 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
812 ret
= pvr2_ctrl_set_value(
813 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
819 ret
= pvr2_ctrl_set_value(
820 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
827 case VIDIOC_LOG_STATUS
:
829 pvr2_hdw_trigger_module_log(hdw
);
833 #ifdef CONFIG_VIDEO_ADV_DEBUG
834 case VIDIOC_DBG_S_REGISTER
:
835 case VIDIOC_DBG_G_REGISTER
:
838 struct v4l2_dbg_register
*req
= (struct v4l2_dbg_register
*)arg
;
839 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
840 ret
= pvr2_hdw_register_access(
841 hdw
, &req
->match
, req
->reg
,
842 cmd
== VIDIOC_DBG_S_REGISTER
, &val
);
843 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
853 pvr2_hdw_commit_ctl(hdw
);
856 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
857 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
858 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
860 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
861 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
862 "pvr2_v4l2_do_ioctl failure, ret=%ld"
863 " command was:", ret
);
864 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
869 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
870 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
876 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
878 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
879 enum pvr2_config cfg
= dip
->config
;
883 /* Construct the unregistration message *before* we actually
884 perform the unregistration step. By doing it this way we don't
885 have to worry about potentially touching deleted resources. */
886 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
887 "pvrusb2: unregistered device %s [%s]",
888 video_device_node_name(&dip
->devbase
),
889 pvr2_config_get_name(cfg
));
892 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
898 /* Actual deallocation happens later when all internal references
900 video_unregister_device(&dip
->devbase
);
902 printk(KERN_INFO
"%s\n", msg
);
907 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
910 if (!dip
->devbase
.parent
) return;
911 dip
->devbase
.parent
= NULL
;
912 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
916 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
919 pvr2_v4l2_dev_destroy(vp
->dev_video
);
920 vp
->dev_video
= NULL
;
923 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
924 vp
->dev_radio
= NULL
;
927 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
928 pvr2_channel_done(&vp
->channel
);
933 static void pvr2_video_device_release(struct video_device
*vdev
)
935 struct pvr2_v4l2_dev
*dev
;
936 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
941 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
943 struct pvr2_v4l2
*vp
;
944 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
945 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
946 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
947 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
948 if (vp
->vfirst
) return;
949 pvr2_v4l2_destroy_no_lock(vp
);
953 static long pvr2_v4l2_ioctl(struct file
*file
,
954 unsigned int cmd
, unsigned long arg
)
957 return video_usercopy(file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
961 static int pvr2_v4l2_release(struct file
*file
)
963 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
964 struct pvr2_v4l2
*vp
= fhp
->vhead
;
965 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
967 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
970 struct pvr2_stream
*sp
;
971 pvr2_hdw_set_streaming(hdw
,0);
972 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
973 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
974 pvr2_ioread_destroy(fhp
->rhp
);
978 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
979 file
->private_data
= NULL
;
982 fhp
->vnext
->vprev
= fhp
->vprev
;
984 vp
->vlast
= fhp
->vprev
;
987 fhp
->vprev
->vnext
= fhp
->vnext
;
989 vp
->vfirst
= fhp
->vnext
;
994 pvr2_channel_done(&fhp
->channel
);
995 pvr2_trace(PVR2_TRACE_STRUCT
,
996 "Destroying pvr_v4l2_fh id=%p",fhp
);
997 if (fhp
->input_map
) {
998 kfree(fhp
->input_map
);
999 fhp
->input_map
= NULL
;
1002 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1003 pvr2_v4l2_destroy_no_lock(vp
);
1009 static int pvr2_v4l2_open(struct file
*file
)
1011 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1012 struct pvr2_v4l2_fh
*fhp
;
1013 struct pvr2_v4l2
*vp
;
1014 struct pvr2_hdw
*hdw
;
1015 unsigned int input_mask
= 0;
1016 unsigned int input_cnt
,idx
;
1019 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1022 hdw
= vp
->channel
.hdw
;
1024 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1026 if (!pvr2_hdw_dev_ok(hdw
)) {
1027 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1028 "pvr2_v4l2_open: hardware not ready");
1032 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1037 init_waitqueue_head(&fhp
->wait_data
);
1040 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1041 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1043 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1044 /* Opening device as a radio, legal input selection subset
1045 is just the radio. */
1046 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1048 /* Opening the main V4L device, legal input selection
1049 subset includes all analog inputs. */
1050 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1051 (1 << PVR2_CVAL_INPUT_TV
) |
1052 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1053 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1055 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1057 pvr2_channel_done(&fhp
->channel
);
1058 pvr2_trace(PVR2_TRACE_STRUCT
,
1059 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1066 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1068 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1069 if (input_mask
& (1 << idx
)) input_cnt
++;
1071 fhp
->input_cnt
= input_cnt
;
1072 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1073 if (!fhp
->input_map
) {
1074 pvr2_channel_done(&fhp
->channel
);
1075 pvr2_trace(PVR2_TRACE_STRUCT
,
1076 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1082 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1083 if (!(input_mask
& (1 << idx
))) continue;
1084 fhp
->input_map
[input_cnt
++] = idx
;
1088 fhp
->vprev
= vp
->vlast
;
1090 vp
->vlast
->vnext
= fhp
;
1098 file
->private_data
= fhp
;
1099 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1101 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1107 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1109 wake_up(&fhp
->wait_data
);
1112 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1115 struct pvr2_stream
*sp
;
1116 struct pvr2_hdw
*hdw
;
1117 if (fh
->rhp
) return 0;
1119 if (!fh
->pdi
->stream
) {
1120 /* No stream defined for this node. This means that we're
1121 not currently allowed to stream from this node. */
1125 /* First read() attempt. Try to claim the stream and start
1127 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1128 fh
->pdi
->stream
)) != 0) {
1129 /* Someone else must already have it */
1133 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1135 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1139 hdw
= fh
->channel
.mc_head
->hdw
;
1140 sp
= fh
->pdi
->stream
->stream
;
1141 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1142 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1143 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1144 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1148 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1149 char __user
*buff
, size_t count
, loff_t
*ppos
)
1151 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1154 if (fh
->fw_mode_flag
) {
1155 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1159 unsigned int offs
= *ppos
;
1161 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1162 if (!tbuf
) return -ENOMEM
;
1166 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1167 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1173 if (copy_to_user(buff
,tbuf
,c2
)) {
1188 ret
= pvr2_v4l2_iosetup(fh
);
1195 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1196 if (ret
>= 0) break;
1197 if (ret
!= -EAGAIN
) break;
1198 if (file
->f_flags
& O_NONBLOCK
) break;
1199 /* Doing blocking I/O. Wait here. */
1200 ret
= wait_event_interruptible(
1202 pvr2_ioread_avail(fh
->rhp
) >= 0);
1210 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1212 unsigned int mask
= 0;
1213 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1216 if (fh
->fw_mode_flag
) {
1217 mask
|= POLLIN
| POLLRDNORM
;
1222 ret
= pvr2_v4l2_iosetup(fh
);
1223 if (ret
) return POLLERR
;
1226 poll_wait(file
,&fh
->wait_data
,wait
);
1228 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1229 mask
|= POLLIN
| POLLRDNORM
;
1236 static const struct v4l2_file_operations vdev_fops
= {
1237 .owner
= THIS_MODULE
,
1238 .open
= pvr2_v4l2_open
,
1239 .release
= pvr2_v4l2_release
,
1240 .read
= pvr2_v4l2_read
,
1241 .ioctl
= pvr2_v4l2_ioctl
,
1242 .poll
= pvr2_v4l2_poll
,
1246 static struct video_device vdev_template
= {
1251 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1252 struct pvr2_v4l2
*vp
,
1255 struct usb_device
*usbdev
;
1261 usbdev
= pvr2_hdw_get_dev(vp
->channel
.mc_head
->hdw
);
1262 dip
->v4l_type
= v4l_type
;
1264 case VFL_TYPE_GRABBER
:
1265 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1266 dip
->config
= pvr2_config_mpeg
;
1267 dip
->minor_type
= pvr2_v4l_type_video
;
1270 pr_err(KBUILD_MODNAME
1271 ": Failed to set up pvrusb2 v4l video dev"
1272 " due to missing stream instance\n");
1277 dip
->config
= pvr2_config_vbi
;
1278 dip
->minor_type
= pvr2_v4l_type_vbi
;
1281 case VFL_TYPE_RADIO
:
1282 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1283 dip
->config
= pvr2_config_mpeg
;
1284 dip
->minor_type
= pvr2_v4l_type_radio
;
1288 /* Bail out (this should be impossible) */
1289 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1290 " due to unrecognized config\n");
1294 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1295 dip
->devbase
.release
= pvr2_video_device_release
;
1298 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1299 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1300 mindevnum
= nr_ptr
[unit_number
];
1302 dip
->devbase
.parent
= &usbdev
->dev
;
1303 if ((video_register_device(&dip
->devbase
,
1304 dip
->v4l_type
, mindevnum
) < 0) &&
1305 (video_register_device(&dip
->devbase
,
1306 dip
->v4l_type
, -1) < 0)) {
1307 pr_err(KBUILD_MODNAME
1308 ": Failed to register pvrusb2 v4l device\n");
1311 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1312 video_device_node_name(&dip
->devbase
),
1313 pvr2_config_get_name(dip
->config
));
1315 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1316 dip
->minor_type
,dip
->devbase
.minor
);
1320 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1322 struct pvr2_v4l2
*vp
;
1324 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1326 pvr2_channel_init(&vp
->channel
,mnp
);
1327 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1329 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1331 /* register streams */
1332 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1333 if (!vp
->dev_video
) goto fail
;
1334 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1335 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1336 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1337 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1338 if (!vp
->dev_radio
) goto fail
;
1339 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1344 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1345 pvr2_v4l2_destroy_no_lock(vp
);
1350 Stuff for Emacs to see, in order to encourage consistent editing style:
1351 *** Local Variables: ***
1353 *** fill-column: 75 ***
1354 *** tab-width: 8 ***
1355 *** c-basic-offset: 8 ***