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);
231 case VIDIOC_QUERYSTD
:
233 v4l2_std_id
*std
= arg
;
235 ret
= pvr2_hdw_get_detected_std(hdw
, std
);
242 ret
= pvr2_ctrl_get_value(
243 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),&val
);
244 *(v4l2_std_id
*)arg
= val
;
250 ret
= pvr2_ctrl_set_value(
251 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_STDCUR
),
252 *(v4l2_std_id
*)arg
);
256 case VIDIOC_ENUMINPUT
:
258 struct pvr2_ctrl
*cptr
;
259 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
260 struct v4l2_input tmp
;
264 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
266 memset(&tmp
,0,sizeof(tmp
));
267 tmp
.index
= vi
->index
;
269 if (vi
->index
>= fh
->input_cnt
) {
273 val
= fh
->input_map
[vi
->index
];
275 case PVR2_CVAL_INPUT_TV
:
276 case PVR2_CVAL_INPUT_DTV
:
277 case PVR2_CVAL_INPUT_RADIO
:
278 tmp
.type
= V4L2_INPUT_TYPE_TUNER
;
280 case PVR2_CVAL_INPUT_SVIDEO
:
281 case PVR2_CVAL_INPUT_COMPOSITE
:
282 tmp
.type
= V4L2_INPUT_TYPE_CAMERA
;
291 pvr2_ctrl_get_valname(cptr
,val
,
292 tmp
.name
,sizeof(tmp
.name
)-1,&cnt
);
295 /* Don't bother with audioset, since this driver currently
296 always switches the audio whenever the video is
299 /* Handling std is a tougher problem. It doesn't make
300 sense in cases where a device might be multi-standard.
301 We could just copy out the current value for the
302 standard, but it can change over time. For now just
305 memcpy(vi
, &tmp
, sizeof(tmp
));
314 struct pvr2_ctrl
*cptr
;
315 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
317 cptr
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
319 ret
= pvr2_ctrl_get_value(cptr
,&val
);
321 for (idx
= 0; idx
< fh
->input_cnt
; idx
++) {
322 if (fh
->input_map
[idx
] == val
) {
332 struct v4l2_input
*vi
= (struct v4l2_input
*)arg
;
333 if (vi
->index
>= fh
->input_cnt
) {
337 ret
= pvr2_ctrl_set_value(
338 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
339 fh
->input_map
[vi
->index
]);
343 case VIDIOC_ENUMAUDIO
:
345 /* pkt: FIXME: We are returning one "fake" input here
346 which could very well be called "whatever_we_like".
347 This is for apps that want to see an audio input
348 just to feel comfortable, as well as to test if
349 it can do stereo or sth. There is actually no guarantee
350 that the actual audio input cannot change behind the app's
351 back, but most applications should not mind that either.
353 Hopefully, mplayer people will work with us on this (this
354 whole mess is to support mplayer pvr://), or Hans will come
355 up with a more standard way to say "we have inputs but we
356 don 't want you to change them independent of video" which
359 struct v4l2_audio
*vin
= arg
;
361 if (vin
->index
> 0) break;
362 strncpy(vin
->name
, "PVRUSB2 Audio",14);
363 vin
->capability
= V4L2_AUDCAP_STEREO
;
371 /* pkt: FIXME: see above comment (VIDIOC_ENUMAUDIO) */
372 struct v4l2_audio
*vin
= arg
;
373 memset(vin
,0,sizeof(*vin
));
375 strncpy(vin
->name
, "PVRUSB2 Audio",14);
376 vin
->capability
= V4L2_AUDCAP_STEREO
;
383 struct v4l2_tuner
*vt
= (struct v4l2_tuner
*)arg
;
385 if (vt
->index
!= 0) break; /* Only answer for the 1st tuner */
387 pvr2_hdw_execute_tuner_poll(hdw
);
388 ret
= pvr2_hdw_get_tuner_status(hdw
,vt
);
394 struct v4l2_tuner
*vt
=(struct v4l2_tuner
*)arg
;
399 ret
= pvr2_ctrl_set_value(
400 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_AUDIOMODE
),
405 case VIDIOC_S_FREQUENCY
:
407 const struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
409 struct v4l2_tuner vt
;
411 struct pvr2_ctrl
*ctrlp
;
412 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
414 ctrlp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
);
415 ret
= pvr2_ctrl_get_value(ctrlp
,&cur_input
);
417 if (vf
->type
== V4L2_TUNER_RADIO
) {
418 if (cur_input
!= PVR2_CVAL_INPUT_RADIO
) {
419 pvr2_ctrl_set_value(ctrlp
,
420 PVR2_CVAL_INPUT_RADIO
);
423 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
424 pvr2_ctrl_set_value(ctrlp
,
429 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
434 ret
= pvr2_ctrl_set_value(
435 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),fv
);
439 case VIDIOC_G_FREQUENCY
:
441 struct v4l2_frequency
*vf
= (struct v4l2_frequency
*)arg
;
444 struct v4l2_tuner vt
;
445 ret
= pvr2_hdw_get_tuner_status(hdw
,&vt
);
447 ret
= pvr2_ctrl_get_value(
448 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_FREQUENCY
),
452 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_INPUT
),
454 if (cur_input
== PVR2_CVAL_INPUT_RADIO
) {
455 vf
->type
= V4L2_TUNER_RADIO
;
457 vf
->type
= V4L2_TUNER_ANALOG_TV
;
459 if (vt
.capability
& V4L2_TUNER_CAP_LOW
) {
460 val
= (val
* 2) / 125;
468 case VIDIOC_ENUM_FMT
:
470 struct v4l2_fmtdesc
*fd
= (struct v4l2_fmtdesc
*)arg
;
472 /* Only one format is supported : mpeg.*/
476 memcpy(fd
, pvr_fmtdesc
, sizeof(struct v4l2_fmtdesc
));
483 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
486 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
487 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
488 sizeof(struct v4l2_format
));
491 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
),
493 vf
->fmt
.pix
.width
= val
;
496 pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
),
498 vf
->fmt
.pix
.height
= val
;
501 case V4L2_BUF_TYPE_VBI_CAPTURE
:
502 // ????? Still need to figure out to do VBI correctly
515 struct v4l2_format
*vf
= (struct v4l2_format
*)arg
;
519 case V4L2_BUF_TYPE_VIDEO_CAPTURE
: {
521 struct pvr2_ctrl
*hcp
,*vcp
;
522 int h
= vf
->fmt
.pix
.height
;
523 int w
= vf
->fmt
.pix
.width
;
524 hcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_HRES
);
525 vcp
= pvr2_hdw_get_ctrl_by_id(hdw
,PVR2_CID_VRES
);
527 lmin
= pvr2_ctrl_get_min(hcp
);
528 lmax
= pvr2_ctrl_get_max(hcp
);
529 pvr2_ctrl_get_def(hcp
, &ldef
);
532 } else if (w
< lmin
) {
534 } else if (w
> lmax
) {
537 lmin
= pvr2_ctrl_get_min(vcp
);
538 lmax
= pvr2_ctrl_get_max(vcp
);
539 pvr2_ctrl_get_def(vcp
, &ldef
);
542 } else if (h
< lmin
) {
544 } else if (h
> lmax
) {
548 memcpy(vf
, &pvr_format
[PVR_FORMAT_PIX
],
549 sizeof(struct v4l2_format
));
550 vf
->fmt
.pix
.width
= w
;
551 vf
->fmt
.pix
.height
= h
;
553 if (cmd
== VIDIOC_S_FMT
) {
554 pvr2_ctrl_set_value(hcp
,vf
->fmt
.pix
.width
);
555 pvr2_ctrl_set_value(vcp
,vf
->fmt
.pix
.height
);
558 case V4L2_BUF_TYPE_VBI_CAPTURE
:
559 // ????? Still need to figure out to do VBI correctly
569 case VIDIOC_STREAMON
:
571 if (!fh
->pdi
->stream
) {
572 /* No stream defined for this node. This means
573 that we're not currently allowed to stream from
578 ret
= pvr2_hdw_set_stream_type(hdw
,pdi
->config
);
579 if (ret
< 0) return ret
;
580 ret
= pvr2_hdw_set_streaming(hdw
,!0);
584 case VIDIOC_STREAMOFF
:
586 if (!fh
->pdi
->stream
) {
587 /* No stream defined for this node. This means
588 that we're not currently allowed to stream from
593 ret
= pvr2_hdw_set_streaming(hdw
,0);
597 case VIDIOC_QUERYCTRL
:
599 struct pvr2_ctrl
*cptr
;
601 struct v4l2_queryctrl
*vc
= (struct v4l2_queryctrl
*)arg
;
603 if (vc
->id
& V4L2_CTRL_FLAG_NEXT_CTRL
) {
604 cptr
= pvr2_hdw_get_ctrl_nextv4l(
605 hdw
,(vc
->id
& ~V4L2_CTRL_FLAG_NEXT_CTRL
));
606 if (cptr
) vc
->id
= pvr2_ctrl_get_v4lid(cptr
);
608 cptr
= pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
);
611 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
612 "QUERYCTRL id=0x%x not implemented here",
618 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
619 "QUERYCTRL id=0x%x mapping name=%s (%s)",
620 vc
->id
,pvr2_ctrl_get_name(cptr
),
621 pvr2_ctrl_get_desc(cptr
));
622 strlcpy(vc
->name
,pvr2_ctrl_get_desc(cptr
),sizeof(vc
->name
));
623 vc
->flags
= pvr2_ctrl_get_v4lflags(cptr
);
624 pvr2_ctrl_get_def(cptr
, &val
);
625 vc
->default_value
= val
;
626 switch (pvr2_ctrl_get_type(cptr
)) {
628 vc
->type
= V4L2_CTRL_TYPE_MENU
;
630 vc
->maximum
= pvr2_ctrl_get_cnt(cptr
) - 1;
634 vc
->type
= V4L2_CTRL_TYPE_BOOLEAN
;
640 vc
->type
= V4L2_CTRL_TYPE_INTEGER
;
641 vc
->minimum
= pvr2_ctrl_get_min(cptr
);
642 vc
->maximum
= pvr2_ctrl_get_max(cptr
);
646 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
647 "QUERYCTRL id=0x%x name=%s not mappable",
648 vc
->id
,pvr2_ctrl_get_name(cptr
));
655 case VIDIOC_QUERYMENU
:
657 struct v4l2_querymenu
*vm
= (struct v4l2_querymenu
*)arg
;
658 unsigned int cnt
= 0;
659 ret
= pvr2_ctrl_get_valname(pvr2_hdw_get_ctrl_v4l(hdw
,vm
->id
),
661 vm
->name
,sizeof(vm
->name
)-1,
669 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
671 ret
= pvr2_ctrl_get_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
679 struct v4l2_control
*vc
= (struct v4l2_control
*)arg
;
680 ret
= pvr2_ctrl_set_value(pvr2_hdw_get_ctrl_v4l(hdw
,vc
->id
),
685 case VIDIOC_G_EXT_CTRLS
:
687 struct v4l2_ext_controls
*ctls
=
688 (struct v4l2_ext_controls
*)arg
;
689 struct v4l2_ext_control
*ctrl
;
693 for (idx
= 0; idx
< ctls
->count
; idx
++) {
694 ctrl
= ctls
->controls
+ idx
;
695 ret
= pvr2_ctrl_get_value(
696 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),&val
);
698 ctls
->error_idx
= idx
;
701 /* Ensure that if read as a 64 bit value, the user
702 will still get a hopefully sane value */
709 case VIDIOC_S_EXT_CTRLS
:
711 struct v4l2_ext_controls
*ctls
=
712 (struct v4l2_ext_controls
*)arg
;
713 struct v4l2_ext_control
*ctrl
;
716 for (idx
= 0; idx
< ctls
->count
; idx
++) {
717 ctrl
= ctls
->controls
+ idx
;
718 ret
= pvr2_ctrl_set_value(
719 pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
),
722 ctls
->error_idx
= idx
;
729 case VIDIOC_TRY_EXT_CTRLS
:
731 struct v4l2_ext_controls
*ctls
=
732 (struct v4l2_ext_controls
*)arg
;
733 struct v4l2_ext_control
*ctrl
;
734 struct pvr2_ctrl
*pctl
;
736 /* For the moment just validate that the requested control
739 for (idx
= 0; idx
< ctls
->count
; idx
++) {
740 ctrl
= ctls
->controls
+ idx
;
741 pctl
= pvr2_hdw_get_ctrl_v4l(hdw
,ctrl
->id
);
744 ctls
->error_idx
= idx
;
753 struct v4l2_cropcap
*cap
= (struct v4l2_cropcap
*)arg
;
754 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
758 ret
= pvr2_hdw_get_cropcap(hdw
, cap
);
759 cap
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
; /* paranoia */
764 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
766 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
770 ret
= pvr2_ctrl_get_value(
771 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
), &val
);
777 ret
= pvr2_ctrl_get_value(
778 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
), &val
);
784 ret
= pvr2_ctrl_get_value(
785 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
), &val
);
791 ret
= pvr2_ctrl_get_value(
792 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
), &val
);
797 crop
->c
.height
= val
;
801 struct v4l2_crop
*crop
= (struct v4l2_crop
*)arg
;
802 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
806 ret
= pvr2_ctrl_set_value(
807 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPL
),
813 ret
= pvr2_ctrl_set_value(
814 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPT
),
820 ret
= pvr2_ctrl_set_value(
821 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPW
),
827 ret
= pvr2_ctrl_set_value(
828 pvr2_hdw_get_ctrl_by_id(hdw
, PVR2_CID_CROPH
),
835 case VIDIOC_LOG_STATUS
:
837 pvr2_hdw_trigger_module_log(hdw
);
841 #ifdef CONFIG_VIDEO_ADV_DEBUG
842 case VIDIOC_DBG_S_REGISTER
:
843 case VIDIOC_DBG_G_REGISTER
:
846 struct v4l2_dbg_register
*req
= (struct v4l2_dbg_register
*)arg
;
847 if (cmd
== VIDIOC_DBG_S_REGISTER
) val
= req
->val
;
848 ret
= pvr2_hdw_register_access(
849 hdw
, &req
->match
, req
->reg
,
850 cmd
== VIDIOC_DBG_S_REGISTER
, &val
);
851 if (cmd
== VIDIOC_DBG_G_REGISTER
) req
->val
= val
;
861 pvr2_hdw_commit_ctl(hdw
);
864 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
865 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
866 "pvr2_v4l2_do_ioctl failure, ret=%ld", ret
);
868 if (pvrusb2_debug
& PVR2_TRACE_V4LIOCTL
) {
869 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
870 "pvr2_v4l2_do_ioctl failure, ret=%ld"
871 " command was:", ret
);
872 v4l_print_ioctl(pvr2_hdw_get_driver_name(hdw
),
877 pvr2_trace(PVR2_TRACE_V4LIOCTL
,
878 "pvr2_v4l2_do_ioctl complete, ret=%ld (0x%lx)",
884 static void pvr2_v4l2_dev_destroy(struct pvr2_v4l2_dev
*dip
)
886 struct pvr2_hdw
*hdw
= dip
->v4lp
->channel
.mc_head
->hdw
;
887 enum pvr2_config cfg
= dip
->config
;
891 /* Construct the unregistration message *before* we actually
892 perform the unregistration step. By doing it this way we don't
893 have to worry about potentially touching deleted resources. */
894 mcnt
= scnprintf(msg
, sizeof(msg
) - 1,
895 "pvrusb2: unregistered device %s [%s]",
896 video_device_node_name(&dip
->devbase
),
897 pvr2_config_get_name(cfg
));
900 pvr2_hdw_v4l_store_minor_number(hdw
,dip
->minor_type
,-1);
906 /* Actual deallocation happens later when all internal references
908 video_unregister_device(&dip
->devbase
);
910 printk(KERN_INFO
"%s\n", msg
);
915 static void pvr2_v4l2_dev_disassociate_parent(struct pvr2_v4l2_dev
*dip
)
918 if (!dip
->devbase
.parent
) return;
919 dip
->devbase
.parent
= NULL
;
920 device_move(&dip
->devbase
.dev
, NULL
, DPM_ORDER_NONE
);
924 static void pvr2_v4l2_destroy_no_lock(struct pvr2_v4l2
*vp
)
927 pvr2_v4l2_dev_destroy(vp
->dev_video
);
928 vp
->dev_video
= NULL
;
931 pvr2_v4l2_dev_destroy(vp
->dev_radio
);
932 vp
->dev_radio
= NULL
;
935 pvr2_trace(PVR2_TRACE_STRUCT
,"Destroying pvr2_v4l2 id=%p",vp
);
936 pvr2_channel_done(&vp
->channel
);
941 static void pvr2_video_device_release(struct video_device
*vdev
)
943 struct pvr2_v4l2_dev
*dev
;
944 dev
= container_of(vdev
,struct pvr2_v4l2_dev
,devbase
);
949 static void pvr2_v4l2_internal_check(struct pvr2_channel
*chp
)
951 struct pvr2_v4l2
*vp
;
952 vp
= container_of(chp
,struct pvr2_v4l2
,channel
);
953 if (!vp
->channel
.mc_head
->disconnect_flag
) return;
954 pvr2_v4l2_dev_disassociate_parent(vp
->dev_video
);
955 pvr2_v4l2_dev_disassociate_parent(vp
->dev_radio
);
956 if (vp
->vfirst
) return;
957 pvr2_v4l2_destroy_no_lock(vp
);
961 static long pvr2_v4l2_ioctl(struct file
*file
,
962 unsigned int cmd
, unsigned long arg
)
965 return video_usercopy(file
, cmd
, arg
, pvr2_v4l2_do_ioctl
);
969 static int pvr2_v4l2_release(struct file
*file
)
971 struct pvr2_v4l2_fh
*fhp
= file
->private_data
;
972 struct pvr2_v4l2
*vp
= fhp
->vhead
;
973 struct pvr2_hdw
*hdw
= fhp
->channel
.mc_head
->hdw
;
975 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_release");
978 struct pvr2_stream
*sp
;
979 pvr2_hdw_set_streaming(hdw
,0);
980 sp
= pvr2_ioread_get_stream(fhp
->rhp
);
981 if (sp
) pvr2_stream_set_callback(sp
,NULL
,NULL
);
982 pvr2_ioread_destroy(fhp
->rhp
);
986 v4l2_prio_close(&vp
->prio
, fhp
->prio
);
987 file
->private_data
= NULL
;
990 fhp
->vnext
->vprev
= fhp
->vprev
;
992 vp
->vlast
= fhp
->vprev
;
995 fhp
->vprev
->vnext
= fhp
->vnext
;
997 vp
->vfirst
= fhp
->vnext
;
1002 pvr2_channel_done(&fhp
->channel
);
1003 pvr2_trace(PVR2_TRACE_STRUCT
,
1004 "Destroying pvr_v4l2_fh id=%p",fhp
);
1005 if (fhp
->input_map
) {
1006 kfree(fhp
->input_map
);
1007 fhp
->input_map
= NULL
;
1010 if (vp
->channel
.mc_head
->disconnect_flag
&& !vp
->vfirst
) {
1011 pvr2_v4l2_destroy_no_lock(vp
);
1017 static int pvr2_v4l2_open(struct file
*file
)
1019 struct pvr2_v4l2_dev
*dip
; /* Our own context pointer */
1020 struct pvr2_v4l2_fh
*fhp
;
1021 struct pvr2_v4l2
*vp
;
1022 struct pvr2_hdw
*hdw
;
1023 unsigned int input_mask
= 0;
1024 unsigned int input_cnt
,idx
;
1027 dip
= container_of(video_devdata(file
),struct pvr2_v4l2_dev
,devbase
);
1030 hdw
= vp
->channel
.hdw
;
1032 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,"pvr2_v4l2_open");
1034 if (!pvr2_hdw_dev_ok(hdw
)) {
1035 pvr2_trace(PVR2_TRACE_OPEN_CLOSE
,
1036 "pvr2_v4l2_open: hardware not ready");
1040 fhp
= kzalloc(sizeof(*fhp
),GFP_KERNEL
);
1045 init_waitqueue_head(&fhp
->wait_data
);
1048 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr_v4l2_fh id=%p",fhp
);
1049 pvr2_channel_init(&fhp
->channel
,vp
->channel
.mc_head
);
1051 if (dip
->v4l_type
== VFL_TYPE_RADIO
) {
1052 /* Opening device as a radio, legal input selection subset
1053 is just the radio. */
1054 input_mask
= (1 << PVR2_CVAL_INPUT_RADIO
);
1056 /* Opening the main V4L device, legal input selection
1057 subset includes all analog inputs. */
1058 input_mask
= ((1 << PVR2_CVAL_INPUT_RADIO
) |
1059 (1 << PVR2_CVAL_INPUT_TV
) |
1060 (1 << PVR2_CVAL_INPUT_COMPOSITE
) |
1061 (1 << PVR2_CVAL_INPUT_SVIDEO
));
1063 ret
= pvr2_channel_limit_inputs(&fhp
->channel
,input_mask
);
1065 pvr2_channel_done(&fhp
->channel
);
1066 pvr2_trace(PVR2_TRACE_STRUCT
,
1067 "Destroying pvr_v4l2_fh id=%p (input mask error)",
1074 input_mask
&= pvr2_hdw_get_input_available(hdw
);
1076 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1077 if (input_mask
& (1 << idx
)) input_cnt
++;
1079 fhp
->input_cnt
= input_cnt
;
1080 fhp
->input_map
= kzalloc(input_cnt
,GFP_KERNEL
);
1081 if (!fhp
->input_map
) {
1082 pvr2_channel_done(&fhp
->channel
);
1083 pvr2_trace(PVR2_TRACE_STRUCT
,
1084 "Destroying pvr_v4l2_fh id=%p (input map failure)",
1090 for (idx
= 0; idx
< (sizeof(input_mask
) << 3); idx
++) {
1091 if (!(input_mask
& (1 << idx
))) continue;
1092 fhp
->input_map
[input_cnt
++] = idx
;
1096 fhp
->vprev
= vp
->vlast
;
1098 vp
->vlast
->vnext
= fhp
;
1106 file
->private_data
= fhp
;
1107 v4l2_prio_open(&vp
->prio
, &fhp
->prio
);
1109 fhp
->fw_mode_flag
= pvr2_hdw_cpufw_get_enabled(hdw
);
1115 static void pvr2_v4l2_notify(struct pvr2_v4l2_fh
*fhp
)
1117 wake_up(&fhp
->wait_data
);
1120 static int pvr2_v4l2_iosetup(struct pvr2_v4l2_fh
*fh
)
1123 struct pvr2_stream
*sp
;
1124 struct pvr2_hdw
*hdw
;
1125 if (fh
->rhp
) return 0;
1127 if (!fh
->pdi
->stream
) {
1128 /* No stream defined for this node. This means that we're
1129 not currently allowed to stream from this node. */
1133 /* First read() attempt. Try to claim the stream and start
1135 if ((ret
= pvr2_channel_claim_stream(&fh
->channel
,
1136 fh
->pdi
->stream
)) != 0) {
1137 /* Someone else must already have it */
1141 fh
->rhp
= pvr2_channel_create_mpeg_stream(fh
->pdi
->stream
);
1143 pvr2_channel_claim_stream(&fh
->channel
,NULL
);
1147 hdw
= fh
->channel
.mc_head
->hdw
;
1148 sp
= fh
->pdi
->stream
->stream
;
1149 pvr2_stream_set_callback(sp
,(pvr2_stream_callback
)pvr2_v4l2_notify
,fh
);
1150 pvr2_hdw_set_stream_type(hdw
,fh
->pdi
->config
);
1151 if ((ret
= pvr2_hdw_set_streaming(hdw
,!0)) < 0) return ret
;
1152 return pvr2_ioread_set_enabled(fh
->rhp
,!0);
1156 static ssize_t
pvr2_v4l2_read(struct file
*file
,
1157 char __user
*buff
, size_t count
, loff_t
*ppos
)
1159 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1162 if (fh
->fw_mode_flag
) {
1163 struct pvr2_hdw
*hdw
= fh
->channel
.mc_head
->hdw
;
1167 unsigned int offs
= *ppos
;
1169 tbuf
= kmalloc(PAGE_SIZE
,GFP_KERNEL
);
1170 if (!tbuf
) return -ENOMEM
;
1174 if (c1
> PAGE_SIZE
) c1
= PAGE_SIZE
;
1175 c2
= pvr2_hdw_cpufw_get(hdw
,offs
,tbuf
,c1
);
1181 if (copy_to_user(buff
,tbuf
,c2
)) {
1196 ret
= pvr2_v4l2_iosetup(fh
);
1203 ret
= pvr2_ioread_read(fh
->rhp
,buff
,count
);
1204 if (ret
>= 0) break;
1205 if (ret
!= -EAGAIN
) break;
1206 if (file
->f_flags
& O_NONBLOCK
) break;
1207 /* Doing blocking I/O. Wait here. */
1208 ret
= wait_event_interruptible(
1210 pvr2_ioread_avail(fh
->rhp
) >= 0);
1218 static unsigned int pvr2_v4l2_poll(struct file
*file
, poll_table
*wait
)
1220 unsigned int mask
= 0;
1221 struct pvr2_v4l2_fh
*fh
= file
->private_data
;
1224 if (fh
->fw_mode_flag
) {
1225 mask
|= POLLIN
| POLLRDNORM
;
1230 ret
= pvr2_v4l2_iosetup(fh
);
1231 if (ret
) return POLLERR
;
1234 poll_wait(file
,&fh
->wait_data
,wait
);
1236 if (pvr2_ioread_avail(fh
->rhp
) >= 0) {
1237 mask
|= POLLIN
| POLLRDNORM
;
1244 static const struct v4l2_file_operations vdev_fops
= {
1245 .owner
= THIS_MODULE
,
1246 .open
= pvr2_v4l2_open
,
1247 .release
= pvr2_v4l2_release
,
1248 .read
= pvr2_v4l2_read
,
1249 .ioctl
= pvr2_v4l2_ioctl
,
1250 .poll
= pvr2_v4l2_poll
,
1254 static struct video_device vdev_template
= {
1259 static void pvr2_v4l2_dev_init(struct pvr2_v4l2_dev
*dip
,
1260 struct pvr2_v4l2
*vp
,
1263 struct usb_device
*usbdev
;
1269 usbdev
= pvr2_hdw_get_dev(vp
->channel
.mc_head
->hdw
);
1270 dip
->v4l_type
= v4l_type
;
1272 case VFL_TYPE_GRABBER
:
1273 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1274 dip
->config
= pvr2_config_mpeg
;
1275 dip
->minor_type
= pvr2_v4l_type_video
;
1278 pr_err(KBUILD_MODNAME
1279 ": Failed to set up pvrusb2 v4l video dev"
1280 " due to missing stream instance\n");
1285 dip
->config
= pvr2_config_vbi
;
1286 dip
->minor_type
= pvr2_v4l_type_vbi
;
1289 case VFL_TYPE_RADIO
:
1290 dip
->stream
= &vp
->channel
.mc_head
->video_stream
;
1291 dip
->config
= pvr2_config_mpeg
;
1292 dip
->minor_type
= pvr2_v4l_type_radio
;
1296 /* Bail out (this should be impossible) */
1297 pr_err(KBUILD_MODNAME
": Failed to set up pvrusb2 v4l dev"
1298 " due to unrecognized config\n");
1302 memcpy(&dip
->devbase
,&vdev_template
,sizeof(vdev_template
));
1303 dip
->devbase
.release
= pvr2_video_device_release
;
1306 unit_number
= pvr2_hdw_get_unit_number(vp
->channel
.mc_head
->hdw
);
1307 if (nr_ptr
&& (unit_number
>= 0) && (unit_number
< PVR_NUM
)) {
1308 mindevnum
= nr_ptr
[unit_number
];
1310 dip
->devbase
.parent
= &usbdev
->dev
;
1311 if ((video_register_device(&dip
->devbase
,
1312 dip
->v4l_type
, mindevnum
) < 0) &&
1313 (video_register_device(&dip
->devbase
,
1314 dip
->v4l_type
, -1) < 0)) {
1315 pr_err(KBUILD_MODNAME
1316 ": Failed to register pvrusb2 v4l device\n");
1319 printk(KERN_INFO
"pvrusb2: registered device %s [%s]\n",
1320 video_device_node_name(&dip
->devbase
),
1321 pvr2_config_get_name(dip
->config
));
1323 pvr2_hdw_v4l_store_minor_number(vp
->channel
.mc_head
->hdw
,
1324 dip
->minor_type
,dip
->devbase
.minor
);
1328 struct pvr2_v4l2
*pvr2_v4l2_create(struct pvr2_context
*mnp
)
1330 struct pvr2_v4l2
*vp
;
1332 vp
= kzalloc(sizeof(*vp
),GFP_KERNEL
);
1334 pvr2_channel_init(&vp
->channel
,mnp
);
1335 pvr2_trace(PVR2_TRACE_STRUCT
,"Creating pvr2_v4l2 id=%p",vp
);
1337 vp
->channel
.check_func
= pvr2_v4l2_internal_check
;
1339 /* register streams */
1340 vp
->dev_video
= kzalloc(sizeof(*vp
->dev_video
),GFP_KERNEL
);
1341 if (!vp
->dev_video
) goto fail
;
1342 pvr2_v4l2_dev_init(vp
->dev_video
,vp
,VFL_TYPE_GRABBER
);
1343 if (pvr2_hdw_get_input_available(vp
->channel
.mc_head
->hdw
) &
1344 (1 << PVR2_CVAL_INPUT_RADIO
)) {
1345 vp
->dev_radio
= kzalloc(sizeof(*vp
->dev_radio
),GFP_KERNEL
);
1346 if (!vp
->dev_radio
) goto fail
;
1347 pvr2_v4l2_dev_init(vp
->dev_radio
,vp
,VFL_TYPE_RADIO
);
1352 pvr2_trace(PVR2_TRACE_STRUCT
,"Failure creating pvr2_v4l2 id=%p",vp
);
1353 pvr2_v4l2_destroy_no_lock(vp
);
1358 Stuff for Emacs to see, in order to encourage consistent editing style:
1359 *** Local Variables: ***
1361 *** fill-column: 75 ***
1362 *** tab-width: 8 ***
1363 *** c-basic-offset: 8 ***