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 <media/v4l2-dev.h>
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-ioctl.h>
40 struct pvr2_v4l2_dev
{
41 struct video_device devbase
; /* MUST be first! */
42 struct pvr2_v4l2
*v4lp
;
43 struct pvr2_context_stream
*stream
;
44 /* Information about this device: */
45 enum pvr2_config config
; /* Expected stream format */
46 int v4l_type
; /* V4L defined type for this device node */
47 enum pvr2_v4l_type minor_type
; /* pvr2-understood minor device type */
51 struct pvr2_channel channel
;
52 struct pvr2_v4l2_dev
*pdi
;
53 enum v4l2_priority prio
;
54 struct pvr2_ioread
*rhp
;
56 struct pvr2_v4l2
*vhead
;
57 struct pvr2_v4l2_fh
*vnext
;
58 struct pvr2_v4l2_fh
*vprev
;
59 wait_queue_head_t wait_data
;
61 /* Map contiguous ordinal value to input id */
62 unsigned char *input_map
;
63 unsigned int input_cnt
;
67 struct pvr2_channel channel
;
68 struct pvr2_v4l2_fh
*vfirst
;
69 struct pvr2_v4l2_fh
*vlast
;
71 struct v4l2_prio_state prio
;
73 /* streams - Note that these must be separately, individually,
74 * allocated pointers. This is because the v4l core is going to
75 * manage their deletion - separately, individually... */
76 struct pvr2_v4l2_dev
*dev_video
;
77 struct pvr2_v4l2_dev
*dev_radio
;
80 static int video_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
81 module_param_array(video_nr
, int, NULL
, 0444);
82 MODULE_PARM_DESC(video_nr
, "Offset for device's video dev minor");
83 static int radio_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
84 module_param_array(radio_nr
, int, NULL
, 0444);
85 MODULE_PARM_DESC(radio_nr
, "Offset for device's radio dev minor");
86 static int vbi_nr
[PVR_NUM
] = {[0 ... PVR_NUM
-1] = -1};
87 module_param_array(vbi_nr
, int, NULL
, 0444);
88 MODULE_PARM_DESC(vbi_nr
, "Offset for device's vbi dev minor");
90 static struct v4l2_capability pvr_capability
={
92 .card
= "Hauppauge WinTV pvr-usb2",
94 .version
= KERNEL_VERSION(0, 9, 0),
95 .capabilities
= (V4L2_CAP_VIDEO_CAPTURE
|
96 V4L2_CAP_TUNER
| V4L2_CAP_AUDIO
| V4L2_CAP_RADIO
|
101 static struct v4l2_fmtdesc pvr_fmtdesc
[] = {
104 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
105 .flags
= V4L2_FMT_FLAG_COMPRESSED
,
106 .description
= "MPEG1/2",
107 // This should really be V4L2_PIX_FMT_MPEG, but xawtv
108 // breaks when I do that.
109 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
110 .reserved
= { 0, 0, 0, 0 }
114 #define PVR_FORMAT_PIX 0
115 #define PVR_FORMAT_VBI 1
117 static struct v4l2_format pvr_format
[] = {
119 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
124 // This should really be V4L2_PIX_FMT_MPEG,
125 // but xawtv breaks when I do that.
126 .pixelformat
= 0, // V4L2_PIX_FMT_MPEG,
127 .field
= V4L2_FIELD_INTERLACED
,
128 .bytesperline
= 0, // doesn't make sense
130 //FIXME : Don't know what to put here...
131 .sizeimage
= (32*1024),
132 .colorspace
= 0, // doesn't make sense here
138 .type
= V4L2_BUF_TYPE_VBI_CAPTURE
,
141 .sampling_rate
= 27000000,
143 .samples_per_line
= 1443,
144 .sample_format
= V4L2_PIX_FMT_GREY
,
158 * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
161 static long pvr2_v4l2_do_ioctl(struct file
*file
, unsigned int cmd
, void *arg
)
163 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
164 struct pvr2_v4l2
*vp
= fh
->vhead
;
165 struct pvr2_v4l2_dev
*pdi
= fh
->pdi
;
166 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
169 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
170 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),cmd
);
173 if (!pvr2_hdw_dev_ok(hdw
)) {
174 pvr2_trace(PVR2_TRACE_ERROR_LEGS
,
175 "ioctl failed - bad or no context");
185 case VIDIOC_S_FREQUENCY
:
186 ret
= v4l2_prio_check(&vp
->prio
, fh
->prio
);
192 case VIDIOC_QUERYCAP
:
194 struct v4l2_capability
*cap
= arg
;
196 memcpy(cap
, &pvr_capability
, sizeof(struct v4l2_capability
));
197 strlcpy(cap
->bus_info
,pvr2_hdw_get_bus_info(hdw
),
198 sizeof(cap
->bus_info
));
199 strlcpy(cap
->card
,pvr2_hdw_get_desc(hdw
),sizeof(cap
->card
));
205 case VIDIOC_G_PRIORITY
:
207 enum v4l2_priority
*p
= arg
;
209 *p
= v4l2_prio_max(&vp
->prio
);
214 case VIDIOC_S_PRIORITY
:
216 enum v4l2_priority
*prio
= arg
;
218 ret
= v4l2_prio_change(&vp
->prio
, &fh
->prio
, *prio
);
224 struct v4l2_standard
*vs
= (struct v4l2_standard
*)arg
;
226 ret
= pvr2_hdw_get_stdenum_value(hdw
,vs
,idx
+1);
233 ret
= pvr2_ctrl_get_value(
234 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
235 *(v4l2_std_id
*)arg
= val
;
241 ret
= pvr2_ctrl_set_value(
242 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
243 *(v4l2_std_id
*)arg
);
247 case VIDIOC_ENUMINPUT
:
249 struct pvr2_ctrl
*cptr
;
250 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
251 struct v4l2_input tmp
;
255 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
257 memset(&tmp
,0,sizeof(tmp
));
258 tmp
.index
= vi
->index
;
260 if (vi
->index
>= fh
->input_cnt
) {
264 val
= fh
->input_map
[vi
->index
];
266 case PVR2_CVAL_INPUT_TV
:
267 case PVR2_CVAL_INPUT_DTV
:
268 case PVR2_CVAL_INPUT_RADIO
:
269 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
271 case PVR2_CVAL_INPUT_SVIDEO
:
272 case PVR2_CVAL_INPUT_COMPOSITE
:
273 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
282 pvr2_ctrl_get_valname(cptr
,val
,
283 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
286 /* Don't bother with audioset, since this driver currently
287 always switches the audio whenever the video is
290 /* Handling std is a tougher problem. It doesn't make
291 sense in cases where a device might be multi-standard.
292 We could just copy out the current value for the
293 standard, but it can change over time. For now just
296 memcpy(vi
, &tmp
, sizeof(tmp
));
305 struct pvr2_ctrl
*cptr
;
306 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
308 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
310 ret
= pvr2_ctrl_get_value(cptr
,&val
);
312 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
313 if (fh
->input_map
[idx
] == val
) {
323 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
324 if (vi
->index
>= fh
->input_cnt
) {
328 ret
= pvr2_ctrl_set_value(
329 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
330 fh
->input_map
[vi
->index
]);
334 case VIDIOC_ENUMAUDIO
:
336 /* pkt: FIXME: We are returning one "fake" input here
337 which could very well be called "whatever_we_like".
338 This is for apps that want to see an audio input
339 just to feel comfortable, as well as to test if
340 it can do stereo or sth. There is actually no guarantee
341 that the actual audio input cannot change behind the app's
342 back, but most applications should not mind that either.
344 Hopefully, mplayer people will work with us on this (this
345 whole mess is to support mplayer pvr://), or Hans will come
346 up with a more standard way to say "we have inputs but we
347 don 't want you to change them independent of video" which
350 struct v4l2_audio
*vin
= arg
;
352 if (vin
->index
> 0) break;
353 strncpy(vin
->name
, "PVRUSB2 Audio",14);
354 vin
->capability
= V4L2_AUDCAP_STEREO
;
362 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
363 struct v4l2_audio
*vin
= arg
;
364 memset(vin
,0,sizeof(*vin
));
366 strncpy(vin
->name
, "PVRUSB2 Audio",14);
367 vin
->capability
= V4L2_AUDCAP_STEREO
;
379 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
381 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
383 pvr2_hdw_execute_tuner_poll(hdw
);
384 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
390 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
395 ret
= pvr2_ctrl_set_value(
396 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
401 case VIDIOC_S_FREQUENCY
:
403 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
405 struct v4l2_tuner vt
;
407 struct pvr2_ctrl
*ctrlp
;
408 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
410 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
411 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
413 if (vf
->type
== V4L2_TUNER_RADIO
) {
414 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
415 pvr2_ctrl_set_value(ctrlp
,
416 PVR2_CVAL_INPUT_RADIO
);
419 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
420 pvr2_ctrl_set_value(ctrlp
,
425 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
430 ret
= pvr2_ctrl_set_value(
431 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
435 case VIDIOC_G_FREQUENCY
:
437 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
440 struct v4l2_tuner vt
;
441 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
443 ret
= pvr2_ctrl_get_value(
444 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
448 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
450 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
451 vf
->type
= V4L2_TUNER_RADIO
;
453 vf
->type
= V4L2_TUNER_ANALOG_TV
;
455 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
456 val
= (val
* 2) / 125;
464 case VIDIOC_ENUM_FMT
:
466 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
468 /* Only one format is supported : mpeg.*/
472 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
479 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
482 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
483 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
484 sizeof(struct v4l2_format
));
487 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
489 vf
->fmt
.pix
.width
= val
;
492 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
494 vf
->fmt
.pix
.height
= val
;
497 case V4L2_BUF_TYPE_VBI_CAPTURE
:
498 // ????? Still need to figure out to do VBI correctly
511 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
515 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
517 struct pvr2_ctrl
*hcp
,*vcp
;
518 int h
= vf
->fmt
.pix
.height
;
519 int w
= vf
->fmt
.pix
.width
;
520 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
521 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
523 lmin
= pvr2_ctrl_get_min(hcp
);
524 lmax
= pvr2_ctrl_get_max(hcp
);
525 pvr2_ctrl_get_def(hcp
, &ldef
);
528 } else if (w
< lmin
) {
530 } else if (w
> lmax
) {
533 lmin
= pvr2_ctrl_get_min(vcp
);
534 lmax
= pvr2_ctrl_get_max(vcp
);
535 pvr2_ctrl_get_def(vcp
, &ldef
);
538 } else if (h
< lmin
) {
540 } else if (h
> lmax
) {
544 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
545 sizeof(struct v4l2_format
));
546 vf
->fmt
.pix
.width
= w
;
547 vf
->fmt
.pix
.height
= h
;
549 if (cmd
== VIDIOC_S_FMT
) {
550 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
551 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
554 case V4L2_BUF_TYPE_VBI_CAPTURE
:
555 // ????? Still need to figure out to do VBI correctly
565 case VIDIOC_STREAMON
:
567 if (!fh
->pdi
->stream
) {
568 /* No stream defined for this node. This means
569 that we're not currently allowed to stream from
574 ret
= pvr2_hdw_set_stream_type(hdw
,pdi
->config
);
575 if (ret
< 0) return ret
;
576 ret
= pvr2_hdw_set_streaming(hdw
,!0);
580 case VIDIOC_STREAMOFF
:
582 if (!fh
->pdi
->stream
) {
583 /* No stream defined for this node. This means
584 that we're not currently allowed to stream from
589 ret
= pvr2_hdw_set_streaming(hdw
,0);
593 case VIDIOC_QUERYCTRL
:
595 struct pvr2_ctrl
*cptr
;
597 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
599 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
600 cptr
= pvr2_hdw_get_ctrl_nextv4l(
601 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
602 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
604 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
607 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
608 "QUERYCTRL id=0x%x not implemented here",
614 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
615 "QUERYCTRL id=0x%x mapping name=%s (%s)",
616 vc
->id
,pvr2_ctrl_get_name(cptr
),
617 pvr2_ctrl_get_desc(cptr
));
618 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
619 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
620 pvr2_ctrl_get_def(cptr
, &val
);
621 vc
->default_value
= val
;
622 switch (pvr2_ctrl_get_type(cptr
)) {
624 vc
->type
= V4L2_CTRL_TYPE_MENU
;
626 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
630 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
636 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
637 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
638 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
642 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
643 "QUERYCTRL id=0x%x name=%s not mappable",
644 vc
->id
,pvr2_ctrl_get_name(cptr
));
651 case VIDIOC_QUERYMENU
:
653 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
654 unsigned int cnt
= 0;
655 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
657 vm
->name
,sizeof(vm
->name
)-1,
665 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
667 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
675 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
676 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
681 case VIDIOC_G_EXT_CTRLS
:
683 struct v4l2_ext_controls
*ctls
=
684 (struct v4l2_ext_controls
*)arg
;
685 struct v4l2_ext_control
*ctrl
;
689 for (idx
= 0; idx
< ctls
->count
; idx
++) {
690 ctrl
= ctls
->controls
+ idx
;
691 ret
= pvr2_ctrl_get_value(
692 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
694 ctls
->error_idx
= idx
;
697 /* Ensure that if read as a 64 bit value, the user
698 will still get a hopefully sane value */
705 case VIDIOC_S_EXT_CTRLS
:
707 struct v4l2_ext_controls
*ctls
=
708 (struct v4l2_ext_controls
*)arg
;
709 struct v4l2_ext_control
*ctrl
;
712 for (idx
= 0; idx
< ctls
->count
; idx
++) {
713 ctrl
= ctls
->controls
+ idx
;
714 ret
= pvr2_ctrl_set_value(
715 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
718 ctls
->error_idx
= idx
;
725 case VIDIOC_TRY_EXT_CTRLS
:
727 struct v4l2_ext_controls
*ctls
=
728 (struct v4l2_ext_controls
*)arg
;
729 struct v4l2_ext_control
*ctrl
;
730 struct pvr2_ctrl
*pctl
;
732 /* For the moment just validate that the requested control
735 for (idx
= 0; idx
< ctls
->count
; idx
++) {
736 ctrl
= ctls
->controls
+ idx
;
737 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
740 ctls
->error_idx
= idx
;
749 struct v4l2_cropcap
*cap
= (struct v4l2_cropcap
*)arg
;
750 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
754 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
755 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
760 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
762 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
766 ret
= pvr2_ctrl_get_value(
767 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
773 ret
= pvr2_ctrl_get_value(
774 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
780 ret
= pvr2_ctrl_get_value(
781 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
787 ret
= pvr2_ctrl_get_value(
788 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
793 crop
->c
.height
= val
;
797 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
798 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
802 ret
= pvr2_ctrl_set_value(
803 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
809 ret
= pvr2_ctrl_set_value(
810 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
816 ret
= pvr2_ctrl_set_value(
817 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
823 ret
= pvr2_ctrl_set_value(
824 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
831 case VIDIOC_LOG_STATUS
:
833 pvr2_hdw_trigger_module_log(hdw
);
837 #ifdef CONFIG_VIDEO_ADV_DEBUG
838 case VIDIOC_DBG_S_REGISTER
:
839 case VIDIOC_DBG_G_REGISTER
:
842 struct v4l2_dbg_register
*req
= (struct v4l2_dbg_register
*)arg
;
843 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
844 ret
= pvr2_hdw_register_access(
845 hdw
, &req
->match
, req
->reg
,
846 cmd
== VIDIOC_DBG_S_REGISTER
, &val
);
847 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
857 pvr2_hdw_commit_ctl(hdw
);
860 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
861 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
862 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
864 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
865 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
866 "pvr2_v4l2_do_ioctl failure, ret=%ld"
867 " command was:", ret
);
868 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
873 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
874 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
880 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
882 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
883 enum pvr2_config cfg
= dip
->config
;
887 /* Construct the unregistration message *before* we actually
888 perform the unregistration step. By doing it this way we don't
889 have to worry about potentially touching deleted resources. */
890 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
891 "pvrusb2: unregistered device %s [%s]",
892 video_device_node_name(&dip
->devbase
),
893 pvr2_config_get_name(cfg
));
896 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
902 /* Actual deallocation happens later when all internal references
904 video_unregister_device(&dip
->devbase
);
906 printk(KERN_INFO
"%s\n", msg
);
911 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
914 if (!dip
->devbase
.parent
) return;
915 dip
->devbase
.parent
= NULL
;
916 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
920 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
923 pvr2_v4l2_dev_destroy(vp
->dev_video
);
924 vp
->dev_video
= NULL
;
927 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
928 vp
->dev_radio
= NULL
;
931 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
932 pvr2_channel_done(&vp
->channel
);
937 static void pvr2_video_device_release(struct video_device
*vdev
)
939 struct pvr2_v4l2_dev
*dev
;
940 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
945 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
947 struct pvr2_v4l2
*vp
;
948 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
949 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
950 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
951 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
952 if (vp
->vfirst
) return;
953 pvr2_v4l2_destroy_no_lock(vp
);
957 static long pvr2_v4l2_ioctl(struct file
*file
,
958 unsigned int cmd
, unsigned long arg
)
961 return video_usercopy(file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
965 static int pvr2_v4l2_release(struct file
*file
)
967 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
968 struct pvr2_v4l2
*vp
= fhp
->vhead
;
969 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
971 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
974 struct pvr2_stream
*sp
;
975 pvr2_hdw_set_streaming(hdw
,0);
976 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
977 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
978 pvr2_ioread_destroy(fhp
->rhp
);
982 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
983 file
->private_data
= NULL
;
986 fhp
->vnext
->vprev
= fhp
->vprev
;
988 vp
->vlast
= fhp
->vprev
;
991 fhp
->vprev
->vnext
= fhp
->vnext
;
993 vp
->vfirst
= fhp
->vnext
;
998 pvr2_channel_done(&fhp
->channel
);
999 pvr2_trace(PVR2_TRACE_STRUCT
,
1000 "Destroying pvr_v4l2_fh id=%p",fhp
);
1001 if (fhp
->input_map
) {
1002 kfree(fhp
->input_map
);
1003 fhp
->input_map
= NULL
;
1006 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1007 pvr2_v4l2_destroy_no_lock(vp
);
1013 static int pvr2_v4l2_open(struct file
*file
)
1015 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1016 struct pvr2_v4l2_fh
*fhp
;
1017 struct pvr2_v4l2
*vp
;
1018 struct pvr2_hdw
*hdw
;
1019 unsigned int input_mask
= 0;
1020 unsigned int input_cnt
,idx
;
1023 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1026 hdw
= vp
->channel
.hdw
;
1028 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1030 if (!pvr2_hdw_dev_ok(hdw
)) {
1031 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1032 "pvr2_v4l2_open: hardware not ready");
1036 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1041 init_waitqueue_head(&fhp
->wait_data
);
1044 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1045 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1047 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1048 /* Opening device as a radio, legal input selection subset
1049 is just the radio. */
1050 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1052 /* Opening the main V4L device, legal input selection
1053 subset includes all analog inputs. */
1054 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1055 (1 << PVR2_CVAL_INPUT_TV
) |
1056 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1057 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1059 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1061 pvr2_channel_done(&fhp
->channel
);
1062 pvr2_trace(PVR2_TRACE_STRUCT
,
1063 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1070 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1072 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1073 if (input_mask
& (1 << idx
)) input_cnt
++;
1075 fhp
->input_cnt
= input_cnt
;
1076 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1077 if (!fhp
->input_map
) {
1078 pvr2_channel_done(&fhp
->channel
);
1079 pvr2_trace(PVR2_TRACE_STRUCT
,
1080 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1086 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1087 if (!(input_mask
& (1 << idx
))) continue;
1088 fhp
->input_map
[input_cnt
++] = idx
;
1092 fhp
->vprev
= vp
->vlast
;
1094 vp
->vlast
->vnext
= fhp
;
1102 file
->private_data
= fhp
;
1103 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1105 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1111 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1113 wake_up(&fhp
->wait_data
);
1116 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1119 struct pvr2_stream
*sp
;
1120 struct pvr2_hdw
*hdw
;
1121 if (fh
->rhp
) return 0;
1123 if (!fh
->pdi
->stream
) {
1124 /* No stream defined for this node. This means that we're
1125 not currently allowed to stream from this node. */
1129 /* First read() attempt. Try to claim the stream and start
1131 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1132 fh
->pdi
->stream
)) != 0) {
1133 /* Someone else must already have it */
1137 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1139 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1143 hdw
= fh
->channel
.mc_head
->hdw
;
1144 sp
= fh
->pdi
->stream
->stream
;
1145 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1146 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1147 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1148 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1152 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1153 char __user
*buff
, size_t count
, loff_t
*ppos
)
1155 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1158 if (fh
->fw_mode_flag
) {
1159 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1163 unsigned int offs
= *ppos
;
1165 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1166 if (!tbuf
) return -ENOMEM
;
1170 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1171 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1177 if (copy_to_user(buff
,tbuf
,c2
)) {
1192 ret
= pvr2_v4l2_iosetup(fh
);
1199 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1200 if (ret
>= 0) break;
1201 if (ret
!= -EAGAIN
) break;
1202 if (file
->f_flags
& O_NONBLOCK
) break;
1203 /* Doing blocking I/O. Wait here. */
1204 ret
= wait_event_interruptible(
1206 pvr2_ioread_avail(fh
->rhp
) >= 0);
1214 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1216 unsigned int mask
= 0;
1217 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1220 if (fh
->fw_mode_flag
) {
1221 mask
|= POLLIN
| POLLRDNORM
;
1226 ret
= pvr2_v4l2_iosetup(fh
);
1227 if (ret
) return POLLERR
;
1230 poll_wait(file
,&fh
->wait_data
,wait
);
1232 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1233 mask
|= POLLIN
| POLLRDNORM
;
1240 static const struct v4l2_file_operations vdev_fops
= {
1241 .owner
= THIS_MODULE
,
1242 .open
= pvr2_v4l2_open
,
1243 .release
= pvr2_v4l2_release
,
1244 .read
= pvr2_v4l2_read
,
1245 .ioctl
= pvr2_v4l2_ioctl
,
1246 .poll
= pvr2_v4l2_poll
,
1250 static struct video_device vdev_template
= {
1255 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1256 struct pvr2_v4l2
*vp
,
1259 struct usb_device
*usbdev
;
1265 usbdev
= pvr2_hdw_get_dev(vp
->channel
.mc_head
->hdw
);
1266 dip
->v4l_type
= v4l_type
;
1268 case VFL_TYPE_GRABBER
:
1269 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1270 dip
->config
= pvr2_config_mpeg
;
1271 dip
->minor_type
= pvr2_v4l_type_video
;
1274 pr_err(KBUILD_MODNAME
1275 ": Failed to set up pvrusb2 v4l video dev"
1276 " due to missing stream instance\n");
1281 dip
->config
= pvr2_config_vbi
;
1282 dip
->minor_type
= pvr2_v4l_type_vbi
;
1285 case VFL_TYPE_RADIO
:
1286 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1287 dip
->config
= pvr2_config_mpeg
;
1288 dip
->minor_type
= pvr2_v4l_type_radio
;
1292 /* Bail out (this should be impossible) */
1293 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1294 " due to unrecognized config\n");
1298 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1299 dip
->devbase
.release
= pvr2_video_device_release
;
1302 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1303 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1304 mindevnum
= nr_ptr
[unit_number
];
1306 dip
->devbase
.parent
= &usbdev
->dev
;
1307 if ((video_register_device(&dip
->devbase
,
1308 dip
->v4l_type
, mindevnum
) < 0) &&
1309 (video_register_device(&dip
->devbase
,
1310 dip
->v4l_type
, -1) < 0)) {
1311 pr_err(KBUILD_MODNAME
1312 ": Failed to register pvrusb2 v4l device\n");
1315 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1316 video_device_node_name(&dip
->devbase
),
1317 pvr2_config_get_name(dip
->config
));
1319 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1320 dip
->minor_type
,dip
->devbase
.minor
);
1324 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1326 struct pvr2_v4l2
*vp
;
1328 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1330 pvr2_channel_init(&vp
->channel
,mnp
);
1331 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1333 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1335 /* register streams */
1336 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1337 if (!vp
->dev_video
) goto fail
;
1338 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1339 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1340 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1341 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1342 if (!vp
->dev_radio
) goto fail
;
1343 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1348 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1349 pvr2_v4l2_destroy_no_lock(vp
);
1354 Stuff for Emacs to see, in order to encourage consistent editing style:
1355 *** Local Variables: ***
1357 *** fill-column: 75 ***
1358 *** tab-width: 8 ***
1359 *** c-basic-offset: 8 ***